MCPcopy Index your code
hub / github.com/python/cpython / _handle_long_word

Method _handle_long_word

Lib/textwrap.py:197–236  ·  view source on GitHub ↗

_handle_long_word(chunks : [string], cur_line : [string], cur_len : int, width : int) Handle a chunk of text (most likely a word, not whitespace) that is too long to fit in any line.

(self, reversed_chunks, cur_line, cur_len, width)

Source from the content-addressed store, hash-verified

195 i += 1
196
197 def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
198 """_handle_long_word(chunks : [string],
199 cur_line : [string],
200 cur_len : int, width : int)
201
202 Handle a chunk of text (most likely a word, not whitespace) that
203 is too long to fit in any line.
204 """
205 # Figure out when indent is larger than the specified width, and make
206 # sure at least one character is stripped off on every pass
207 if width < 1:
208 space_left = 1
209 else:
210 space_left = width - cur_len
211
212 # If we're allowed to break long words, then do so: put as much
213 # of the next chunk onto the current line as will fit.
214 if self.break_long_words and space_left > 0:
215 end = space_left
216 chunk = reversed_chunks[-1]
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
223 cur_line.append(chunk[:end])
224 reversed_chunks[-1] = chunk[end:]
225
226 # Otherwise, we have to preserve the long word intact. Only add
227 # it to the current line if there's nothing already there --
228 # that minimizes how much we violate the width constraint.
229 elif not cur_line:
230 cur_line.append(reversed_chunks.pop())
231
232 # If we're not allowed to break long words, and there's already
233 # text on the current line, do nothing. Next time through the
234 # main loop of _wrap_chunks(), we'll wind up here again, but
235 # cur_len will be zero, so the next line will be entirely
236 # devoted to the long word that we can't handle right now.
237
238 def _wrap_chunks(self, chunks):
239 """_wrap_chunks(chunks : [string]) -> [string]

Callers 1

_wrap_chunksMethod · 0.95

Calls 4

anyFunction · 0.70
rfindMethod · 0.45
appendMethod · 0.45
popMethod · 0.45

Tested by

no test coverage detected