forked from JoshData/fast_diff_match_patch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.cpp
More file actions
51 lines (39 loc) · 1.53 KB
/
interface.cpp
File metadata and controls
51 lines (39 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <Python.h>
#include <QtCore>
#include "diff_match_patch.h"
static PyObject *
diff_match_patch_diff(PyObject *self, PyObject *args, PyObject *kwargs)
{
const char *a, *b;
float timelimit = 0.0;
int checklines = 1;
static char *kwlist[] = { "left_document", "right_document", "timelimit", "checklines", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ss|fb", kwlist,
&a, &b, &timelimit, &checklines))
return NULL;
PyObject *ret = PyList_New(0);
PyObject *opcodes[3];
opcodes[DELETE] = PyString_InternFromString("-");
opcodes[INSERT] = PyString_InternFromString("+");
opcodes[EQUAL] = PyString_InternFromString("=");
diff_match_patch dmp = diff_match_patch();
dmp.Diff_Timeout = timelimit;
QList<Diff> diff = dmp.diff_main(QString::fromUtf8(a), QString::fromUtf8(b), checklines);
foreach(Diff entry, diff) {
PyObject* tuple = PyTuple_New(2);
PyTuple_SetItem(tuple, 0, opcodes[entry.operation]);
PyTuple_SetItem(tuple, 1, PyInt_FromLong(entry.text.length()));
PyList_Append(ret, tuple);
}
return ret;
}
static PyMethodDef SpamMethods[] = {
{"diff", (PyObject* (*)(PyObject*, PyObject*))diff_match_patch_diff, METH_VARARGS|METH_KEYWORDS,
"Compute the difference between two UTF-8 strings. Returns a list of tuples (OP, LEN)."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
initdiff_match_patch(void)
{
(void) Py_InitModule("diff_match_patch", SpamMethods);
}