author | Wagner Bruna <wbruna@softwareexpress.com.br> |
Wed, 31 May 2017 19:24:00 -0300 | |
branch | stable |
changeset 32574 | 870248603a4e |
parent 31641 | f2b334e6c7e0 |
child 32369 | 3b88a7fa97d8 |
permissions | -rw-r--r-- |
7703
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
1 |
# bdiff.py - Python implementation of bdiff.c |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
2 |
# |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
3 |
# Copyright 2009 Matt Mackall <mpm@selenic.com> and others |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
4 |
# |
8225
46293a0c7e9f
updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents:
7944
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
10263 | 6 |
# GNU General Public License version 2 or any later version. |
7703
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
7 |
|
27335
c4e3ff497f89
bdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
15530
diff
changeset
|
8 |
from __future__ import absolute_import |
c4e3ff497f89
bdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
15530
diff
changeset
|
9 |
|
c4e3ff497f89
bdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
15530
diff
changeset
|
10 |
import difflib |
c4e3ff497f89
bdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
15530
diff
changeset
|
11 |
import re |
c4e3ff497f89
bdiff: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
15530
diff
changeset
|
12 |
import struct |
7944
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
13 |
|
29833
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
14 |
from . import policy |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
15 |
policynocffi = policy.policynocffi |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
16 |
modulepolicy = policy.policy |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
17 |
|
7944
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
18 |
def splitnewlines(text): |
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
19 |
'''like str.splitlines, but only split on newlines.''' |
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
20 |
lines = [l + '\n' for l in text.split('\n')] |
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
21 |
if lines: |
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
22 |
if lines[-1] == '\n': |
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
23 |
lines.pop() |
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
24 |
else: |
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
25 |
lines[-1] = lines[-1][:-1] |
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
26 |
return lines |
7703
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
27 |
|
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
28 |
def _normalizeblocks(a, b, blocks): |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
29 |
prev = None |
14066
14fac6c0536a
pure bdiff: don't use a generator
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
10282
diff
changeset
|
30 |
r = [] |
7703
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
31 |
for curr in blocks: |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
32 |
if prev is None: |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
33 |
prev = curr |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
34 |
continue |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
35 |
shift = 0 |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
36 |
|
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
37 |
a1, b1, l1 = prev |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
38 |
a1end = a1 + l1 |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
39 |
b1end = b1 + l1 |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
40 |
|
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
41 |
a2, b2, l2 = curr |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
42 |
a2end = a2 + l2 |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
43 |
b2end = b2 + l2 |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
44 |
if a1end == a2: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
45 |
while (a1end + shift < a2end and |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
46 |
a[a1end + shift] == b[b1end + shift]): |
7703
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
47 |
shift += 1 |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
48 |
elif b1end == b2: |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
49 |
while (b1end + shift < b2end and |
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
50 |
a[a1end + shift] == b[b1end + shift]): |
7703
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
51 |
shift += 1 |
14066
14fac6c0536a
pure bdiff: don't use a generator
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
10282
diff
changeset
|
52 |
r.append((a1, b1, l1 + shift)) |
10282
08a0f04b56bd
many, many trivial check-code fixups
Matt Mackall <mpm@selenic.com>
parents:
10263
diff
changeset
|
53 |
prev = a2 + shift, b2 + shift, l2 - shift |
14066
14fac6c0536a
pure bdiff: don't use a generator
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
10282
diff
changeset
|
54 |
r.append(prev) |
14fac6c0536a
pure bdiff: don't use a generator
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
10282
diff
changeset
|
55 |
return r |
7703
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
56 |
|
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
57 |
def bdiff(a, b): |
31641
f2b334e6c7e0
py3: use bytes() to cast to immutable bytes in pure.bdiff.bdiff()
Yuya Nishihara <yuya@tcha.org>
parents:
31640
diff
changeset
|
58 |
a = bytes(a).splitlines(True) |
f2b334e6c7e0
py3: use bytes() to cast to immutable bytes in pure.bdiff.bdiff()
Yuya Nishihara <yuya@tcha.org>
parents:
31640
diff
changeset
|
59 |
b = bytes(b).splitlines(True) |
7703
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
60 |
|
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
61 |
if not a: |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
62 |
s = "".join(b) |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
63 |
return s and (struct.pack(">lll", 0, 0, len(s)) + s) |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
64 |
|
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
65 |
bin = [] |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
66 |
p = [0] |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
67 |
for i in a: p.append(p[-1] + len(i)) |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
68 |
|
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
69 |
d = difflib.SequenceMatcher(None, a, b).get_matching_blocks() |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
70 |
d = _normalizeblocks(a, b, d) |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
71 |
la = 0 |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
72 |
lb = 0 |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
73 |
for am, bm, size in d: |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
74 |
s = "".join(b[lb:bm]) |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
75 |
if am > la or s: |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
76 |
bin.append(struct.pack(">lll", p[la], p[am], len(s)) + s) |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
77 |
la = am + size |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
78 |
lb = bm + size |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
79 |
|
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
80 |
return "".join(bin) |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
81 |
|
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
82 |
def blocks(a, b): |
7944
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
83 |
an = splitnewlines(a) |
e9b48afd0e78
pure/bdiff: fix circular import
Matt Mackall <mpm@selenic.com>
parents:
7703
diff
changeset
|
84 |
bn = splitnewlines(b) |
7703
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
85 |
d = difflib.SequenceMatcher(None, an, bn).get_matching_blocks() |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
86 |
d = _normalizeblocks(an, bn, d) |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
87 |
return [(i, i + n, j, j + n) for (i, j, n) in d] |
9044d3567f6d
pure Python implementation of bdiff.c
Martin Geisler <mg@daimi.au.dk>
parents:
diff
changeset
|
88 |
|
15530
eeac5e179243
mdiff: replace wscleanup() regexps with C loops
Patrick Mezard <pmezard@gmail.com>
parents:
14066
diff
changeset
|
89 |
def fixws(text, allws): |
eeac5e179243
mdiff: replace wscleanup() regexps with C loops
Patrick Mezard <pmezard@gmail.com>
parents:
14066
diff
changeset
|
90 |
if allws: |
eeac5e179243
mdiff: replace wscleanup() regexps with C loops
Patrick Mezard <pmezard@gmail.com>
parents:
14066
diff
changeset
|
91 |
text = re.sub('[ \t\r]+', '', text) |
eeac5e179243
mdiff: replace wscleanup() regexps with C loops
Patrick Mezard <pmezard@gmail.com>
parents:
14066
diff
changeset
|
92 |
else: |
eeac5e179243
mdiff: replace wscleanup() regexps with C loops
Patrick Mezard <pmezard@gmail.com>
parents:
14066
diff
changeset
|
93 |
text = re.sub('[ \t\r]+', ' ', text) |
eeac5e179243
mdiff: replace wscleanup() regexps with C loops
Patrick Mezard <pmezard@gmail.com>
parents:
14066
diff
changeset
|
94 |
text = text.replace(' \n', '\n') |
eeac5e179243
mdiff: replace wscleanup() regexps with C loops
Patrick Mezard <pmezard@gmail.com>
parents:
14066
diff
changeset
|
95 |
return text |
29833
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
96 |
|
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
97 |
if modulepolicy not in policynocffi: |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
98 |
try: |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
99 |
from _bdiff_cffi import ffi, lib |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
100 |
except ImportError: |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
101 |
if modulepolicy == 'cffi': # strict cffi import |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
102 |
raise |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
103 |
else: |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
104 |
def blocks(sa, sb): |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
105 |
a = ffi.new("struct bdiff_line**") |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
106 |
b = ffi.new("struct bdiff_line**") |
30042
d24e03da24b5
lazymanifest: write a more efficient, pypy friendly version of lazymanifest
Maciej Fijalkowski <fijall@gmail.com>
parents:
29834
diff
changeset
|
107 |
ac = ffi.new("char[]", str(sa)) |
d24e03da24b5
lazymanifest: write a more efficient, pypy friendly version of lazymanifest
Maciej Fijalkowski <fijall@gmail.com>
parents:
29834
diff
changeset
|
108 |
bc = ffi.new("char[]", str(sb)) |
29834
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
109 |
l = ffi.new("struct bdiff_hunk*") |
29833
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
110 |
try: |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
111 |
an = lib.bdiff_splitlines(ac, len(sa), a) |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
112 |
bn = lib.bdiff_splitlines(bc, len(sb), b) |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
113 |
if not a[0] or not b[0]: |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
114 |
raise MemoryError |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
115 |
count = lib.bdiff_diff(a[0], an, b[0], bn, l) |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
116 |
if count < 0: |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
117 |
raise MemoryError |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
118 |
rl = [None] * count |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
119 |
h = l.next |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
120 |
i = 0 |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
121 |
while h: |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
122 |
rl[i] = (h.a1, h.a2, h.b1, h.b2) |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
123 |
h = h.next |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
124 |
i += 1 |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
125 |
finally: |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
126 |
lib.free(a[0]) |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
127 |
lib.free(b[0]) |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
128 |
lib.bdiff_freehunks(l.next) |
a8933d992a71
bdiff: implement cffi version of blocks
Maciej Fijalkowski <fijall@gmail.com>
parents:
28389
diff
changeset
|
129 |
return rl |
29834
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
130 |
|
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
131 |
def bdiff(sa, sb): |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
132 |
a = ffi.new("struct bdiff_line**") |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
133 |
b = ffi.new("struct bdiff_line**") |
30042
d24e03da24b5
lazymanifest: write a more efficient, pypy friendly version of lazymanifest
Maciej Fijalkowski <fijall@gmail.com>
parents:
29834
diff
changeset
|
134 |
ac = ffi.new("char[]", str(sa)) |
d24e03da24b5
lazymanifest: write a more efficient, pypy friendly version of lazymanifest
Maciej Fijalkowski <fijall@gmail.com>
parents:
29834
diff
changeset
|
135 |
bc = ffi.new("char[]", str(sb)) |
29834
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
136 |
l = ffi.new("struct bdiff_hunk*") |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
137 |
try: |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
138 |
an = lib.bdiff_splitlines(ac, len(sa), a) |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
139 |
bn = lib.bdiff_splitlines(bc, len(sb), b) |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
140 |
if not a[0] or not b[0]: |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
141 |
raise MemoryError |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
142 |
count = lib.bdiff_diff(a[0], an, b[0], bn, l) |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
143 |
if count < 0: |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
144 |
raise MemoryError |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
145 |
rl = [] |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
146 |
h = l.next |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
147 |
la = lb = 0 |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
148 |
while h: |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
149 |
if h.a1 != la or h.b1 != lb: |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
150 |
lgt = (b[0] + h.b1).l - (b[0] + lb).l |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
151 |
rl.append(struct.pack(">lll", (a[0] + la).l - a[0].l, |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
152 |
(a[0] + h.a1).l - a[0].l, lgt)) |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
153 |
rl.append(str(ffi.buffer((b[0] + lb).l, lgt))) |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
154 |
la = h.a2 |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
155 |
lb = h.b2 |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
156 |
h = h.next |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
157 |
|
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
158 |
finally: |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
159 |
lib.free(a[0]) |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
160 |
lib.free(b[0]) |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
161 |
lib.bdiff_freehunks(l.next) |
1ea77b75d266
bdiff: implement cffi version of bdiff
Maciej Fijalkowski <fijall@gmail.com>
parents:
29833
diff
changeset
|
162 |
return "".join(rl) |