Mercurial > hg
comparison mercurial/cffi/bdiff.py @ 32513:25b37900d6e0
cffi: remove superfluous "if True" blocks
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Tue, 02 May 2017 21:45:10 +0900 |
parents | 0e8b0b9a7acc |
children | 857876ebaed4 |
comparison
equal
deleted
inserted
replaced
32512:0e8b0b9a7acc | 32513:25b37900d6e0 |
---|---|
13 from . import _bdiff | 13 from . import _bdiff |
14 | 14 |
15 ffi = _bdiff.ffi | 15 ffi = _bdiff.ffi |
16 lib = _bdiff.lib | 16 lib = _bdiff.lib |
17 | 17 |
18 if True: | 18 def blocks(sa, sb): |
19 if True: | 19 a = ffi.new("struct bdiff_line**") |
20 def blocks(sa, sb): | 20 b = ffi.new("struct bdiff_line**") |
21 a = ffi.new("struct bdiff_line**") | 21 ac = ffi.new("char[]", str(sa)) |
22 b = ffi.new("struct bdiff_line**") | 22 bc = ffi.new("char[]", str(sb)) |
23 ac = ffi.new("char[]", str(sa)) | 23 l = ffi.new("struct bdiff_hunk*") |
24 bc = ffi.new("char[]", str(sb)) | 24 try: |
25 l = ffi.new("struct bdiff_hunk*") | 25 an = lib.bdiff_splitlines(ac, len(sa), a) |
26 try: | 26 bn = lib.bdiff_splitlines(bc, len(sb), b) |
27 an = lib.bdiff_splitlines(ac, len(sa), a) | 27 if not a[0] or not b[0]: |
28 bn = lib.bdiff_splitlines(bc, len(sb), b) | 28 raise MemoryError |
29 if not a[0] or not b[0]: | 29 count = lib.bdiff_diff(a[0], an, b[0], bn, l) |
30 raise MemoryError | 30 if count < 0: |
31 count = lib.bdiff_diff(a[0], an, b[0], bn, l) | 31 raise MemoryError |
32 if count < 0: | 32 rl = [None] * count |
33 raise MemoryError | 33 h = l.next |
34 rl = [None] * count | 34 i = 0 |
35 h = l.next | 35 while h: |
36 i = 0 | 36 rl[i] = (h.a1, h.a2, h.b1, h.b2) |
37 while h: | 37 h = h.next |
38 rl[i] = (h.a1, h.a2, h.b1, h.b2) | 38 i += 1 |
39 h = h.next | 39 finally: |
40 i += 1 | 40 lib.free(a[0]) |
41 finally: | 41 lib.free(b[0]) |
42 lib.free(a[0]) | 42 lib.bdiff_freehunks(l.next) |
43 lib.free(b[0]) | 43 return rl |
44 lib.bdiff_freehunks(l.next) | |
45 return rl | |
46 | 44 |
47 def bdiff(sa, sb): | 45 def bdiff(sa, sb): |
48 a = ffi.new("struct bdiff_line**") | 46 a = ffi.new("struct bdiff_line**") |
49 b = ffi.new("struct bdiff_line**") | 47 b = ffi.new("struct bdiff_line**") |
50 ac = ffi.new("char[]", str(sa)) | 48 ac = ffi.new("char[]", str(sa)) |
51 bc = ffi.new("char[]", str(sb)) | 49 bc = ffi.new("char[]", str(sb)) |
52 l = ffi.new("struct bdiff_hunk*") | 50 l = ffi.new("struct bdiff_hunk*") |
53 try: | 51 try: |
54 an = lib.bdiff_splitlines(ac, len(sa), a) | 52 an = lib.bdiff_splitlines(ac, len(sa), a) |
55 bn = lib.bdiff_splitlines(bc, len(sb), b) | 53 bn = lib.bdiff_splitlines(bc, len(sb), b) |
56 if not a[0] or not b[0]: | 54 if not a[0] or not b[0]: |
57 raise MemoryError | 55 raise MemoryError |
58 count = lib.bdiff_diff(a[0], an, b[0], bn, l) | 56 count = lib.bdiff_diff(a[0], an, b[0], bn, l) |
59 if count < 0: | 57 if count < 0: |
60 raise MemoryError | 58 raise MemoryError |
61 rl = [] | 59 rl = [] |
62 h = l.next | 60 h = l.next |
63 la = lb = 0 | 61 la = lb = 0 |
64 while h: | 62 while h: |
65 if h.a1 != la or h.b1 != lb: | 63 if h.a1 != la or h.b1 != lb: |
66 lgt = (b[0] + h.b1).l - (b[0] + lb).l | 64 lgt = (b[0] + h.b1).l - (b[0] + lb).l |
67 rl.append(struct.pack(">lll", (a[0] + la).l - a[0].l, | 65 rl.append(struct.pack(">lll", (a[0] + la).l - a[0].l, |
68 (a[0] + h.a1).l - a[0].l, lgt)) | 66 (a[0] + h.a1).l - a[0].l, lgt)) |
69 rl.append(str(ffi.buffer((b[0] + lb).l, lgt))) | 67 rl.append(str(ffi.buffer((b[0] + lb).l, lgt))) |
70 la = h.a2 | 68 la = h.a2 |
71 lb = h.b2 | 69 lb = h.b2 |
72 h = h.next | 70 h = h.next |
73 | 71 |
74 finally: | 72 finally: |
75 lib.free(a[0]) | 73 lib.free(a[0]) |
76 lib.free(b[0]) | 74 lib.free(b[0]) |
77 lib.bdiff_freehunks(l.next) | 75 lib.bdiff_freehunks(l.next) |
78 return "".join(rl) | 76 return "".join(rl) |