D11024: encoding: move case-related utils up
Alphare (Raphaël Gomès)
phabricator at mercurial-scm.org
Thu Jul 8 13:56:58 UTC 2021
Alphare created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.
REVISION SUMMARY
This will be useful for the next commit that needs this code earlier.
REPOSITORY
rHG Mercurial
BRANCH
default
REVISION DETAIL
https://phab.mercurial-scm.org/D11024
AFFECTED FILES
mercurial/encoding.py
CHANGE DETAILS
diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -284,6 +284,57 @@
strmethod = pycompat.identity
+
+def lower(s):
+ # type: (bytes) -> bytes
+ """best-effort encoding-aware case-folding of local string s"""
+ try:
+ return asciilower(s)
+ except UnicodeDecodeError:
+ pass
+ try:
+ if isinstance(s, localstr):
+ u = s._utf8.decode("utf-8")
+ else:
+ u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
+
+ lu = u.lower()
+ if u == lu:
+ return s # preserve localstring
+ return lu.encode(_sysstr(encoding))
+ except UnicodeError:
+ return s.lower() # we don't know how to fold this except in ASCII
+ except LookupError as k:
+ raise error.Abort(k, hint=b"please check your locale settings")
+
+
+def upper(s):
+ # type: (bytes) -> bytes
+ """best-effort encoding-aware case-folding of local string s"""
+ try:
+ return asciiupper(s)
+ except UnicodeDecodeError:
+ return upperfallback(s)
+
+
+def upperfallback(s):
+ # type: (Any) -> Any
+ try:
+ if isinstance(s, localstr):
+ u = s._utf8.decode("utf-8")
+ else:
+ u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
+
+ uu = u.upper()
+ if u == uu:
+ return s # preserve localstring
+ return uu.encode(_sysstr(encoding))
+ except UnicodeError:
+ return s.upper() # we don't know how to fold this except in ASCII
+ except LookupError as k:
+ raise error.Abort(k, hint=b"please check your locale settings")
+
+
if not _nativeenviron:
# now encoding and helper functions are available, recreate the environ
# dict to be exported to other modules
@@ -441,56 +492,6 @@
return ellipsis # no enough room for multi-column characters
-def lower(s):
- # type: (bytes) -> bytes
- """best-effort encoding-aware case-folding of local string s"""
- try:
- return asciilower(s)
- except UnicodeDecodeError:
- pass
- try:
- if isinstance(s, localstr):
- u = s._utf8.decode("utf-8")
- else:
- u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
-
- lu = u.lower()
- if u == lu:
- return s # preserve localstring
- return lu.encode(_sysstr(encoding))
- except UnicodeError:
- return s.lower() # we don't know how to fold this except in ASCII
- except LookupError as k:
- raise error.Abort(k, hint=b"please check your locale settings")
-
-
-def upper(s):
- # type: (bytes) -> bytes
- """best-effort encoding-aware case-folding of local string s"""
- try:
- return asciiupper(s)
- except UnicodeDecodeError:
- return upperfallback(s)
-
-
-def upperfallback(s):
- # type: (Any) -> Any
- try:
- if isinstance(s, localstr):
- u = s._utf8.decode("utf-8")
- else:
- u = s.decode(_sysstr(encoding), _sysstr(encodingmode))
-
- uu = u.upper()
- if u == uu:
- return s # preserve localstring
- return uu.encode(_sysstr(encoding))
- except UnicodeError:
- return s.upper() # we don't know how to fold this except in ASCII
- except LookupError as k:
- raise error.Abort(k, hint=b"please check your locale settings")
-
-
class normcasespecs(object):
"""what a platform's normcase does to ASCII strings
To: Alphare, #hg-reviewers
Cc: mercurial-patches, mercurial-devel
More information about the Mercurial-devel
mailing list