encoding: add an encoding-aware lower function
authorMatt Mackall <mpm@selenic.com>
Sat, 30 Apr 2011 10:57:13 -0500
changeset 14069 e38846a79a23
parent 14068 04ce8fa1015d
child 14070 305c97670d7a
encoding: add an encoding-aware lower function
mercurial/encoding.py
--- a/mercurial/encoding.py	Sat Apr 30 12:39:46 2011 +0200
+++ b/mercurial/encoding.py	Sat Apr 30 10:57:13 2011 -0500
@@ -148,3 +148,17 @@
         return sum([w(c) in wide and 2 or 1 for c in d])
     return len(d)
 
+def lower(s):
+    "best-effort encoding-aware case-folding of local string s"
+    try:
+        if isinstance(s, localstr):
+            u = s._utf8.decode("utf-8")
+        else:
+            u = s.decode(encoding, encodingmode)
+
+        lu = u.lower()
+        if u == lu:
+            return s # preserve localstring
+        return lu.encode(encoding)
+    except UnicodeError:
+        return s.lower() # we don't know how to fold this except in ASCII