單字符字符串是邊緣情況。對於兩個或多個字符,插入一個字符始終可以正確檢測。下面是一個簡單的算法來證明:
import difflib
def show_diffs(limit):
characters = 'abcdefghijklmnopqrstuvwxyz'
differ = difflib.Differ()
for length in range(1, limit + 1):
for pos in range(0, length + 1):
line_a = characters[:length]
line_b = line_a[:pos] + 'A' + line_a[pos:]
diff = list(differ.compare([line_a], [line_b]))
if len(diff) == 2 and diff[0][0] == '-' and diff[1][0] == '+':
marker = 'N' # Insertion not detected
elif len(diff) == 3 and diff[0][0] == '-' and diff[1][0] == '+' and diff[2][0] == '?':
marker = 'Y' # Insertion detected
else:
print('ERROR: unexpected diff for %r -> %r:\n%r' % (line_a, line_b, diff))
return
print('%s %r -> %r' % (marker, line_a, line_b))
show_diffs(limit=3)
據「失敗」只爲1個字符的字符串:
N 'a' -> 'Aa'
N 'a' -> 'aA'
Y 'ab' -> 'Aab'
Y 'ab' -> 'aAb'
Y 'ab' -> 'abA'
Y 'abc' -> 'Aabc'
Y 'abc' -> 'aAbc'
Y 'abc' -> 'abAc'
Y 'abc' -> 'abcA'
由於生病只是處理這種邊緣情況 – ealeon