Show More
@@ -0,0 +1,21 b'' | |||||
|
1 | #ifndef _HG_BDIFF_H_ | |||
|
2 | #define _HG_BDIFF_H_ | |||
|
3 | ||||
|
4 | struct bdiff_line { | |||
|
5 | int hash, n, e; | |||
|
6 | ssize_t len; | |||
|
7 | const char *l; | |||
|
8 | }; | |||
|
9 | ||||
|
10 | struct bdiff_hunk; | |||
|
11 | struct bdiff_hunk { | |||
|
12 | int a1, a2, b1, b2; | |||
|
13 | struct bdiff_hunk *next; | |||
|
14 | }; | |||
|
15 | ||||
|
16 | int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr); | |||
|
17 | int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b, int bn, | |||
|
18 | struct bdiff_hunk *base); | |||
|
19 | void bdiff_freehunks(struct bdiff_hunk *l); | |||
|
20 | ||||
|
21 | #endif |
@@ -0,0 +1,203 b'' | |||||
|
1 | /* | |||
|
2 | bdiff.c - efficient binary diff extension for Mercurial | |||
|
3 | ||||
|
4 | Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> | |||
|
5 | ||||
|
6 | This software may be used and distributed according to the terms of | |||
|
7 | the GNU General Public License, incorporated herein by reference. | |||
|
8 | ||||
|
9 | Based roughly on Python difflib | |||
|
10 | */ | |||
|
11 | ||||
|
12 | #define PY_SSIZE_T_CLEAN | |||
|
13 | #include <Python.h> | |||
|
14 | #include <stdlib.h> | |||
|
15 | #include <string.h> | |||
|
16 | #include <limits.h> | |||
|
17 | ||||
|
18 | #include "bdiff.h" | |||
|
19 | #include "bitmanipulation.h" | |||
|
20 | ||||
|
21 | ||||
|
22 | static PyObject *blocks(PyObject *self, PyObject *args) | |||
|
23 | { | |||
|
24 | PyObject *sa, *sb, *rl = NULL, *m; | |||
|
25 | struct bdiff_line *a, *b; | |||
|
26 | struct bdiff_hunk l, *h; | |||
|
27 | int an, bn, count, pos = 0; | |||
|
28 | ||||
|
29 | l.next = NULL; | |||
|
30 | ||||
|
31 | if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) | |||
|
32 | return NULL; | |||
|
33 | ||||
|
34 | an = bdiff_splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a); | |||
|
35 | bn = bdiff_splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b); | |||
|
36 | ||||
|
37 | if (!a || !b) | |||
|
38 | goto nomem; | |||
|
39 | ||||
|
40 | count = bdiff_diff(a, an, b, bn, &l); | |||
|
41 | if (count < 0) | |||
|
42 | goto nomem; | |||
|
43 | ||||
|
44 | rl = PyList_New(count); | |||
|
45 | if (!rl) | |||
|
46 | goto nomem; | |||
|
47 | ||||
|
48 | for (h = l.next; h; h = h->next) { | |||
|
49 | m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); | |||
|
50 | PyList_SetItem(rl, pos, m); | |||
|
51 | pos++; | |||
|
52 | } | |||
|
53 | ||||
|
54 | nomem: | |||
|
55 | free(a); | |||
|
56 | free(b); | |||
|
57 | bdiff_freehunks(l.next); | |||
|
58 | return rl ? rl : PyErr_NoMemory(); | |||
|
59 | } | |||
|
60 | ||||
|
61 | static PyObject *bdiff(PyObject *self, PyObject *args) | |||
|
62 | { | |||
|
63 | char *sa, *sb, *rb; | |||
|
64 | PyObject *result = NULL; | |||
|
65 | struct bdiff_line *al, *bl; | |||
|
66 | struct bdiff_hunk l, *h; | |||
|
67 | int an, bn, count; | |||
|
68 | Py_ssize_t len = 0, la, lb; | |||
|
69 | PyThreadState *_save; | |||
|
70 | ||||
|
71 | l.next = NULL; | |||
|
72 | ||||
|
73 | if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) | |||
|
74 | return NULL; | |||
|
75 | ||||
|
76 | if (la > UINT_MAX || lb > UINT_MAX) { | |||
|
77 | PyErr_SetString(PyExc_ValueError, "bdiff inputs too large"); | |||
|
78 | return NULL; | |||
|
79 | } | |||
|
80 | ||||
|
81 | _save = PyEval_SaveThread(); | |||
|
82 | an = bdiff_splitlines(sa, la, &al); | |||
|
83 | bn = bdiff_splitlines(sb, lb, &bl); | |||
|
84 | if (!al || !bl) | |||
|
85 | goto nomem; | |||
|
86 | ||||
|
87 | count = bdiff_diff(al, an, bl, bn, &l); | |||
|
88 | if (count < 0) | |||
|
89 | goto nomem; | |||
|
90 | ||||
|
91 | /* calculate length of output */ | |||
|
92 | la = lb = 0; | |||
|
93 | for (h = l.next; h; h = h->next) { | |||
|
94 | if (h->a1 != la || h->b1 != lb) | |||
|
95 | len += 12 + bl[h->b1].l - bl[lb].l; | |||
|
96 | la = h->a2; | |||
|
97 | lb = h->b2; | |||
|
98 | } | |||
|
99 | PyEval_RestoreThread(_save); | |||
|
100 | _save = NULL; | |||
|
101 | ||||
|
102 | result = PyBytes_FromStringAndSize(NULL, len); | |||
|
103 | ||||
|
104 | if (!result) | |||
|
105 | goto nomem; | |||
|
106 | ||||
|
107 | /* build binary patch */ | |||
|
108 | rb = PyBytes_AsString(result); | |||
|
109 | la = lb = 0; | |||
|
110 | ||||
|
111 | for (h = l.next; h; h = h->next) { | |||
|
112 | if (h->a1 != la || h->b1 != lb) { | |||
|
113 | len = bl[h->b1].l - bl[lb].l; | |||
|
114 | putbe32((uint32_t)(al[la].l - al->l), rb); | |||
|
115 | putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4); | |||
|
116 | putbe32((uint32_t)len, rb + 8); | |||
|
117 | memcpy(rb + 12, bl[lb].l, len); | |||
|
118 | rb += 12 + len; | |||
|
119 | } | |||
|
120 | la = h->a2; | |||
|
121 | lb = h->b2; | |||
|
122 | } | |||
|
123 | ||||
|
124 | nomem: | |||
|
125 | if (_save) | |||
|
126 | PyEval_RestoreThread(_save); | |||
|
127 | free(al); | |||
|
128 | free(bl); | |||
|
129 | bdiff_freehunks(l.next); | |||
|
130 | return result ? result : PyErr_NoMemory(); | |||
|
131 | } | |||
|
132 | ||||
|
133 | /* | |||
|
134 | * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise, | |||
|
135 | * reduce whitespace sequences to a single space and trim remaining whitespace | |||
|
136 | * from end of lines. | |||
|
137 | */ | |||
|
138 | static PyObject *fixws(PyObject *self, PyObject *args) | |||
|
139 | { | |||
|
140 | PyObject *s, *result = NULL; | |||
|
141 | char allws, c; | |||
|
142 | const char *r; | |||
|
143 | Py_ssize_t i, rlen, wlen = 0; | |||
|
144 | char *w; | |||
|
145 | ||||
|
146 | if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws)) | |||
|
147 | return NULL; | |||
|
148 | r = PyBytes_AsString(s); | |||
|
149 | rlen = PyBytes_Size(s); | |||
|
150 | ||||
|
151 | w = (char *)malloc(rlen ? rlen : 1); | |||
|
152 | if (!w) | |||
|
153 | goto nomem; | |||
|
154 | ||||
|
155 | for (i = 0; i != rlen; i++) { | |||
|
156 | c = r[i]; | |||
|
157 | if (c == ' ' || c == '\t' || c == '\r') { | |||
|
158 | if (!allws && (wlen == 0 || w[wlen - 1] != ' ')) | |||
|
159 | w[wlen++] = ' '; | |||
|
160 | } else if (c == '\n' && !allws | |||
|
161 | && wlen > 0 && w[wlen - 1] == ' ') { | |||
|
162 | w[wlen - 1] = '\n'; | |||
|
163 | } else { | |||
|
164 | w[wlen++] = c; | |||
|
165 | } | |||
|
166 | } | |||
|
167 | ||||
|
168 | result = PyBytes_FromStringAndSize(w, wlen); | |||
|
169 | ||||
|
170 | nomem: | |||
|
171 | free(w); | |||
|
172 | return result ? result : PyErr_NoMemory(); | |||
|
173 | } | |||
|
174 | ||||
|
175 | ||||
|
176 | static char mdiff_doc[] = "Efficient binary diff."; | |||
|
177 | ||||
|
178 | static PyMethodDef methods[] = { | |||
|
179 | {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"}, | |||
|
180 | {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"}, | |||
|
181 | {"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"}, | |||
|
182 | {NULL, NULL} | |||
|
183 | }; | |||
|
184 | ||||
|
185 | #ifdef IS_PY3K | |||
|
186 | static struct PyModuleDef bdiff_module = { | |||
|
187 | PyModuleDef_HEAD_INIT, | |||
|
188 | "bdiff", | |||
|
189 | mdiff_doc, | |||
|
190 | -1, | |||
|
191 | methods | |||
|
192 | }; | |||
|
193 | ||||
|
194 | PyMODINIT_FUNC PyInit_bdiff(void) | |||
|
195 | { | |||
|
196 | return PyModule_Create(&bdiff_module); | |||
|
197 | } | |||
|
198 | #else | |||
|
199 | PyMODINIT_FUNC initbdiff(void) | |||
|
200 | { | |||
|
201 | Py_InitModule3("bdiff", methods, mdiff_doc); | |||
|
202 | } | |||
|
203 | #endif |
@@ -9,33 +9,19 b'' | |||||
9 | Based roughly on Python difflib |
|
9 | Based roughly on Python difflib | |
10 | */ |
|
10 | */ | |
11 |
|
11 | |||
12 | #define PY_SSIZE_T_CLEAN |
|
|||
13 | #include <Python.h> |
|
|||
14 | #include <stdlib.h> |
|
12 | #include <stdlib.h> | |
15 | #include <string.h> |
|
13 | #include <string.h> | |
16 | #include <limits.h> |
|
14 | #include <limits.h> | |
17 |
|
15 | |||
18 | #include "compat.h" |
|
16 | #include "compat.h" | |
19 | #include "util.h" |
|
|||
20 | #include "bitmanipulation.h" |
|
17 | #include "bitmanipulation.h" | |
21 |
|
18 | #include "bdiff.h" | ||
22 | struct bdiff_line { |
|
|||
23 | int hash, n, e; |
|
|||
24 | ssize_t len; |
|
|||
25 | const char *l; |
|
|||
26 | }; |
|
|||
27 |
|
19 | |||
28 | struct pos { |
|
20 | struct pos { | |
29 | int pos, len; |
|
21 | int pos, len; | |
30 | }; |
|
22 | }; | |
31 |
|
23 | |||
32 | struct bdiff_hunk; |
|
24 | int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr) | |
33 | struct bdiff_hunk { |
|
|||
34 | int a1, a2, b1, b2; |
|
|||
35 | struct bdiff_hunk *next; |
|
|||
36 | }; |
|
|||
37 |
|
||||
38 | static int bdiff_splitlines(const char *a, ssize_t len, struct bdiff_line **lr) |
|
|||
39 | { |
|
25 | { | |
40 | unsigned hash; |
|
26 | unsigned hash; | |
41 | int i; |
|
27 | int i; | |
@@ -244,7 +230,7 b' static struct bdiff_hunk *recurse(struct' | |||||
244 | } |
|
230 | } | |
245 | } |
|
231 | } | |
246 |
|
232 | |||
247 |
|
|
233 | int bdiff_diff(struct bdiff_line *a, int an, struct bdiff_line *b, | |
248 | int bn, struct bdiff_hunk *base) |
|
234 | int bn, struct bdiff_hunk *base) | |
249 | { |
|
235 | { | |
250 | struct bdiff_hunk *curr; |
|
236 | struct bdiff_hunk *curr; | |
@@ -298,7 +284,7 b' static int bdiff_diff(struct bdiff_line ' | |||||
298 | return count; |
|
284 | return count; | |
299 | } |
|
285 | } | |
300 |
|
286 | |||
301 |
|
|
287 | void bdiff_freehunks(struct bdiff_hunk *l) | |
302 | { |
|
288 | { | |
303 | struct bdiff_hunk *n; |
|
289 | struct bdiff_hunk *n; | |
304 | for (; l; l = n) { |
|
290 | for (; l; l = n) { | |
@@ -307,186 +293,4 b' static void bdiff_freehunks(struct bdiff' | |||||
307 | } |
|
293 | } | |
308 | } |
|
294 | } | |
309 |
|
295 | |||
310 | static PyObject *blocks(PyObject *self, PyObject *args) |
|
|||
311 | { |
|
|||
312 | PyObject *sa, *sb, *rl = NULL, *m; |
|
|||
313 | struct bdiff_line *a, *b; |
|
|||
314 | struct bdiff_hunk l, *h; |
|
|||
315 | int an, bn, count, pos = 0; |
|
|||
316 |
|
296 | |||
317 | l.next = NULL; |
|
|||
318 |
|
||||
319 | if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) |
|
|||
320 | return NULL; |
|
|||
321 |
|
||||
322 | an = bdiff_splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a); |
|
|||
323 | bn = bdiff_splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b); |
|
|||
324 |
|
||||
325 | if (!a || !b) |
|
|||
326 | goto nomem; |
|
|||
327 |
|
||||
328 | count = bdiff_diff(a, an, b, bn, &l); |
|
|||
329 | if (count < 0) |
|
|||
330 | goto nomem; |
|
|||
331 |
|
||||
332 | rl = PyList_New(count); |
|
|||
333 | if (!rl) |
|
|||
334 | goto nomem; |
|
|||
335 |
|
||||
336 | for (h = l.next; h; h = h->next) { |
|
|||
337 | m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); |
|
|||
338 | PyList_SetItem(rl, pos, m); |
|
|||
339 | pos++; |
|
|||
340 | } |
|
|||
341 |
|
||||
342 | nomem: |
|
|||
343 | free(a); |
|
|||
344 | free(b); |
|
|||
345 | bdiff_freehunks(l.next); |
|
|||
346 | return rl ? rl : PyErr_NoMemory(); |
|
|||
347 | } |
|
|||
348 |
|
||||
349 | static PyObject *bdiff(PyObject *self, PyObject *args) |
|
|||
350 | { |
|
|||
351 | char *sa, *sb, *rb; |
|
|||
352 | PyObject *result = NULL; |
|
|||
353 | struct bdiff_line *al, *bl; |
|
|||
354 | struct bdiff_hunk l, *h; |
|
|||
355 | int an, bn, count; |
|
|||
356 | Py_ssize_t len = 0, la, lb; |
|
|||
357 | PyThreadState *_save; |
|
|||
358 |
|
||||
359 | l.next = NULL; |
|
|||
360 |
|
||||
361 | if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) |
|
|||
362 | return NULL; |
|
|||
363 |
|
||||
364 | if (la > UINT_MAX || lb > UINT_MAX) { |
|
|||
365 | PyErr_SetString(PyExc_ValueError, "bdiff inputs too large"); |
|
|||
366 | return NULL; |
|
|||
367 | } |
|
|||
368 |
|
||||
369 | _save = PyEval_SaveThread(); |
|
|||
370 | an = bdiff_splitlines(sa, la, &al); |
|
|||
371 | bn = bdiff_splitlines(sb, lb, &bl); |
|
|||
372 | if (!al || !bl) |
|
|||
373 | goto nomem; |
|
|||
374 |
|
||||
375 | count = bdiff_diff(al, an, bl, bn, &l); |
|
|||
376 | if (count < 0) |
|
|||
377 | goto nomem; |
|
|||
378 |
|
||||
379 | /* calculate length of output */ |
|
|||
380 | la = lb = 0; |
|
|||
381 | for (h = l.next; h; h = h->next) { |
|
|||
382 | if (h->a1 != la || h->b1 != lb) |
|
|||
383 | len += 12 + bl[h->b1].l - bl[lb].l; |
|
|||
384 | la = h->a2; |
|
|||
385 | lb = h->b2; |
|
|||
386 | } |
|
|||
387 | PyEval_RestoreThread(_save); |
|
|||
388 | _save = NULL; |
|
|||
389 |
|
||||
390 | result = PyBytes_FromStringAndSize(NULL, len); |
|
|||
391 |
|
||||
392 | if (!result) |
|
|||
393 | goto nomem; |
|
|||
394 |
|
||||
395 | /* build binary patch */ |
|
|||
396 | rb = PyBytes_AsString(result); |
|
|||
397 | la = lb = 0; |
|
|||
398 |
|
||||
399 | for (h = l.next; h; h = h->next) { |
|
|||
400 | if (h->a1 != la || h->b1 != lb) { |
|
|||
401 | len = bl[h->b1].l - bl[lb].l; |
|
|||
402 | putbe32((uint32_t)(al[la].l - al->l), rb); |
|
|||
403 | putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4); |
|
|||
404 | putbe32((uint32_t)len, rb + 8); |
|
|||
405 | memcpy(rb + 12, bl[lb].l, len); |
|
|||
406 | rb += 12 + len; |
|
|||
407 | } |
|
|||
408 | la = h->a2; |
|
|||
409 | lb = h->b2; |
|
|||
410 | } |
|
|||
411 |
|
||||
412 | nomem: |
|
|||
413 | if (_save) |
|
|||
414 | PyEval_RestoreThread(_save); |
|
|||
415 | free(al); |
|
|||
416 | free(bl); |
|
|||
417 | bdiff_freehunks(l.next); |
|
|||
418 | return result ? result : PyErr_NoMemory(); |
|
|||
419 | } |
|
|||
420 |
|
||||
421 | /* |
|
|||
422 | * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise, |
|
|||
423 | * reduce whitespace sequences to a single space and trim remaining whitespace |
|
|||
424 | * from end of lines. |
|
|||
425 | */ |
|
|||
426 | static PyObject *fixws(PyObject *self, PyObject *args) |
|
|||
427 | { |
|
|||
428 | PyObject *s, *result = NULL; |
|
|||
429 | char allws, c; |
|
|||
430 | const char *r; |
|
|||
431 | Py_ssize_t i, rlen, wlen = 0; |
|
|||
432 | char *w; |
|
|||
433 |
|
||||
434 | if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws)) |
|
|||
435 | return NULL; |
|
|||
436 | r = PyBytes_AsString(s); |
|
|||
437 | rlen = PyBytes_Size(s); |
|
|||
438 |
|
||||
439 | w = (char *)malloc(rlen ? rlen : 1); |
|
|||
440 | if (!w) |
|
|||
441 | goto nomem; |
|
|||
442 |
|
||||
443 | for (i = 0; i != rlen; i++) { |
|
|||
444 | c = r[i]; |
|
|||
445 | if (c == ' ' || c == '\t' || c == '\r') { |
|
|||
446 | if (!allws && (wlen == 0 || w[wlen - 1] != ' ')) |
|
|||
447 | w[wlen++] = ' '; |
|
|||
448 | } else if (c == '\n' && !allws |
|
|||
449 | && wlen > 0 && w[wlen - 1] == ' ') { |
|
|||
450 | w[wlen - 1] = '\n'; |
|
|||
451 | } else { |
|
|||
452 | w[wlen++] = c; |
|
|||
453 | } |
|
|||
454 | } |
|
|||
455 |
|
||||
456 | result = PyBytes_FromStringAndSize(w, wlen); |
|
|||
457 |
|
||||
458 | nomem: |
|
|||
459 | free(w); |
|
|||
460 | return result ? result : PyErr_NoMemory(); |
|
|||
461 | } |
|
|||
462 |
|
||||
463 |
|
||||
464 | static char mdiff_doc[] = "Efficient binary diff."; |
|
|||
465 |
|
||||
466 | static PyMethodDef methods[] = { |
|
|||
467 | {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"}, |
|
|||
468 | {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"}, |
|
|||
469 | {"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"}, |
|
|||
470 | {NULL, NULL} |
|
|||
471 | }; |
|
|||
472 |
|
||||
473 | #ifdef IS_PY3K |
|
|||
474 | static struct PyModuleDef bdiff_module = { |
|
|||
475 | PyModuleDef_HEAD_INIT, |
|
|||
476 | "bdiff", |
|
|||
477 | mdiff_doc, |
|
|||
478 | -1, |
|
|||
479 | methods |
|
|||
480 | }; |
|
|||
481 |
|
||||
482 | PyMODINIT_FUNC PyInit_bdiff(void) |
|
|||
483 | { |
|
|||
484 | return PyModule_Create(&bdiff_module); |
|
|||
485 | } |
|
|||
486 | #else |
|
|||
487 | PyMODINIT_FUNC initbdiff(void) |
|
|||
488 | { |
|
|||
489 | Py_InitModule3("bdiff", methods, mdiff_doc); |
|
|||
490 | } |
|
|||
491 | #endif |
|
|||
492 |
|
@@ -553,8 +553,9 b" if sys.platform == 'darwin':" | |||||
553 | extmodules = [ |
|
553 | extmodules = [ | |
554 | Extension('mercurial.base85', ['mercurial/base85.c'], |
|
554 | Extension('mercurial.base85', ['mercurial/base85.c'], | |
555 | depends=common_depends), |
|
555 | depends=common_depends), | |
556 |
Extension('mercurial.bdiff', ['mercurial/bdiff.c' |
|
556 | Extension('mercurial.bdiff', ['mercurial/bdiff.c', | |
557 | depends=common_depends), |
|
557 | 'mercurial/bdiff_module.c'], | |
|
558 | depends=common_depends + ['mercurial/bdiff.h']), | |||
558 | Extension('mercurial.diffhelpers', ['mercurial/diffhelpers.c'], |
|
559 | Extension('mercurial.diffhelpers', ['mercurial/diffhelpers.c'], | |
559 | depends=common_depends), |
|
560 | depends=common_depends), | |
560 | Extension('mercurial.mpatch', ['mercurial/mpatch.c'], |
|
561 | Extension('mercurial.mpatch', ['mercurial/mpatch.c'], |
General Comments 0
You need to be logged in to leave comments.
Login now