Fix off-by-one error in LCS calculation

This commit is contained in:
Andy Stewart
2019-09-24 17:10:26 +01:00
parent d2796a277e
commit ccd4972d23
2 changed files with 3 additions and 2 deletions

View File

@@ -135,11 +135,11 @@ function! s:lcs(s1, s2)
for i in range(1, len(a:s1))
for j in range(1, len(a:s2))
if a:s1[i] ==# a:s2[j]
if a:s1[i-1] ==# a:s2[j-1]
let matrix[i][j] = 1 + matrix[i-1][j-1]
if matrix[i][j] > maxlength
let maxlength = matrix[i][j]
let endindex = i
let endindex = i - 1
endif
endif
endfor

View File

@@ -1026,4 +1026,5 @@ function Test_lcs()
call assert_equal('', gitgutter#diff_highlight#lcs('', 'foo'))
call assert_equal('', gitgutter#diff_highlight#lcs('foo', ''))
call assert_equal('bar', gitgutter#diff_highlight#lcs('foobarbaz', 'bbart'))
call assert_equal('transaction', gitgutter#diff_highlight#lcs('transaction.unexplained_amount', 'amount(transaction)'))
endfunction