[tor-commits] [stem/master] str_tools crop() issue with zero size
atagar at torproject.org
atagar at torproject.org
Sun Apr 10 18:20:33 UTC 2016
commit 2f31248f254d5dae2ed11c0a70b98e465c5b185c
Author: Damian Johnson <atagar at torproject.org>
Date: Sun Apr 10 11:19:26 2016 -0700
str_tools crop() issue with zero size
Our str_tools crop() function provided a longer string than it should when
given a width less than three. This is because ending with an ellipse subtracts
three causing us to have a negative size, causing a wrap.
---
stem/util/str_tools.py | 3 +++
test/unit/util/str_tools.py | 1 +
2 files changed, 4 insertions(+)
diff --git a/stem/util/str_tools.py b/stem/util/str_tools.py
index a64f6c2..a01eb8a 100644
--- a/stem/util/str_tools.py
+++ b/stem/util/str_tools.py
@@ -210,6 +210,9 @@ def crop(msg, size, min_word_length = 4, min_crop = 0, ending = Ending.ELLIPSE,
# ellipse, and cropping words requires an extra space for hyphens
if ending == Ending.ELLIPSE:
+ if size < 3:
+ return ('', msg) if get_remainder else ''
+
size -= 3
elif min_word_length and ending == Ending.HYPHEN:
min_word_length += 1
diff --git a/test/unit/util/str_tools.py b/test/unit/util/str_tools.py
index 922026c..3d0936f 100644
--- a/test/unit/util/str_tools.py
+++ b/test/unit/util/str_tools.py
@@ -30,6 +30,7 @@ class TestStrTools(unittest.TestCase):
self.assertEqual('This is a looo...', str_tools.crop('This is a looooong message', 17))
self.assertEqual('This is a...', str_tools.crop('This is a looooong message', 12))
self.assertEqual('', str_tools.crop('This is a looooong message', 3))
+ self.assertEqual('', str_tools.crop('This is a looooong message', 0))
def test_size_label(self):
"""
More information about the tor-commits
mailing list