Show More
@@ -1,203 +1,204 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | bdiff.c - efficient binary diff extension for Mercurial |
|
3 | 3 | |
|
4 | 4 | Copyright 2005, 2006 Matt Mackall <mpm@selenic.com> |
|
5 | 5 | |
|
6 | 6 | This software may be used and distributed according to the terms of |
|
7 | 7 | the GNU General Public License, incorporated herein by reference. |
|
8 | 8 | |
|
9 | 9 | Based roughly on Python difflib |
|
10 | 10 | */ |
|
11 | 11 | |
|
12 | 12 | #define PY_SSIZE_T_CLEAN |
|
13 | 13 | #include <Python.h> |
|
14 | 14 | #include <stdlib.h> |
|
15 | 15 | #include <string.h> |
|
16 | 16 | #include <limits.h> |
|
17 | 17 | |
|
18 | 18 | #include "bdiff.h" |
|
19 | 19 | #include "bitmanipulation.h" |
|
20 | #include "util.h" | |
|
20 | 21 | |
|
21 | 22 | |
|
22 | 23 | static PyObject *blocks(PyObject *self, PyObject *args) |
|
23 | 24 | { |
|
24 | 25 | PyObject *sa, *sb, *rl = NULL, *m; |
|
25 | 26 | struct bdiff_line *a, *b; |
|
26 | 27 | struct bdiff_hunk l, *h; |
|
27 | 28 | int an, bn, count, pos = 0; |
|
28 | 29 | |
|
29 | 30 | l.next = NULL; |
|
30 | 31 | |
|
31 | 32 | if (!PyArg_ParseTuple(args, "SS:bdiff", &sa, &sb)) |
|
32 | 33 | return NULL; |
|
33 | 34 | |
|
34 | 35 | an = bdiff_splitlines(PyBytes_AsString(sa), PyBytes_Size(sa), &a); |
|
35 | 36 | bn = bdiff_splitlines(PyBytes_AsString(sb), PyBytes_Size(sb), &b); |
|
36 | 37 | |
|
37 | 38 | if (!a || !b) |
|
38 | 39 | goto nomem; |
|
39 | 40 | |
|
40 | 41 | count = bdiff_diff(a, an, b, bn, &l); |
|
41 | 42 | if (count < 0) |
|
42 | 43 | goto nomem; |
|
43 | 44 | |
|
44 | 45 | rl = PyList_New(count); |
|
45 | 46 | if (!rl) |
|
46 | 47 | goto nomem; |
|
47 | 48 | |
|
48 | 49 | for (h = l.next; h; h = h->next) { |
|
49 | 50 | m = Py_BuildValue("iiii", h->a1, h->a2, h->b1, h->b2); |
|
50 | 51 | PyList_SetItem(rl, pos, m); |
|
51 | 52 | pos++; |
|
52 | 53 | } |
|
53 | 54 | |
|
54 | 55 | nomem: |
|
55 | 56 | free(a); |
|
56 | 57 | free(b); |
|
57 | 58 | bdiff_freehunks(l.next); |
|
58 | 59 | return rl ? rl : PyErr_NoMemory(); |
|
59 | 60 | } |
|
60 | 61 | |
|
61 | 62 | static PyObject *bdiff(PyObject *self, PyObject *args) |
|
62 | 63 | { |
|
63 | 64 | char *sa, *sb, *rb; |
|
64 | 65 | PyObject *result = NULL; |
|
65 | 66 | struct bdiff_line *al, *bl; |
|
66 | 67 | struct bdiff_hunk l, *h; |
|
67 | 68 | int an, bn, count; |
|
68 | 69 | Py_ssize_t len = 0, la, lb; |
|
69 | 70 | PyThreadState *_save; |
|
70 | 71 | |
|
71 | 72 | l.next = NULL; |
|
72 | 73 | |
|
73 | 74 | if (!PyArg_ParseTuple(args, "s#s#:bdiff", &sa, &la, &sb, &lb)) |
|
74 | 75 | return NULL; |
|
75 | 76 | |
|
76 | 77 | if (la > UINT_MAX || lb > UINT_MAX) { |
|
77 | 78 | PyErr_SetString(PyExc_ValueError, "bdiff inputs too large"); |
|
78 | 79 | return NULL; |
|
79 | 80 | } |
|
80 | 81 | |
|
81 | 82 | _save = PyEval_SaveThread(); |
|
82 | 83 | an = bdiff_splitlines(sa, la, &al); |
|
83 | 84 | bn = bdiff_splitlines(sb, lb, &bl); |
|
84 | 85 | if (!al || !bl) |
|
85 | 86 | goto nomem; |
|
86 | 87 | |
|
87 | 88 | count = bdiff_diff(al, an, bl, bn, &l); |
|
88 | 89 | if (count < 0) |
|
89 | 90 | goto nomem; |
|
90 | 91 | |
|
91 | 92 | /* calculate length of output */ |
|
92 | 93 | la = lb = 0; |
|
93 | 94 | for (h = l.next; h; h = h->next) { |
|
94 | 95 | if (h->a1 != la || h->b1 != lb) |
|
95 | 96 | len += 12 + bl[h->b1].l - bl[lb].l; |
|
96 | 97 | la = h->a2; |
|
97 | 98 | lb = h->b2; |
|
98 | 99 | } |
|
99 | 100 | PyEval_RestoreThread(_save); |
|
100 | 101 | _save = NULL; |
|
101 | 102 | |
|
102 | 103 | result = PyBytes_FromStringAndSize(NULL, len); |
|
103 | 104 | |
|
104 | 105 | if (!result) |
|
105 | 106 | goto nomem; |
|
106 | 107 | |
|
107 | 108 | /* build binary patch */ |
|
108 | 109 | rb = PyBytes_AsString(result); |
|
109 | 110 | la = lb = 0; |
|
110 | 111 | |
|
111 | 112 | for (h = l.next; h; h = h->next) { |
|
112 | 113 | if (h->a1 != la || h->b1 != lb) { |
|
113 | 114 | len = bl[h->b1].l - bl[lb].l; |
|
114 | 115 | putbe32((uint32_t)(al[la].l - al->l), rb); |
|
115 | 116 | putbe32((uint32_t)(al[h->a1].l - al->l), rb + 4); |
|
116 | 117 | putbe32((uint32_t)len, rb + 8); |
|
117 | 118 | memcpy(rb + 12, bl[lb].l, len); |
|
118 | 119 | rb += 12 + len; |
|
119 | 120 | } |
|
120 | 121 | la = h->a2; |
|
121 | 122 | lb = h->b2; |
|
122 | 123 | } |
|
123 | 124 | |
|
124 | 125 | nomem: |
|
125 | 126 | if (_save) |
|
126 | 127 | PyEval_RestoreThread(_save); |
|
127 | 128 | free(al); |
|
128 | 129 | free(bl); |
|
129 | 130 | bdiff_freehunks(l.next); |
|
130 | 131 | return result ? result : PyErr_NoMemory(); |
|
131 | 132 | } |
|
132 | 133 | |
|
133 | 134 | /* |
|
134 | 135 | * If allws != 0, remove all whitespace (' ', \t and \r). Otherwise, |
|
135 | 136 | * reduce whitespace sequences to a single space and trim remaining whitespace |
|
136 | 137 | * from end of lines. |
|
137 | 138 | */ |
|
138 | 139 | static PyObject *fixws(PyObject *self, PyObject *args) |
|
139 | 140 | { |
|
140 | 141 | PyObject *s, *result = NULL; |
|
141 | 142 | char allws, c; |
|
142 | 143 | const char *r; |
|
143 | 144 | Py_ssize_t i, rlen, wlen = 0; |
|
144 | 145 | char *w; |
|
145 | 146 | |
|
146 | 147 | if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws)) |
|
147 | 148 | return NULL; |
|
148 | 149 | r = PyBytes_AsString(s); |
|
149 | 150 | rlen = PyBytes_Size(s); |
|
150 | 151 | |
|
151 | 152 | w = (char *)malloc(rlen ? rlen : 1); |
|
152 | 153 | if (!w) |
|
153 | 154 | goto nomem; |
|
154 | 155 | |
|
155 | 156 | for (i = 0; i != rlen; i++) { |
|
156 | 157 | c = r[i]; |
|
157 | 158 | if (c == ' ' || c == '\t' || c == '\r') { |
|
158 | 159 | if (!allws && (wlen == 0 || w[wlen - 1] != ' ')) |
|
159 | 160 | w[wlen++] = ' '; |
|
160 | 161 | } else if (c == '\n' && !allws |
|
161 | 162 | && wlen > 0 && w[wlen - 1] == ' ') { |
|
162 | 163 | w[wlen - 1] = '\n'; |
|
163 | 164 | } else { |
|
164 | 165 | w[wlen++] = c; |
|
165 | 166 | } |
|
166 | 167 | } |
|
167 | 168 | |
|
168 | 169 | result = PyBytes_FromStringAndSize(w, wlen); |
|
169 | 170 | |
|
170 | 171 | nomem: |
|
171 | 172 | free(w); |
|
172 | 173 | return result ? result : PyErr_NoMemory(); |
|
173 | 174 | } |
|
174 | 175 | |
|
175 | 176 | |
|
176 | 177 | static char mdiff_doc[] = "Efficient binary diff."; |
|
177 | 178 | |
|
178 | 179 | static PyMethodDef methods[] = { |
|
179 | 180 | {"bdiff", bdiff, METH_VARARGS, "calculate a binary diff\n"}, |
|
180 | 181 | {"blocks", blocks, METH_VARARGS, "find a list of matching lines\n"}, |
|
181 | 182 | {"fixws", fixws, METH_VARARGS, "normalize diff whitespaces\n"}, |
|
182 | 183 | {NULL, NULL} |
|
183 | 184 | }; |
|
184 | 185 | |
|
185 | 186 | #ifdef IS_PY3K |
|
186 | 187 | static struct PyModuleDef bdiff_module = { |
|
187 | 188 | PyModuleDef_HEAD_INIT, |
|
188 | 189 | "bdiff", |
|
189 | 190 | mdiff_doc, |
|
190 | 191 | -1, |
|
191 | 192 | methods |
|
192 | 193 | }; |
|
193 | 194 | |
|
194 | 195 | PyMODINIT_FUNC PyInit_bdiff(void) |
|
195 | 196 | { |
|
196 | 197 | return PyModule_Create(&bdiff_module); |
|
197 | 198 | } |
|
198 | 199 | #else |
|
199 | 200 | PyMODINIT_FUNC initbdiff(void) |
|
200 | 201 | { |
|
201 | 202 | Py_InitModule3("bdiff", methods, mdiff_doc); |
|
202 | 203 | } |
|
203 | 204 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now