]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Improved performance of filters.do_truncate() 317/head
authorEric Sh <eric@shedokan.com>
Thu, 3 Apr 2014 11:19:05 +0000 (14:19 +0300)
committerEric Sh <eric@shedokan.com>
Thu, 3 Apr 2014 11:19:05 +0000 (14:19 +0300)
jinja2/filters.py

index 41f91d9a97ea96672842e28d9b568eec6fe2ba45..882f915b9bd72461a77cc199b67eaa02abfcfcc7 100644 (file)
@@ -477,16 +477,11 @@ def do_truncate(s, length=255, killwords=False, end='...'):
         return s
     elif killwords:
         return s[:length - len(end)] + end
-    words = s.split(' ')
-    result = []
-    m = 0
-    for word in words:
-        m += len(word) + 1
-        if m > (length - len(end)):
-            break
-        result.append(word)
-    result.append(end)
-    return u' '.join(result)
+
+    result = s[:length - len(end)].rsplit(' ', 1)[0]
+    if len(result) < length:
+        result += ' '
+    return result + end
 
 @environmentfilter
 def do_wordwrap(environment, s, width=79, break_long_words=True,