diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-08-21 19:18:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-21 19:18:26 (GMT) |
commit | 7e293f5e4c27592c6808bd7db13d7bd09dd7f5a0 (patch) | |
tree | 9b6cb3fbd9fdcf60c68e39955629fc897109cb83 /Lib/difflib.py | |
parent | 349d897e0f54f2991ffe518c8ac3ef55c30c293b (diff) | |
download | cpython-7e293f5e4c27592c6808bd7db13d7bd09dd7f5a0.zip cpython-7e293f5e4c27592c6808bd7db13d7bd09dd7f5a0.tar.gz cpython-7e293f5e4c27592c6808bd7db13d7bd09dd7f5a0.tar.bz2 |
Fix difflib `?` hint in diff output when dealing with tabs (GH-15201)
(cherry picked from commit e1c638da6a065af6803028ced1afcc679e63f59d)
Co-authored-by: Anthony Sottile <asottile@umich.edu>
Diffstat (limited to 'Lib/difflib.py')
-rw-r--r-- | Lib/difflib.py | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/Lib/difflib.py b/Lib/difflib.py index 887c3c2..3de1b3d 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -733,20 +733,15 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6): # Strip scores for the best n matches return [x for score, x in result] -def _count_leading(line, ch): - """ - Return number of `ch` characters at the start of `line`. - Example: +def _keep_original_ws(s, tag_s): + """Replace whitespace with the original whitespace characters in `s`""" + return ''.join( + c if tag_c == " " and c.isspace() else tag_c + for c, tag_c in zip(s, tag_s) + ) - >>> _count_leading(' abc', ' ') - 3 - """ - i, n = 0, len(line) - while i < n and line[i] == ch: - i += 1 - return i class Differ: r""" @@ -1033,7 +1028,7 @@ class Differ: def _qformat(self, aline, bline, atags, btags): r""" - Format "?" output and deal with leading tabs. + Format "?" output and deal with tabs. Example: @@ -1047,22 +1042,16 @@ class Differ: '+ \tabcdefGhijkl\n' '? \t ^ ^ ^\n' """ - - # Can hurt, but will probably help most of the time. - common = min(_count_leading(aline, "\t"), - _count_leading(bline, "\t")) - common = min(common, _count_leading(atags[:common], " ")) - common = min(common, _count_leading(btags[:common], " ")) - atags = atags[common:].rstrip() - btags = btags[common:].rstrip() + atags = _keep_original_ws(aline, atags).rstrip() + btags = _keep_original_ws(bline, btags).rstrip() yield "- " + aline if atags: - yield "? %s%s\n" % ("\t" * common, atags) + yield f"? {atags}\n" yield "+ " + bline if btags: - yield "? %s%s\n" % ("\t" * common, btags) + yield f"? {btags}\n" # With respect to junk, an earlier version of ndiff simply refused to # *start* a match with a junk element. The result was cases like this: |