Show More
@@ -1,88 +1,88 | |||||
1 | # bdiff.py - CFFI implementation of bdiff.c |
|
1 | # bdiff.py - CFFI implementation of bdiff.c | |
2 | # |
|
2 | # | |
3 | # Copyright 2016 Maciej Fijalkowski <fijall@gmail.com> |
|
3 | # Copyright 2016 Maciej Fijalkowski <fijall@gmail.com> | |
4 | # |
|
4 | # | |
5 | # This software may be used and distributed according to the terms of the |
|
5 | # This software may be used and distributed according to the terms of the | |
6 | # GNU General Public License version 2 or any later version. |
|
6 | # GNU General Public License version 2 or any later version. | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | import struct |
|
9 | import struct | |
10 |
|
10 | |||
11 | from typing import ( |
|
11 | from typing import ( | |
12 | List, |
|
12 | List, | |
13 | Tuple, |
|
13 | Tuple, | |
14 | ) |
|
14 | ) | |
15 |
|
15 | |||
16 | from ..pure.bdiff import * |
|
16 | from ..pure.bdiff import * | |
17 | from . import _bdiff # pytype: disable=import-error |
|
17 | from . import _bdiff # pytype: disable=import-error | |
18 |
|
18 | |||
19 | ffi = _bdiff.ffi |
|
19 | ffi = _bdiff.ffi | |
20 | lib = _bdiff.lib |
|
20 | lib = _bdiff.lib | |
21 |
|
21 | |||
22 |
|
22 | |||
23 | def blocks(sa: bytes, sb: bytes) -> List[Tuple[int, int, int, int]]: |
|
23 | def blocks(sa: bytes, sb: bytes) -> List[Tuple[int, int, int, int]]: | |
24 | a = ffi.new("struct bdiff_line**") |
|
24 | a = ffi.new("struct bdiff_line**") | |
25 | b = ffi.new("struct bdiff_line**") |
|
25 | b = ffi.new("struct bdiff_line**") | |
26 | ac = ffi.new("char[]", str(sa)) |
|
26 | ac = ffi.new("char[]", str(sa)) | |
27 | bc = ffi.new("char[]", str(sb)) |
|
27 | bc = ffi.new("char[]", str(sb)) | |
28 | l = ffi.new("struct bdiff_hunk*") |
|
28 | l = ffi.new("struct bdiff_hunk*") | |
29 | try: |
|
29 | try: | |
30 | an = lib.bdiff_splitlines(ac, len(sa), a) |
|
30 | an = lib.bdiff_splitlines(ac, len(sa), a) | |
31 | bn = lib.bdiff_splitlines(bc, len(sb), b) |
|
31 | bn = lib.bdiff_splitlines(bc, len(sb), b) | |
32 | if not a[0] or not b[0]: |
|
32 | if not a[0] or not b[0]: | |
33 | raise MemoryError |
|
33 | raise MemoryError | |
34 | count = lib.bdiff_diff(a[0], an, b[0], bn, l) |
|
34 | count = lib.bdiff_diff(a[0], an, b[0], bn, l) | |
35 | if count < 0: |
|
35 | if count < 0: | |
36 | raise MemoryError |
|
36 | raise MemoryError | |
37 | rl = [(0, 0, 0, 0)] * count |
|
37 | rl = [(0, 0, 0, 0)] * count | |
38 | h = l.next |
|
38 | h = l.next | |
39 | i = 0 |
|
39 | i = 0 | |
40 | while h: |
|
40 | while h: | |
41 | rl[i] = (h.a1, h.a2, h.b1, h.b2) |
|
41 | rl[i] = (h.a1, h.a2, h.b1, h.b2) | |
42 | h = h.next |
|
42 | h = h.next | |
43 | i += 1 |
|
43 | i += 1 | |
44 | finally: |
|
44 | finally: | |
45 | lib.free(a[0]) |
|
45 | lib.free(a[0]) | |
46 | lib.free(b[0]) |
|
46 | lib.free(b[0]) | |
47 | lib.bdiff_freehunks(l.next) |
|
47 | lib.bdiff_freehunks(l.next) | |
48 | return rl |
|
48 | return rl | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | def bdiff(sa: bytes, sb: bytes) -> bytes: |
|
51 | def bdiff(sa: bytes, sb: bytes) -> bytes: | |
52 | a = ffi.new("struct bdiff_line**") |
|
52 | a = ffi.new("struct bdiff_line**") | |
53 | b = ffi.new("struct bdiff_line**") |
|
53 | b = ffi.new("struct bdiff_line**") | |
54 | ac = ffi.new("char[]", str(sa)) |
|
54 | ac = ffi.new("char[]", str(sa)) | |
55 | bc = ffi.new("char[]", str(sb)) |
|
55 | bc = ffi.new("char[]", str(sb)) | |
56 | l = ffi.new("struct bdiff_hunk*") |
|
56 | l = ffi.new("struct bdiff_hunk*") | |
57 | try: |
|
57 | try: | |
58 | an = lib.bdiff_splitlines(ac, len(sa), a) |
|
58 | an = lib.bdiff_splitlines(ac, len(sa), a) | |
59 | bn = lib.bdiff_splitlines(bc, len(sb), b) |
|
59 | bn = lib.bdiff_splitlines(bc, len(sb), b) | |
60 | if not a[0] or not b[0]: |
|
60 | if not a[0] or not b[0]: | |
61 | raise MemoryError |
|
61 | raise MemoryError | |
62 | count = lib.bdiff_diff(a[0], an, b[0], bn, l) |
|
62 | count = lib.bdiff_diff(a[0], an, b[0], bn, l) | |
63 | if count < 0: |
|
63 | if count < 0: | |
64 | raise MemoryError |
|
64 | raise MemoryError | |
65 | rl = [] |
|
65 | rl = [] | |
66 | h = l.next |
|
66 | h = l.next | |
67 | la = lb = 0 |
|
67 | la = lb = 0 | |
68 | while h: |
|
68 | while h: | |
69 | if h.a1 != la or h.b1 != lb: |
|
69 | if h.a1 != la or h.b1 != lb: | |
70 | lgt = (b[0] + h.b1).l - (b[0] + lb).l |
|
70 | lgt = (b[0] + h.b1).l - (b[0] + lb).l | |
71 | rl.append( |
|
71 | rl.append( | |
72 | struct.pack( |
|
72 | struct.pack( | |
73 | b">lll", |
|
73 | b">lll", | |
74 | (a[0] + la).l - a[0].l, |
|
74 | (a[0] + la).l - a[0].l, | |
75 | (a[0] + h.a1).l - a[0].l, |
|
75 | (a[0] + h.a1).l - a[0].l, | |
76 | lgt, |
|
76 | lgt, | |
77 | ) |
|
77 | ) | |
78 | ) |
|
78 | ) | |
79 |
rl.append( |
|
79 | rl.append(bytes(ffi.buffer((b[0] + lb).l, lgt))) | |
80 | la = h.a2 |
|
80 | la = h.a2 | |
81 | lb = h.b2 |
|
81 | lb = h.b2 | |
82 | h = h.next |
|
82 | h = h.next | |
83 |
|
83 | |||
84 | finally: |
|
84 | finally: | |
85 | lib.free(a[0]) |
|
85 | lib.free(a[0]) | |
86 | lib.free(b[0]) |
|
86 | lib.free(b[0]) | |
87 | lib.bdiff_freehunks(l.next) |
|
87 | lib.bdiff_freehunks(l.next) | |
88 | return b"".join(rl) |
|
88 | return b"".join(rl) |
General Comments 0
You need to be logged in to leave comments.
Login now