@@ -135,4 +135,82 @@ describe("Testing utils module", function()
135135 eq (utils .strsplit (" ,foo,bar," , " ," ), { " " , " foo" , " bar" , " " })
136136 eq (utils .strsplit (" foobar" , " ," ), { " foobar" })
137137 end )
138+
139+ it (" regex_strip_anchors" , function ()
140+ -- nil input
141+ eq ({ utils .regex_strip_anchors (nil ) }, { [2 ] = 0 , [3 ] = 0 })
142+ -- empty string
143+ eq ({ utils .regex_strip_anchors (" " ) }, { " " , 0 , 0 })
144+ -- no anchors
145+ eq ({ utils .regex_strip_anchors (" foobar" ) }, { " foobar" , 0 , 0 })
146+ -- only start anchor
147+ eq ({ utils .regex_strip_anchors (" ^foobar" ) }, { " foobar" , 1 , 0 })
148+ -- only end anchor
149+ eq ({ utils .regex_strip_anchors (" foobar$" ) }, { " foobar" , 0 , 1 })
150+ -- both anchors
151+ eq ({ utils .regex_strip_anchors (" ^foobar$" ) }, { " foobar" , 1 , 1 })
152+ -- anchors in middle (should not be stripped)
153+ eq ({ utils .regex_strip_anchors (" foo^bar$baz" ) }, { " foo^bar$baz" , 0 , 0 })
154+ -- only anchors
155+ eq ({ utils .regex_strip_anchors (" ^" ) }, { " " , 1 , 0 })
156+ eq ({ utils .regex_strip_anchors (" $" ) }, { " " , 0 , 1 })
157+ eq ({ utils .regex_strip_anchors (" ^$" ) }, { " " , 1 , 1 })
158+ -- single character with anchors
159+ eq ({ utils .regex_strip_anchors (" ^x$" ) }, { " x" , 1 , 1 })
160+ -- should not strip escaped $ (prepended by odd number of backslashes)
161+ eq ({ utils .regex_strip_anchors ([[ x\$]] ) }, { [[ x\$]] , 0 , 0 })
162+ eq ({ utils .regex_strip_anchors ([[ x\\$]] ) }, { [[ x\\]] , 0 , 1 })
163+ eq ({ utils .regex_strip_anchors ([[ x\\\$]] ) }, { [[ x\\\$]] , 0 , 0 })
164+ end )
165+
166+ it (" ctag_match" , function ()
167+ -- no slashes
168+ eq (utils .ctag_match (" foobar" ), nil )
169+ -- single slash (needs at least 2 slashes)
170+ eq (utils .ctag_match (" foo/bar" ), nil )
171+ -- anchored slahes
172+ eq ({ utils .ctag_match (" /foo/" ) }, { 1 , 5 })
173+ -- two slashes (finds the two rightmost unescaped slashes)
174+ eq ({ utils .ctag_match (" /foo/bar/baz" ) }, { 5 , 9 })
175+ -- escaped slash only (should ignore escaped ones)
176+ eq (utils .ctag_match ([[ /foo\/bar]] ), nil )
177+ eq (utils .ctag_match ([[ /foo\\\/bar]] ), nil )
178+ -- escaped slash backslash before slash
179+ eq ({ utils .ctag_match ([[ /foo\\/bar]] ) }, { 1 , 7 })
180+ eq ({ utils .ctag_match ([[ /foo\\\\/bar]] ) }, { 1 , 9 })
181+ -- mixed escaped and unescaped
182+ eq ({ utils .ctag_match ([[ foo/bar\/baz/qux]] ) }, { 4 , 13 })
183+ -- reverse search from end
184+ eq ({ utils .ctag_match (" /a/b/c/d" ) }, { 5 , 7 })
185+ end )
186+
187+ it (" ctag_escape" , function ()
188+ -- empty string
189+ eq (utils .ctag_escape (" " ), " " )
190+ -- no special characters
191+ eq (utils .ctag_escape (" foobar" ), " foobar" )
192+ -- escaped backslash (\\ becomes \, then rg_escape doubles it back)
193+ eq (utils .ctag_escape (" foo\\ bar" ), " foo\\\\ bar" )
194+ -- unescape escaped slash (\/ becomes /)
195+ eq (utils .ctag_escape (" foo\\ /bar" ), " foo/bar" )
196+ -- regex escape (rg_escape behavior)
197+ eq (utils .ctag_escape (" foo.bar" ), " foo\\ .bar" )
198+ eq (utils .ctag_escape (" foo*bar" ), " foo\\ *bar" )
199+ -- ^ at start gets unescaped (was escaped by rg_escape)
200+ eq (utils .ctag_escape (" ^foobar" ), " ^foobar" )
201+ -- $ at end gets unescaped (was escaped by rg_escape)
202+ eq (utils .ctag_escape (" foobar$" ), " foobar$" )
203+ -- already escaped ^ at start stays escaped (rg_escape adds another backslash)
204+ eq (utils .ctag_escape (" \\ ^foobar" ), " \\\\\\ ^foobar" )
205+ -- already escaped $ at end stays escaped
206+ eq (utils .ctag_escape (" foobar\\ $" ), " foobar\\\\ $" )
207+ -- ^ in middle stays escaped
208+ eq (utils .ctag_escape (" foo^bar" ), " foo\\ ^bar" )
209+ -- $ in middle stays escaped
210+ eq (utils .ctag_escape (" foo$bar" ), " foo\\ $bar" )
211+ -- combination of patterns
212+ eq (utils .ctag_escape (" ^foo.bar$" ), " ^foo\\ .bar$" )
213+ -- escaped backslash before dot (\. becomes ., then rg_escape escapes both)
214+ eq (utils .ctag_escape (" foo\\ .bar" ), " foo\\\\\\ .bar" )
215+ end )
138216end )
0 commit comments