Patchwork D11024: encoding: move case-related utils up

login
register
mail settings
Submitter phabricator
Date July 8, 2021, 1:56 p.m.
Message ID <differential-rev-PHID-DREV-phrqr37wb3h66ltjgiw6-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/49338/
State Superseded
Headers show

Comments

phabricator - July 8, 2021, 1:56 p.m.
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




To: Alphare, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

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