Mercurial > hg
annotate mercurial/diffhelpers.c @ 10281:e7d3b509af8b
Introduce check-code.py
check-code is a simple regex-based framework for checking our code and
tests for common style and portability errors. Currently, it knows a
fair amount about our Python and C style, and a little about common
shell script portability problems.
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Mon, 25 Jan 2010 00:05:22 -0600 |
parents | 9c59cdafcc24 |
children | 08a0f04b56bd |
rev | line source |
---|---|
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 /* |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
2 * diffhelpers.c - helper routines for mpatch |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
3 * |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 * Copyright 2007 Chris Mason <chris.mason@oracle.com> |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
5 * |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 * This software may be used and distributed according to the terms |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 * of the GNU General Public License v2, incorporated herein by reference. |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
8 */ |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
10 #include <Python.h> |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
11 #include <stdlib.h> |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
12 #include <string.h> |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
13 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 static char diffhelpers_doc[] = "Efficient diff parsing"; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 static PyObject *diffhelpers_Error; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 /* fixup the last lines of a and b when the patch has no newline at eof */ |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 static void _fix_newline(PyObject *hunk, PyObject *a, PyObject *b) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
21 int hunksz = PyList_Size(hunk); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
22 PyObject *s = PyList_GET_ITEM(hunk, hunksz-1); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
23 char *l = PyString_AS_STRING(s); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
24 int alen = PyList_Size(a); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
25 int blen = PyList_Size(b); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
26 char c = l[0]; |
10146
9c59cdafcc24
diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents:
10129
diff
changeset
|
27 PyObject *hline; |
9c59cdafcc24
diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents:
10129
diff
changeset
|
28 int sz = PyString_GET_SIZE(s); |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
29 |
10146
9c59cdafcc24
diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents:
10129
diff
changeset
|
30 if (sz > 1 && l[sz-2] == '\r') |
9c59cdafcc24
diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents:
10129
diff
changeset
|
31 /* tolerate CRLF in last line */ |
9c59cdafcc24
diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents:
10129
diff
changeset
|
32 sz -= 1; |
9c59cdafcc24
diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents:
10129
diff
changeset
|
33 hline = PyString_FromStringAndSize(l, sz-1); |
9c59cdafcc24
diffhelpers: fix variable declaration for MSVC (not C99)
Patrick Mezard <pmezard@gmail.com>
parents:
10129
diff
changeset
|
34 |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
35 if (c == ' ' || c == '+') { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
36 PyObject *rline = PyString_FromStringAndSize(l+1, sz-2); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
37 PyList_SetItem(b, blen-1, rline); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
38 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
39 if (c == ' ' || c == '-') { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
40 Py_INCREF(hline); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
41 PyList_SetItem(a, alen-1, hline); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
42 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
43 PyList_SetItem(hunk, hunksz-1, hline); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
44 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
45 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
46 /* python callable form of _fix_newline */ |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
47 static PyObject * |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
48 fix_newline(PyObject *self, PyObject *args) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
49 { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
50 PyObject *hunk, *a, *b; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
51 if (!PyArg_ParseTuple(args, "OOO", &hunk, &a, &b)) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
52 return NULL; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
53 _fix_newline(hunk, a, b); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
54 return Py_BuildValue("l", 0); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
55 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
56 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
57 /* |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
58 * read lines from fp into the hunk. The hunk is parsed into two arrays |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
59 * a and b. a gets the old state of the text, b gets the new state |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
60 * The control char from the hunk is saved when inserting into a, but not b |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
61 * (for performance while deleting files) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
62 */ |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
63 static PyObject * |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
64 addlines(PyObject *self, PyObject *args) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
65 { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
66 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
67 PyObject *fp, *hunk, *a, *b, *x; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
68 int i; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
69 int lena, lenb; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
70 int num; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
71 int todoa, todob; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
72 char *s, c; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
73 PyObject *l; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
74 if (!PyArg_ParseTuple(args, "OOiiOO", &fp, &hunk, &lena, &lenb, &a, &b)) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
75 return NULL; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
76 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
77 while(1) { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
78 todoa = lena - PyList_Size(a); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
79 todob = lenb - PyList_Size(b); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
80 num = todoa > todob ? todoa : todob; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
81 if (num == 0) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
82 break; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
83 for (i = 0 ; i < num ; i++) { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
84 x = PyFile_GetLine(fp, 0); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
85 s = PyString_AS_STRING(x); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
86 c = *s; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
87 if (strcmp(s, "\\ No newline at end of file\n") == 0) { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
88 _fix_newline(hunk, a, b); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
89 continue; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
90 } |
5483
ec2cc1dadbf7
Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents:
4897
diff
changeset
|
91 if (c == '\n') { |
ec2cc1dadbf7
Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents:
4897
diff
changeset
|
92 /* Some patches may be missing the control char |
ec2cc1dadbf7
Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents:
4897
diff
changeset
|
93 * on empty lines. Supply a leading space. */ |
ec2cc1dadbf7
Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents:
4897
diff
changeset
|
94 Py_DECREF(x); |
ec2cc1dadbf7
Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents:
4897
diff
changeset
|
95 x = PyString_FromString(" \n"); |
ec2cc1dadbf7
Handle patches with misformatted empty lines
Hollis Blanchard <hollisb@us.ibm.com>
parents:
4897
diff
changeset
|
96 } |
4897
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
97 PyList_Append(hunk, x); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
98 if (c == '+') { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
99 l = PyString_FromString(s + 1); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
100 PyList_Append(b, l); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
101 Py_DECREF(l); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
102 } else if (c == '-') { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
103 PyList_Append(a, x); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
104 } else { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
105 l = PyString_FromString(s + 1); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
106 PyList_Append(b, l); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
107 Py_DECREF(l); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
108 PyList_Append(a, x); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
109 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
110 Py_DECREF(x); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
111 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
112 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
113 return Py_BuildValue("l", 0); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
114 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
115 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
116 /* |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
117 * compare the lines in a with the lines in b. a is assumed to have |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
118 * a control char at the start of each line, this char is ignored in the |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
119 * compare |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
120 */ |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
121 static PyObject * |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
122 testhunk(PyObject *self, PyObject *args) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
123 { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
124 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
125 PyObject *a, *b; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
126 long bstart; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
127 int alen, blen; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
128 int i; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
129 char *sa, *sb; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
130 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
131 if (!PyArg_ParseTuple(args, "OOl", &a, &b, &bstart)) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
132 return NULL; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
133 alen = PyList_Size(a); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
134 blen = PyList_Size(b); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
135 if (alen > blen - bstart) { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
136 return Py_BuildValue("l", -1); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
137 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
138 for (i = 0 ; i < alen ; i++) { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
139 sa = PyString_AS_STRING(PyList_GET_ITEM(a, i)); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
140 sb = PyString_AS_STRING(PyList_GET_ITEM(b, i + bstart)); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
141 if (strcmp(sa+1, sb) != 0) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
142 return Py_BuildValue("l", -1); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
143 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
144 return Py_BuildValue("l", 0); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
145 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
146 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
147 static PyMethodDef methods[] = { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
148 {"addlines", addlines, METH_VARARGS, "add lines to a hunk\n"}, |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
149 {"fix_newline", fix_newline, METH_VARARGS, "fixup newline counters\n"}, |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
150 {"testhunk", testhunk, METH_VARARGS, "test lines in a hunk\n"}, |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
151 {NULL, NULL} |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
152 }; |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
153 |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
154 PyMODINIT_FUNC |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
155 initdiffhelpers(void) |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
156 { |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
157 Py_InitModule3("diffhelpers", methods, diffhelpers_doc); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
158 diffhelpers_Error = PyErr_NewException("diffhelpers.diffhelpersError", |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
159 NULL, NULL); |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
160 } |
4574925db5c0
Add Chris Mason's mpatch library.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
161 |