From 4b543cda526e39e6fecc4f894a1e2f4e6d090eed Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Mon, 28 Mar 2011 01:35:37 -0400 Subject: [PATCH] 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! --- autoload/tabular.vim | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/autoload/tabular.vim b/autoload/tabular.vim index a3b8432..f12ea34 100644 --- a/autoload/tabular.vim +++ b/autoload/tabular.vim @@ -70,38 +70,38 @@ endfunction function! s:SplitDelim(string, delim) let rv = [] let beg = 0 - let idx = 0 let len = len(a:string) + let searchoff = 0 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 break endif - let matchstr = matchstr(a:string, a:delim, beg, 1) + let matchstr = matchstr(a:string, a:delim, beg + searchoff, 1) let length = strlen(matchstr) - if beg < mid - let rv += [ a:string[beg : mid-1] ] - else - let rv += [ "" ] + if length == 0 && beg == mid + " Zero-length match for a zero-length delimiter - advance past it + let searchoff += 1 + continue endif - let beg = mid + length - let idx = beg - if beg == mid - " Empty match, advance "beg" by one to avoid infinite loop let rv += [ "" ] - let beg += 1 - else " beg > mid - let rv += [ a:string[mid : beg-1] ] + else + let rv += [ a:string[beg : mid-1] ] endif + + let rv += [ matchstr ] + + let beg = mid + length + let searchoff = 0 endwhile - let rv += [ strpart(a:string, idx) ] + let rv += [ strpart(a:string, beg) ] return rv endfunction