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