Add ingo#regexp#deconstruct#RemoveMultis()

This commit is contained in:
Ingo Karkat
2018-08-15 14:14:56 +02:00
parent 8977c63983
commit 4c92ff2fa9
2 changed files with 29 additions and 0 deletions
+18
View File
@@ -27,6 +27,24 @@ function! ingo#regexp#deconstruct#RemovePositionAtoms( pattern )
return substitute(a:pattern, '\%(\%(^\|[^\\]\)\%(\\\\\)*\\\)\@<!\%(\\\%([\^<>]\|_\^\|_\$\|%[\^$V#]\|%[<>]\?''.\|%[<>]\?\d\+[lcv]\)\|[\^$]\)', '', 'g')
endfunction
function! ingo#regexp#deconstruct#RemoveMultis( pattern )
"******************************************************************************
"* PURPOSE:
" Remove multi items (*, \+, etc.) that signify the multiplicity of the
" previous atom from a:pattern.
"* ASSUMPTIONS / PRECONDITIONS:
" Does not consider "very magic" (/\v)-style syntax. If you may have this,
" convert via ingo#regexp#magic#Normalize() first.
"* EFFECTS / POSTCONDITIONS:
" None.
"* INPUTS:
" a:pattern regular expression
"* RETURN VALUES:
" Modified a:pattern with multi items removed.
"******************************************************************************
return substitute(a:pattern, '\%(\%(^\|[^\\]\)\%(\\\\\)*\\\)\@<!\%(\*\|\\[+=?]\|\\{-\?\d*,\?\d*}\|\\@\%(>\|=\|!\|<=\|<!\)\)', '', 'g')
endfunction
let s:specialLookup = {
\ 'e': "\e",
\ 't': "\t",
@@ -0,0 +1,11 @@
" Test removing multis.
call vimtest#StartTap()
call vimtap#Plan(4)
call vimtap#Is(ingo#regexp#deconstruct#RemoveMultis('foobar'), 'foobar', 'no multis')
call vimtap#Is(ingo#regexp#deconstruct#RemoveMultis('fo\{1,10}bar\? .* l\([aeiou]\)ll\1'), 'fobar . l\([aeiou]\)ll\1', '\? * \{} multis')
call vimtap#Is(ingo#regexp#deconstruct#RemoveMultis('fo\{-}ba\{-1}r\{-,10} \{2,7}x\\{1,2}'), 'fobar x\\{1,2}', 'various \{} multis')
call vimtap#Is(ingo#regexp#deconstruct#RemoveMultis('a\@<= foo\(bar\)\@=bar'), 'a foo\(bar\)bar', '\@= multis')
call vimtest#Quit()