def sededit(a, b, context=0):
'''
Take two strings and output a sed-like diff
'''
if a == b:
return ''
a_len = len(a)
b_len = len(b)
start1, end1, start2, end2 = a_len, 0, b_len, 0
s = difflib.SequenceMatcher(None, a, b)
for tag, i1, i2, j1, j2 in s.get_opcodes():
if tag == 'equal':
continue
elif tag == 'insert':
ins = 1
else:
ins = 0
start1 = max(min(i1-context-ins, start1), 0)
start2 = max(min(j1-context-ins, start2), 0)
end1 = min(max(i2+context+ins, end1), a_len)
end2 = min(max(j2+context+ins, end2), b_len)
return 's/%s%s%s/%s/' % (
('' if start1 else '^'), a[start1:end1],
('$' if end1 == a_len else ''), b[start2:end2])
评论列表
文章目录