annotate Lib/difflib.py @ 35276:4adbd331d335 legacy-trunk

Fix all wrong instances of "it's".
author Georg Brandl <georg@python.org>
date Fri, 22 Jul 2005 21:49:32 +0000
parents ec29955b8543
children 16bfe9386068
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1 #! /usr/bin/env python
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
2
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
3 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
4 Module difflib -- helpers for computing deltas between objects.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
5
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
6 Function get_close_matches(word, possibilities, n=3, cutoff=0.6):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
7 Use SequenceMatcher to return list of the best "good enough" matches.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
8
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
9 Function context_diff(a, b):
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
10 For two lists of strings, return a delta in context diff format.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
11
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
12 Function ndiff(a, b):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
13 Return a delta: the difference between `a` and `b` (lists of strings).
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
14
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
15 Function restore(delta, which):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
16 Return one of the two sequences that generated an ndiff delta.
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
17
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
18 Function unified_diff(a, b):
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
19 For two lists of strings, return a delta in unified diff format.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
20
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
21 Class SequenceMatcher:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
22 A flexible class for comparing pairs of sequences of any type.
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
23
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
24 Class Differ:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
25 For producing human-readable deltas from sequences of lines of text.
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
26
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
27 Class HtmlDiff:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
28 For producing HTML side by side comparison with change highlights.
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
29 """
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
30
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
31 __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher',
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
32 'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff',
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
33 'unified_diff', 'HtmlDiff']
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
34
32339
408a40e65247 Apply heapq.nlargest() to find best matches.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
35 import heapq
408a40e65247 Apply heapq.nlargest() to find best matches.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
36
29719
fa4cf878c538 Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents: 29517
diff changeset
37 def _calculate_ratio(matches, length):
fa4cf878c538 Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents: 29517
diff changeset
38 if length:
fa4cf878c538 Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents: 29517
diff changeset
39 return 2.0 * matches / length
fa4cf878c538 Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents: 29517
diff changeset
40 return 1.0
fa4cf878c538 Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents: 29517
diff changeset
41
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
42 class SequenceMatcher:
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
43
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
44 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
45 SequenceMatcher is a flexible class for comparing pairs of sequences of
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
46 any type, so long as the sequence elements are hashable. The basic
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
47 algorithm predates, and is a little fancier than, an algorithm
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
48 published in the late 1980's by Ratcliff and Obershelp under the
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
49 hyperbolic name "gestalt pattern matching". The basic idea is to find
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
50 the longest contiguous matching subsequence that contains no "junk"
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
51 elements (R-O doesn't address junk). The same idea is then applied
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
52 recursively to the pieces of the sequences to the left and to the right
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
53 of the matching subsequence. This does not yield minimal edit
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
54 sequences, but does tend to yield matches that "look right" to people.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
55
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
56 SequenceMatcher tries to compute a "human-friendly diff" between two
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
57 sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
58 longest *contiguous* & junk-free matching subsequence. That's what
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
59 catches peoples' eyes. The Windows(tm) windiff has another interesting
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
60 notion, pairing up elements that appear uniquely in each sequence.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
61 That, and the method here, appear to yield more intuitive difference
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
62 reports than does diff. This method appears to be the least vulnerable
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
63 to synching up on blocks of "junk lines", though (like blank lines in
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
64 ordinary text files, or maybe "<P>" lines in HTML files). That may be
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
65 because this is the only method of the 3 that has a *concept* of
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
66 "junk" <wink>.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
67
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
68 Example, comparing two strings, and considering blanks to be "junk":
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
69
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
70 >>> s = SequenceMatcher(lambda x: x == " ",
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
71 ... "private Thread currentThread;",
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
72 ... "private volatile Thread currentThread;")
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
73 >>>
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
74
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
75 .ratio() returns a float in [0, 1], measuring the "similarity" of the
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
76 sequences. As a rule of thumb, a .ratio() value over 0.6 means the
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
77 sequences are close matches:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
78
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
79 >>> print round(s.ratio(), 3)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
80 0.866
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
81 >>>
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
82
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
83 If you're only interested in where the sequences match,
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
84 .get_matching_blocks() is handy:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
85
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
86 >>> for block in s.get_matching_blocks():
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
87 ... print "a[%d] and b[%d] match for %d elements" % block
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
88 a[0] and b[0] match for 8 elements
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
89 a[8] and b[17] match for 6 elements
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
90 a[14] and b[23] match for 15 elements
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
91 a[29] and b[38] match for 0 elements
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
92
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
93 Note that the last tuple returned by .get_matching_blocks() is always a
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
94 dummy, (len(a), len(b), 0), and this is the only case in which the last
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
95 tuple element (number of elements matched) is 0.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
96
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
97 If you want to know how to change the first sequence into the second,
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
98 use .get_opcodes():
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
99
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
100 >>> for opcode in s.get_opcodes():
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
101 ... print "%6s a[%d:%d] b[%d:%d]" % opcode
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
102 equal a[0:8] b[0:8]
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
103 insert a[8:8] b[8:17]
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
104 equal a[8:14] b[17:23]
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
105 equal a[14:29] b[23:38]
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
106
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
107 See the Differ class for a fancy human-friendly file differencer, which
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
108 uses SequenceMatcher both to compare sequences of lines, and to compare
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
109 sequences of characters within similar (near-matching) lines.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
110
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
111 See also function get_close_matches() in this module, which shows how
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
112 simple code building on SequenceMatcher can be used to do useful work.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
113
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
114 Timing: Basic R-O is cubic time worst case and quadratic time expected
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
115 case. SequenceMatcher is quadratic time for the worst case and has
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
116 expected-case behavior dependent in a complicated way on how many
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
117 elements the sequences have in common; best case time is linear.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
118
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
119 Methods:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
120
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
121 __init__(isjunk=None, a='', b='')
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
122 Construct a SequenceMatcher.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
123
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
124 set_seqs(a, b)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
125 Set the two sequences to be compared.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
126
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
127 set_seq1(a)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
128 Set the first sequence to be compared.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
129
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
130 set_seq2(b)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
131 Set the second sequence to be compared.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
132
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
133 find_longest_match(alo, ahi, blo, bhi)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
134 Find longest matching block in a[alo:ahi] and b[blo:bhi].
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
135
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
136 get_matching_blocks()
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
137 Return list of triples describing matching subsequences.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
138
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
139 get_opcodes()
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
140 Return list of 5-tuples describing how to turn a into b.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
141
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
142 ratio()
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
143 Return a measure of the sequences' similarity (float in [0,1]).
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
144
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
145 quick_ratio()
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
146 Return an upper bound on .ratio() relatively quickly.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
147
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
148 real_quick_ratio()
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
149 Return an upper bound on ratio() very quickly.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
150 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
151
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
152 def __init__(self, isjunk=None, a='', b=''):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
153 """Construct a SequenceMatcher.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
154
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
155 Optional arg isjunk is None (the default), or a one-argument
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
156 function that takes a sequence element and returns true iff the
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
157 element is junk. None is equivalent to passing "lambda x: 0", i.e.
16695
e14c5f34fa05 Fix a few small typos in the docstrings.
Fred Drake <fdrake@acm.org>
parents: 16551
diff changeset
158 no elements are considered to be junk. For example, pass
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
159 lambda x: x in " \\t"
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
160 if you're comparing lines as sequences of characters, and don't
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
161 want to synch up on blanks or hard tabs.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
162
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
163 Optional arg a is the first of two sequences to be compared. By
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
164 default, an empty string. The elements of a must be hashable. See
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
165 also .set_seqs() and .set_seq1().
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
166
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
167 Optional arg b is the second of two sequences to be compared. By
16695
e14c5f34fa05 Fix a few small typos in the docstrings.
Fred Drake <fdrake@acm.org>
parents: 16551
diff changeset
168 default, an empty string. The elements of b must be hashable. See
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
169 also .set_seqs() and .set_seq2().
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
170 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
171
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
172 # Members:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
173 # a
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
174 # first sequence
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
175 # b
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
176 # second sequence; differences are computed as "what do
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
177 # we need to do to 'a' to change it into 'b'?"
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
178 # b2j
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
179 # for x in b, b2j[x] is a list of the indices (into b)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
180 # at which x appears; junk elements do not appear
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
181 # fullbcount
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
182 # for x in b, fullbcount[x] == the number of times x
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
183 # appears in b; only materialized if really needed (used
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
184 # only for computing quick_ratio())
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
185 # matching_blocks
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
186 # a list of (i, j, k) triples, where a[i:i+k] == b[j:j+k];
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
187 # ascending & non-overlapping in i and in j; terminated by
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
188 # a dummy (len(a), len(b), 0) sentinel
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
189 # opcodes
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
190 # a list of (tag, i1, i2, j1, j2) tuples, where tag is
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
191 # one of
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
192 # 'replace' a[i1:i2] should be replaced by b[j1:j2]
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
193 # 'delete' a[i1:i2] should be deleted
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
194 # 'insert' b[j1:j2] should be inserted
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
195 # 'equal' a[i1:i2] == b[j1:j2]
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
196 # isjunk
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
197 # a user-supplied function taking a sequence element and
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
198 # returning true iff the element is "junk" -- this has
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
199 # subtle but helpful effects on the algorithm, which I'll
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
200 # get around to writing up someday <0.9 wink>.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
201 # DON'T USE! Only __chain_b uses this. Use isbjunk.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
202 # isbjunk
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
203 # for x in b, isbjunk(x) == isjunk(x) but much faster;
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
204 # it's really the has_key method of a hidden dict.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
205 # DOES NOT WORK for x in a!
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
206 # isbpopular
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
207 # for x in b, isbpopular(x) is true iff b is reasonably long
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
208 # (at least 200 elements) and x accounts for more than 1% of
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
209 # its elements. DOES NOT WORK for x in a!
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
210
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
211 self.isjunk = isjunk
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
212 self.a = self.b = None
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
213 self.set_seqs(a, b)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
214
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
215 def set_seqs(self, a, b):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
216 """Set the two sequences to be compared.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
217
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
218 >>> s = SequenceMatcher()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
219 >>> s.set_seqs("abcd", "bcde")
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
220 >>> s.ratio()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
221 0.75
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
222 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
223
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
224 self.set_seq1(a)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
225 self.set_seq2(b)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
226
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
227 def set_seq1(self, a):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
228 """Set the first sequence to be compared.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
229
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
230 The second sequence to be compared is not changed.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
231
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
232 >>> s = SequenceMatcher(None, "abcd", "bcde")
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
233 >>> s.ratio()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
234 0.75
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
235 >>> s.set_seq1("bcde")
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
236 >>> s.ratio()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
237 1.0
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
238 >>>
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
239
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
240 SequenceMatcher computes and caches detailed information about the
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
241 second sequence, so if you want to compare one sequence S against
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
242 many sequences, use .set_seq2(S) once and call .set_seq1(x)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
243 repeatedly for each of the other sequences.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
244
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
245 See also set_seqs() and set_seq2().
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
246 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
247
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
248 if a is self.a:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
249 return
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
250 self.a = a
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
251 self.matching_blocks = self.opcodes = None
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
252
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
253 def set_seq2(self, b):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
254 """Set the second sequence to be compared.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
255
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
256 The first sequence to be compared is not changed.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
257
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
258 >>> s = SequenceMatcher(None, "abcd", "bcde")
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
259 >>> s.ratio()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
260 0.75
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
261 >>> s.set_seq2("abcd")
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
262 >>> s.ratio()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
263 1.0
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
264 >>>
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
265
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
266 SequenceMatcher computes and caches detailed information about the
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
267 second sequence, so if you want to compare one sequence S against
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
268 many sequences, use .set_seq2(S) once and call .set_seq1(x)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
269 repeatedly for each of the other sequences.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
270
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
271 See also set_seqs() and set_seq1().
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
272 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
273
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
274 if b is self.b:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
275 return
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
276 self.b = b
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
277 self.matching_blocks = self.opcodes = None
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
278 self.fullbcount = None
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
279 self.__chain_b()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
280
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
281 # For each element x in b, set b2j[x] to a list of the indices in
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
282 # b where x appears; the indices are in increasing order; note that
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
283 # the number of times x appears in b is len(b2j[x]) ...
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
284 # when self.isjunk is defined, junk elements don't show up in this
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
285 # map at all, which stops the central find_longest_match method
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
286 # from starting any matching block at a junk element ...
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
287 # also creates the fast isbjunk function ...
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
288 # b2j also does not contain entries for "popular" elements, meaning
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
289 # elements that account for more than 1% of the total elements, and
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
290 # when the sequence is reasonably large (>= 200 elements); this can
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
291 # be viewed as an adaptive notion of semi-junk, and yields an enormous
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
292 # speedup when, e.g., comparing program files with hundreds of
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
293 # instances of "return NULL;" ...
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
294 # note that this is only called when b changes; so for cross-product
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
295 # kinds of matches, it's best to call set_seq2 once, then set_seq1
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
296 # repeatedly
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
297
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
298 def __chain_b(self):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
299 # Because isjunk is a user-defined (not C) function, and we test
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
300 # for junk a LOT, it's important to minimize the number of calls.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
301 # Before the tricks described here, __chain_b was by far the most
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
302 # time-consuming routine in the whole module! If anyone sees
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
303 # Jim Roskind, thank him again for profile.py -- I never would
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
304 # have guessed that.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
305 # The first trick is to build b2j ignoring the possibility
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
306 # of junk. I.e., we don't call isjunk at all yet. Throwing
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
307 # out the junk later is much cheaper than building b2j "right"
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
308 # from the start.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
309 b = self.b
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
310 n = len(b)
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
311 self.b2j = b2j = {}
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
312 populardict = {}
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
313 for i, elt in enumerate(b):
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
314 if elt in b2j:
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
315 indices = b2j[elt]
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
316 if n >= 200 and len(indices) * 100 > n:
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
317 populardict[elt] = 1
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
318 del indices[:]
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
319 else:
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
320 indices.append(i)
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
321 else:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
322 b2j[elt] = [i]
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
323
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
324 # Purge leftover indices for popular elements.
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
325 for elt in populardict:
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
326 del b2j[elt]
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
327
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
328 # Now b2j.keys() contains elements uniquely, and especially when
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
329 # the sequence is a string, that's usually a good deal smaller
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
330 # than len(string). The difference is the number of isjunk calls
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
331 # saved.
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
332 isjunk = self.isjunk
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
333 junkdict = {}
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
334 if isjunk:
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
335 for d in populardict, b2j:
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
336 for elt in d.keys():
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
337 if isjunk(elt):
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
338 junkdict[elt] = 1
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
339 del d[elt]
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
340
23620
a1de0bab5475 SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents: 23300
diff changeset
341 # Now for x in b, isjunk(x) == x in junkdict, but the
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
342 # latter is much faster. Note too that while there may be a
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
343 # lot of junk in the sequence, the number of *unique* junk
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
344 # elements is probably small. So the memory burden of keeping
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
345 # this dict alive is likely trivial compared to the size of b2j.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
346 self.isbjunk = junkdict.has_key
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
347 self.isbpopular = populardict.has_key
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
348
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
349 def find_longest_match(self, alo, ahi, blo, bhi):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
350 """Find longest matching block in a[alo:ahi] and b[blo:bhi].
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
351
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
352 If isjunk is not defined:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
353
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
354 Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
355 alo <= i <= i+k <= ahi
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
356 blo <= j <= j+k <= bhi
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
357 and for all (i',j',k') meeting those conditions,
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
358 k >= k'
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
359 i <= i'
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
360 and if i == i', j <= j'
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
361
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
362 In other words, of all maximal matching blocks, return one that
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
363 starts earliest in a, and of all those maximal matching blocks that
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
364 start earliest in a, return the one that starts earliest in b.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
365
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
366 >>> s = SequenceMatcher(None, " abcd", "abcd abcd")
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
367 >>> s.find_longest_match(0, 5, 0, 9)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
368 (0, 4, 5)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
369
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
370 If isjunk is defined, first the longest matching block is
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
371 determined as above, but with the additional restriction that no
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
372 junk element appears in the block. Then that block is extended as
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
373 far as possible by matching (only) junk elements on both sides. So
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
374 the resulting block never matches on junk except as identical junk
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
375 happens to be adjacent to an "interesting" match.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
376
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
377 Here's the same example as before, but considering blanks to be
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
378 junk. That prevents " abcd" from matching the " abcd" at the tail
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
379 end of the second sequence directly. Instead only the "abcd" can
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
380 match, and matches the leftmost "abcd" in the second sequence:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
381
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
382 >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
383 >>> s.find_longest_match(0, 5, 0, 9)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
384 (1, 0, 4)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
385
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
386 If no blocks match, return (alo, blo, 0).
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
387
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
388 >>> s = SequenceMatcher(None, "ab", "c")
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
389 >>> s.find_longest_match(0, 2, 0, 1)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
390 (0, 0, 0)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
391 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
392
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
393 # CAUTION: stripping common prefix or suffix would be incorrect.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
394 # E.g.,
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
395 # ab
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
396 # acab
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
397 # Longest matching block is "ab", but if common prefix is
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
398 # stripped, it's "a" (tied with "b"). UNIX(tm) diff does so
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
399 # strip, so ends up claiming that ab is changed to acab by
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
400 # inserting "ca" in the middle. That's minimal but unintuitive:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
401 # "it's obvious" that someone inserted "ac" at the front.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
402 # Windiff ends up at the same place as diff, but by pairing up
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
403 # the unique 'b's and then matching the first two 'a's.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
404
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
405 a, b, b2j, isbjunk = self.a, self.b, self.b2j, self.isbjunk
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
406 besti, bestj, bestsize = alo, blo, 0
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
407 # find longest junk-free match
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
408 # during an iteration of the loop, j2len[j] = length of longest
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
409 # junk-free match ending with a[i-1] and b[j]
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
410 j2len = {}
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
411 nothing = []
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
412 for i in xrange(alo, ahi):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
413 # look at all instances of a[i] in b; note that because
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
414 # b2j has no junk keys, the loop is skipped if a[i] is junk
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
415 j2lenget = j2len.get
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
416 newj2len = {}
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
417 for j in b2j.get(a[i], nothing):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
418 # a[i] matches b[j]
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
419 if j < blo:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
420 continue
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
421 if j >= bhi:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
422 break
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
423 k = newj2len[j] = j2lenget(j-1, 0) + 1
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
424 if k > bestsize:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
425 besti, bestj, bestsize = i-k+1, j-k+1, k
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
426 j2len = newj2len
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
427
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
428 # Extend the best by non-junk elements on each end. In particular,
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
429 # "popular" non-junk elements aren't in b2j, which greatly speeds
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
430 # the inner loop above, but also means "the best" match so far
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
431 # doesn't contain any junk *or* popular non-junk elements.
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
432 while besti > alo and bestj > blo and \
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
433 not isbjunk(b[bestj-1]) and \
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
434 a[besti-1] == b[bestj-1]:
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
435 besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
436 while besti+bestsize < ahi and bestj+bestsize < bhi and \
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
437 not isbjunk(b[bestj+bestsize]) and \
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
438 a[besti+bestsize] == b[bestj+bestsize]:
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
439 bestsize += 1
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
440
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
441 # Now that we have a wholly interesting match (albeit possibly
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
442 # empty!), we may as well suck up the matching junk on each
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
443 # side of it too. Can't think of a good reason not to, and it
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
444 # saves post-processing the (possibly considerable) expense of
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
445 # figuring out what to do with it. In the case of an empty
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
446 # interesting match, this is clearly the right thing to do,
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
447 # because no other kind of match is possible in the regions.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
448 while besti > alo and bestj > blo and \
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
449 isbjunk(b[bestj-1]) and \
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
450 a[besti-1] == b[bestj-1]:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
451 besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
452 while besti+bestsize < ahi and bestj+bestsize < bhi and \
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
453 isbjunk(b[bestj+bestsize]) and \
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
454 a[besti+bestsize] == b[bestj+bestsize]:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
455 bestsize = bestsize + 1
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
456
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
457 return besti, bestj, bestsize
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
458
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
459 def get_matching_blocks(self):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
460 """Return list of triples describing matching subsequences.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
461
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
462 Each triple is of the form (i, j, n), and means that
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
463 a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
464 i and in j.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
465
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
466 The last triple is a dummy, (len(a), len(b), 0), and is the only
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
467 triple with n==0.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
468
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
469 >>> s = SequenceMatcher(None, "abxcd", "abcd")
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
470 >>> s.get_matching_blocks()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
471 [(0, 0, 2), (3, 2, 2), (5, 4, 0)]
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
472 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
473
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
474 if self.matching_blocks is not None:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
475 return self.matching_blocks
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
476 self.matching_blocks = []
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
477 la, lb = len(self.a), len(self.b)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
478 self.__helper(0, la, 0, lb, self.matching_blocks)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
479 self.matching_blocks.append( (la, lb, 0) )
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
480 return self.matching_blocks
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
481
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
482 # builds list of matching blocks covering a[alo:ahi] and
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
483 # b[blo:bhi], appending them in increasing order to answer
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
484
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
485 def __helper(self, alo, ahi, blo, bhi, answer):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
486 i, j, k = x = self.find_longest_match(alo, ahi, blo, bhi)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
487 # a[alo:i] vs b[blo:j] unknown
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
488 # a[i:i+k] same as b[j:j+k]
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
489 # a[i+k:ahi] vs b[j+k:bhi] unknown
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
490 if k:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
491 if alo < i and blo < j:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
492 self.__helper(alo, i, blo, j, answer)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
493 answer.append(x)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
494 if i+k < ahi and j+k < bhi:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
495 self.__helper(i+k, ahi, j+k, bhi, answer)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
496
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
497 def get_opcodes(self):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
498 """Return list of 5-tuples describing how to turn a into b.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
499
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
500 Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
501 has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
502 tuple preceding it, and likewise for j1 == the previous j2.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
503
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
504 The tags are strings, with these meanings:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
505
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
506 'replace': a[i1:i2] should be replaced by b[j1:j2]
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
507 'delete': a[i1:i2] should be deleted.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
508 Note that j1==j2 in this case.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
509 'insert': b[j1:j2] should be inserted at a[i1:i1].
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
510 Note that i1==i2 in this case.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
511 'equal': a[i1:i2] == b[j1:j2]
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
512
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
513 >>> a = "qabxcd"
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
514 >>> b = "abycdf"
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
515 >>> s = SequenceMatcher(None, a, b)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
516 >>> for tag, i1, i2, j1, j2 in s.get_opcodes():
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
517 ... print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
518 ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2]))
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
519 delete a[0:1] (q) b[0:0] ()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
520 equal a[1:3] (ab) b[0:2] (ab)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
521 replace a[3:4] (x) b[2:3] (y)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
522 equal a[4:6] (cd) b[3:5] (cd)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
523 insert a[6:6] () b[5:6] (f)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
524 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
525
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
526 if self.opcodes is not None:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
527 return self.opcodes
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
528 i = j = 0
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
529 self.opcodes = answer = []
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
530 for ai, bj, size in self.get_matching_blocks():
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
531 # invariant: we've pumped out correct diffs to change
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
532 # a[:i] into b[:j], and the next matching block is
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
533 # a[ai:ai+size] == b[bj:bj+size]. So we need to pump
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
534 # out a diff to change a[i:ai] into b[j:bj], pump out
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
535 # the matching block, and move (i,j) beyond the match
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
536 tag = ''
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
537 if i < ai and j < bj:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
538 tag = 'replace'
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
539 elif i < ai:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
540 tag = 'delete'
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
541 elif j < bj:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
542 tag = 'insert'
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
543 if tag:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
544 answer.append( (tag, i, ai, j, bj) )
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
545 i, j = ai+size, bj+size
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
546 # the list of matching blocks is terminated by a
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
547 # sentinel with size 0
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
548 if size:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
549 answer.append( ('equal', ai, i, bj, j) )
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
550 return answer
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
551
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
552 def get_grouped_opcodes(self, n=3):
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
553 """ Isolate change clusters by eliminating ranges with no changes.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
554
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
555 Return a generator of groups with upto n lines of context.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
556 Each group is in the same format as returned by get_opcodes().
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
557
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
558 >>> from pprint import pprint
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
559 >>> a = map(str, range(1,40))
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
560 >>> b = a[:]
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
561 >>> b[8:8] = ['i'] # Make an insertion
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
562 >>> b[20] += 'x' # Make a replacement
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
563 >>> b[23:28] = [] # Make a deletion
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
564 >>> b[30] += 'y' # Make another replacement
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
565 >>> pprint(list(SequenceMatcher(None,a,b).get_grouped_opcodes()))
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
566 [[('equal', 5, 8, 5, 8), ('insert', 8, 8, 8, 9), ('equal', 8, 11, 9, 12)],
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
567 [('equal', 16, 19, 17, 20),
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
568 ('replace', 19, 20, 20, 21),
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
569 ('equal', 20, 22, 21, 23),
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
570 ('delete', 22, 27, 23, 23),
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
571 ('equal', 27, 30, 23, 26)],
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
572 [('equal', 31, 34, 27, 30),
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
573 ('replace', 34, 35, 30, 31),
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
574 ('equal', 35, 38, 31, 34)]]
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
575 """
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
576
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
577 codes = self.get_opcodes()
32612
5546c7ae24f7 SequenceMatcher(None, [], []).get_grouped_opcodes() now returns a generator
Brett Cannon <bcannon@gmail.com>
parents: 32357
diff changeset
578 if not codes:
5546c7ae24f7 SequenceMatcher(None, [], []).get_grouped_opcodes() now returns a generator
Brett Cannon <bcannon@gmail.com>
parents: 32357
diff changeset
579 codes = [("equal", 0, 1, 0, 1)]
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
580 # Fixup leading and trailing groups if they show no changes.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
581 if codes[0][0] == 'equal':
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
582 tag, i1, i2, j1, j2 = codes[0]
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
583 codes[0] = tag, max(i1, i2-n), i2, max(j1, j2-n), j2
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
584 if codes[-1][0] == 'equal':
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
585 tag, i1, i2, j1, j2 = codes[-1]
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
586 codes[-1] = tag, i1, min(i2, i1+n), j1, min(j2, j1+n)
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
587
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
588 nn = n + n
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
589 group = []
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
590 for tag, i1, i2, j1, j2 in codes:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
591 # End the current group and start a new one whenever
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
592 # there is a large range with no changes.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
593 if tag == 'equal' and i2-i1 > nn:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
594 group.append((tag, i1, min(i2, i1+n), j1, min(j2, j1+n)))
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
595 yield group
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
596 group = []
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
597 i1, j1 = max(i1, i2-n), max(j1, j2-n)
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
598 group.append((tag, i1, i2, j1 ,j2))
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
599 if group and not (len(group)==1 and group[0][0] == 'equal'):
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
600 yield group
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
601
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
602 def ratio(self):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
603 """Return a measure of the sequences' similarity (float in [0,1]).
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
604
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
605 Where T is the total number of elements in both sequences, and
32867
8d25835f88ae Repair typo in docstring.
Tim Peters <tim.peters@gmail.com>
parents: 32612
diff changeset
606 M is the number of matches, this is 2.0*M / T.
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
607 Note that this is 1 if the sequences are identical, and 0 if
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
608 they have nothing in common.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
609
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
610 .ratio() is expensive to compute if you haven't already computed
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
611 .get_matching_blocks() or .get_opcodes(), in which case you may
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
612 want to try .quick_ratio() or .real_quick_ratio() first to get an
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
613 upper bound.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
614
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
615 >>> s = SequenceMatcher(None, "abcd", "bcde")
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
616 >>> s.ratio()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
617 0.75
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
618 >>> s.quick_ratio()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
619 0.75
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
620 >>> s.real_quick_ratio()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
621 1.0
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
622 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
623
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
624 matches = reduce(lambda sum, triple: sum + triple[-1],
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
625 self.get_matching_blocks(), 0)
29719
fa4cf878c538 Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents: 29517
diff changeset
626 return _calculate_ratio(matches, len(self.a) + len(self.b))
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
627
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
628 def quick_ratio(self):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
629 """Return an upper bound on ratio() relatively quickly.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
630
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
631 This isn't defined beyond that it is an upper bound on .ratio(), and
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
632 is faster to compute.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
633 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
634
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
635 # viewing a and b as multisets, set matches to the cardinality
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
636 # of their intersection; this counts the number of matches
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
637 # without regard to order, so is clearly an upper bound
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
638 if self.fullbcount is None:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
639 self.fullbcount = fullbcount = {}
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
640 for elt in self.b:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
641 fullbcount[elt] = fullbcount.get(elt, 0) + 1
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
642 fullbcount = self.fullbcount
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
643 # avail[x] is the number of times x appears in 'b' less the
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
644 # number of times we've seen it in 'a' so far ... kinda
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
645 avail = {}
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
646 availhas, matches = avail.has_key, 0
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
647 for elt in self.a:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
648 if availhas(elt):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
649 numb = avail[elt]
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
650 else:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
651 numb = fullbcount.get(elt, 0)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
652 avail[elt] = numb - 1
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
653 if numb > 0:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
654 matches = matches + 1
29719
fa4cf878c538 Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents: 29517
diff changeset
655 return _calculate_ratio(matches, len(self.a) + len(self.b))
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
656
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
657 def real_quick_ratio(self):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
658 """Return an upper bound on ratio() very quickly.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
659
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
660 This isn't defined beyond that it is an upper bound on .ratio(), and
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
661 is faster to compute than either .ratio() or .quick_ratio().
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
662 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
663
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
664 la, lb = len(self.a), len(self.b)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
665 # can't have more matches than the number of elements in the
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
666 # shorter sequence
29719
fa4cf878c538 Fix SF bug #763023, difflib.py: ratio() zero division not caught
Neal Norwitz <nnorwitz@gmail.com>
parents: 29517
diff changeset
667 return _calculate_ratio(min(la, lb), la + lb)
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
668
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
669 def get_close_matches(word, possibilities, n=3, cutoff=0.6):
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
670 """Use SequenceMatcher to return list of the best "good enough" matches.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
671
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
672 word is a sequence for which close matches are desired (typically a
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
673 string).
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
674
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
675 possibilities is a list of sequences against which to match word
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
676 (typically a list of strings).
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
677
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
678 Optional arg n (default 3) is the maximum number of close matches to
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
679 return. n must be > 0.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
680
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
681 Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
682 that don't score at least that similar to word are ignored.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
683
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
684 The best (no more than n) matches among the possibilities are returned
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
685 in a list, sorted by similarity score, most similar first.
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
686
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
687 >>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"])
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
688 ['apple', 'ape']
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
689 >>> import keyword as _keyword
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
690 >>> get_close_matches("wheel", _keyword.kwlist)
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
691 ['while']
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
692 >>> get_close_matches("apple", _keyword.kwlist)
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
693 []
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
694 >>> get_close_matches("accept", _keyword.kwlist)
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
695 ['except']
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
696 """
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
697
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
698 if not n > 0:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30698
diff changeset
699 raise ValueError("n must be > 0: %r" % (n,))
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
700 if not 0.0 <= cutoff <= 1.0:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30698
diff changeset
701 raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,))
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
702 result = []
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
703 s = SequenceMatcher()
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
704 s.set_seq2(word)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
705 for x in possibilities:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
706 s.set_seq1(x)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
707 if s.real_quick_ratio() >= cutoff and \
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
708 s.quick_ratio() >= cutoff and \
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
709 s.ratio() >= cutoff:
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
710 result.append((s.ratio(), x))
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
711
30698
3e3fd56ce595 Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents: 29891
diff changeset
712 # Move the best scorers to head of list
32357
a46bde9e0c74 Reverse argument order for nsmallest() and nlargest().
Raymond Hettinger <python@rcn.com>
parents: 32339
diff changeset
713 result = heapq.nlargest(n, result)
30698
3e3fd56ce595 Let library modules use the new keyword arguments for list.sort().
Raymond Hettinger <python@rcn.com>
parents: 29891
diff changeset
714 # Strip scores for the best n matches
32339
408a40e65247 Apply heapq.nlargest() to find best matches.
Raymond Hettinger <python@rcn.com>
parents: 31515
diff changeset
715 return [x for score, x in result]
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
716
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
717 def _count_leading(line, ch):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
718 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
719 Return number of `ch` characters at the start of `line`.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
720
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
721 Example:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
722
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
723 >>> _count_leading(' abc', ' ')
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
724 3
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
725 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
726
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
727 i, n = 0, len(line)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
728 while i < n and line[i] == ch:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
729 i += 1
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
730 return i
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
731
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
732 class Differ:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
733 r"""
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
734 Differ is a class for comparing sequences of lines of text, and
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
735 producing human-readable differences or deltas. Differ uses
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
736 SequenceMatcher both to compare sequences of lines, and to compare
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
737 sequences of characters within similar (near-matching) lines.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
738
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
739 Each line of a Differ delta begins with a two-letter code:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
740
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
741 '- ' line unique to sequence 1
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
742 '+ ' line unique to sequence 2
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
743 ' ' line common to both sequences
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
744 '? ' line not present in either input sequence
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
745
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
746 Lines beginning with '? ' attempt to guide the eye to intraline
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
747 differences, and were not present in either input sequence. These lines
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
748 can be confusing if the sequences contain tab characters.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
749
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
750 Note that Differ makes no claim to produce a *minimal* diff. To the
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
751 contrary, minimal diffs are often counter-intuitive, because they synch
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
752 up anywhere possible, sometimes accidental matches 100 pages apart.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
753 Restricting synch points to contiguous matches preserves some notion of
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
754 locality, at the occasional cost of producing a longer diff.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
755
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
756 Example: Comparing two texts.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
757
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
758 First we set up the texts, sequences of individual single-line strings
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
759 ending with newlines (such sequences can also be obtained from the
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
760 `readlines()` method of file-like objects):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
761
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
762 >>> text1 = ''' 1. Beautiful is better than ugly.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
763 ... 2. Explicit is better than implicit.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
764 ... 3. Simple is better than complex.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
765 ... 4. Complex is better than complicated.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
766 ... '''.splitlines(1)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
767 >>> len(text1)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
768 4
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
769 >>> text1[0][-1]
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
770 '\n'
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
771 >>> text2 = ''' 1. Beautiful is better than ugly.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
772 ... 3. Simple is better than complex.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
773 ... 4. Complicated is better than complex.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
774 ... 5. Flat is better than nested.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
775 ... '''.splitlines(1)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
776
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
777 Next we instantiate a Differ object:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
778
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
779 >>> d = Differ()
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
780
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
781 Note that when instantiating a Differ object we may pass functions to
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
782 filter out line and character 'junk'. See Differ.__init__ for details.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
783
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
784 Finally, we compare the two:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
785
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
786 >>> result = list(d.compare(text1, text2))
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
787
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
788 'result' is a list of strings, so let's pretty-print it:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
789
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
790 >>> from pprint import pprint as _pprint
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
791 >>> _pprint(result)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
792 [' 1. Beautiful is better than ugly.\n',
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
793 '- 2. Explicit is better than implicit.\n',
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
794 '- 3. Simple is better than complex.\n',
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
795 '+ 3. Simple is better than complex.\n',
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
796 '? ++\n',
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
797 '- 4. Complex is better than complicated.\n',
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
798 '? ^ ---- ^\n',
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
799 '+ 4. Complicated is better than complex.\n',
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
800 '? ++++ ^ ^\n',
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
801 '+ 5. Flat is better than nested.\n']
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
802
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
803 As a single multi-line string it looks like this:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
804
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
805 >>> print ''.join(result),
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
806 1. Beautiful is better than ugly.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
807 - 2. Explicit is better than implicit.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
808 - 3. Simple is better than complex.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
809 + 3. Simple is better than complex.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
810 ? ++
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
811 - 4. Complex is better than complicated.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
812 ? ^ ---- ^
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
813 + 4. Complicated is better than complex.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
814 ? ++++ ^ ^
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
815 + 5. Flat is better than nested.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
816
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
817 Methods:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
818
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
819 __init__(linejunk=None, charjunk=None)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
820 Construct a text differencer, with optional filters.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
821
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
822 compare(a, b)
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
823 Compare two sequences of lines; generate the resulting delta.
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
824 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
825
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
826 def __init__(self, linejunk=None, charjunk=None):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
827 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
828 Construct a text differencer, with optional filters.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
829
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
830 The two optional keyword parameters are for filter functions:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
831
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
832 - `linejunk`: A function that should accept a single string argument,
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
833 and return true iff the string is junk. The module-level function
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
834 `IS_LINE_JUNK` may be used to filter out lines without visible
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
835 characters, except for at most one splat ('#'). It is recommended
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
836 to leave linejunk None; as of Python 2.3, the underlying
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
837 SequenceMatcher class has grown an adaptive notion of "noise" lines
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
838 that's better than any static definition the author has ever been
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
839 able to craft.
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
840
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
841 - `charjunk`: A function that should accept a string of length 1. The
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
842 module-level function `IS_CHARACTER_JUNK` may be used to filter out
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
843 whitespace characters (a blank or tab; **note**: bad idea to include
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
844 newline in this!). Use of IS_CHARACTER_JUNK is recommended.
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
845 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
846
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
847 self.linejunk = linejunk
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
848 self.charjunk = charjunk
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
849
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
850 def compare(self, a, b):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
851 r"""
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
852 Compare two sequences of lines; generate the resulting delta.
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
853
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
854 Each sequence must contain individual single-line strings ending with
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
855 newlines. Such sequences can be obtained from the `readlines()` method
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
856 of file-like objects. The delta generated also consists of newline-
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
857 terminated strings, ready to be printed as-is via the writeline()
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
858 method of a file-like object.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
859
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
860 Example:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
861
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
862 >>> print ''.join(Differ().compare('one\ntwo\nthree\n'.splitlines(1),
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
863 ... 'ore\ntree\nemu\n'.splitlines(1))),
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
864 - one
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
865 ? ^
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
866 + ore
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
867 ? ^
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
868 - two
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
869 - three
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
870 ? -
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
871 + tree
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
872 + emu
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
873 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
874
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
875 cruncher = SequenceMatcher(self.linejunk, a, b)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
876 for tag, alo, ahi, blo, bhi in cruncher.get_opcodes():
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
877 if tag == 'replace':
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
878 g = self._fancy_replace(a, alo, ahi, b, blo, bhi)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
879 elif tag == 'delete':
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
880 g = self._dump('-', a, alo, ahi)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
881 elif tag == 'insert':
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
882 g = self._dump('+', b, blo, bhi)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
883 elif tag == 'equal':
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
884 g = self._dump(' ', a, alo, ahi)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
885 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30698
diff changeset
886 raise ValueError, 'unknown tag %r' % (tag,)
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
887
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
888 for line in g:
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
889 yield line
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
890
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
891 def _dump(self, tag, x, lo, hi):
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
892 """Generate comparison results for a same-tagged range."""
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
893 for i in xrange(lo, hi):
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
894 yield '%s %s' % (tag, x[i])
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
895
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
896 def _plain_replace(self, a, alo, ahi, b, blo, bhi):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
897 assert alo < ahi and blo < bhi
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
898 # dump the shorter block first -- reduces the burden on short-term
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
899 # memory if the blocks are of very different sizes
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
900 if bhi - blo < ahi - alo:
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
901 first = self._dump('+', b, blo, bhi)
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
902 second = self._dump('-', a, alo, ahi)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
903 else:
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
904 first = self._dump('-', a, alo, ahi)
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
905 second = self._dump('+', b, blo, bhi)
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
906
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
907 for g in first, second:
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
908 for line in g:
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
909 yield line
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
910
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
911 def _fancy_replace(self, a, alo, ahi, b, blo, bhi):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
912 r"""
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
913 When replacing one block of lines with another, search the blocks
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
914 for *similar* lines; the best-matching pair (if any) is used as a
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
915 synch point, and intraline difference marking is done on the
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
916 similar pair. Lots of work, but often worth it.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
917
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
918 Example:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
919
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
920 >>> d = Differ()
29891
9ca291865f9c Fix faulty doctests. There is no results attribute.
Raymond Hettinger <python@rcn.com>
parents: 29719
diff changeset
921 >>> results = d._fancy_replace(['abcDefghiJkl\n'], 0, 1,
9ca291865f9c Fix faulty doctests. There is no results attribute.
Raymond Hettinger <python@rcn.com>
parents: 29719
diff changeset
922 ... ['abcdefGhijkl\n'], 0, 1)
9ca291865f9c Fix faulty doctests. There is no results attribute.
Raymond Hettinger <python@rcn.com>
parents: 29719
diff changeset
923 >>> print ''.join(results),
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
924 - abcDefghiJkl
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
925 ? ^ ^ ^
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
926 + abcdefGhijkl
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
927 ? ^ ^ ^
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
928 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
929
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
930 # don't synch up unless the lines have a similarity score of at
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
931 # least cutoff; best_ratio tracks the best score seen so far
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
932 best_ratio, cutoff = 0.74, 0.75
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
933 cruncher = SequenceMatcher(self.charjunk)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
934 eqi, eqj = None, None # 1st indices of equal lines (if any)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
935
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
936 # search for the pair that matches best without being identical
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
937 # (identical lines must be junk lines, & we don't want to synch up
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
938 # on junk -- unless we have to)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
939 for j in xrange(blo, bhi):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
940 bj = b[j]
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
941 cruncher.set_seq2(bj)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
942 for i in xrange(alo, ahi):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
943 ai = a[i]
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
944 if ai == bj:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
945 if eqi is None:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
946 eqi, eqj = i, j
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
947 continue
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
948 cruncher.set_seq1(ai)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
949 # computing similarity is expensive, so use the quick
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
950 # upper bounds first -- have seen this speed up messy
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
951 # compares by a factor of 3.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
952 # note that ratio() is only expensive to compute the first
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
953 # time it's called on a sequence pair; the expensive part
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
954 # of the computation is cached by cruncher
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
955 if cruncher.real_quick_ratio() > best_ratio and \
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
956 cruncher.quick_ratio() > best_ratio and \
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
957 cruncher.ratio() > best_ratio:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
958 best_ratio, best_i, best_j = cruncher.ratio(), i, j
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
959 if best_ratio < cutoff:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
960 # no non-identical "pretty close" pair
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
961 if eqi is None:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
962 # no identical pair either -- treat it as a straight replace
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
963 for line in self._plain_replace(a, alo, ahi, b, blo, bhi):
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
964 yield line
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
965 return
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
966 # no close pair, but an identical pair -- synch up on that
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
967 best_i, best_j, best_ratio = eqi, eqj, 1.0
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
968 else:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
969 # there's a close pair, so forget the identical pair (if any)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
970 eqi = None
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
971
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
972 # a[best_i] very similar to b[best_j]; eqi is None iff they're not
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
973 # identical
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
974
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
975 # pump out diffs from before the synch point
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
976 for line in self._fancy_helper(a, alo, best_i, b, blo, best_j):
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
977 yield line
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
978
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
979 # do intraline marking on the synch pair
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
980 aelt, belt = a[best_i], b[best_j]
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
981 if eqi is None:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
982 # pump out a '-', '?', '+', '?' quad for the synched lines
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
983 atags = btags = ""
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
984 cruncher.set_seqs(aelt, belt)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
985 for tag, ai1, ai2, bj1, bj2 in cruncher.get_opcodes():
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
986 la, lb = ai2 - ai1, bj2 - bj1
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
987 if tag == 'replace':
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
988 atags += '^' * la
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
989 btags += '^' * lb
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
990 elif tag == 'delete':
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
991 atags += '-' * la
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
992 elif tag == 'insert':
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
993 btags += '+' * lb
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
994 elif tag == 'equal':
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
995 atags += ' ' * la
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
996 btags += ' ' * lb
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
997 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 30698
diff changeset
998 raise ValueError, 'unknown tag %r' % (tag,)
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
999 for line in self._qformat(aelt, belt, atags, btags):
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1000 yield line
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1001 else:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1002 # the synch pair is identical
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1003 yield ' ' + aelt
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1004
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1005 # pump out diffs from after the synch point
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1006 for line in self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi):
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1007 yield line
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1008
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1009 def _fancy_helper(self, a, alo, ahi, b, blo, bhi):
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1010 g = []
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1011 if alo < ahi:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1012 if blo < bhi:
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1013 g = self._fancy_replace(a, alo, ahi, b, blo, bhi)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1014 else:
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1015 g = self._dump('-', a, alo, ahi)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1016 elif blo < bhi:
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1017 g = self._dump('+', b, blo, bhi)
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1018
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1019 for line in g:
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1020 yield line
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1021
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1022 def _qformat(self, aline, bline, atags, btags):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1023 r"""
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1024 Format "?" output and deal with leading tabs.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1025
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1026 Example:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1027
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1028 >>> d = Differ()
29891
9ca291865f9c Fix faulty doctests. There is no results attribute.
Raymond Hettinger <python@rcn.com>
parents: 29719
diff changeset
1029 >>> results = d._qformat('\tabcDefghiJkl\n', '\t\tabcdefGhijkl\n',
9ca291865f9c Fix faulty doctests. There is no results attribute.
Raymond Hettinger <python@rcn.com>
parents: 29719
diff changeset
1030 ... ' ^ ^ ^ ', '+ ^ ^ ^ ')
9ca291865f9c Fix faulty doctests. There is no results attribute.
Raymond Hettinger <python@rcn.com>
parents: 29719
diff changeset
1031 >>> for line in results: print repr(line)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1032 ...
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1033 '- \tabcDefghiJkl\n'
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1034 '? \t ^ ^ ^\n'
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1035 '+ \t\tabcdefGhijkl\n'
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1036 '? \t ^ ^ ^\n'
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1037 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1038
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1039 # Can hurt, but will probably help most of the time.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1040 common = min(_count_leading(aline, "\t"),
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1041 _count_leading(bline, "\t"))
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1042 common = min(common, _count_leading(atags[:common], " "))
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1043 atags = atags[common:].rstrip()
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1044 btags = btags[common:].rstrip()
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1045
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1046 yield "- " + aline
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1047 if atags:
20240
5c4261e74478 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 20038
diff changeset
1048 yield "? %s%s\n" % ("\t" * common, atags)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1049
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1050 yield "+ " + bline
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1051 if btags:
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1052 yield "? %s%s\n" % ("\t" * common, btags)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1053
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1054 # With respect to junk, an earlier version of ndiff simply refused to
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1055 # *start* a match with a junk element. The result was cases like this:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1056 # before: private Thread currentThread;
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1057 # after: private volatile Thread currentThread;
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1058 # If you consider whitespace to be junk, the longest contiguous match
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1059 # not starting with junk is "e Thread currentThread". So ndiff reported
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1060 # that "e volatil" was inserted between the 't' and the 'e' in "private".
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1061 # While an accurate view, to people that's absurd. The current version
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1062 # looks for matching blocks that are entirely junk-free, then extends the
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1063 # longest one of those as far as possible but only with matching junk.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1064 # So now "currentThread" is matched, then extended to suck up the
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1065 # preceding blank; then "private" is matched, and extended to suck up the
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1066 # following blank; then "Thread" is matched; and finally ndiff reports
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1067 # that "volatile " was inserted before "Thread". The only quibble
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1068 # remaining is that perhaps it was really the case that " volatile"
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1069 # was inserted after "private". I can live with that <wink>.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1070
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1071 import re
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1072
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1073 def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1074 r"""
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1075 Return 1 for ignorable line: iff `line` is blank or contains a single '#'.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1076
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1077 Examples:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1078
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1079 >>> IS_LINE_JUNK('\n')
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1080 True
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1081 >>> IS_LINE_JUNK(' # \n')
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1082 True
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1083 >>> IS_LINE_JUNK('hello\n')
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1084 False
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1085 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1086
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1087 return pat(line) is not None
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1088
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1089 def IS_CHARACTER_JUNK(ch, ws=" \t"):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1090 r"""
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1091 Return 1 for ignorable character: iff `ch` is a space or tab.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1092
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1093 Examples:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1094
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1095 >>> IS_CHARACTER_JUNK(' ')
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1096 True
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1097 >>> IS_CHARACTER_JUNK('\t')
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1098 True
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1099 >>> IS_CHARACTER_JUNK('\n')
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1100 False
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1101 >>> IS_CHARACTER_JUNK('x')
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22719
diff changeset
1102 False
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1103 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1104
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1105 return ch in ws
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1106
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1107
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1108 def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1109 tofiledate='', n=3, lineterm='\n'):
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1110 r"""
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1111 Compare two sequences of lines; generate the delta as a unified diff.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1112
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1113 Unified diffs are a compact way of showing line changes and a few
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1114 lines of context. The number of context lines is set by 'n' which
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1115 defaults to three.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1116
29517
94ac1d42a420 Fix docstring
Raymond Hettinger <python@rcn.com>
parents: 29384
diff changeset
1117 By default, the diff control lines (those with ---, +++, or @@) are
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1118 created with a trailing newline. This is helpful so that inputs
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1119 created from file.readlines() result in diffs that are suitable for
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1120 file.writelines() since both the inputs and outputs have trailing
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1121 newlines.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1122
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1123 For inputs that do not have trailing newlines, set the lineterm
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1124 argument to "" so that the output will be uniformly newline free.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1125
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1126 The unidiff format normally has a header for filenames and modification
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1127 times. Any or all of these may be specified using strings for
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1128 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1129 times are normally expressed in the format returned by time.ctime().
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1130
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1131 Example:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1132
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1133 >>> for line in unified_diff('one two three four'.split(),
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1134 ... 'zero one tree four'.split(), 'Original', 'Current',
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1135 ... 'Sat Jan 26 23:30:50 1991', 'Fri Jun 06 10:20:52 2003',
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1136 ... lineterm=''):
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1137 ... print line
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1138 --- Original Sat Jan 26 23:30:50 1991
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1139 +++ Current Fri Jun 06 10:20:52 2003
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1140 @@ -1,4 +1,4 @@
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1141 +zero
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1142 one
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1143 -two
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1144 -three
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1145 +tree
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1146 four
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1147 """
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1148
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1149 started = False
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1150 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1151 if not started:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1152 yield '--- %s %s%s' % (fromfile, fromfiledate, lineterm)
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1153 yield '+++ %s %s%s' % (tofile, tofiledate, lineterm)
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1154 started = True
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1155 i1, i2, j1, j2 = group[0][1], group[-1][2], group[0][3], group[-1][4]
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1156 yield "@@ -%d,%d +%d,%d @@%s" % (i1+1, i2-i1, j1+1, j2-j1, lineterm)
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1157 for tag, i1, i2, j1, j2 in group:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1158 if tag == 'equal':
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1159 for line in a[i1:i2]:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1160 yield ' ' + line
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1161 continue
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1162 if tag == 'replace' or tag == 'delete':
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1163 for line in a[i1:i2]:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1164 yield '-' + line
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1165 if tag == 'replace' or tag == 'insert':
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1166 for line in b[j1:j2]:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1167 yield '+' + line
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1168
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1169 # See http://www.unix.org/single_unix_specification/
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1170 def context_diff(a, b, fromfile='', tofile='',
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1171 fromfiledate='', tofiledate='', n=3, lineterm='\n'):
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1172 r"""
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1173 Compare two sequences of lines; generate the delta as a context diff.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1174
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1175 Context diffs are a compact way of showing line changes and a few
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1176 lines of context. The number of context lines is set by 'n' which
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1177 defaults to three.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1178
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1179 By default, the diff control lines (those with *** or ---) are
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1180 created with a trailing newline. This is helpful so that inputs
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1181 created from file.readlines() result in diffs that are suitable for
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1182 file.writelines() since both the inputs and outputs have trailing
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1183 newlines.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1184
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1185 For inputs that do not have trailing newlines, set the lineterm
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1186 argument to "" so that the output will be uniformly newline free.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1187
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1188 The context diff format normally has a header for filenames and
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1189 modification times. Any or all of these may be specified using
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1190 strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1191 The modification times are normally expressed in the format returned
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1192 by time.ctime(). If not specified, the strings default to blanks.
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1193
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1194 Example:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1195
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1196 >>> print ''.join(context_diff('one\ntwo\nthree\nfour\n'.splitlines(1),
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1197 ... 'zero\none\ntree\nfour\n'.splitlines(1), 'Original', 'Current',
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1198 ... 'Sat Jan 26 23:30:50 1991', 'Fri Jun 06 10:22:46 2003')),
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1199 *** Original Sat Jan 26 23:30:50 1991
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1200 --- Current Fri Jun 06 10:22:46 2003
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1201 ***************
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1202 *** 1,4 ****
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1203 one
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1204 ! two
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1205 ! three
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1206 four
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1207 --- 1,4 ----
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1208 + zero
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1209 one
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1210 ! tree
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1211 four
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1212 """
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1213
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1214 started = False
29384
2d39ec0e55aa For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents: 29381
diff changeset
1215 prefixmap = {'insert':'+ ', 'delete':'- ', 'replace':'! ', 'equal':' '}
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1216 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1217 if not started:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1218 yield '*** %s %s%s' % (fromfile, fromfiledate, lineterm)
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1219 yield '--- %s %s%s' % (tofile, tofiledate, lineterm)
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1220 started = True
29384
2d39ec0e55aa For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents: 29381
diff changeset
1221
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1222 yield '***************%s' % (lineterm,)
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1223 if group[-1][2] - group[0][1] >= 2:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1224 yield '*** %d,%d ****%s' % (group[0][1]+1, group[-1][2], lineterm)
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1225 else:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1226 yield '*** %d ****%s' % (group[-1][2], lineterm)
29384
2d39ec0e55aa For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents: 29381
diff changeset
1227 visiblechanges = [e for e in group if e[0] in ('replace', 'delete')]
2d39ec0e55aa For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents: 29381
diff changeset
1228 if visiblechanges:
2d39ec0e55aa For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents: 29381
diff changeset
1229 for tag, i1, i2, _, _ in group:
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1230 if tag != 'insert':
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1231 for line in a[i1:i2]:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1232 yield prefixmap[tag] + line
29384
2d39ec0e55aa For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents: 29381
diff changeset
1233
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1234 if group[-1][4] - group[0][3] >= 2:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1235 yield '--- %d,%d ----%s' % (group[0][3]+1, group[-1][4], lineterm)
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1236 else:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1237 yield '--- %d ----%s' % (group[-1][4], lineterm)
29384
2d39ec0e55aa For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents: 29381
diff changeset
1238 visiblechanges = [e for e in group if e[0] in ('replace', 'insert')]
2d39ec0e55aa For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents: 29381
diff changeset
1239 if visiblechanges:
2d39ec0e55aa For the context and unified diff functions:
Raymond Hettinger <python@rcn.com>
parents: 29381
diff changeset
1240 for tag, _, _, j1, j2 in group:
29381
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1241 if tag != 'delete':
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1242 for line in b[j1:j2]:
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1243 yield prefixmap[tag] + line
15f1dc5b7ddc Added functions for creating context diffs and unified diffs.
Raymond Hettinger <python@rcn.com>
parents: 25953
diff changeset
1244
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
1245 def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK):
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1246 r"""
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1247 Compare `a` and `b` (lists of strings); return a `Differ`-style delta.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1248
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1249 Optional keyword parameters `linejunk` and `charjunk` are for filter
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1250 functions (or None):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1251
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1252 - linejunk: A function that should accept a single string argument, and
23300
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
1253 return true iff the string is junk. The default is None, and is
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
1254 recommended; as of Python 2.3, an adaptive notion of "noise" lines is
b5f6e9f6e7f4 Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents: 22785
diff changeset
1255 used that does a good job on its own.
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1256
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1257 - charjunk: A function that should accept a string of length 1. The
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1258 default is module-level function IS_CHARACTER_JUNK, which filters out
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1259 whitespace characters (a blank or tab; note: bad idea to include newline
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1260 in this!).
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1261
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1262 Tools/scripts/ndiff.py is a command-line front-end to this function.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1263
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1264 Example:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1265
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1266 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1267 ... 'ore\ntree\nemu\n'.splitlines(1))
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1268 >>> print ''.join(diff),
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1269 - one
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1270 ? ^
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1271 + ore
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1272 ? ^
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1273 - two
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1274 - three
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1275 ? -
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1276 + tree
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1277 + emu
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1278 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1279 return Differ(linejunk, charjunk).compare(a, b)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1280
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1281 def _mdiff(fromlines, tolines, context=None, linejunk=None,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1282 charjunk=IS_CHARACTER_JUNK):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1283 """Returns generator yielding marked up from/to side by side differences.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1284
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1285 Arguments:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1286 fromlines -- list of text lines to compared to tolines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1287 tolines -- list of text lines to be compared to fromlines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1288 context -- number of context lines to display on each side of difference,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1289 if None, all from/to text lines will be generated.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1290 linejunk -- passed on to ndiff (see ndiff documentation)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1291 charjunk -- passed on to ndiff (see ndiff documentation)
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1292
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1293 This function returns an interator which returns a tuple:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1294 (from line tuple, to line tuple, boolean flag)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1295
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1296 from/to line tuple -- (line num, line text)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1297 line num -- integer or None (to indicate a context seperation)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1298 line text -- original line text with following markers inserted:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1299 '\0+' -- marks start of added text
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1300 '\0-' -- marks start of deleted text
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1301 '\0^' -- marks start of changed text
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1302 '\1' -- marks end of added/deleted/changed text
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1303
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1304 boolean flag -- None indicates context separation, True indicates
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1305 either "from" or "to" line contains a change, otherwise False.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1306
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1307 This function/iterator was originally developed to generate side by side
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1308 file difference for making HTML pages (see HtmlDiff class for example
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1309 usage).
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1310
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1311 Note, this function utilizes the ndiff function to generate the side by
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1312 side difference markup. Optional ndiff arguments may be passed to this
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1313 function and they in turn will be passed to ndiff.
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1314 """
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1315 import re
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1316
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1317 # regular expression for finding intraline change indices
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1318 change_re = re.compile('(\++|\-+|\^+)')
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1319
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1320 # create the difference iterator to generate the differences
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1321 diff_lines_iterator = ndiff(fromlines,tolines,linejunk,charjunk)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1322
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1323 def _make_line(lines, format_key, side, num_lines=[0,0]):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1324 """Returns line of text with user's change markup and line formatting.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1325
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1326 lines -- list of lines from the ndiff generator to produce a line of
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1327 text from. When producing the line of text to return, the
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1328 lines used are removed from this list.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1329 format_key -- '+' return first line in list with "add" markup around
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1330 the entire line.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1331 '-' return first line in list with "delete" markup around
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1332 the entire line.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1333 '?' return first line in list with add/delete/change
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1334 intraline markup (indices obtained from second line)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1335 None return first line in list with no markup
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1336 side -- indice into the num_lines list (0=from,1=to)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1337 num_lines -- from/to current line number. This is NOT intended to be a
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1338 passed parameter. It is present as a keyword argument to
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1339 maintain memory of the current line numbers between calls
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1340 of this function.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1341
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1342 Note, this function is purposefully not defined at the module scope so
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1343 that data it needs from its parent function (within whose context it
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1344 is defined) does not need to be of module scope.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1345 """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1346 num_lines[side] += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1347 # Handle case where no user markup is to be added, just return line of
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1348 # text with user's line format to allow for usage of the line number.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1349 if format_key is None:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1350 return (num_lines[side],lines.pop(0)[2:])
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1351 # Handle case of intraline changes
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1352 if format_key == '?':
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1353 text, markers = lines.pop(0), lines.pop(0)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1354 # find intraline changes (store change type and indices in tuples)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1355 sub_info = []
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1356 def record_sub_info(match_object,sub_info=sub_info):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1357 sub_info.append([match_object.group(1)[0],match_object.span()])
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1358 return match_object.group(1)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1359 change_re.sub(record_sub_info,markers)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1360 # process each tuple inserting our special marks that won't be
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1361 # noticed by an xml/html escaper.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1362 for key,(begin,end) in sub_info[::-1]:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1363 text = text[0:begin]+'\0'+key+text[begin:end]+'\1'+text[end:]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1364 text = text[2:]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1365 # Handle case of add/delete entire line
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1366 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1367 text = lines.pop(0)[2:]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1368 # if line of text is just a newline, insert a space so there is
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1369 # something for the user to highlight and see.
33981
ec29955b8543 SF bug 1054821: difflib HtmlDiff() extra space on inserted 1 character lines
Tim Peters <tim.peters@gmail.com>
parents: 33448
diff changeset
1370 if not text:
ec29955b8543 SF bug 1054821: difflib HtmlDiff() extra space on inserted 1 character lines
Tim Peters <tim.peters@gmail.com>
parents: 33448
diff changeset
1371 text = ' '
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1372 # insert marks that won't be noticed by an xml/html escaper.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1373 text = '\0' + format_key + text + '\1'
35276
4adbd331d335 Fix all wrong instances of "it's".
Georg Brandl <georg@python.org>
parents: 33981
diff changeset
1374 # Return line of text, first allow user's line formatter to do its
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1375 # thing (such as adding the line number) then replace the special
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1376 # marks with what the user's change markup.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1377 return (num_lines[side],text)
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1378
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1379 def _line_iterator():
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1380 """Yields from/to lines of text with a change indication.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1381
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1382 This function is an iterator. It itself pulls lines from a
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1383 differencing iterator, processes them and yields them. When it can
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1384 it yields both a "from" and a "to" line, otherwise it will yield one
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1385 or the other. In addition to yielding the lines of from/to text, a
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1386 boolean flag is yielded to indicate if the text line(s) have
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1387 differences in them.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1388
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1389 Note, this function is purposefully not defined at the module scope so
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1390 that data it needs from its parent function (within whose context it
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1391 is defined) does not need to be of module scope.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1392 """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1393 lines = []
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1394 num_blanks_pending, num_blanks_to_yield = 0, 0
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1395 while True:
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1396 # Load up next 4 lines so we can look ahead, create strings which
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1397 # are a concatenation of the first character of each of the 4 lines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1398 # so we can do some very readable comparisons.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1399 while len(lines) < 4:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1400 try:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1401 lines.append(diff_lines_iterator.next())
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1402 except StopIteration:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1403 lines.append('X')
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1404 s = ''.join([line[0] for line in lines])
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1405 if s.startswith('X'):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1406 # When no more lines, pump out any remaining blank lines so the
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1407 # corresponding add/delete lines get a matching blank line so
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1408 # all line pairs get yielded at the next level.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1409 num_blanks_to_yield = num_blanks_pending
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1410 elif s.startswith('-?+?'):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1411 # simple intraline change
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1412 yield _make_line(lines,'?',0), _make_line(lines,'?',1), True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1413 continue
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1414 elif s.startswith('--++'):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1415 # in delete block, add block coming: we do NOT want to get
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1416 # caught up on blank lines yet, just process the delete line
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1417 num_blanks_pending -= 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1418 yield _make_line(lines,'-',0), None, True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1419 continue
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1420 elif s.startswith('--?+') or s.startswith('--+') or \
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1421 s.startswith('- '):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1422 # in delete block and see a intraline change or unchanged line
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1423 # coming: yield the delete line and then blanks
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1424 from_line,to_line = _make_line(lines,'-',0), None
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1425 num_blanks_to_yield,num_blanks_pending = num_blanks_pending-1,0
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1426 elif s.startswith('-+?'):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1427 # intraline change
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1428 yield _make_line(lines,None,0), _make_line(lines,'?',1), True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1429 continue
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1430 elif s.startswith('-?+'):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1431 # intraline change
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1432 yield _make_line(lines,'?',0), _make_line(lines,None,1), True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1433 continue
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1434 elif s.startswith('-'):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1435 # delete FROM line
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1436 num_blanks_pending -= 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1437 yield _make_line(lines,'-',0), None, True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1438 continue
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1439 elif s.startswith('+--'):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1440 # in add block, delete block coming: we do NOT want to get
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1441 # caught up on blank lines yet, just process the add line
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1442 num_blanks_pending += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1443 yield None, _make_line(lines,'+',1), True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1444 continue
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1445 elif s.startswith('+ ') or s.startswith('+-'):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1446 # will be leaving an add block: yield blanks then add line
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1447 from_line, to_line = None, _make_line(lines,'+',1)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1448 num_blanks_to_yield,num_blanks_pending = num_blanks_pending+1,0
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1449 elif s.startswith('+'):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1450 # inside an add block, yield the add line
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1451 num_blanks_pending += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1452 yield None, _make_line(lines,'+',1), True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1453 continue
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1454 elif s.startswith(' '):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1455 # unchanged text, yield it to both sides
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1456 yield _make_line(lines[:],None,0),_make_line(lines,None,1),False
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1457 continue
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1458 # Catch up on the blank lines so when we yield the next from/to
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1459 # pair, they are lined up.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1460 while(num_blanks_to_yield < 0):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1461 num_blanks_to_yield += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1462 yield None,('','\n'),True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1463 while(num_blanks_to_yield > 0):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1464 num_blanks_to_yield -= 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1465 yield ('','\n'),None,True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1466 if s.startswith('X'):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1467 raise StopIteration
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1468 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1469 yield from_line,to_line,True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1470
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1471 def _line_pair_iterator():
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1472 """Yields from/to lines of text with a change indication.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1473
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1474 This function is an iterator. It itself pulls lines from the line
35276
4adbd331d335 Fix all wrong instances of "it's".
Georg Brandl <georg@python.org>
parents: 33981
diff changeset
1475 iterator. Its difference from that iterator is that this function
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1476 always yields a pair of from/to text lines (with the change
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1477 indication). If necessary it will collect single from/to lines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1478 until it has a matching pair from/to pair to yield.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1479
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1480 Note, this function is purposefully not defined at the module scope so
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1481 that data it needs from its parent function (within whose context it
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1482 is defined) does not need to be of module scope.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1483 """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1484 line_iterator = _line_iterator()
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1485 fromlines,tolines=[],[]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1486 while True:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1487 # Collecting lines of text until we have a from/to pair
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1488 while (len(fromlines)==0 or len(tolines)==0):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1489 from_line, to_line, found_diff =line_iterator.next()
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1490 if from_line is not None:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1491 fromlines.append((from_line,found_diff))
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1492 if to_line is not None:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1493 tolines.append((to_line,found_diff))
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1494 # Once we have a pair, remove them from the collection and yield it
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1495 from_line, fromDiff = fromlines.pop(0)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1496 to_line, to_diff = tolines.pop(0)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1497 yield (from_line,to_line,fromDiff or to_diff)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1498
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1499 # Handle case where user does not want context differencing, just yield
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1500 # them up without doing anything else with them.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1501 line_pair_iterator = _line_pair_iterator()
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1502 if context is None:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1503 while True:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1504 yield line_pair_iterator.next()
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1505 # Handle case where user wants context differencing. We must do some
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1506 # storage of lines until we know for sure that they are to be yielded.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1507 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1508 context += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1509 lines_to_write = 0
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1510 while True:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1511 # Store lines up until we find a difference, note use of a
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1512 # circular queue because we only need to keep around what
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1513 # we need for context.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1514 index, contextLines = 0, [None]*(context)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1515 found_diff = False
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1516 while(found_diff is False):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1517 from_line, to_line, found_diff = line_pair_iterator.next()
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1518 i = index % context
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1519 contextLines[i] = (from_line, to_line, found_diff)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1520 index += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1521 # Yield lines that we have collected so far, but first yield
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1522 # the user's separator.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1523 if index > context:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1524 yield None, None, None
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1525 lines_to_write = context
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1526 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1527 lines_to_write = index
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1528 index = 0
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1529 while(lines_to_write):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1530 i = index % context
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1531 index += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1532 yield contextLines[i]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1533 lines_to_write -= 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1534 # Now yield the context lines after the change
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1535 lines_to_write = context-1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1536 while(lines_to_write):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1537 from_line, to_line, found_diff = line_pair_iterator.next()
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1538 # If another change within the context, extend the context
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1539 if found_diff:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1540 lines_to_write = context-1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1541 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1542 lines_to_write -= 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1543 yield from_line, to_line, found_diff
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1544
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1545
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1546 _file_template = """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1547 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1548 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1549
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1550 <html>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1551
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1552 <head>
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1553 <meta http-equiv="Content-Type"
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1554 content="text/html; charset=ISO-8859-1" />
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1555 <title></title>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1556 <style type="text/css">%(styles)s
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1557 </style>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1558 </head>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1559
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1560 <body>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1561 %(table)s%(legend)s
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1562 </body>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1563
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1564 </html>"""
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1565
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1566 _styles = """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1567 table.diff {font-family:Courier; border:medium;}
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1568 .diff_header {background-color:#e0e0e0}
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1569 td.diff_header {text-align:right}
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1570 .diff_next {background-color:#c0c0c0}
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1571 .diff_add {background-color:#aaffaa}
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1572 .diff_chg {background-color:#ffff77}
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1573 .diff_sub {background-color:#ffaaaa}"""
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1574
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1575 _table_template = """
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1576 <table class="diff" id="difflib_chg_%(prefix)s_top"
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1577 cellspacing="0" cellpadding="0" rules="groups" >
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1578 <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1579 <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1580 %(header_row)s
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1581 <tbody>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1582 %(data_rows)s </tbody>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1583 </table>"""
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1584
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1585 _legend = """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1586 <table class="diff" summary="Legends">
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1587 <tr> <th colspan="2"> Legends </th> </tr>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1588 <tr> <td> <table border="" summary="Colors">
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1589 <tr><th> Colors </th> </tr>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1590 <tr><td class="diff_add">&nbsp;Added&nbsp;</td></tr>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1591 <tr><td class="diff_chg">Changed</td> </tr>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1592 <tr><td class="diff_sub">Deleted</td> </tr>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1593 </table></td>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1594 <td> <table border="" summary="Links">
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1595 <tr><th colspan="2"> Links </th> </tr>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1596 <tr><td>(f)irst change</td> </tr>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1597 <tr><td>(n)ext change</td> </tr>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1598 <tr><td>(t)op</td> </tr>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1599 </table></td> </tr>
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1600 </table>"""
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1601
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1602 class HtmlDiff(object):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1603 """For producing HTML side by side comparison with change highlights.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1604
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1605 This class can be used to create an HTML table (or a complete HTML file
33448
5570fae1f01c Typo fix: 'comparisions' is not a word
Andrew M. Kuchling <amk@amk.ca>
parents: 33326
diff changeset
1606 containing the table) showing a side by side, line by line comparison
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1607 of text with inter-line and intra-line change highlights. The table can
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1608 be generated in either full or contextual difference mode.
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1609
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1610 The following methods are provided for HTML generation:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1611
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1612 make_table -- generates HTML for a single side by side table
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1613 make_file -- generates complete HTML file with a single side by side table
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1614
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1615 See tools/scripts/diff.py for an example usage of this class.
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1616 """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1617
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1618 _file_template = _file_template
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1619 _styles = _styles
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1620 _table_template = _table_template
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1621 _legend = _legend
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1622 _default_prefix = 0
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1623
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1624 def __init__(self,tabsize=8,wrapcolumn=None,linejunk=None,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1625 charjunk=IS_CHARACTER_JUNK):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1626 """HtmlDiff instance initializer
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1627
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1628 Arguments:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1629 tabsize -- tab stop spacing, defaults to 8.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1630 wrapcolumn -- column number where lines are broken and wrapped,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1631 defaults to None where lines are not wrapped.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1632 linejunk,charjunk -- keyword arguments passed into ndiff() (used to by
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1633 HtmlDiff() to generate the side by side HTML differences). See
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1634 ndiff() documentation for argument default values and descriptions.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1635 """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1636 self._tabsize = tabsize
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1637 self._wrapcolumn = wrapcolumn
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1638 self._linejunk = linejunk
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1639 self._charjunk = charjunk
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1640
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1641 def make_file(self,fromlines,tolines,fromdesc='',todesc='',context=False,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1642 numlines=5):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1643 """Returns HTML file of side by side comparison with change highlights
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1644
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1645 Arguments:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1646 fromlines -- list of "from" lines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1647 tolines -- list of "to" lines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1648 fromdesc -- "from" file column header string
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1649 todesc -- "to" file column header string
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1650 context -- set to True for contextual differences (defaults to False
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1651 which shows full differences).
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1652 numlines -- number of context lines. When context is set True,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1653 controls number of lines displayed before and after the change.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1654 When context is False, controls the number of lines to place
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1655 the "next" link anchors before the next change (so click of
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1656 "next" link jumps to just before the change).
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1657 """
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1658
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1659 return self._file_template % dict(
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1660 styles = self._styles,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1661 legend = self._legend,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1662 table = self.make_table(fromlines,tolines,fromdesc,todesc,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1663 context=context,numlines=numlines))
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1664
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1665 def _tab_newline_replace(self,fromlines,tolines):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1666 """Returns from/to line lists with tabs expanded and newlines removed.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1667
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1668 Instead of tab characters being replaced by the number of spaces
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1669 needed to fill in to the next tab stop, this function will fill
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1670 the space with tab characters. This is done so that the difference
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1671 algorithms can identify changes in a file when tabs are replaced by
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1672 spaces and vice versa. At the end of the HTML generation, the tab
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1673 characters will be replaced with a nonbreakable space.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1674 """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1675 def expand_tabs(line):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1676 # hide real spaces
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1677 line = line.replace(' ','\0')
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1678 # expand tabs into spaces
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1679 line = line.expandtabs(self._tabsize)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1680 # relace spaces from expanded tabs back into tab characters
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1681 # (we'll replace them with markup after we do differencing)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1682 line = line.replace(' ','\t')
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1683 return line.replace('\0',' ').rstrip('\n')
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1684 fromlines = [expand_tabs(line) for line in fromlines]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1685 tolines = [expand_tabs(line) for line in tolines]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1686 return fromlines,tolines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1687
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1688 def _split_line(self,data_list,line_num,text):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1689 """Builds list of text lines by splitting text lines at wrap point
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1690
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1691 This function will determine if the input text line needs to be
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1692 wrapped (split) into separate lines. If so, the first wrap point
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1693 will be determined and the first line appended to the output
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1694 text line list. This function is used recursively to handle
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1695 the second part of the split line to further split it.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1696 """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1697 # if blank line or context separator, just add it to the output list
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1698 if not line_num:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1699 data_list.append((line_num,text))
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1700 return
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1701
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1702 # if line text doesn't need wrapping, just add it to the output list
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1703 size = len(text)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1704 max = self._wrapcolumn
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1705 if (size <= max) or ((size -(text.count('\0')*3)) <= max):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1706 data_list.append((line_num,text))
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1707 return
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1708
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1709 # scan text looking for the wrap point, keeping track if the wrap
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1710 # point is inside markers
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1711 i = 0
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1712 n = 0
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1713 mark = ''
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1714 while n < max and i < size:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1715 if text[i] == '\0':
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1716 i += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1717 mark = text[i]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1718 i += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1719 elif text[i] == '\1':
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1720 i += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1721 mark = ''
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1722 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1723 i += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1724 n += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1725
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1726 # wrap point is inside text, break it up into separate lines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1727 line1 = text[:i]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1728 line2 = text[i:]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1729
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1730 # if wrap point is inside markers, place end marker at end of first
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1731 # line and start marker at beginning of second line because each
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1732 # line will have its own table tag markup around it.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1733 if mark:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1734 line1 = line1 + '\1'
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1735 line2 = '\0' + mark + line2
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1736
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1737 # tack on first line onto the output list
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1738 data_list.append((line_num,line1))
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1739
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1740 # use this routine again to wrap the remaining text
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1741 self._split_line(data_list,'>',line2)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1742
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1743 def _line_wrapper(self,diffs):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1744 """Returns iterator that splits (wraps) mdiff text lines"""
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1745
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1746 # pull from/to data and flags from mdiff iterator
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1747 for fromdata,todata,flag in diffs:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1748 # check for context separators and pass them through
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1749 if flag is None:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1750 yield fromdata,todata,flag
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1751 continue
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1752 (fromline,fromtext),(toline,totext) = fromdata,todata
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1753 # for each from/to line split it at the wrap column to form
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1754 # list of text lines.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1755 fromlist,tolist = [],[]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1756 self._split_line(fromlist,fromline,fromtext)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1757 self._split_line(tolist,toline,totext)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1758 # yield from/to line in pairs inserting blank lines as
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1759 # necessary when one side has more wrapped lines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1760 while fromlist or tolist:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1761 if fromlist:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1762 fromdata = fromlist.pop(0)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1763 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1764 fromdata = ('',' ')
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1765 if tolist:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1766 todata = tolist.pop(0)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1767 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1768 todata = ('',' ')
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1769 yield fromdata,todata,flag
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1770
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1771 def _collect_lines(self,diffs):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1772 """Collects mdiff output into separate lists
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1773
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1774 Before storing the mdiff from/to data into a list, it is converted
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1775 into a single line of text with HTML markup.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1776 """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1777
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1778 fromlist,tolist,flaglist = [],[],[]
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1779 # pull from/to data and flags from mdiff style iterator
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1780 for fromdata,todata,flag in diffs:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1781 try:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1782 # store HTML markup of the lines into the lists
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1783 fromlist.append(self._format_line(0,flag,*fromdata))
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1784 tolist.append(self._format_line(1,flag,*todata))
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1785 except TypeError:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1786 # exceptions occur for lines where context separators go
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1787 fromlist.append(None)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1788 tolist.append(None)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1789 flaglist.append(flag)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1790 return fromlist,tolist,flaglist
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1791
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1792 def _format_line(self,side,flag,linenum,text):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1793 """Returns HTML markup of "from" / "to" text lines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1794
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1795 side -- 0 or 1 indicating "from" or "to" text
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1796 flag -- indicates if difference on line
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1797 linenum -- line number (used for line number column)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1798 text -- line text to be marked up
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1799 """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1800 try:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1801 linenum = '%d' % linenum
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1802 id = ' id="%s%s"' % (self._prefix[side],linenum)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1803 except TypeError:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1804 # handle blank lines where linenum is '>' or ''
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1805 id = ''
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1806 # replace those things that would get confused with HTML symbols
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1807 text=text.replace("&","&amp;").replace(">","&gt;").replace("<","&lt;")
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1808
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1809 # make space non-breakable so they don't get compressed or line wrapped
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1810 text = text.replace(' ','&nbsp;').rstrip()
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1811
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1812 return '<td class="diff_header"%s>%s</td><td nowrap="nowrap">%s</td>' \
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1813 % (id,linenum,text)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1814
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1815 def _make_prefix(self):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1816 """Create unique anchor prefixes"""
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1817
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1818 # Generate a unique anchor prefix so multiple tables
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1819 # can exist on the same HTML page without conflicts.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1820 fromprefix = "from%d_" % HtmlDiff._default_prefix
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1821 toprefix = "to%d_" % HtmlDiff._default_prefix
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1822 HtmlDiff._default_prefix += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1823 # store prefixes so line format method has access
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1824 self._prefix = [fromprefix,toprefix]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1825
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1826 def _convert_flags(self,fromlist,tolist,flaglist,context,numlines):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1827 """Makes list of "next" links"""
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1828
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1829 # all anchor names will be generated using the unique "to" prefix
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1830 toprefix = self._prefix[1]
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1831
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1832 # process change flags, generating middle column of next anchors/links
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1833 next_id = ['']*len(flaglist)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1834 next_href = ['']*len(flaglist)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1835 num_chg, in_change = 0, False
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1836 last = 0
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1837 for i,flag in enumerate(flaglist):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1838 if flag:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1839 if not in_change:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1840 in_change = True
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1841 last = i
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1842 # at the beginning of a change, drop an anchor a few lines
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1843 # (the context lines) before the change for the previous
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1844 # link
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1845 i = max([0,i-numlines])
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1846 next_id[i] = ' id="difflib_chg_%s_%d"' % (toprefix,num_chg)
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1847 # at the beginning of a change, drop a link to the next
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1848 # change
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1849 num_chg += 1
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1850 next_href[last] = '<a href="#difflib_chg_%s_%d">n</a>' % (
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1851 toprefix,num_chg)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1852 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1853 in_change = False
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1854 # check for cases where there is no content to avoid exceptions
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1855 if not flaglist:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1856 flaglist = [False]
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1857 next_id = ['']
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1858 next_href = ['']
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1859 last = 0
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1860 if context:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1861 fromlist = ['<td></td><td>&nbsp;No Differences Found&nbsp;</td>']
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1862 tolist = fromlist
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1863 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1864 fromlist = tolist = ['<td></td><td>&nbsp;Empty File&nbsp;</td>']
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1865 # if not a change on first line, drop a link
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1866 if not flaglist[0]:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1867 next_href[0] = '<a href="#difflib_chg_%s_0">f</a>' % toprefix
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1868 # redo the last link to link to the top
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1869 next_href[last] = '<a href="#difflib_chg_%s_top">t</a>' % (toprefix)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1870
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1871 return fromlist,tolist,flaglist,next_href,next_id
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1872
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1873 def make_table(self,fromlines,tolines,fromdesc='',todesc='',context=False,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1874 numlines=5):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1875 """Returns HTML table of side by side comparison with change highlights
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1876
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1877 Arguments:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1878 fromlines -- list of "from" lines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1879 tolines -- list of "to" lines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1880 fromdesc -- "from" file column header string
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1881 todesc -- "to" file column header string
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1882 context -- set to True for contextual differences (defaults to False
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1883 which shows full differences).
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1884 numlines -- number of context lines. When context is set True,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1885 controls number of lines displayed before and after the change.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1886 When context is False, controls the number of lines to place
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1887 the "next" link anchors before the next change (so click of
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1888 "next" link jumps to just before the change).
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1889 """
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1890
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1891 # make unique anchor prefixes so that multiple tables may exist
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1892 # on the same page without conflict.
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1893 self._make_prefix()
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1894
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1895 # change tabs to spaces before it gets more difficult after we insert
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1896 # markkup
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1897 fromlines,tolines = self._tab_newline_replace(fromlines,tolines)
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1898
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1899 # create diffs iterator which generates side by side from/to data
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1900 if context:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1901 context_lines = numlines
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1902 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1903 context_lines = None
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1904 diffs = _mdiff(fromlines,tolines,context_lines,linejunk=self._linejunk,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1905 charjunk=self._charjunk)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1906
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1907 # set up iterator to wrap lines that exceed desired width
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1908 if self._wrapcolumn:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1909 diffs = self._line_wrapper(diffs)
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1910
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1911 # collect up from/to lines and flags into lists (also format the lines)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1912 fromlist,tolist,flaglist = self._collect_lines(diffs)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1913
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1914 # process change flags, generating middle column of next anchors/links
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1915 fromlist,tolist,flaglist,next_href,next_id = self._convert_flags(
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1916 fromlist,tolist,flaglist,context,numlines)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1917
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1918 import cStringIO
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1919 s = cStringIO.StringIO()
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1920 fmt = ' <tr><td class="diff_next"%s>%s</td>%s' + \
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1921 '<td class="diff_next">%s</td>%s</tr>\n'
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1922 for i in range(len(flaglist)):
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1923 if flaglist[i] is None:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1924 # mdiff yields None on separator lines skip the bogus ones
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1925 # generated for the first line
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1926 if i > 0:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1927 s.write(' </tbody> \n <tbody>\n')
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1928 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1929 s.write( fmt % (next_id[i],next_href[i],fromlist[i],
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1930 next_href[i],tolist[i]))
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1931 if fromdesc or todesc:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1932 header_row = '<thead><tr>%s%s%s%s</tr></thead>' % (
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1933 '<th class="diff_next"><br /></th>',
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1934 '<th colspan="2" class="diff_header">%s</th>' % fromdesc,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1935 '<th class="diff_next"><br /></th>',
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1936 '<th colspan="2" class="diff_header">%s</th>' % todesc)
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1937 else:
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1938 header_row = ''
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1939
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1940 table = self._table_template % dict(
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1941 data_rows=s.getvalue(),
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1942 header_row=header_row,
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1943 prefix=self._prefix[1])
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1944
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1945 return table.replace('\0+','<span class="diff_add">'). \
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1946 replace('\0-','<span class="diff_sub">'). \
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1947 replace('\0^','<span class="diff_chg">'). \
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1948 replace('\1','</span>'). \
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1949 replace('\t','&nbsp;')
33326
787fff51bf2a Whitespace normalization. test_difflib passes again.
Tim Peters <tim.peters@gmail.com>
parents: 33324
diff changeset
1950
33319
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1951 del re
6d3bdd19beeb Patch #914575: difflib side by side diff support, diff.py s/b/s HTML option.
Martin v. Löwis <martin@v.loewis.de>
parents: 32867
diff changeset
1952
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1953 def restore(delta, which):
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1954 r"""
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1955 Generate one of the two sequences that generated a delta.
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1956
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1957 Given a `delta` produced by `Differ.compare()` or `ndiff()`, extract
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1958 lines originating from file 1 or 2 (parameter `which`), stripping off line
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1959 prefixes.
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1960
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1961 Examples:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1962
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1963 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1964 ... 'ore\ntree\nemu\n'.splitlines(1))
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1965 >>> diff = list(diff)
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1966 >>> print ''.join(restore(diff, 1)),
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1967 one
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1968 two
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1969 three
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1970 >>> print ''.join(restore(diff, 2)),
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1971 ore
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1972 tree
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1973 emu
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1974 """
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1975 try:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1976 tag = {1: "- ", 2: "+ "}[int(which)]
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1977 except KeyError:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1978 raise ValueError, ('unknown delta choice (must be 1 or 2): %r'
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1979 % which)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1980 prefixes = (" ", tag)
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1981 for line in delta:
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1982 if line[:2] in prefixes:
20038
8a6f2ee6eaba Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents: 19200
diff changeset
1983 yield line[2:]
19200
50fb4683a461 SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents: 16703
diff changeset
1984
16551
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1985 def _test():
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1986 import doctest, difflib
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1987 return doctest.testmod(difflib)
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1988
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1989 if __name__ == "__main__":
7de5043827f7 Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
1990 _test()