8000 gh-130167: Optimise ``textwrap.dedent()`` by AA-Turner · Pull Request #131919 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-130167: Optimise textwrap.dedent() #131919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 31, 2025
Prev Previous commit
Next Next commit
Remove early-exit check for whitespace-only input, as suggested by Pi…
…eter Eendebak
  • Loading branch information
AA-Turner committed Mar 30, 2025
commit f2050aef1128021cb6fa5e29b60cb19741d3be2f
9 changes: 3 additions & 6 deletions Lib/textwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,13 @@ def dedent(text):
if not text:
return text

# If the input is entirely whitespace, return normalized lines.
if text.isspace():
return '\n' * text.count('\n')

lines = text.split('\n')

# Get length of leading whitespace, inspired by ``os.path.commonprefix()``.
non_blank_lines = [l for l in lines if l and not l.isspace()]
l1 = min(non_blank_lines)
l2 = max(non_blank_lines)
l1 = min(non_blank_lines, default='')
l2 = max(non_blank_lines, default='')
margin = 0
for margin, c in enumerate(l1):
if c != l2[margin] or c not in ' \t':
break
Expand Down
Loading
0