@@ -413,7 +413,6 @@ def shorten(text, width, **kwargs):
413
413
414
414
# -- Loosely related functionality -------------------------------------
415
415
416
- _whitespace_only_re = re .compile ('^[ \t ]+$' , re .MULTILINE )
417
416
_leading_whitespace_re = re .compile ('(^[ \t ]*)(?:[^ \t \n ])' , re .MULTILINE )
418
417
419
418
def dedent (text ):
@@ -432,7 +431,7 @@ def dedent(text):
432
431
# Look for the longest leading string of spaces and tabs common to
433
432
# all lines.
434
433
margin = None
435
- text = _whitespace_only_re . sub ( '' , text )
434
+ text = ' \n ' . join ( line if line . lstrip () else '' for line in text . split ( ' \n ' ) )
436
435
indents = _leading_whitespace_re .findall (text )
437
436
for indent in indents :
438
437
if margin is None :
@@ -441,7 +440,7 @@ def dedent(text):
441
440
# Current line more deeply indented than previous winner:
442
441
# no change (previous winner is still on top).
443
442
elif indent .startswith (margin ):
444
- pass
443
+ continue
445
444
446
445
# Current line consistent with and no deeper than previous winner:
447
446
# it's the new winner.
@@ -455,15 +454,17 @@ def dedent(text):
455
454
if x != y :
456
455
margin = margin [:i ]
457
456
break
458
-
457
+ if not margin :
458
+ break
459
459
# sanity check (testing/debugging only)
460
460
if 0 and margin :
461
461
for line in text .split ("\n " ):
462
462
assert not line or line .startswith (margin ), \
463
463
"line = %r, margin = %r" % (line , margin )
464
464
465
465
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 ' ))
467
468
return text
468
469
469
470
0 commit comments