Skip to content

Commit 1c2678e

Browse files
committed
minor optimization, using lstrip and more concise margin_len computation
1 parent e703e0d commit 1c2678e

File tree

1 file changed

+2
-10
lines changed

1 file changed

+2
-10
lines changed

Lib/textwrap.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,17 +434,9 @@ def dedent(text):
434434
lines = text.split("\n")
435435

436436
splitting = os.path.commonprefix(tuple(filter(lambda x: x.lstrip(), lines)))
437+
margin_len = len(splitting) - len(splitting.lstrip())
437438

438-
margin_len = 0
439-
440-
for split in splitting:
441-
if ' \t' in split :
442-
margin_len += 1
443-
else:
444-
break
445-
446-
# Apply margin removal (most common case) with minimal operations
447-
return "\n".join([line[margin_len:] if line.strip() else "\n" if line.endswith("\n") else "" for line in lines])
439+
return "\n".join([line[margin_len:] if line.lstrip() else "\n" if line.endswith("\n") else "" for line in lines])
448440

449441

450442
def indent(text, prefix, predicate=None):

0 commit comments

Comments
 (0)