[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [arm/master] fix: miscropping strings breaking on first word



commit e6b64e48e67a5590a4069510f7f53bb28c11ed7d
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Fri Jun 17 19:29:36 2011 -0700

    fix: miscropping strings breaking on first word
    
    When cropping strings where we'd be splitting the first word only the last
    character would be dropped. For instance...
    
    >>> uiTools.cropStr("CookieAuthFileGroupReadable", 8)
    'CookieAuthFileGroupReadabl...'
    
    rather than...
    
    >>> uiTools.cropStr("CookieAuthFileGroupReadable", 8)
    'Cooki...'
---
 src/util/uiTools.py |   21 +++++++++++++++------
 1 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/util/uiTools.py b/src/util/uiTools.py
index 5204960..1b32caa 100644
--- a/src/util/uiTools.py
+++ b/src/util/uiTools.py
@@ -242,13 +242,22 @@ def cropStr(msg, size, minWordLen = 4, minCrop = 0, endType = Ending.ELLIPSE, ge
   
   # checks if there isn't the minimum space needed to include anything
   lastWordbreak = msg.rfind(" ", 0, size + 1)
-  lastWordbreak = len(msg[:lastWordbreak].rstrip()) # drops extra ending whitespaces
-  if (minWordLen != None and size < minWordLen) or (minWordLen == None and lastWordbreak < 1):
-    if getRemainder: return ("", msg)
-    else: return ""
   
-  if minWordLen == None: minWordLen = sys.maxint
-  includeCrop = size - lastWordbreak - 1 >= minWordLen
+  if lastWordbreak == -1:
+    # we're splitting the first word
+    if minWordLen == None or size < minWordLen:
+      if getRemainder: return ("", msg)
+      else: return ""
+    
+    includeCrop = True
+  else:
+    lastWordbreak = len(msg[:lastWordbreak].rstrip()) # drops extra ending whitespaces
+    if (minWordLen != None and size < minWordLen) or (minWordLen == None and lastWordbreak < 1):
+      if getRemainder: return ("", msg)
+      else: return ""
+    
+    if minWordLen == None: minWordLen = sys.maxint
+    includeCrop = size - lastWordbreak - 1 >= minWordLen
   
   # if there's a max crop size then make sure we're cropping at least that many characters
   if includeCrop and minCrop:



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits