comparison mercurial/encoding.py @ 47559:53a864a60281

encoding: move case-related utils up This will be useful for the next commit that needs this code earlier. Differential Revision: https://phab.mercurial-scm.org/D11024
author Rapha?l Gom?s <rgomes@octobus.net>
date Thu, 08 Jul 2021 15:55:04 +0200
parents d4ba4d51f85f
children af633293a5bd
comparison
equal deleted inserted replaced
47558:811a79bfb8bb 47559:53a864a60281
281 def strfromlocal(s): 281 def strfromlocal(s):
282 # type: (bytes) -> str 282 # type: (bytes) -> str
283 return s # pytype: disable=bad-return-type 283 return s # pytype: disable=bad-return-type
284 284
285 strmethod = pycompat.identity 285 strmethod = pycompat.identity
286
287
288 def lower(s):
289 # type: (bytes) -> bytes
290 """best-effort encoding-aware case-folding of local string s"""
291 try:
292 return asciilower(s)
293 except UnicodeDecodeError:
294 pass
295 try:
296 if isinstance(s, localstr):
297 u = s._utf8.decode("utf-8")
298 else:
299 u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
300
301 lu = u.lower()
302 if u == lu:
303 return s # preserve localstring
304 return lu.encode(_sysstr(encoding))
305 except UnicodeError:
306 return s.lower() # we don't know how to fold this except in ASCII
307 except LookupError as k:
308 raise error.Abort(k, hint=b"please check your locale settings")
309
310
311 def upper(s):
312 # type: (bytes) -> bytes
313 """best-effort encoding-aware case-folding of local string s"""
314 try:
315 return asciiupper(s)
316 except UnicodeDecodeError:
317 return upperfallback(s)
318
319
320 def upperfallback(s):
321 # type: (Any) -> Any
322 try:
323 if isinstance(s, localstr):
324 u = s._utf8.decode("utf-8")
325 else:
326 u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
327
328 uu = u.upper()
329 if u == uu:
330 return s # preserve localstring
331 return uu.encode(_sysstr(encoding))
332 except UnicodeError:
333 return s.upper() # we don't know how to fold this except in ASCII
334 except LookupError as k:
335 raise error.Abort(k, hint=b"please check your locale settings")
336
286 337
287 if not _nativeenviron: 338 if not _nativeenviron:
288 # now encoding and helper functions are available, recreate the environ 339 # now encoding and helper functions are available, recreate the environ
289 # dict to be exported to other modules 340 # dict to be exported to other modules
290 environ = { 341 environ = {
439 if ucolwidth(usub) <= width: 490 if ucolwidth(usub) <= width:
440 return concat(usub.encode(_sysstr(encoding))) 491 return concat(usub.encode(_sysstr(encoding)))
441 return ellipsis # no enough room for multi-column characters 492 return ellipsis # no enough room for multi-column characters
442 493
443 494
444 def lower(s):
445 # type: (bytes) -> bytes
446 """best-effort encoding-aware case-folding of local string s"""
447 try:
448 return asciilower(s)
449 except UnicodeDecodeError:
450 pass
451 try:
452 if isinstance(s, localstr):
453 u = s._utf8.decode("utf-8")
454 else:
455 u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
456
457 lu = u.lower()
458 if u == lu:
459 return s # preserve localstring
460 return lu.encode(_sysstr(encoding))
461 except UnicodeError:
462 return s.lower() # we don't know how to fold this except in ASCII
463 except LookupError as k:
464 raise error.Abort(k, hint=b"please check your locale settings")
465
466
467 def upper(s):
468 # type: (bytes) -> bytes
469 """best-effort encoding-aware case-folding of local string s"""
470 try:
471 return asciiupper(s)
472 except UnicodeDecodeError:
473 return upperfallback(s)
474
475
476 def upperfallback(s):
477 # type: (Any) -> Any
478 try:
479 if isinstance(s, localstr):
480 u = s._utf8.decode("utf-8")
481 else:
482 u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
483
484 uu = u.upper()
485 if u == uu:
486 return s # preserve localstring
487 return uu.encode(_sysstr(encoding))
488 except UnicodeError:
489 return s.upper() # we don't know how to fold this except in ASCII
490 except LookupError as k:
491 raise error.Abort(k, hint=b"please check your locale settings")
492
493
494 class normcasespecs(object): 495 class normcasespecs(object):
495 """what a platform's normcase does to ASCII strings 496 """what a platform's normcase does to ASCII strings
496 497
497 This is specified per platform, and should be consistent with what normcase 498 This is specified per platform, and should be consistent with what normcase
498 on that platform actually does. 499 on that platform actually does.