Skip to content

Commit bf3fffa

Browse files
committed
Move the splitting into _wrap_chunks()
1 parent 5e9713a commit bf3fffa

1 file changed

Lines changed: 19 additions & 29 deletions

File tree

‎Lib/textwrap.py‎

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,6 @@ def _fix_sentence_endings(self, chunks):
194194
else:
195195
i += 1
196196

197-
def _break_end(self, chunk, start, width):
198-
"""Return where to break chunk to fit width characters from start."""
199-
end = start + width
200-
if self.break_on_hyphens and len(chunk) > end:
201-
# break after last hyphen, but only if there are
202-
# non-hyphens before it
203-
hyphen = chunk.rfind('-', start, end)
204-
if hyphen > start and any(c != '-' for c in chunk[start:hyphen]):
205-
end = hyphen + 1
206-
return end
207-
208197
def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
209198
"""_handle_long_word(chunks : [string],
210199
cur_line : [string],
@@ -223,26 +212,16 @@ def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
223212
# If we're allowed to break long words, then do so: put as much
224213
# of the next chunk onto the current line as will fit.
225214
if self.break_long_words and space_left > 0:
215+
end = space_left
226216
chunk = reversed_chunks[-1]
227-
end = self._break_end(chunk, 0, space_left)
217+
if self.break_on_hyphens and len(chunk) > space_left:
218+
# break after last hyphen, but only if there are
219+
# non-hyphens before it
220+
hyphen = chunk.rfind('-', 0, space_left)
221+
if hyphen > 0 and any(c != '-' for c in chunk[:hyphen]):
222+
end = hyphen + 1
228223
cur_line.append(chunk[:end])
229-
# Pre-split the rest to avoid quadratic copying. This relies on
230-
# _wrap_chunks() giving every following line the width
231-
# self.width - len(self.subsequent_indent). Words with whitespace
232-
# take the slow path, since a piece could be dropped.
233-
line_width = self.width - len(self.subsequent_indent)
234-
if (line_width >= 1 and len(chunk) - end > line_width and
235-
chunk.split(None, 1) == [chunk]):
236-
pieces = []
237-
while len(chunk) - end > line_width:
238-
start = end
239-
end = self._break_end(chunk, start, line_width)
240-
pieces.append(chunk[start:end])
241-
pieces.append(chunk[end:])
242-
pieces.reverse()
243-
reversed_chunks[-1:] = pieces
244-
else:
245-
reversed_chunks[-1] = chunk[end:]
224+
reversed_chunks[-1] = chunk[end:]
246225

247226
# Otherwise, we have to preserve the long word intact. Only add
248227
# it to the current line if there's nothing already there --
@@ -300,6 +279,17 @@ def _wrap_chunks(self, chunks):
300279
# Maximum width for this line.
301280
width = self.width - len(indent)
302281

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.
285+
word = chunks[-1]
286+
if (lines and self.break_long_words and 0 < width < len(word) and
287+
not (self.break_on_hyphens and '-' in word) and
288+
word.split(None, 1) == [word]):
289+
del chunks[-1]
290+
chunks.extend(word[i:i + width]
291+
for i in reversed(range(0, len(word), width)))
292+
303293
# First chunk on line is whitespace -- drop it, unless this
304294
# is the very beginning of the text (ie. no lines started yet).
305295
if self.drop_whitespace and chunks[-1].strip() == '' and lines:

0 commit comments

Comments
 (0)