diff --git a/Doc/library/difflib.rst b/Doc/library/difflib.rst --- a/Doc/library/difflib.rst +++ b/Doc/library/difflib.rst @@ -136,7 +136,7 @@ .. function:: context_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\\n') - Compare *a* and *b* (lists of strings); return a delta (a :term:`generator` + Compare *a* and *b* (sequences of strings); return a delta (a :term:`generator` generating the delta lines) in context diff format. Context diffs are a compact way of showing just the lines that have changed plus @@ -267,7 +267,7 @@ .. function:: unified_diff(a, b, fromfile='', tofile='', fromfiledate='', tofiledate='', n=3, lineterm='\\n') - Compare *a* and *b* (lists of strings); return a delta (a :term:`generator` + Compare *a* and *b* (sequences of strings); return a delta (a :term:`generator` generating the delta lines) in unified diff format. Unified diffs are a compact way of showing just the lines that have changed plus @@ -396,7 +396,9 @@ .. method:: find_longest_match(alo, ahi, blo, bhi) - Find longest matching block in ``a[alo:ahi]`` and ``b[blo:bhi]``. + Find longest matching block in ``a[alo:ahi]`` and ``b[blo:bhi]``, + returning a :term:`named tuple` ``Match(a, b, size)``. + If no blocks match, this returns ``(alo, blo, 0)``. If *isjunk* was omitted or ``None``, :meth:`find_longest_match` returns ``(i, j, k)`` such that ``a[i:i+k]`` is equal to ``b[j:j+k]``, where ``alo @@ -427,16 +429,14 @@ >>> s.find_longest_match(0, 5, 0, 9) Match(a=1, b=0, size=4) - If no blocks match, this returns ``(alo, blo, 0)``. - - This method returns a :term:`named tuple` ``Match(a, b, size)``. - .. method:: get_matching_blocks() - Return list of triples describing matching subsequences. Each triple is of - the form ``(i, j, n)``, and means that ``a[i:i+n] == b[j:j+n]``. The - triples are monotonically increasing in *i* and *j*. + Return an iterable containing triples that describe matching + subsequences. Each triple is a :term:`named tuple` + ``Match(a, b, size)``. For each tuple ``(i, j, n)``, + ``a[i:i+n] == b[j:j+n]``. The triples are monotonically increasing + in *i* and *j*. The last triple is a dummy, and has the value ``(len(a), len(b), 0)``. It is the only triple with ``n == 0``. If ``(i, j, n)`` and ``(i', j', n')`` @@ -449,7 +449,7 @@ .. doctest:: >>> s = SequenceMatcher(None, "abxcd", "abcd") - >>> s.get_matching_blocks() + >>> list(s.get_matching_blocks()) [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)] diff --git a/Lib/difflib.py b/Lib/difflib.py --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -337,6 +337,13 @@ def find_longest_match(self, alo, ahi, blo, bhi): """Find longest matching block in a[alo:ahi] and b[blo:bhi]. + The return value is a named tuple (a,b,size). + If no blocks match, return (alo, blo, 0). + + >>> s = SequenceMatcher(None, "ab", "c") + >>> s.find_longest_match(0, 2, 0, 1) + Match(a=0, b=0, size=0) + If isjunk is not defined: Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where @@ -370,12 +377,6 @@ >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") >>> s.find_longest_match(0, 5, 0, 9) Match(a=1, b=0, size=4) - - If no blocks match, return (alo, blo, 0). - - >>> s = SequenceMatcher(None, "ab", "c") - >>> s.find_longest_match(0, 2, 0, 1) - Match(a=0, b=0, size=0) """ # CAUTION: stripping common prefix or suffix would be incorrect. @@ -449,7 +450,7 @@ Each triple is of the form (i, j, n), and means that a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in - i and in j. New in Python 2.5, it's also guaranteed that if + i and in j. It's also guaranteed that if (i, j, n) and (i', j', n') are adjacent triples in the list, and the second is not the last triple in the list, then i+n != i' or j+n != j'. IOW, adjacent triples never describe adjacent equal @@ -464,7 +465,7 @@ """ if self.matching_blocks is not None: - return self.matching_blocks + return map(Match._make, self.matching_blocks) la, lb = len(self.a), len(self.b) # This is most naturally expressed as a recursive algorithm, but