Show More
@@ -43,25 +43,28 def textdiff(a, b): | |||||
43 |
|
43 | |||
44 | def sortdiff(a, b): |
|
44 | def sortdiff(a, b): | |
45 | la = lb = 0 |
|
45 | la = lb = 0 | |
46 | lena = len(a) |
|
46 | ||
47 | lenb = len(b) |
|
|||
48 | while 1: |
|
47 | while 1: | |
49 | am, bm, = la, lb |
|
48 | if la >= len(a) or lb >= len(b): break | |
50 | while lb < lenb and la < len and a[la] == b[lb] : |
|
49 | if b[lb] < a[la]: | |
|
50 | si = lb | |||
|
51 | while lb < len(b) and b[lb] < a[la] : lb += 1 | |||
|
52 | yield "insert", la, la, si, lb | |||
|
53 | elif a[la] < b[lb]: | |||
|
54 | si = la | |||
|
55 | while la < len(a) and a[la] < b[lb]: la += 1 | |||
|
56 | yield "delete", si, la, lb, lb | |||
|
57 | else: | |||
51 | la += 1 |
|
58 | la += 1 | |
52 | lb += 1 |
|
59 | lb += 1 | |
53 | if la>am: yield (am, bm, la-am) |
|
60 | ||
54 | while lb < lenb and b[lb] < a[la]: lb += 1 |
|
61 | if lb < len(b): | |
55 | if lb>=lenb: break |
|
62 | yield "insert", la, la, lb, len(b) | |
56 | while la < lena and b[lb] > a[la]: la += 1 |
|
63 | ||
57 |
|
|
64 | if la < len(a): | |
58 |
yield |
|
65 | yield "delete", la, len(a), lb, lb | |
59 |
|
66 | |||
60 | def diff(a, b, sorted=0): |
|
67 | def diff(a, b, sorted=0): | |
61 | if not a: |
|
|||
62 | s = "".join(b) |
|
|||
63 | return s and (struct.pack(">lll", 0, 0, len(s)) + s) |
|
|||
64 |
|
||||
65 | bin = [] |
|
68 | bin = [] | |
66 | p = [0] |
|
69 | p = [0] | |
67 | for i in a: p.append(p[-1] + len(i)) |
|
70 | for i in a: p.append(p[-1] + len(i)) | |
@@ -73,15 +76,12 def diff(a, b, sorted=0): | |||||
73 | print a, b |
|
76 | print a, b | |
74 | raise |
|
77 | raise | |
75 | else: |
|
78 | else: | |
76 |
d = difflib.SequenceMatcher(None, a, b).get_ |
|
79 | d = difflib.SequenceMatcher(None, a, b).get_opcodes() | |
77 | la = 0 |
|
80 | ||
78 | lb = 0 |
|
81 | for o, m, n, s, t in d: | |
79 | for am, bm, size in d: |
|
82 | if o == 'equal': continue | |
80 |
s = "".join(b[ |
|
83 | s = "".join(b[s:t]) | |
81 | if am > la or s: |
|
84 | bin.append(struct.pack(">lll", p[m], p[n], len(s)) + s) | |
82 | bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s) |
|
|||
83 | la = am + size |
|
|||
84 | lb = bm + size |
|
|||
85 |
|
85 | |||
86 | return "".join(bin) |
|
86 | return "".join(bin) | |
87 |
|
87 |
General Comments 0
You need to be logged in to leave comments.
Login now