Skip to content

Instantly share code, notes, and snippets.

@cablehead
Created January 18, 2012 07:18
Show Gist options
  • Save cablehead/1631761 to your computer and use it in GitHub Desktop.
Save cablehead/1631761 to your computer and use it in GitHub Desktop.
from diffmatchpatch import diff_match_patch
DMP = diff_match_patch()
def diff(a, b):
diffs = DMP.diff_main(a, b)
if len(diffs) > 2:
DMP.diff_cleanupEfficiency(diffs)
if len(diffs) > 0:
delta = DMP.diff_toDelta(diffs)
return delta
def patch(a, d):
diffs = DMP.diff_fromDelta(a, d)
patches = DMP.patch_make(a, diffs)
result = DMP.patch_apply(patches, a)
return result[0]
def diffpatch(a, b):
d = diff(a, b)
print d
end = patch(a, d)
print repr(d), end == b
# diffpatch(u'\ud83d\udc7f y', u'\ud83d\udc7f x')
# diffpatch(u"\U0001f47f y", u"\U0001f47f x")
diffpatch(u"\U0001f47f y".encode('utf-8'), u"\U0001f47f x".encode('utf-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment