comparison mercurial/encoding.py @ 14069:e38846a79a23

encoding: add an encoding-aware lower function
author Matt Mackall <mpm@selenic.com>
date Sat, 30 Apr 2011 10:57:13 -0500
parents b7b26e54e37a
children 61807854004e
comparison
equal deleted inserted replaced
14068:04ce8fa1015d 14069:e38846a79a23
146 wide = "WFA" 146 wide = "WFA"
147 w = unicodedata.east_asian_width 147 w = unicodedata.east_asian_width
148 return sum([w(c) in wide and 2 or 1 for c in d]) 148 return sum([w(c) in wide and 2 or 1 for c in d])
149 return len(d) 149 return len(d)
150 150
151 def lower(s):
152 "best-effort encoding-aware case-folding of local string s"
153 try:
154 if isinstance(s, localstr):
155 u = s._utf8.decode("utf-8")
156 else:
157 u = s.decode(encoding, encodingmode)
158
159 lu = u.lower()
160 if u == lu:
161 return s # preserve localstring
162 return lu.encode(encoding)
163 except UnicodeError:
164 return s.lower() # we don't know how to fold this except in ASCII