Skip to content

Instantly share code, notes, and snippets.

@cablehead
Created January 18, 2012 19:10
Show Gist options
  • Save cablehead/1634975 to your computer and use it in GitHub Desktop.
Save cablehead/1634975 to your computer and use it in GitHub Desktop.
import base64
from diffmatchpatch import diff_match_patch
DMP = diff_match_patch()
def diff(a, b):
a = base64.b64encode(a.encode('utf-8'))
b = base64.b64encode(b.encode('utf-8'))
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):
a = base64.b64encode(a.encode('utf-8'))
diffs = DMP.diff_fromDelta(a, d)
patches = DMP.patch_make(a, diffs)
result = DMP.patch_apply(patches, a)
return base64.b64decode(result[0]).decode('utf-8')
client_a = u'\ud83d\udc7f 1'
client_b = u'\ud83d\udc7f 2'
client_diff = diff(client_a, client_b)
server_a = u'\U0001f47f 1'
server_b = u'\U0001f47f 2'
server_diff = diff(server_a, server_b)
print client_diff == server_diff
print repr(patch(server_a, client_diff))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment