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