diff-match-patch icon indicating copy to clipboard operation
diff-match-patch copied to clipboard

old + delta => new

Open josephernest opened this issue 5 years ago • 0 comments

import diff_match_patch as dmp_module

old = "hello how are you? very good and you? blablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablabla. thanks for asking! this text will be removed."
delta = "+will be removed.\t=138\t+new text here.\t=19\t-27"

dmp = dmp_module.diff_match_patch()
new = dmp.patch_apply(dmp.patch_make(old, dmp.diff_fromDelta(old, delta)), old)[0]
print(new)

Isn't there a simpler way to apply a delta to an input string, rather than doing the complex :

  • delta -> diff with diff_fromDelta (uses old once)
  • diff -> patch with patch_make (uses old again)
  • patch -> output string with patch_apply (uses old a third time!)

i.e.

dmp.patch_apply(dmp.patch_make(old, dmp.diff_fromDelta(old, delta)), old)

This seems to be optimal in terms of byte size (if the two strings are 200 KB and changes are only 20 bytes, delta will be only < 50 bytes, which is good for transfer over network)...

... but probably not in terms of CPU: if old is a big string, it will be processed three times by the previous functions!

Isn't there a better way to apply a delta to a string?

josephernest avatar Feb 18 '20 10:02 josephernest