##// END OF EJS Templates
bdiff: deal better with duplicate lines...
Matt Mackall -
r29013:9a8363d2 stable
parent child Browse files
Show More
@@ -1,474 +1,474 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 = -1;
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 = 0; i < bn; 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 != -1;
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 != -1;
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 = -1; /* 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 all lines in b after the current block */
155 for (j = a[i].n; j < b1; j = b[j].n)
155 for (j = a[i].n; j >= b2; 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 >= b1; 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 || (k == mk && i <= mi)) {
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
262
263 if (!next)
263 if (!next)
264 break;
264 break;
265
265
266 if (curr->a2 == next->a1 || curr->b2 == next->b1)
266 if (curr->a2 == next->a1 || curr->b2 == next->b1)
267 while (curr->a2 < an && curr->b2 < bn
267 while (curr->a2 < an && curr->b2 < bn
268 && next->a1 < next->a2
268 && next->a1 < next->a2
269 && next->b1 < next->b2
269 && next->b1 < next->b2
270 && !cmp(a + curr->a2, b + curr->b2)) {
270 && !cmp(a + curr->a2, b + curr->b2)) {
271 curr->a2++;
271 curr->a2++;
272 next->a1++;
272 next->a1++;
273 curr->b2++;
273 curr->b2++;
274 next->b1++;
274 next->b1++;
275 }
275 }
276 }
276 }
277
277
278 for (curr = base->next; curr; curr = curr->next)
278 for (curr = base->next; curr; curr = curr->next)
279 count++;
279 count++;
280 return count;
280 return count;
281 }
281 }
282
282
283 static void freehunks(struct hunk *l)
283 static void freehunks(struct hunk *l)
284 {
284 {
285 struct hunk *n;
285 struct hunk *n;
286 for (; l; l = n) {
286 for (; l; l = n) {
287 n = l->next;
287 n = l->next;
288 free(l);
288 free(l);
289 }
289 }
290 }
290 }
291
291
292 static PyObject *blocks(PyObject *self, PyObject *args)
292 static PyObject *blocks(PyObject *self, PyObject *args)
293 {
293 {
294 PyObject *sa, *sb, *rl = NULL, *m;
294 PyObject *sa, *sb, *rl = NULL, *m;
295 struct line *a, *b;
295 struct line *a, *b;
296 struct hunk l, *h;
296 struct hunk l, *h;
297 int an, bn, count, pos = 0;
297 int an, bn, count, pos = 0;
298
298
299 l.next = NULL;
299 l.next = NULL;
300
300
301 if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
301 if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb))
302 return NULL;
302 return NULL;
303
303
304 an = splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a);
304 an = splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a);
305 bn = splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b);
305 bn = splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b);
306
306
307 if (!a || !b)
307 if (!a || !b)
308 goto nomem;
308 goto nomem;
309
309
310 count = diff(a, an, b, bn, &l);
310 count = diff(a, an, b, bn, &l);
311 if (count < 0)
311 if (count < 0)
312 goto nomem;
312 goto nomem;
313
313
314 rl = PyList_New(count);
314 rl = PyList_New(count);
315 if (!rl)
315 if (!rl)
316 goto nomem;
316 goto nomem;
317
317
318 for (h = l.next; h; h = h->next) {
318 for (h = l.next; h; h = h->next) {
319 m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2);
319 m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2);
320 PyList_SetItem(rl, pos, m);
320 PyList_SetItem(rl, pos, m);
321 pos++;
321 pos++;
322 }
322 }
323
323
324 nomem:
324 nomem:
325 free(a);
325 free(a);
326 free(b);
326 free(b);
327 freehunks(l.next);
327 freehunks(l.next);
328 return rl ? rl : PyErr_NoMemory();
328 return rl ? rl : PyErr_NoMemory();
329 }
329 }
330
330
331 static PyObject *bdiff(PyObject *self, PyObject *args)
331 static PyObject *bdiff(PyObject *self, PyObject *args)
332 {
332 {
333 char *sa, *sb, *rb;
333 char *sa, *sb, *rb;
334 PyObject *result = NULL;
334 PyObject *result = NULL;
335 struct line *al, *bl;
335 struct line *al, *bl;
336 struct hunk l, *h;
336 struct hunk l, *h;
337 int an, bn, count;
337 int an, bn, count;
338 Py_ssize_t len = 0, la, lb;
338 Py_ssize_t len = 0, la, lb;
339 PyThreadState *_save;
339 PyThreadState *_save;
340
340
341 l.next = NULL;
341 l.next = NULL;
342
342
343 if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
343 if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb))
344 return NULL;
344 return NULL;
345
345
346 if (la > UINT_MAX || lb > UINT_MAX) {
346 if (la > UINT_MAX || lb > UINT_MAX) {
347 PyErr_SetString(PyExc_ValueError, "bdiff inputs too large");
347 PyErr_SetString(PyExc_ValueError, "bdiff inputs too large");
348 return NULL;
348 return NULL;
349 }
349 }
350
350
351 _save = PyEval_SaveThread();
351 _save = PyEval_SaveThread();
352 an = splitlines(sa, la, &al);
352 an = splitlines(sa, la, &al);
353 bn = splitlines(sb, lb, &bl);
353 bn = splitlines(sb, lb, &bl);
354 if (!al || !bl)
354 if (!al || !bl)
355 goto nomem;
355 goto nomem;
356
356
357 count = diff(al, an, bl, bn, &l);
357 count = diff(al, an, bl, bn, &l);
358 if (count < 0)
358 if (count < 0)
359 goto nomem;
359 goto nomem;
360
360
361 /* calculate length of output */
361 /* calculate length of output */
362 la = lb = 0;
362 la = lb = 0;
363 for (h = l.next; h; h = h->next) {
363 for (h = l.next; h; h = h->next) {
364 if (h->a1 != la || h->b1 != lb)
364 if (h->a1 != la || h->b1 != lb)
365 len += 12 + bl[h->b1].l - bl[lb].l;
365 len += 12 + bl[h->b1].l - bl[lb].l;
366 la = h->a2;
366 la = h->a2;
367 lb = h->b2;
367 lb = h->b2;
368 }
368 }
369 PyEval_RestoreThread(_save);
369 PyEval_RestoreThread(_save);
370 _save = NULL;
370 _save = NULL;
371
371
372 result = PyBytes_FromStringAndSize(NULL, len);
372 result = PyBytes_FromStringAndSize(NULL, len);
373
373
374 if (!result)
374 if (!result)
375 goto nomem;
375 goto nomem;
376
376
377 /* build binary patch */
377 /* build binary patch */
378 rb = PyBytes_AsString(result);
378 rb = PyBytes_AsString(result);
379 la = lb = 0;
379 la = lb = 0;
380
380
381 for (h = l.next; h; h = h->next) {
381 for (h = l.next; h; h = h->next) {
382 if (h->a1 != la || h->b1 != lb) {
382 if (h->a1 != la || h->b1 != lb) {
383 len = bl[h->b1].l - bl[lb].l;
383 len = bl[h->b1].l - bl[lb].l;
384 putbe32((uint32_t)(al[la].l - al->l), rb);
384 putbe32((uint32_t)(al[la].l - al->l), rb);
385 putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4);
385 putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4);
386 putbe32((uint32_t)len, rb + 8);
386 putbe32((uint32_t)len, rb + 8);
387 memcpy(rb + 12, bl[lb].l, len);
387 memcpy(rb + 12, bl[lb].l, len);
388 rb += 12 + len;
388 rb += 12 + len;
389 }
389 }
390 la = h->a2;
390 la = h->a2;
391 lb = h->b2;
391 lb = h->b2;
392 }
392 }
393
393
394 nomem:
394 nomem:
395 if (_save)
395 if (_save)
396 PyEval_RestoreThread(_save);
396 PyEval_RestoreThread(_save);
397 free(al);
397 free(al);
398 free(bl);
398 free(bl);
399 freehunks(l.next);
399 freehunks(l.next);
400 return result ? result : PyErr_NoMemory();
400 return result ? result : PyErr_NoMemory();
401 }
401 }
402
402
403 /*
403 /*
404 * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise,
404 * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise,
405 * reduce whitespace sequences to a single space and trim remaining whitespace
405 * reduce whitespace sequences to a single space and trim remaining whitespace
406 * from end of lines.
406 * from end of lines.
407 */
407 */
408 static PyObject *fixws(PyObject *self, PyObject *args)
408 static PyObject *fixws(PyObject *self, PyObject *args)
409 {
409 {
410 PyObject *s, *result = NULL;
410 PyObject *s, *result = NULL;
411 char allws, c;
411 char allws, c;
412 const char *r;
412 const char *r;
413 Py_ssize_t i, rlen, wlen = 0;
413 Py_ssize_t i, rlen, wlen = 0;
414 char *w;
414 char *w;
415
415
416 if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws))
416 if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws))
417 return NULL;
417 return NULL;
418 r = PyBytes_AsString(s);
418 r = PyBytes_AsString(s);
419 rlen = PyBytes_Size(s);
419 rlen = PyBytes_Size(s);
420
420
421 w = (char *)malloc(rlen ? rlen : 1);
421 w = (char *)malloc(rlen ? rlen : 1);
422 if (!w)
422 if (!w)
423 goto nomem;
423 goto nomem;
424
424
425 for (i = 0; i != rlen; i++) {
425 for (i = 0; i != rlen; i++) {
426 c = r[i];
426 c = r[i];
427 if (c == ' ' || c == '\t' || c == '\r') {
427 if (c == ' ' || c == '\t' || c == '\r') {
428 if (!allws && (wlen == 0 || w[wlen - 1] != ' '))
428 if (!allws && (wlen == 0 || w[wlen - 1] != ' '))
429 w[wlen++] = ' ';
429 w[wlen++] = ' ';
430 } else if (c == '\n' && !allws
430 } else if (c == '\n' && !allws
431 && wlen > 0 && w[wlen - 1] == ' ') {
431 && wlen > 0 && w[wlen - 1] == ' ') {
432 w[wlen - 1] = '\n';
432 w[wlen - 1] = '\n';
433 } else {
433 } else {
434 w[wlen++] = c;
434 w[wlen++] = c;
435 }
435 }
436 }
436 }
437
437
438 result = PyBytes_FromStringAndSize(w, wlen);
438 result = PyBytes_FromStringAndSize(w, wlen);
439
439
440 nomem:
440 nomem:
441 free(w);
441 free(w);
442 return result ? result : PyErr_NoMemory();
442 return result ? result : PyErr_NoMemory();
443 }
443 }
444
444
445
445
446 static char mdiff_doc[] = "Efficient binary diff.";
446 static char mdiff_doc[] = "Efficient binary diff.";
447
447
448 static PyMethodDef methods[] = {
448 static PyMethodDef methods[] = {
449 {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"},
449 {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"},
450 {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"},
450 {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"},
451 {"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"},
451 {"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"},
452 {NULL, NULL}
452 {NULL, NULL}
453 };
453 };
454
454
455 #ifdef IS_PY3K
455 #ifdef IS_PY3K
456 static struct PyModuleDef bdiff_module = {
456 static struct PyModuleDef bdiff_module = {
457 PyModuleDef_HEAD_INIT,
457 PyModuleDef_HEAD_INIT,
458 "bdiff",
458 "bdiff",
459 mdiff_doc,
459 mdiff_doc,
460 -1,
460 -1,
461 methods
461 methods
462 };
462 };
463
463
464 PyMODINIT_FUNC PyInit_bdiff(void)
464 PyMODINIT_FUNC PyInit_bdiff(void)
465 {
465 {
466 return PyModule_Create(&bdiff_module);
466 return PyModule_Create(&bdiff_module);
467 }
467 }
468 #else
468 #else
469 PyMODINIT_FUNC initbdiff(void)
469 PyMODINIT_FUNC initbdiff(void)
470 {
470 {
471 Py_InitModule3("bdiff", methods, mdiff_doc);
471 Py_InitModule3("bdiff", methods, mdiff_doc);
472 }
472 }
473 #endif
473 #endif
474
474
@@ -1,70 +1,73 b''
1 from __future__ import absolute_import, print_function
1 from __future__ import absolute_import, print_function
2 import struct
2 import struct
3 from mercurial import (
3 from mercurial import (
4 bdiff,
4 bdiff,
5 mpatch,
5 mpatch,
6 )
6 )
7
7
8 def test1(a, b):
8 def test1(a, b):
9 d = bdiff.bdiff(a, b)
9 d = bdiff.bdiff(a, b)
10 c = a
10 c = a
11 if d:
11 if d:
12 c = mpatch.patches(a, [d])
12 c = mpatch.patches(a, [d])
13 if c != b:
13 if c != b:
14 print("***", repr(a), repr(b))
14 print("***", repr(a), repr(b))
15 print("bad:")
15 print("bad:")
16 print(repr(c)[:200])
16 print(repr(c)[:200])
17 print(repr(d))
17 print(repr(d))
18
18
19 def test(a, b):
19 def test(a, b):
20 print("***", repr(a), repr(b))
20 print("***", repr(a), repr(b))
21 test1(a, b)
21 test1(a, b)
22 test1(b, a)
22 test1(b, a)
23
23
24 test("a\nc\n\n\n\n", "a\nb\n\n\n")
24 test("a\nc\n\n\n\n", "a\nb\n\n\n")
25 test("a\nb\nc\n", "a\nc\n")
25 test("a\nb\nc\n", "a\nc\n")
26 test("", "")
26 test("", "")
27 test("a\nb\nc", "a\nb\nc")
27 test("a\nb\nc", "a\nb\nc")
28 test("a\nb\nc\nd\n", "a\nd\n")
28 test("a\nb\nc\nd\n", "a\nd\n")
29 test("a\nb\nc\nd\n", "a\nc\ne\n")
29 test("a\nb\nc\nd\n", "a\nc\ne\n")
30 test("a\nb\nc\n", "a\nc\n")
30 test("a\nb\nc\n", "a\nc\n")
31 test("a\n", "c\na\nb\n")
31 test("a\n", "c\na\nb\n")
32 test("a\n", "")
32 test("a\n", "")
33 test("a\n", "b\nc\n")
33 test("a\n", "b\nc\n")
34 test("a\n", "c\na\n")
34 test("a\n", "c\na\n")
35 test("", "adjfkjdjksdhfksj")
35 test("", "adjfkjdjksdhfksj")
36 test("", "ab")
36 test("", "ab")
37 test("", "abc")
37 test("", "abc")
38 test("a", "a")
38 test("a", "a")
39 test("ab", "ab")
39 test("ab", "ab")
40 test("abc", "abc")
40 test("abc", "abc")
41 test("a\n", "a\n")
41 test("a\n", "a\n")
42 test("a\nb", "a\nb")
42 test("a\nb", "a\nb")
43
43
44 #issue1295
44 #issue1295
45 def showdiff(a, b):
45 def showdiff(a, b):
46 bin = bdiff.bdiff(a, b)
46 bin = bdiff.bdiff(a, b)
47 pos = 0
47 pos = 0
48 while pos < len(bin):
48 while pos < len(bin):
49 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
49 p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
50 pos += 12
50 pos += 12
51 print(p1, p2, repr(bin[pos:pos + l]))
51 print(p1, p2, repr(bin[pos:pos + l]))
52 pos += l
52 pos += l
53 showdiff("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\nx\n\nz\n")
53 showdiff("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\nx\n\nz\n")
54 showdiff("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\ny\n\nx\n\nz\n")
54 showdiff("x\n\nx\n\nx\n\nx\n\nz\n", "x\n\nx\n\ny\n\nx\n\ny\n\nx\n\nz\n")
55 # we should pick up abbbc. rather than bc.de as the longest match
56 showdiff("a\nb\nb\nb\nc\n.\nd\ne\n.\nf\n",
57 "a\nb\nb\na\nb\nb\nb\nc\n.\nb\nc\n.\nd\ne\nf\n")
55
58
56 print("done")
59 print("done")
57
60
58 def testfixws(a, b, allws):
61 def testfixws(a, b, allws):
59 c = bdiff.fixws(a, allws)
62 c = bdiff.fixws(a, allws)
60 if c != b:
63 if c != b:
61 print("*** fixws", repr(a), repr(b), allws)
64 print("*** fixws", repr(a), repr(b), allws)
62 print("got:")
65 print("got:")
63 print(repr(c))
66 print(repr(c))
64
67
65 testfixws(" \ta\r b\t\n", "ab\n", 1)
68 testfixws(" \ta\r b\t\n", "ab\n", 1)
66 testfixws(" \ta\r b\t\n", " a b\n", 0)
69 testfixws(" \ta\r b\t\n", " a b\n", 0)
67 testfixws("", "", 1)
70 testfixws("", "", 1)
68 testfixws("", "", 0)
71 testfixws("", "", 0)
69
72
70 print("done")
73 print("done")
@@ -1,24 +1,27 b''
1 *** 'a\nc\n\n\n\n' 'a\nb\n\n\n'
1 *** 'a\nc\n\n\n\n' 'a\nb\n\n\n'
2 *** 'a\nb\nc\n' 'a\nc\n'
2 *** 'a\nb\nc\n' 'a\nc\n'
3 *** '' ''
3 *** '' ''
4 *** 'a\nb\nc' 'a\nb\nc'
4 *** 'a\nb\nc' 'a\nb\nc'
5 *** 'a\nb\nc\nd\n' 'a\nd\n'
5 *** 'a\nb\nc\nd\n' 'a\nd\n'
6 *** 'a\nb\nc\nd\n' 'a\nc\ne\n'
6 *** 'a\nb\nc\nd\n' 'a\nc\ne\n'
7 *** 'a\nb\nc\n' 'a\nc\n'
7 *** 'a\nb\nc\n' 'a\nc\n'
8 *** 'a\n' 'c\na\nb\n'
8 *** 'a\n' 'c\na\nb\n'
9 *** 'a\n' ''
9 *** 'a\n' ''
10 *** 'a\n' 'b\nc\n'
10 *** 'a\n' 'b\nc\n'
11 *** 'a\n' 'c\na\n'
11 *** 'a\n' 'c\na\n'
12 *** '' 'adjfkjdjksdhfksj'
12 *** '' 'adjfkjdjksdhfksj'
13 *** '' 'ab'
13 *** '' 'ab'
14 *** '' 'abc'
14 *** '' 'abc'
15 *** 'a' 'a'
15 *** 'a' 'a'
16 *** 'ab' 'ab'
16 *** 'ab' 'ab'
17 *** 'abc' 'abc'
17 *** 'abc' 'abc'
18 *** 'a\n' 'a\n'
18 *** 'a\n' 'a\n'
19 *** 'a\nb' 'a\nb'
19 *** 'a\nb' 'a\nb'
20 6 6 'y\n\n'
20 6 6 'y\n\n'
21 6 6 'y\n\n'
21 6 6 'y\n\n'
22 9 9 'y\n\n'
22 9 9 'y\n\n'
23 0 0 'a\nb\nb\n'
24 12 12 'b\nc\n.\n'
25 16 18 ''
23 done
26 done
24 done
27 done
General Comments 0
You need to be logged in to leave comments. Login now