Patchwork [2,of,8,STABLE] util: replace 'ellipsis' implementation by 'encoding.trim'

login
register
mail settings
Submitter Katsunori FUJIWARA
Date June 13, 2014, 4:22 p.m.
Message ID <2feac9a645eb92f8d70a.1402676550@feefifofum>
Download mbox | patch
Permalink /patch/4988/
State Superseded
Headers show

Comments

Katsunori FUJIWARA - June 13, 2014, 4:22 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1402675901 -32400
#      Sat Jun 14 01:11:41 2014 +0900
# Branch stable
# Node ID 2feac9a645eb92f8d70a83a513d151ee5fd55759
# Parent  b0986d208bd4dc9a5043326457fbb6dd5c3e25d6
util: replace 'ellipsis' implementation by 'encoding.trim'

Before this patch, 'util.ellipsis' tries to avoid splitting at
intermediate multi-byte sequence, but its implementation is incorrect.

Internal function '_ellipsis' trims specified unicode sequence not at
most maxlength 'columns in display', but at most maxlength number of
'unicode characters'.

    def _ellipsis(text, maxlength):
        if len(text) <= maxlength:
            return text, False
        else:
            return "%s..." % (text[:maxlength - 3]), True

In many encodings, number of unicode characters can be different from
columns in display.

This patch replaces 'ellipsis' implementation by 'encoding.trim',
which can trim string at most maxlength columns in display correctly,
even though specified string contains multi-byte characters.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1294,16 +1294,8 @@ 
         return "%s..." % (text[:maxlength - 3]), True
 
 def ellipsis(text, maxlength=400):
-    """Trim string to at most maxlength (default: 400) characters."""
-    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]
+    """Trim string to at most maxlength (default: 400) columns in display."""
+    return encoding.trim(text, maxlength, ellipsis='...')
 
 def unitcountfn(*unittable):
     '''return a function that renders a readable count of some quantity'''