Skip to content

Commit

Permalink
add a new kwarg to return the diff as a big string primarily for debu…
Browse files Browse the repository at this point in the history
…gging purposes
  • Loading branch information
JoshData committed Jun 27, 2014
1 parent b68ff27 commit 8da99d5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
Binary file modified build/lib.linux-x86_64-2.7/diff_match_patch.so
Binary file not shown.
Binary file modified build/lib.linux-x86_64-3.4/diff_match_patch.cpython-34m.so
Binary file not shown.
16 changes: 14 additions & 2 deletions interface.cpp
Expand Up @@ -18,6 +18,7 @@ diff_match_patch_diff(PyObject *self, PyObject *args, PyObject *kwargs)
int checklines = 1;
int cleanupSemantic = 1;
int counts_only = 1;
int as_patch = 0;
char format_spec[64];

static char *kwlist[] = {
Expand All @@ -27,13 +28,14 @@ diff_match_patch_diff(PyObject *self, PyObject *args, PyObject *kwargs)
strdup("checklines"),
strdup("cleanup_semantic"),
strdup("counts_only"),
strdup("as_patch"),
NULL };

sprintf(format_spec, "%c%c|fbbb", FMTSPEC, FMTSPEC);
sprintf(format_spec, "%c%c|fbbbb", FMTSPEC, FMTSPEC);
if (!PyArg_ParseTupleAndKeywords(args, kwargs, format_spec, kwlist,
&a, &b,
&timelimit, &checklines, &cleanupSemantic,
&counts_only))
&counts_only, &as_patch))
return NULL;

PyObject *ret = PyList_New(0);
Expand All @@ -52,6 +54,16 @@ diff_match_patch_diff(PyObject *self, PyObject *args, PyObject *kwargs)
if (cleanupSemantic)
dmp.diff_cleanupSemantic(diff);

if (as_patch) {
typename DMP::Patches patch = dmp.patch_make(a, diff);
CPPTYPE patch_str = dmp.patch_toText(patch);

if (FMTSPEC == 'u')
return PyUnicode_FromUnicode((Py_UNICODE*)patch_str.data(), patch_str.size());
else
return PyString_FromStringAndSize((const char*)patch_str.data(), patch_str.size());
}

typename std::list<typename DMP::Diff>::const_iterator entryiter;
for (entryiter = diff.begin(); entryiter != diff.end(); entryiter++) {
typename DMP::Diff entry = *entryiter;
Expand Down
4 changes: 4 additions & 0 deletions test.py
Expand Up @@ -14,6 +14,10 @@ def test(text1, text2, diff_function):
print ("<", repr(text1))
print (">", repr(text2))
print()

print (diff_function(text1, text2, checklines=False, cleanup_semantic=True, as_patch=True))
print()

changes = diff_function(
text1, text2,
timelimit=15,
Expand Down

0 comments on commit 8da99d5

Please sign in to comment.