Mercurial > hg
changeset 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 | 04ce8fa1015d |
children | 305c97670d7a |
files | mercurial/encoding.py |
diffstat | 1 files changed, 14 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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