@@ -1490,8 +1490,7 @@ def decode(s):
14901490 for line in lines :
14911491 yield line .encode ('ascii' , 'surrogateescape' )
14921492
1493- def ndiff (a , b , linejunk = None , charjunk = IS_CHARACTER_JUNK ,
1494- linematcher = None , charmatcher = None ):
1493+ def ndiff (a , b , linejunk = None , charjunk = IS_CHARACTER_JUNK , differ = None ):
14951494 r"""
14961495 Compare `a` and `b` (lists of strings); return a `Differ`-style delta.
14971496
@@ -1509,13 +1508,9 @@ def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK,
15091508 whitespace characters (a blank or tab; note: it's a bad idea to
15101509 include newline in this!).
15111510
1512- - `linematcher`: callable that takes 3 positional arguments.
1513- i.e. matcher(isjunk, a, b) which returns SequenceMatcherBase instance
1514- Default (if None) is SequenceMatcher class.
1515-
1516- - `charmatcher`: callable that takes 3 positional arguments.
1517- i.e. matcher(isjunk, a, b) which returns SequenceMatcherBase instance
1518- Default (if None) is SequenceMatcher class.
1511+ - `differ`: callable that takes 2 positional arguments.
1512+ i.e. differ(linejunk, charjunk) which returns `Differ` instance
1513+ Default (if None) is Differ class.
15191514
15201515 Tools/scripts/ndiff.py is a command-line front-end to this function.
15211516
@@ -1534,11 +1529,15 @@ def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK,
15341529 + tree
15351530 + emu
15361531 """
1537- return Differ (linejunk , charjunk , linematcher , charmatcher ).compare (a , b )
1532+ if differ is None :
1533+ differ = Differ
1534+ elif not callable (differ ):
1535+ raise TypeError ("differ must be callable: %r" % (differ ,))
1536+
1537+ return differ (linejunk , charjunk ).compare (a , b )
15381538
15391539def _mdiff (fromlines , tolines , context = None ,
1540- linejunk = None , charjunk = IS_CHARACTER_JUNK ,
1541- linematcher = None , charmatcher = None ):
1540+ linejunk = None , charjunk = IS_CHARACTER_JUNK , differ = None ):
15421541 r"""Returns generator yielding marked up from/to side by side differences.
15431542
15441543 Arguments:
@@ -1548,7 +1547,7 @@ def _mdiff(fromlines, tolines, context=None,
15481547 if None, all from/to text lines will be generated.
15491548 linejunk -- passed on to ndiff (see ndiff documentation)
15501549 charjunk -- passed on to ndiff (see ndiff documentation)
1551- linematcher -- passed on to ndiff (see ndiff documentation)
1550+ differ -- passed on to ndiff (see ndiff documentation)
15521551 charmatcher -- passed on to ndiff (see ndiff documentation)
15531552
15541553 This function returns an iterator which returns a tuple:
@@ -1579,8 +1578,7 @@ def _mdiff(fromlines, tolines, context=None,
15791578 change_re = re .compile (r'(\++|\-+|\^+)' )
15801579
15811580 # create the difference iterator to generate the differences
1582- diff_lines_iterator = ndiff (fromlines , tolines , linejunk , charjunk ,
1583- linematcher , charmatcher )
1581+ diff_lines_iterator = ndiff (fromlines , tolines , linejunk , charjunk , differ )
15841582
15851583 def _make_line (lines , format_key , side , num_lines = [0 ,0 ]):
15861584 """Returns line of text with user's change markup and line formatting.
@@ -1925,15 +1923,14 @@ class HtmlDiff(object):
19251923 _default_prefix = 0
19261924
19271925 def __init__ (self ,tabsize = 8 , wrapcolumn = None ,
1928- linejunk = None , charjunk = IS_CHARACTER_JUNK ,
1929- linematcher = None , charmatcher = None ):
1926+ linejunk = None , charjunk = IS_CHARACTER_JUNK , differ = None ):
19301927 """HtmlDiff instance initializer
19311928
19321929 Arguments:
19331930 tabsize -- tab stop spacing, defaults to 8.
19341931 wrapcolumn -- column number where lines are broken and wrapped,
19351932 defaults to None where lines are not wrapped.
1936- linejunk,charjunk,linematcher,charmatcher -- keyword arguments
1933+ linejunk,charjunk,differ -- keyword arguments
19371934 passed into ndiff() (used by HtmlDiff() to generate the side
19381935 by side HTML differences). See ndiff() documentation for
19391936 argument default values and descriptions.
@@ -1942,8 +1939,7 @@ def __init__(self,tabsize=8, wrapcolumn=None,
19421939 self ._wrapcolumn = wrapcolumn
19431940 self ._linejunk = linejunk
19441941 self ._charjunk = charjunk
1945- self ._linematcher = linematcher
1946- self ._charmatcher = charmatcher
1942+ self ._differ = differ
19471943
19481944 def make_file (self , fromlines , tolines , fromdesc = '' , todesc = '' ,
19491945 context = False , numlines = 5 , * , charset = 'utf-8' ):
@@ -2216,7 +2212,7 @@ def make_table(self,fromlines,tolines,fromdesc='',todesc='',context=False,
22162212 context_lines = None
22172213 diffs = _mdiff (fromlines , tolines , context_lines ,
22182214 linejunk = self ._linejunk , charjunk = self ._charjunk ,
2219- linematcher = self ._linematcher , charmatcher = self . _charmatcher )
2215+ differ = self ._differ )
22202216
22212217 # set up iterator to wrap lines that exceed desired width
22222218 if self ._wrapcolumn :
@@ -2774,7 +2770,7 @@ class GestaltSequenceMatcher(SequenceMatcherBase):
27742770
27752771 Time Complexity:
27762772 find_longest_match : O(n)
2777- get_matching_blocks : O(n) average for common diff case
2773+ get_matching_blocks : O(n) - O(nlogn) for average diff case
27782774 O(n^2) worst case.
27792775
27802776 Example of worst case complexity `get_matching_blocks` case:
0 commit comments