diff --git a/lc.vim b/lc.vim index 59703a9..e784061 100644 --- a/lc.vim +++ b/lc.vim @@ -5,26 +5,29 @@ class Item const val: string endclass -def GetLongestSuffix(hay: string, ndl: string): string +def GetLongestSuffix(hay: string, ndl: string): tuple var s = ndl var l = len(ndl) - while stridx(hay, s) == -1 - s = strpart(s, 1) + while true + const idx = stridx(hay, s) + if idx != -1 + return (s, idx) + endif l = l - 1 if l == 0 - return "" + break endif + s = strpart(s, 1) endwhile - return s + return ("", 0) enddef -# echo GetLongestSuffix("hello world", "world")->assert_equal("world") -# echo GetLongestSuffix("hello world", "foo world")->assert_equal("o world") -# echo GetLongestSuffix("hello world foo bar blah", "foo world")->assert_equal("o world") +# echo GetLongestSuffix("hello world", "world")->assert_equal(("world", 6)) +# echo GetLongestSuffix("hello world", "foo world")->assert_equal(("o world", 4)) +# echo GetLongestSuffix("hello world foo bar blah", "foo world")->assert_equal(("o world", 4)) # echo v:errors def GetMatch(base: string, line: string): Item - const s = GetLongestSuffix(line, base) - const idx = stridx(line, s) + const [s, idx] = GetLongestSuffix(line, base) const slen = strlen(s) const m = strpart(line, idx + slen) return Item.new(slen, base .. m)