Skip to content

Commit 19e76d3

Browse files
committed
Further micro-optimisation for entirely blank input
1 parent fec6717 commit 19e76d3

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Lib/textwrap.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,14 @@ def dedent(text):
429429
if not text:
430430
return text
431431

432-
lines = text.split('\n')
432+
# If the input is entirely whitespace, return normalized lines
433+
if text.isspace():
434+
return '\n' * text.count('\n')
433435

434-
non_blank_lines = [l for l in lines if l and not l.isspace()]
435-
if not non_blank_lines:
436-
return '\n'.join([l if l and not l.isspace() else '' for l in lines])
436+
lines = text.split('\n')
437437

438438
# Get length of leading whitespace, inspired by ``os.path.commonprefix()``
439+
non_blank_lines = [l for l in lines if l and not l.isspace()]
439440
l1 = min(non_blank_lines)
440441
l2 = max(non_blank_lines)
441442
margin = 0

0 commit comments

Comments
 (0)