Skip to content

Commit 80c8c53

Browse files
committed
Stop the pre-split at whitespace, hyphens and max_lines
A word that failed the whitespace check re-ran split() on the rest of the word for every line, which was slower than main. Pre-split only the pieces before the first whitespace or hyphen, and only as many as the remaining max_lines can show; the rest of the word keeps the old path.
1 parent ed1cfc2 commit 80c8c53

2 files changed

Lines changed: 55 additions & 10 deletions

File tree

‎Lib/test/test_textwrap.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,8 @@ def test_break_very_long_word(self):
631631
self.check_wrap('x ' + 'a' * 25, 10,
632632
['x aaaaaaaa', ' aaaaaaaa', ' aaaaaaaa', ' a'],
633633
subsequent_indent=' ')
634+
self.check_wrap('a' * 12, 6, [' aaaa', 'aaaaaa', 'aa'],
635+
initial_indent=' ')
634636

635637
# Each piece is broken after its last hyphen.
636638
self.check_wrap('12345-67890-12345-67890-12345', 10,
@@ -640,6 +642,35 @@ def test_break_very_long_word(self):
640642
self.check_wrap('abc-1234567890123-4567890123456', 10,
641643
['abc-123456', '7890123-45', '6789012345', '6'],
642644
break_on_hyphens=False)
645+
self.check_wrap('x ' + 'a' * 10 + '-bb', 4,
646+
['x aa', 'aaaa', 'aaaa', '-bb'])
647+
648+
def test_break_very_long_word_with_whitespace(self):
649+
# Non-ASCII whitespace is part of a word. A piece of the word that
650+
# is all whitespace is dropped like any other whitespace.
651+
self.check_wrap('x aa' + '\xa0' * 6 + ' z', 4, ['x aa', ' z'])
652+
self.check_wrap('x ' + 'a' * 10 + '\xa0' + 'b' * 6, 4,
653+
['x aa', 'aaaa', 'aaaa', '\xa0bbb', 'bbb'])
654+
self.check_wrap('x aa' + '\n' * 6 + 'z', 4, ['x aa', 'z'],
655+
replace_whitespace=False)
656+
657+
def test_break_very_long_word_wide_indent(self):
658+
# An indent as wide as the width leaves one character per line.
659+
self.check_wrap('x aaa', 3, ['x', ' a', ' a', ' a'],
660+
subsequent_indent=' ')
661+
self.check_wrap('x aaa', 3, ['x', ' a', ' a', ' a'],
662+
subsequent_indent=' ')
663+
664+
def test_break_very_long_word_max_lines(self):
665+
for n, expect in [
666+
(9, ['x aa', 'aaaa', 'aaa']),
667+
(10, ['x aa', 'aaaa', 'aaaa']),
668+
(11, ['x aa', 'aaaa', '..']),
669+
(40, ['x aa', 'aaaa', '..']),
670+
]:
671+
with self.subTest(n=n):
672+
self.check_wrap('x ' + 'a' * n, 4, expect,
673+
max_lines=3, placeholder='..')
643674

644675
def test_nobreak_long(self):
645676
# Test with break_long_words disabled

‎Lib/textwrap.py‎

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
# some Unicode spaces (like \u00a0) are non-breaking whitespaces.
1515
_whitespace = '\t\n\x0b\x0c\r '
1616

17+
# Used by TextWrapper._wrap_chunks() to pre-split long words.
18+
_any_whitespace_re = re.compile(r'\s')
19+
_whitespace_or_hyphen_re = re.compile(r'[\s-]')
20+
1721
class TextWrapper:
1822
"""
1923
Object for wrapping/filling text. The public interface consists of
@@ -279,17 +283,27 @@ def _wrap_chunks(self, chunks):
279283
# Maximum width for this line.
280284
width = self.width - len(indent)
281285

282-
# This and all following lines have the same width, so split a
283-
# long word for all of them at once. Breaking off one line at a
284-
# time would copy the rest of the word each time.
286+
# Pre-split a long word for this and the following lines, which
287+
# all have the same width, so the rest is not copied for every
288+
# line. Stop before the first whitespace (a piece of it could be
289+
# dropped) or hyphen (a break point), and at the line limit.
285290
word = chunks[-1]
286-
if (lines and self.max_lines is None and self.break_long_words and
287-
0 < width < len(word) and
288-
not (self.break_on_hyphens and '-' in word) and
289-
word.split(None, 1) == [word]):
290-
del chunks[-1]
291-
chunks.extend(word[i:i + width]
292-
for i in reversed(range(0, len(word), width)))
291+
if lines and self.break_long_words and 0 < width < len(word):
292+
n = len(word)
293+
if self.max_lines is not None:
294+
n = min(n, width * (self.max_lines - len(lines)))
295+
if self.break_on_hyphens:
296+
m = _whitespace_or_hyphen_re.search(word, 0, n)
297+
else:
298+
m = _any_whitespace_re.search(word, 0, n)
299+
if m is not None:
300+
n = m.start() // width * width
301+
if n:
302+
del chunks[-1]
303+
if n < len(word):
304+
chunks.append(word[n:])
305+
chunks.extend(word[i:i + width]
306+
for i in reversed(range(0, n, width)))
293307

294308
# First chunk on line is whitespace -- drop it, unless this
295309
# is the very beginning of the text (ie. no lines started yet).

0 commit comments

Comments
 (0)