comparison mercurial/util.py @ 13225:e3bf16703e26 stable 1.7.3

util: fix ellipsis() not to break multi-byte sequence (issue2564) It tries to convert localstr to unicode before truncating. Because we cannot assume that the given text is encoded in local encoding, it falls back to raw string in case of unicode error.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 25 Dec 2010 21:59:00 +0900
parents 5d0a30fad7de
children d18a748d9c33 035684c6b69a
comparison
equal deleted inserted replaced
13224:0a1eefaf98f2 13225:e3bf16703e26
1240 r = author.find('>') 1240 r = author.find('>')
1241 if r == -1: 1241 if r == -1:
1242 r = None 1242 r = None
1243 return author[author.find('<') + 1:r] 1243 return author[author.find('<') + 1:r]
1244 1244
1245 def _ellipsis(text, maxlength):
1246 if len(text) <= maxlength:
1247 return text, False
1248 else:
1249 return "%s..." % (text[:maxlength - 3]), True
1250
1245 def ellipsis(text, maxlength=400): 1251 def ellipsis(text, maxlength=400):
1246 """Trim string to at most maxlength (default: 400) characters.""" 1252 """Trim string to at most maxlength (default: 400) characters."""
1247 if len(text) <= maxlength: 1253 try:
1248 return text 1254 # use unicode not to split at intermediate multi-byte sequence
1249 else: 1255 utext, truncated = _ellipsis(text.decode(encoding.encoding),
1250 return "%s..." % (text[:maxlength - 3]) 1256 maxlength)
1257 if not truncated:
1258 return text
1259 return utext.encode(encoding.encoding)
1260 except (UnicodeDecodeError, UnicodeEncodeError):
1261 return _ellipsis(text, maxlength)[0]
1251 1262
1252 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False): 1263 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
1253 '''yield every hg repository under path, recursively.''' 1264 '''yield every hg repository under path, recursively.'''
1254 def errhandler(err): 1265 def errhandler(err):
1255 if err.filename == path: 1266 if err.filename == path: