Show More
@@ -1,479 +1,481 b'' | |||||
1 | /* |
|
1 | /* | |
2 | bdiff.c - efficient binary diff extension for Mercurial |
|
2 | bdiff.c - efficient binary diff extension for Mercurial | |
3 |
|
3 | |||
4 | Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
|
4 | Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | |
5 |
|
5 | |||
6 | This software may be used and distributed according to the terms of |
|
6 | This software may be used and distributed according to the terms of | |
7 | the GNU General Public License, incorporated herein by reference. |
|
7 | the GNU General Public License, incorporated herein by reference. | |
8 |
|
8 | |||
9 | Based roughly on Python difflib |
|
9 | Based roughly on Python difflib | |
10 | */ |
|
10 | */ | |
11 |
|
11 | |||
12 | #define PY_SSIZE_T_CLEAN |
|
12 | #define PY_SSIZE_T_CLEAN | |
13 | #include <Python.h> |
|
13 | #include <Python.h> | |
14 | #include <stdlib.h> |
|
14 | #include <stdlib.h> | |
15 | #include <string.h> |
|
15 | #include <string.h> | |
16 | #include <limits.h> |
|
16 | #include <limits.h> | |
17 |
|
17 | |||
18 | #include "util.h" |
|
18 | #include "util.h" | |
19 |
|
19 | |||
20 | struct line { |
|
20 | struct line { | |
21 | int hash, n, e; |
|
21 | int hash, n, e; | |
22 | Py_ssize_t len; |
|
22 | Py_ssize_t len; | |
23 | const char *l; |
|
23 | const char *l; | |
24 | }; |
|
24 | }; | |
25 |
|
25 | |||
26 | struct pos { |
|
26 | struct pos { | |
27 | int pos, len; |
|
27 | int pos, len; | |
28 | }; |
|
28 | }; | |
29 |
|
29 | |||
30 | struct hunk; |
|
30 | struct hunk; | |
31 | struct hunk { |
|
31 | struct hunk { | |
32 | int a1, a2, b1, b2; |
|
32 | int a1, a2, b1, b2; | |
33 | struct hunk *next; |
|
33 | struct hunk *next; | |
34 | }; |
|
34 | }; | |
35 |
|
35 | |||
36 | static int splitlines(const char *a, Py_ssize_t len, struct line **lr) |
|
36 | static int splitlines(const char *a, Py_ssize_t len, struct line **lr) | |
37 | { |
|
37 | { | |
38 | unsigned hash; |
|
38 | unsigned hash; | |
39 | int i; |
|
39 | int i; | |
40 | const char *p, *b = a; |
|
40 | const char *p, *b = a; | |
41 | const char * const plast = a + len - 1; |
|
41 | const char * const plast = a + len - 1; | |
42 | struct line *l; |
|
42 | struct line *l; | |
43 |
|
43 | |||
44 | /* count the lines */ |
|
44 | /* count the lines */ | |
45 | i = 1; /* extra line for sentinel */ |
|
45 | i = 1; /* extra line for sentinel */ | |
46 | for (p = a; p < a + len; p++) |
|
46 | for (p = a; p < a + len; p++) | |
47 | if (*p == '\n' || p == plast) |
|
47 | if (*p == '\n' || p == plast) | |
48 | i++; |
|
48 | i++; | |
49 |
|
49 | |||
50 | *lr = l = (struct line *)malloc(sizeof(struct line) * i); |
|
50 | *lr = l = (struct line *)malloc(sizeof(struct line) * i); | |
51 | if (!l) |
|
51 | if (!l) | |
52 | return -1; |
|
52 | return -1; | |
53 |
|
53 | |||
54 | /* build the line array and calculate hashes */ |
|
54 | /* build the line array and calculate hashes */ | |
55 | hash = 0; |
|
55 | hash = 0; | |
56 | for (p = a; p < a + len; p++) { |
|
56 | for (p = a; p < a + len; p++) { | |
57 | /* Leonid Yuriev's hash */ |
|
57 | /* Leonid Yuriev's hash */ | |
58 | hash = (hash * 1664525) + (unsigned char)*p + 1013904223; |
|
58 | hash = (hash * 1664525) + (unsigned char)*p + 1013904223; | |
59 |
|
59 | |||
60 | if (*p == '\n' || p == plast) { |
|
60 | if (*p == '\n' || p == plast) { | |
61 | l->hash = hash; |
|
61 | l->hash = hash; | |
62 | hash = 0; |
|
62 | hash = 0; | |
63 | l->len = p - b + 1; |
|
63 | l->len = p - b + 1; | |
64 | l->l = b; |
|
64 | l->l = b; | |
65 | l->n = INT_MAX; |
|
65 | l->n = INT_MAX; | |
66 | l++; |
|
66 | l++; | |
67 | b = p + 1; |
|
67 | b = p + 1; | |
68 | } |
|
68 | } | |
69 | } |
|
69 | } | |
70 |
|
70 | |||
71 | /* set up a sentinel */ |
|
71 | /* set up a sentinel */ | |
72 | l->hash = 0; |
|
72 | l->hash = 0; | |
73 | l->len = 0; |
|
73 | l->len = 0; | |
74 | l->l = a + len; |
|
74 | l->l = a + len; | |
75 | return i - 1; |
|
75 | return i - 1; | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | static inline int cmp(struct line *a, struct line *b) |
|
78 | static inline int cmp(struct line *a, struct line *b) | |
79 | { |
|
79 | { | |
80 | return a->hash != b->hash || a->len != b->len || memcmp(a->l, b->l, a->len); |
|
80 | return a->hash != b->hash || a->len != b->len || memcmp(a->l, b->l, a->len); | |
81 | } |
|
81 | } | |
82 |
|
82 | |||
83 | static int equatelines(struct line *a, int an, struct line *b, int bn) |
|
83 | static int equatelines(struct line *a, int an, struct line *b, int bn) | |
84 | { |
|
84 | { | |
85 | int i, j, buckets = 1, t, scale; |
|
85 | int i, j, buckets = 1, t, scale; | |
86 | struct pos *h = NULL; |
|
86 | struct pos *h = NULL; | |
87 |
|
87 | |||
88 | /* build a hash table of the next highest power of 2 */ |
|
88 | /* build a hash table of the next highest power of 2 */ | |
89 | while (buckets < bn + 1) |
|
89 | while (buckets < bn + 1) | |
90 | buckets *= 2; |
|
90 | buckets *= 2; | |
91 |
|
91 | |||
92 | /* try to allocate a large hash table to avoid collisions */ |
|
92 | /* try to allocate a large hash table to avoid collisions */ | |
93 | for (scale = 4; scale; scale /= 2) { |
|
93 | for (scale = 4; scale; scale /= 2) { | |
94 | h = (struct pos *)malloc(scale * buckets * sizeof(struct pos)); |
|
94 | h = (struct pos *)malloc(scale * buckets * sizeof(struct pos)); | |
95 | if (h) |
|
95 | if (h) | |
96 | break; |
|
96 | break; | |
97 | } |
|
97 | } | |
98 |
|
98 | |||
99 | if (!h) |
|
99 | if (!h) | |
100 | return 0; |
|
100 | return 0; | |
101 |
|
101 | |||
102 | buckets = buckets * scale - 1; |
|
102 | buckets = buckets * scale - 1; | |
103 |
|
103 | |||
104 | /* clear the hash table */ |
|
104 | /* clear the hash table */ | |
105 | for (i = 0; i <= buckets; i++) { |
|
105 | for (i = 0; i <= buckets; i++) { | |
106 | h[i].pos = INT_MAX; |
|
106 | h[i].pos = INT_MAX; | |
107 | h[i].len = 0; |
|
107 | h[i].len = 0; | |
108 | } |
|
108 | } | |
109 |
|
109 | |||
110 | /* add lines to the hash table chains */ |
|
110 | /* add lines to the hash table chains */ | |
111 | for (i = bn - 1; i >= 0; i--) { |
|
111 | for (i = bn - 1; i >= 0; i--) { | |
112 | /* find the equivalence class */ |
|
112 | /* find the equivalence class */ | |
113 | for (j = b[i].hash & buckets; h[j].pos != INT_MAX; |
|
113 | for (j = b[i].hash & buckets; h[j].pos != INT_MAX; | |
114 | j = (j + 1) & buckets) |
|
114 | j = (j + 1) & buckets) | |
115 | if (!cmp(b + i, b + h[j].pos)) |
|
115 | if (!cmp(b + i, b + h[j].pos)) | |
116 | break; |
|
116 | break; | |
117 |
|
117 | |||
118 | /* add to the head of the equivalence class */ |
|
118 | /* add to the head of the equivalence class */ | |
119 | b[i].n = h[j].pos; |
|
119 | b[i].n = h[j].pos; | |
120 | b[i].e = j; |
|
120 | b[i].e = j; | |
121 | h[j].pos = i; |
|
121 | h[j].pos = i; | |
122 | h[j].len++; /* keep track of popularity */ |
|
122 | h[j].len++; /* keep track of popularity */ | |
123 | } |
|
123 | } | |
124 |
|
124 | |||
125 | /* compute popularity threshold */ |
|
125 | /* compute popularity threshold */ | |
126 | t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1); |
|
126 | t = (bn >= 31000) ? bn / 1000 : 1000000 / (bn + 1); | |
127 |
|
127 | |||
128 | /* match items in a to their equivalence class in b */ |
|
128 | /* match items in a to their equivalence class in b */ | |
129 | for (i = 0; i < an; i++) { |
|
129 | for (i = 0; i < an; i++) { | |
130 | /* find the equivalence class */ |
|
130 | /* find the equivalence class */ | |
131 | for (j = a[i].hash & buckets; h[j].pos != INT_MAX; |
|
131 | for (j = a[i].hash & buckets; h[j].pos != INT_MAX; | |
132 | j = (j + 1) & buckets) |
|
132 | j = (j + 1) & buckets) | |
133 | if (!cmp(a + i, b + h[j].pos)) |
|
133 | if (!cmp(a + i, b + h[j].pos)) | |
134 | break; |
|
134 | break; | |
135 |
|
135 | |||
136 | a[i].e = j; /* use equivalence class for quick compare */ |
|
136 | a[i].e = j; /* use equivalence class for quick compare */ | |
137 | if (h[j].len <= t) |
|
137 | if (h[j].len <= t) | |
138 | a[i].n = h[j].pos; /* point to head of match list */ |
|
138 | a[i].n = h[j].pos; /* point to head of match list */ | |
139 | else |
|
139 | else | |
140 | a[i].n = INT_MAX; /* too popular */ |
|
140 | a[i].n = INT_MAX; /* too popular */ | |
141 | } |
|
141 | } | |
142 |
|
142 | |||
143 | /* discard hash tables */ |
|
143 | /* discard hash tables */ | |
144 | free(h); |
|
144 | free(h); | |
145 | return 1; |
|
145 | return 1; | |
146 | } |
|
146 | } | |
147 |
|
147 | |||
148 | static int longest_match(struct line *a, struct line *b, struct pos *pos, |
|
148 | static int longest_match(struct line *a, struct line *b, struct pos *pos, | |
149 | int a1, int a2, int b1, int b2, int *omi, int *omj) |
|
149 | int a1, int a2, int b1, int b2, int *omi, int *omj) | |
150 | { |
|
150 | { | |
151 | int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k; |
|
151 | int mi = a1, mj = b1, mk = 0, mb = 0, i, j, k; | |
152 |
|
152 | |||
153 | for (i = a1; i < a2; i++) { |
|
153 | for (i = a1; i < a2; i++) { | |
154 | /* skip things before the current block */ |
|
154 | /* skip things before the current block */ | |
155 | for (j = a[i].n; j < b1; j = b[j].n) |
|
155 | for (j = a[i].n; j < b1; j = b[j].n) | |
156 | ; |
|
156 | ; | |
157 |
|
157 | |||
158 | /* loop through all lines match a[i] in b */ |
|
158 | /* loop through all lines match a[i] in b */ | |
159 | for (; j < b2; j = b[j].n) { |
|
159 | for (; j < b2; j = b[j].n) { | |
160 | /* does this extend an earlier match? */ |
|
160 | /* does this extend an earlier match? */ | |
161 | if (i > a1 && j > b1 && pos[j - 1].pos == i - 1) |
|
161 | if (i > a1 && j > b1 && pos[j - 1].pos == i - 1) | |
162 | k = pos[j - 1].len + 1; |
|
162 | k = pos[j - 1].len + 1; | |
163 | else |
|
163 | else | |
164 | k = 1; |
|
164 | k = 1; | |
165 | pos[j].pos = i; |
|
165 | pos[j].pos = i; | |
166 | pos[j].len = k; |
|
166 | pos[j].len = k; | |
167 |
|
167 | |||
168 | /* best match so far? */ |
|
168 | /* best match so far? */ | |
169 | if (k > mk) { |
|
169 | if (k > mk) { | |
170 | mi = i; |
|
170 | mi = i; | |
171 | mj = j; |
|
171 | mj = j; | |
172 | mk = k; |
|
172 | mk = k; | |
173 | } |
|
173 | } | |
174 | } |
|
174 | } | |
175 | } |
|
175 | } | |
176 |
|
176 | |||
177 | if (mk) { |
|
177 | if (mk) { | |
178 | mi = mi - mk + 1; |
|
178 | mi = mi - mk + 1; | |
179 | mj = mj - mk + 1; |
|
179 | mj = mj - mk + 1; | |
180 | } |
|
180 | } | |
181 |
|
181 | |||
182 | /* expand match to include neighboring popular lines */ |
|
182 | /* expand match to include neighboring popular lines */ | |
183 | while (mi - mb > a1 && mj - mb > b1 && |
|
183 | while (mi - mb > a1 && mj - mb > b1 && | |
184 | a[mi - mb - 1].e == b[mj - mb - 1].e) |
|
184 | a[mi - mb - 1].e == b[mj - mb - 1].e) | |
185 | mb++; |
|
185 | mb++; | |
186 | while (mi + mk < a2 && mj + mk < b2 && |
|
186 | while (mi + mk < a2 && mj + mk < b2 && | |
187 | a[mi + mk].e == b[mj + mk].e) |
|
187 | a[mi + mk].e == b[mj + mk].e) | |
188 | mk++; |
|
188 | mk++; | |
189 |
|
189 | |||
190 | *omi = mi - mb; |
|
190 | *omi = mi - mb; | |
191 | *omj = mj - mb; |
|
191 | *omj = mj - mb; | |
192 |
|
192 | |||
193 | return mk + mb; |
|
193 | return mk + mb; | |
194 | } |
|
194 | } | |
195 |
|
195 | |||
196 | static struct hunk *recurse(struct line *a, struct line *b, struct pos *pos, |
|
196 | static struct hunk *recurse(struct line *a, struct line *b, struct pos *pos, | |
197 | int a1, int a2, int b1, int b2, struct hunk *l) |
|
197 | int a1, int a2, int b1, int b2, struct hunk *l) | |
198 | { |
|
198 | { | |
199 | int i, j, k; |
|
199 | int i, j, k; | |
200 |
|
200 | |||
201 | while (1) { |
|
201 | while (1) { | |
202 | /* find the longest match in this chunk */ |
|
202 | /* find the longest match in this chunk */ | |
203 | k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j); |
|
203 | k = longest_match(a, b, pos, a1, a2, b1, b2, &i, &j); | |
204 | if (!k) |
|
204 | if (!k) | |
205 | return l; |
|
205 | return l; | |
206 |
|
206 | |||
207 | /* and recurse on the remaining chunks on either side */ |
|
207 | /* and recurse on the remaining chunks on either side */ | |
208 | l = recurse(a, b, pos, a1, i, b1, j, l); |
|
208 | l = recurse(a, b, pos, a1, i, b1, j, l); | |
209 | if (!l) |
|
209 | if (!l) | |
210 | return NULL; |
|
210 | return NULL; | |
211 |
|
211 | |||
212 | l->next = (struct hunk *)malloc(sizeof(struct hunk)); |
|
212 | l->next = (struct hunk *)malloc(sizeof(struct hunk)); | |
213 | if (!l->next) |
|
213 | if (!l->next) | |
214 | return NULL; |
|
214 | return NULL; | |
215 |
|
215 | |||
216 | l = l->next; |
|
216 | l = l->next; | |
217 | l->a1 = i; |
|
217 | l->a1 = i; | |
218 | l->a2 = i + k; |
|
218 | l->a2 = i + k; | |
219 | l->b1 = j; |
|
219 | l->b1 = j; | |
220 | l->b2 = j + k; |
|
220 | l->b2 = j + k; | |
221 | l->next = NULL; |
|
221 | l->next = NULL; | |
222 |
|
222 | |||
223 | /* tail-recursion didn't happen, so do equivalent iteration */ |
|
223 | /* tail-recursion didn't happen, so do equivalent iteration */ | |
224 | a1 = i + k; |
|
224 | a1 = i + k; | |
225 | b1 = j + k; |
|
225 | b1 = j + k; | |
226 | } |
|
226 | } | |
227 | } |
|
227 | } | |
228 |
|
228 | |||
229 | static int diff(struct line *a, int an, struct line *b, int bn, |
|
229 | static int diff(struct line *a, int an, struct line *b, int bn, | |
230 | struct hunk *base) |
|
230 | struct hunk *base) | |
231 | { |
|
231 | { | |
232 | struct hunk *curr; |
|
232 | struct hunk *curr; | |
233 | struct pos *pos; |
|
233 | struct pos *pos; | |
234 | int t, count = 0; |
|
234 | int t, count = 0; | |
235 |
|
235 | |||
236 | /* allocate and fill arrays */ |
|
236 | /* allocate and fill arrays */ | |
237 | t = equatelines(a, an, b, bn); |
|
237 | t = equatelines(a, an, b, bn); | |
238 | pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos)); |
|
238 | pos = (struct pos *)calloc(bn ? bn : 1, sizeof(struct pos)); | |
239 |
|
239 | |||
240 | if (pos && t) { |
|
240 | if (pos && t) { | |
241 | /* generate the matching block list */ |
|
241 | /* generate the matching block list */ | |
242 |
|
242 | |||
243 | curr = recurse(a, b, pos, 0, an, 0, bn, base); |
|
243 | curr = recurse(a, b, pos, 0, an, 0, bn, base); | |
244 | if (!curr) |
|
244 | if (!curr) | |
245 | return -1; |
|
245 | return -1; | |
246 |
|
246 | |||
247 | /* sentinel end hunk */ |
|
247 | /* sentinel end hunk */ | |
248 | curr->next = (struct hunk *)malloc(sizeof(struct hunk)); |
|
248 | curr->next = (struct hunk *)malloc(sizeof(struct hunk)); | |
249 | if (!curr->next) |
|
249 | if (!curr->next) | |
250 | return -1; |
|
250 | return -1; | |
251 | curr = curr->next; |
|
251 | curr = curr->next; | |
252 | curr->a1 = curr->a2 = an; |
|
252 | curr->a1 = curr->a2 = an; | |
253 | curr->b1 = curr->b2 = bn; |
|
253 | curr->b1 = curr->b2 = bn; | |
254 | curr->next = NULL; |
|
254 | curr->next = NULL; | |
255 | } |
|
255 | } | |
256 |
|
256 | |||
257 | free(pos); |
|
257 | free(pos); | |
258 |
|
258 | |||
259 | /* normalize the hunk list, try to push each hunk towards the end */ |
|
259 | /* normalize the hunk list, try to push each hunk towards the end */ | |
260 | for (curr = base->next; curr; curr = curr->next) { |
|
260 | for (curr = base->next; curr; curr = curr->next) { | |
261 | struct hunk *next = curr->next; |
|
261 | struct hunk *next = curr->next; | |
262 | int shift = 0; |
|
262 | int shift = 0; | |
263 |
|
263 | |||
264 | if (!next) |
|
264 | if (!next) | |
265 | break; |
|
265 | break; | |
266 |
|
266 | |||
267 | if (curr->a2 == next->a1) |
|
267 | if (curr->a2 == next->a1) | |
268 | while (curr->a2 + shift < an && curr->b2 + shift < bn |
|
268 | while (curr->a2 + shift < an && curr->b2 + shift < bn | |
269 | && !cmp(a + curr->a2 + shift, |
|
269 | && !cmp(a + curr->a2 + shift, | |
270 | b + curr->b2 + shift)) |
|
270 | b + curr->b2 + shift)) | |
271 | shift++; |
|
271 | shift++; | |
272 | else if (curr->b2 == next->b1) |
|
272 | else if (curr->b2 == next->b1) | |
273 | while (curr->b2 + shift < bn && curr->a2 + shift < an |
|
273 | while (curr->b2 + shift < bn && curr->a2 + shift < an | |
274 | && !cmp(b + curr->b2 + shift, |
|
274 | && !cmp(b + curr->b2 + shift, | |
275 | a + curr->a2 + shift)) |
|
275 | a + curr->a2 + shift)) | |
276 | shift++; |
|
276 | shift++; | |
277 | if (!shift) |
|
277 | if (!shift) | |
278 | continue; |
|
278 | continue; | |
279 | curr->b2 += shift; |
|
279 | curr->b2 += shift; | |
280 | next->b1 += shift; |
|
280 | next->b1 += shift; | |
281 | curr->a2 += shift; |
|
281 | curr->a2 += shift; | |
282 | next->a1 += shift; |
|
282 | next->a1 += shift; | |
283 | } |
|
283 | } | |
284 |
|
284 | |||
285 | for (curr = base->next; curr; curr = curr->next) |
|
285 | for (curr = base->next; curr; curr = curr->next) | |
286 | count++; |
|
286 | count++; | |
287 | return count; |
|
287 | return count; | |
288 | } |
|
288 | } | |
289 |
|
289 | |||
290 | static void freehunks(struct hunk *l) |
|
290 | static void freehunks(struct hunk *l) | |
291 | { |
|
291 | { | |
292 | struct hunk *n; |
|
292 | struct hunk *n; | |
293 | for (; l; l = n) { |
|
293 | for (; l; l = n) { | |
294 | n = l->next; |
|
294 | n = l->next; | |
295 | free(l); |
|
295 | free(l); | |
296 | } |
|
296 | } | |
297 | } |
|
297 | } | |
298 |
|
298 | |||
299 | static PyObject *blocks(PyObject *self, PyObject *args) |
|
299 | static PyObject *blocks(PyObject *self, PyObject *args) | |
300 | { |
|
300 | { | |
301 | PyObject *sa, *sb, *rl = NULL, *m; |
|
301 | PyObject *sa, *sb, *rl = NULL, *m; | |
302 | struct line *a, *b; |
|
302 | struct line *a, *b; | |
303 | struct hunk l, *h; |
|
303 | struct hunk l, *h; | |
304 | int an, bn, count, pos = 0; |
|
304 | int an, bn, count, pos = 0; | |
305 |
|
305 | |||
|
306 | l.next = NULL; | |||
|
307 | ||||
306 | if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) |
|
308 | if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) | |
307 | return NULL; |
|
309 | return NULL; | |
308 |
|
310 | |||
309 | an = splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a); |
|
311 | an = splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a); | |
310 | bn = splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b); |
|
312 | bn = splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b); | |
311 |
|
313 | |||
312 | if (!a || !b) |
|
314 | if (!a || !b) | |
313 | goto nomem; |
|
315 | goto nomem; | |
314 |
|
316 | |||
315 | l.next = NULL; |
|
|||
316 | count = diff(a, an, b, bn, &l); |
|
317 | count = diff(a, an, b, bn, &l); | |
317 | if (count < 0) |
|
318 | if (count < 0) | |
318 | goto nomem; |
|
319 | goto nomem; | |
319 |
|
320 | |||
320 | rl = PyList_New(count); |
|
321 | rl = PyList_New(count); | |
321 | if (!rl) |
|
322 | if (!rl) | |
322 | goto nomem; |
|
323 | goto nomem; | |
323 |
|
324 | |||
324 | for (h = l.next; h; h = h->next) { |
|
325 | for (h = l.next; h; h = h->next) { | |
325 | m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); |
|
326 | m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); | |
326 | PyList_SetItem(rl, pos, m); |
|
327 | PyList_SetItem(rl, pos, m); | |
327 | pos++; |
|
328 | pos++; | |
328 | } |
|
329 | } | |
329 |
|
330 | |||
330 | nomem: |
|
331 | nomem: | |
331 | free(a); |
|
332 | free(a); | |
332 | free(b); |
|
333 | free(b); | |
333 | freehunks(l.next); |
|
334 | freehunks(l.next); | |
334 | return rl ? rl : PyErr_NoMemory(); |
|
335 | return rl ? rl : PyErr_NoMemory(); | |
335 | } |
|
336 | } | |
336 |
|
337 | |||
337 | static PyObject *bdiff(PyObject *self, PyObject *args) |
|
338 | static PyObject *bdiff(PyObject *self, PyObject *args) | |
338 | { |
|
339 | { | |
339 | char *sa, *sb, *rb; |
|
340 | char *sa, *sb, *rb; | |
340 | PyObject *result = NULL; |
|
341 | PyObject *result = NULL; | |
341 | struct line *al, *bl; |
|
342 | struct line *al, *bl; | |
342 | struct hunk l, *h; |
|
343 | struct hunk l, *h; | |
343 | int an, bn, count; |
|
344 | int an, bn, count; | |
344 | Py_ssize_t len = 0, la, lb; |
|
345 | Py_ssize_t len = 0, la, lb; | |
345 | PyThreadState *_save; |
|
346 | PyThreadState *_save; | |
346 |
|
347 | |||
|
348 | l.next = NULL; | |||
|
349 | ||||
347 | if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) |
|
350 | if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) | |
348 | return NULL; |
|
351 | return NULL; | |
349 |
|
352 | |||
350 | if (la > UINT_MAX || lb > UINT_MAX) { |
|
353 | if (la > UINT_MAX || lb > UINT_MAX) { | |
351 | PyErr_SetString(PyExc_ValueError, "bdiff inputs too large"); |
|
354 | PyErr_SetString(PyExc_ValueError, "bdiff inputs too large"); | |
352 | return NULL; |
|
355 | return NULL; | |
353 | } |
|
356 | } | |
354 |
|
357 | |||
355 | _save = PyEval_SaveThread(); |
|
358 | _save = PyEval_SaveThread(); | |
356 | an = splitlines(sa, la, &al); |
|
359 | an = splitlines(sa, la, &al); | |
357 | bn = splitlines(sb, lb, &bl); |
|
360 | bn = splitlines(sb, lb, &bl); | |
358 | if (!al || !bl) |
|
361 | if (!al || !bl) | |
359 | goto nomem; |
|
362 | goto nomem; | |
360 |
|
363 | |||
361 | l.next = NULL; |
|
|||
362 | count = diff(al, an, bl, bn, &l); |
|
364 | count = diff(al, an, bl, bn, &l); | |
363 | if (count < 0) |
|
365 | if (count < 0) | |
364 | goto nomem; |
|
366 | goto nomem; | |
365 |
|
367 | |||
366 | /* calculate length of output */ |
|
368 | /* calculate length of output */ | |
367 | la = lb = 0; |
|
369 | la = lb = 0; | |
368 | for (h = l.next; h; h = h->next) { |
|
370 | for (h = l.next; h; h = h->next) { | |
369 | if (h->a1 != la || h->b1 != lb) |
|
371 | if (h->a1 != la || h->b1 != lb) | |
370 | len += 12 + bl[h->b1].l - bl[lb].l; |
|
372 | len += 12 + bl[h->b1].l - bl[lb].l; | |
371 | la = h->a2; |
|
373 | la = h->a2; | |
372 | lb = h->b2; |
|
374 | lb = h->b2; | |
373 | } |
|
375 | } | |
374 | PyEval_RestoreThread(_save); |
|
376 | PyEval_RestoreThread(_save); | |
375 | _save = NULL; |
|
377 | _save = NULL; | |
376 |
|
378 | |||
377 | result = PyBytes_FromStringAndSize(NULL, len); |
|
379 | result = PyBytes_FromStringAndSize(NULL, len); | |
378 |
|
380 | |||
379 | if (!result) |
|
381 | if (!result) | |
380 | goto nomem; |
|
382 | goto nomem; | |
381 |
|
383 | |||
382 | /* build binary patch */ |
|
384 | /* build binary patch */ | |
383 | rb = PyBytes_AsString(result); |
|
385 | rb = PyBytes_AsString(result); | |
384 | la = lb = 0; |
|
386 | la = lb = 0; | |
385 |
|
387 | |||
386 | for (h = l.next; h; h = h->next) { |
|
388 | for (h = l.next; h; h = h->next) { | |
387 | if (h->a1 != la || h->b1 != lb) { |
|
389 | if (h->a1 != la || h->b1 != lb) { | |
388 | len = bl[h->b1].l - bl[lb].l; |
|
390 | len = bl[h->b1].l - bl[lb].l; | |
389 | putbe32((uint32_t)(al[la].l - al->l), rb); |
|
391 | putbe32((uint32_t)(al[la].l - al->l), rb); | |
390 | putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4); |
|
392 | putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4); | |
391 | putbe32((uint32_t)len, rb + 8); |
|
393 | putbe32((uint32_t)len, rb + 8); | |
392 | memcpy(rb + 12, bl[lb].l, len); |
|
394 | memcpy(rb + 12, bl[lb].l, len); | |
393 | rb += 12 + len; |
|
395 | rb += 12 + len; | |
394 | } |
|
396 | } | |
395 | la = h->a2; |
|
397 | la = h->a2; | |
396 | lb = h->b2; |
|
398 | lb = h->b2; | |
397 | } |
|
399 | } | |
398 |
|
400 | |||
399 | nomem: |
|
401 | nomem: | |
400 | if (_save) |
|
402 | if (_save) | |
401 | PyEval_RestoreThread(_save); |
|
403 | PyEval_RestoreThread(_save); | |
402 | free(al); |
|
404 | free(al); | |
403 | free(bl); |
|
405 | free(bl); | |
404 | freehunks(l.next); |
|
406 | freehunks(l.next); | |
405 | return result ? result : PyErr_NoMemory(); |
|
407 | return result ? result : PyErr_NoMemory(); | |
406 | } |
|
408 | } | |
407 |
|
409 | |||
408 | /* |
|
410 | /* | |
409 | * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise, |
|
411 | * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise, | |
410 | * reduce whitespace sequences to a single space and trim remaining whitespace |
|
412 | * reduce whitespace sequences to a single space and trim remaining whitespace | |
411 | * from end of lines. |
|
413 | * from end of lines. | |
412 | */ |
|
414 | */ | |
413 | static PyObject *fixws(PyObject *self, PyObject *args) |
|
415 | static PyObject *fixws(PyObject *self, PyObject *args) | |
414 | { |
|
416 | { | |
415 | PyObject *s, *result = NULL; |
|
417 | PyObject *s, *result = NULL; | |
416 | char allws, c; |
|
418 | char allws, c; | |
417 | const char *r; |
|
419 | const char *r; | |
418 | Py_ssize_t i, rlen, wlen = 0; |
|
420 | Py_ssize_t i, rlen, wlen = 0; | |
419 | char *w; |
|
421 | char *w; | |
420 |
|
422 | |||
421 | if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws)) |
|
423 | if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws)) | |
422 | return NULL; |
|
424 | return NULL; | |
423 | r = PyBytes_AsString(s); |
|
425 | r = PyBytes_AsString(s); | |
424 | rlen = PyBytes_Size(s); |
|
426 | rlen = PyBytes_Size(s); | |
425 |
|
427 | |||
426 | w = (char *)malloc(rlen ? rlen : 1); |
|
428 | w = (char *)malloc(rlen ? rlen : 1); | |
427 | if (!w) |
|
429 | if (!w) | |
428 | goto nomem; |
|
430 | goto nomem; | |
429 |
|
431 | |||
430 | for (i = 0; i != rlen; i++) { |
|
432 | for (i = 0; i != rlen; i++) { | |
431 | c = r[i]; |
|
433 | c = r[i]; | |
432 | if (c == ' ' || c == '\t' || c == '\r') { |
|
434 | if (c == ' ' || c == '\t' || c == '\r') { | |
433 | if (!allws && (wlen == 0 || w[wlen - 1] != ' ')) |
|
435 | if (!allws && (wlen == 0 || w[wlen - 1] != ' ')) | |
434 | w[wlen++] = ' '; |
|
436 | w[wlen++] = ' '; | |
435 | } else if (c == '\n' && !allws |
|
437 | } else if (c == '\n' && !allws | |
436 | && wlen > 0 && w[wlen - 1] == ' ') { |
|
438 | && wlen > 0 && w[wlen - 1] == ' ') { | |
437 | w[wlen - 1] = '\n'; |
|
439 | w[wlen - 1] = '\n'; | |
438 | } else { |
|
440 | } else { | |
439 | w[wlen++] = c; |
|
441 | w[wlen++] = c; | |
440 | } |
|
442 | } | |
441 | } |
|
443 | } | |
442 |
|
444 | |||
443 | result = PyBytes_FromStringAndSize(w, wlen); |
|
445 | result = PyBytes_FromStringAndSize(w, wlen); | |
444 |
|
446 | |||
445 | nomem: |
|
447 | nomem: | |
446 | free(w); |
|
448 | free(w); | |
447 | return result ? result : PyErr_NoMemory(); |
|
449 | return result ? result : PyErr_NoMemory(); | |
448 | } |
|
450 | } | |
449 |
|
451 | |||
450 |
|
452 | |||
451 | static char mdiff_doc[] = "Efficient binary diff."; |
|
453 | static char mdiff_doc[] = "Efficient binary diff."; | |
452 |
|
454 | |||
453 | static PyMethodDef methods[] = { |
|
455 | static PyMethodDef methods[] = { | |
454 | {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"}, |
|
456 | {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"}, | |
455 | {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"}, |
|
457 | {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"}, | |
456 | {"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"}, |
|
458 | {"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"}, | |
457 | {NULL, NULL} |
|
459 | {NULL, NULL} | |
458 | }; |
|
460 | }; | |
459 |
|
461 | |||
460 | #ifdef IS_PY3K |
|
462 | #ifdef IS_PY3K | |
461 | static struct PyModuleDef bdiff_module = { |
|
463 | static struct PyModuleDef bdiff_module = { | |
462 | PyModuleDef_HEAD_INIT, |
|
464 | PyModuleDef_HEAD_INIT, | |
463 | "bdiff", |
|
465 | "bdiff", | |
464 | mdiff_doc, |
|
466 | mdiff_doc, | |
465 | -1, |
|
467 | -1, | |
466 | methods |
|
468 | methods | |
467 | }; |
|
469 | }; | |
468 |
|
470 | |||
469 | PyMODINIT_FUNC PyInit_bdiff(void) |
|
471 | PyMODINIT_FUNC PyInit_bdiff(void) | |
470 | { |
|
472 | { | |
471 | return PyModule_Create(&bdiff_module); |
|
473 | return PyModule_Create(&bdiff_module); | |
472 | } |
|
474 | } | |
473 | #else |
|
475 | #else | |
474 | PyMODINIT_FUNC initbdiff(void) |
|
476 | PyMODINIT_FUNC initbdiff(void) | |
475 | { |
|
477 | { | |
476 | Py_InitModule3("bdiff", methods, mdiff_doc); |
|
478 | Py_InitModule3("bdiff", methods, mdiff_doc); | |
477 | } |
|
479 | } | |
478 | #endif |
|
480 | #endif | |
479 |
|
481 |
General Comments 0
You need to be logged in to leave comments.
Login now