Mercurial > public > mercurial-scm > hg
comparison mercurial/encoding.py @ 17235:3745ae495ce5 stable
encoding: use s.decode to trigger UnicodeDecodeError
When calling encode on a str, the string is first decoded using the
default encoding and then encoded. So
s.encode('ascii') == s.decode().encode('ascii')
We don't care about the encode step here -- we're just after the
UnicodeDecodeError raised by decode if it finds a non-ASCII character.
This way is also marginally faster since it saves the construction of
the extra str object.
author | Martin Geisler <mg@aragost.com> |
---|---|
date | Mon, 23 Jul 2012 15:55:22 -0600 |
parents | 72c6240a4b7d |
children | 9fb8312dbdbd |
comparison
equal
deleted
inserted
replaced
17234:0cfece81e051 | 17235:3745ae495ce5 |
---|---|
166 return t | 166 return t |
167 | 167 |
168 def lower(s): | 168 def lower(s): |
169 "best-effort encoding-aware case-folding of local string s" | 169 "best-effort encoding-aware case-folding of local string s" |
170 try: | 170 try: |
171 return s.encode('ascii').lower() | 171 s.decode('ascii') # throw exception for non-ASCII character |
172 except UnicodeError: | 172 return s.lower() |
173 except UnicodeDecodeError: | |
173 pass | 174 pass |
174 try: | 175 try: |
175 if isinstance(s, localstr): | 176 if isinstance(s, localstr): |
176 u = s._utf8.decode("utf-8") | 177 u = s._utf8.decode("utf-8") |
177 else: | 178 else: |