Skip to content
57 changes: 57 additions & 0 deletions Lib/test/test_textwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,63 @@ def test_break_long(self):
'word,',
'anyways?'])

def test_break_very_long_word(self):
# A word much longer than the width is broken into many lines.
text = ''.join(map(str, range(1000)))
lines = wrap(text, 7)
self.assertEqual(''.join(lines), text)
self.assertEqual([len(line) for line in lines],
[7] * (len(text) // 7) + [len(text) % 7])

# The first line can be narrower than the following ones, and the
# last piece of the word shares its line with the next words.
self.check_wrap('a' * 25 + ' b c', 10,
['* aaaaaaaa', ' aaaaaaaa', ' aaaaaaaa', ' a b c'],
initial_indent='* ', subsequent_indent=' ')
self.check_wrap('x ' + 'a' * 25, 10,
['x aaaaaaaa', ' aaaaaaaa', ' aaaaaaaa', ' a'],
subsequent_indent=' ')
self.check_wrap('a' * 12, 6, [' aaaa', 'aaaaaa', 'aa'],
initial_indent=' ')

# Each piece is broken after its last hyphen.
self.check_wrap('12345-67890-12345-67890-12345', 10,
['12345-', '67890-', '12345-', '67890-', '12345'])
self.check_wrap('abc-1234567890123-4567890123456', 10,
['abc-', '1234567890', '123-', '4567890123', '456'])
self.check_wrap('abc-1234567890123-4567890123456', 10,
['abc-123456', '7890123-45', '6789012345', '6'],
break_on_hyphens=False)
self.check_wrap('x ' + 'a' * 10 + '-bb', 4,
['x aa', 'aaaa', 'aaaa', '-bb'])

def test_break_very_long_word_with_whitespace(self):
# Non-ASCII whitespace is part of a word. A piece of the word that
# is all whitespace is dropped like any other whitespace.
self.check_wrap('x aa' + '\xa0' * 6 + ' z', 4, ['x aa', ' z'])
self.check_wrap('x ' + 'a' * 10 + '\xa0' + 'b' * 6, 4,
['x aa', 'aaaa', 'aaaa', '\xa0bbb', 'bbb'])
self.check_wrap('x aa' + '\n' * 6 + 'z', 4, ['x aa', 'z'],
replace_whitespace=False)

def test_break_very_long_word_wide_indent(self):
# An indent as wide as the width leaves one character per line.
self.check_wrap('x aaa', 3, ['x', ' a', ' a', ' a'],
subsequent_indent=' ')
self.check_wrap('x aaa', 3, ['x', ' a', ' a', ' a'],
subsequent_indent=' ')

def test_break_very_long_word_max_lines(self):
for n, expect in [
(9, ['x aa', 'aaaa', 'aaa']),
(10, ['x aa', 'aaaa', 'aaaa']),
(11, ['x aa', 'aaaa', '..']),
(40, ['x aa', 'aaaa', '..']),
]:
with self.subTest(n=n):
self.check_wrap('x ' + 'a' * n, 4, expect,
max_lines=3, placeholder='..')

def test_nobreak_long(self):
# Test with break_long_words disabled
self.wrapper.break_long_words = 0
Expand Down
26 changes: 26 additions & 0 deletions Lib/textwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
# some Unicode spaces (like \u00a0) are non-breaking whitespaces.
_whitespace = '\t\n\x0b\x0c\r '

# Used by TextWrapper._wrap_chunks() to pre-split long words.
_any_whitespace_re = re.compile(r'\s')
_whitespace_or_hyphen_re = re.compile(r'[\s-]')

class TextWrapper:
"""
Object for wrapping/filling text. The public interface consists of
Expand Down Expand Up @@ -279,6 +283,28 @@ def _wrap_chunks(self, chunks):
# Maximum width for this line.
width = self.width - len(indent)

# Pre-split a long word for this and the following lines, which
# all have the same width, so the rest is not copied for every
# line. Stop before the first whitespace (a piece of it could be
# dropped) or hyphen (a break point), and at the line limit.
word = chunks[-1]
if lines and self.break_long_words and 0 < width < len(word):
n = len(word)
if self.max_lines is not None:
n = min(n, width * (self.max_lines - len(lines)))
if self.break_on_hyphens:
m = _whitespace_or_hyphen_re.search(word, 0, n)
else:
m = _any_whitespace_re.search(word, 0, n)
if m is not None:
n = m.start() // width * width
if n:
del chunks[-1]
if n < len(word):
chunks.append(word[n:])
chunks.extend(word[i:i + width]
for i in reversed(range(0, n, width)))

# First chunk on line is whitespace -- drop it, unless this
# is the very beginning of the text (ie. no lines started yet).
if self.drop_whitespace and chunks[-1].strip() == '' and lines:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix quadratic time complexity in :mod:`textwrap` when breaking a long word
into many lines.
Loading