@@ -413,7 +413,6 @@ def shorten(text, width, **kwargs):
413413
414414# -- Loosely related functionality -------------------------------------
415415
416- _whitespace_only_re = re .compile ('^[ \t ]+$' , re .MULTILINE )
417416_leading_whitespace_re = re .compile ('(^[ \t ]*)(?:[^ \t \n ])' , re .MULTILINE )
418417
419418def dedent (text ):
@@ -432,7 +431,7 @@ def dedent(text):
432431 # Look for the longest leading string of spaces and tabs common to
433432 # all lines.
434433 margin = None
435- text = _whitespace_only_re . sub ( '' , text )
434+ text = ' \n ' . join ( line if line . lstrip () else '' for line in text . split ( ' \n ' ) )
436435 indents = _leading_whitespace_re .findall (text )
437436 for indent in indents :
438437 if margin is None :
@@ -441,7 +440,7 @@ def dedent(text):
441440 # Current line more deeply indented than previous winner:
442441 # no change (previous winner is still on top).
443442 elif indent .startswith (margin ):
444- pass
443+ continue
445444
446445 # Current line consistent with and no deeper than previous winner:
447446 # it's the new winner.
@@ -455,15 +454,17 @@ def dedent(text):
455454 if x != y :
456455 margin = margin [:i ]
457456 break
458-
457+ if not margin :
458+ break
459459 # sanity check (testing/debugging only)
460460 if 0 and margin :
461461 for line in text .split ("\n " ):
462462 assert not line or line .startswith (margin ), \
463463 "line = %r, margin = %r" % (line , margin )
464464
465465 if margin :
466- text = re .sub (r'(?m)^' + margin , '' , text )
466+ l = len (margin )
467+ text = '\n ' .join ( line [l :] for line in text .split ('\n ' ))
467468 return text
468469
469470
0 commit comments