diff-match-patch
diff-match-patch copied to clipboard
old + delta => new
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->diffwithdiff_fromDelta(usesoldonce)diff->patchwithpatch_make(usesoldagain)patch-> output string withpatch_apply(usesolda 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?