Avoid doing stridx twice
This commit is contained in:
23
lc.vim
23
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<string, number>
|
||||
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)
|
||||
|
Reference in New Issue
Block a user