crystal icon indicating copy to clipboard operation
crystal copied to clipboard

`Regex` match methods do not accept negative position

Open straight-shoota opened this issue 2 years ago • 3 comments

A negative index into a string usually means to start counting from the end ("foo"[-2..] == "oo"). This does not work for Regex's match methods though: /oo/.match("foo", -2) == nil.

I think it makes sense to add this to the implementation for consistency.

straight-shoota avatar Nov 27 '22 14:11 straight-shoota

Regex#match depends on String#char_index_to_byte_index which doesn't support negative arguments. Not sure about the rest of Regex though

HertzDevil avatar Nov 27 '22 14:11 HertzDevil

Btw. the equivalent works in Ruby: /oo/.match?("foo", -2) # => true

straight-shoota avatar Nov 27 '22 14:11 straight-shoota

There are actually two notions of an index; character indices depend on String as mentioned above, byte indices should only require single-line additions to Regex#match_at_byte_index and #matches_at_byte_index?.

HertzDevil avatar Apr 21 '24 17:04 HertzDevil