diff 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
line wrap: on
line diff
--- a/mercurial/util.py	Fri Dec 31 15:14:51 2010 +0100
+++ b/mercurial/util.py	Sat Dec 25 21:59:00 2010 +0900
@@ -1242,12 +1242,23 @@
         r = None
     return author[author.find('<') + 1:r]
 
+def _ellipsis(text, maxlength):
+    if len(text) <= maxlength:
+        return text, False
+    else:
+        return "%s..." % (text[:maxlength - 3]), True
+
 def ellipsis(text, maxlength=400):
     """Trim string to at most maxlength (default: 400) characters."""
-    if len(text) <= maxlength:
-        return text
-    else:
-        return "%s..." % (text[:maxlength - 3])
+    try:
+        # use unicode not to split at intermediate multi-byte sequence
+        utext, truncated = _ellipsis(text.decode(encoding.encoding),
+                                     maxlength)
+        if not truncated:
+            return text
+        return utext.encode(encoding.encoding)
+    except (UnicodeDecodeError, UnicodeEncodeError):
+        return _ellipsis(text, maxlength)[0]
 
 def walkrepos(path, followsym=False, seen_dirs=None, recurse=False):
     '''yield every hg repository under path, recursively.'''