Rewrite of s:SplitDelim

The original implementation had some serious problems with delimiters
that were allowed to match with zero-width.  This should correct
https://github.com/godlygeek/tabular/issues/3 - please let me know if
you find any regressions!
This commit is contained in:
Matt Wozniski 2011-03-28 01:35:37 -04:00
parent ae01c3df35
commit 4b543cda52

View File

@ -70,38 +70,38 @@ endfunction
function! s:SplitDelim(string, delim) function! s:SplitDelim(string, delim)
let rv = [] let rv = []
let beg = 0 let beg = 0
let idx = 0
let len = len(a:string) let len = len(a:string)
let searchoff = 0
while 1 while 1
let mid = match(a:string, a:delim, beg, 1) let mid = match(a:string, a:delim, beg + searchoff, 1)
if mid == -1 || mid == len if mid == -1 || mid == len
break break
endif endif
let matchstr = matchstr(a:string, a:delim, beg, 1) let matchstr = matchstr(a:string, a:delim, beg + searchoff, 1)
let length = strlen(matchstr) let length = strlen(matchstr)
if beg < mid if length == 0 && beg == mid
let rv += [ a:string[beg : mid-1] ] " Zero-length match for a zero-length delimiter - advance past it
else let searchoff += 1
let rv += [ "" ] continue
endif endif
let beg = mid + length
let idx = beg
if beg == mid if beg == mid
" Empty match, advance "beg" by one to avoid infinite loop
let rv += [ "" ] let rv += [ "" ]
let beg += 1 else
else " beg > mid let rv += [ a:string[beg : mid-1] ]
let rv += [ a:string[mid : beg-1] ]
endif endif
let rv += [ matchstr ]
let beg = mid + length
let searchoff = 0
endwhile endwhile
let rv += [ strpart(a:string, idx) ] let rv += [ strpart(a:string, beg) ]
return rv return rv
endfunction endfunction