Show More
@@ -1,76 +1,77 b'' | |||||
1 | #!/usr/bin/python |
|
1 | #!/usr/bin/python | |
2 | import difflib, struct |
|
2 | import difflib, struct | |
3 | from cStringIO import StringIO |
|
3 | from cStringIO import StringIO | |
4 |
|
4 | |||
5 | def unidiff(a, b, fn): |
|
5 | def unidiff(a, b, fn): | |
|
6 | if not a and not b: return "" | |||
6 | a = a.splitlines(1) |
|
7 | a = a.splitlines(1) | |
7 | b = b.splitlines(1) |
|
8 | b = b.splitlines(1) | |
8 | l = difflib.unified_diff(a, b, fn, fn) |
|
9 | l = list(difflib.unified_diff(a, b, fn, fn)) | |
9 | return "".join(l) |
|
10 | return "".join(l) | |
10 |
|
11 | |||
11 | def textdiff(a, b): |
|
12 | def textdiff(a, b): | |
12 | return diff(a.splitlines(1), b.splitlines(1)) |
|
13 | return diff(a.splitlines(1), b.splitlines(1)) | |
13 |
|
14 | |||
14 | def sortdiff(a, b): |
|
15 | def sortdiff(a, b): | |
15 | la = lb = 0 |
|
16 | la = lb = 0 | |
16 |
|
17 | |||
17 | while 1: |
|
18 | while 1: | |
18 | if la >= len(a) or lb >= len(b): break |
|
19 | if la >= len(a) or lb >= len(b): break | |
19 | if b[lb] < a[la]: |
|
20 | if b[lb] < a[la]: | |
20 | si = lb |
|
21 | si = lb | |
21 | while lb < len(b) and b[lb] < a[la] : lb += 1 |
|
22 | while lb < len(b) and b[lb] < a[la] : lb += 1 | |
22 | yield "insert", la, la, si, lb |
|
23 | yield "insert", la, la, si, lb | |
23 | elif a[la] < b[lb]: |
|
24 | elif a[la] < b[lb]: | |
24 | si = la |
|
25 | si = la | |
25 | while la < len(a) and a[la] < b[lb]: la += 1 |
|
26 | while la < len(a) and a[la] < b[lb]: la += 1 | |
26 | yield "delete", si, la, lb, lb |
|
27 | yield "delete", si, la, lb, lb | |
27 | else: |
|
28 | else: | |
28 | la += 1 |
|
29 | la += 1 | |
29 | lb += 1 |
|
30 | lb += 1 | |
30 |
|
31 | |||
31 | si = lb |
|
32 | si = lb | |
32 | while lb < len(b): |
|
33 | while lb < len(b): | |
33 | lb += 1 |
|
34 | lb += 1 | |
34 | yield "insert", la, la, si, lb |
|
35 | yield "insert", la, la, si, lb | |
35 |
|
36 | |||
36 | si = la |
|
37 | si = la | |
37 | while la < len(a): |
|
38 | while la < len(a): | |
38 | la += 1 |
|
39 | la += 1 | |
39 | yield "delete", si, la, lb, lb |
|
40 | yield "delete", si, la, lb, lb | |
40 |
|
41 | |||
41 | def diff(a, b, sorted=0): |
|
42 | def diff(a, b, sorted=0): | |
42 | bin = [] |
|
43 | bin = [] | |
43 | p = [0] |
|
44 | p = [0] | |
44 | for i in a: p.append(p[-1] + len(i)) |
|
45 | for i in a: p.append(p[-1] + len(i)) | |
45 |
|
46 | |||
46 | if sorted: |
|
47 | if sorted: | |
47 | d = sortdiff(a, b) |
|
48 | d = sortdiff(a, b) | |
48 | else: |
|
49 | else: | |
49 | d = difflib.SequenceMatcher(None, a, b).get_opcodes() |
|
50 | d = difflib.SequenceMatcher(None, a, b).get_opcodes() | |
50 |
|
51 | |||
51 | for o, m, n, s, t in d: |
|
52 | for o, m, n, s, t in d: | |
52 | if o == 'equal': continue |
|
53 | if o == 'equal': continue | |
53 | s = "".join(b[s:t]) |
|
54 | s = "".join(b[s:t]) | |
54 | bin.append(struct.pack(">lll", p[m], p[n], len(s)) + s) |
|
55 | bin.append(struct.pack(">lll", p[m], p[n], len(s)) + s) | |
55 |
|
56 | |||
56 | return "".join(bin) |
|
57 | return "".join(bin) | |
57 |
|
58 | |||
58 | def patch(a, bin): |
|
59 | def patch(a, bin): | |
59 | last = pos = 0 |
|
60 | last = pos = 0 | |
60 | r = [] |
|
61 | r = [] | |
61 |
|
62 | |||
62 | while pos < len(bin): |
|
63 | while pos < len(bin): | |
63 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) |
|
64 | p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12]) | |
64 | pos += 12 |
|
65 | pos += 12 | |
65 | r.append(a[last:p1]) |
|
66 | r.append(a[last:p1]) | |
66 | r.append(bin[pos:pos + l]) |
|
67 | r.append(bin[pos:pos + l]) | |
67 | pos += l |
|
68 | pos += l | |
68 | last = p2 |
|
69 | last = p2 | |
69 | r.append(a[last:]) |
|
70 | r.append(a[last:]) | |
70 |
|
71 | |||
71 | return "".join(r) |
|
72 | return "".join(r) | |
72 |
|
73 | |||
73 |
|
74 | |||
74 |
|
75 | |||
75 |
|
76 | |||
76 |
|
77 |
General Comments 0
You need to be logged in to leave comments.
Login now