You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inconsistency of IndexError vs nil for unknown capture group #139
This is proving problematic on TruffleRuby.
We would like to upstream our pure-Ruby implementation of StringScanner, but to do so we want to avoid exposing internals as much as possible.
Things like p->regex is something I don't want to expose to code living outside the truffleruby repository.
Having to workaround by keeping extra state for whether the last match was with a String or a Regexp feels very messy and wrong semantically.
Could the behavior be unified for both of these cases?
Given the Regexp-matching methods predate the String ones, I think it would be best to have IndexError in both cases.
Using a String pattern should be equivalent to a Regexp in StringScanner & transparent.
Though returning nil for both cases can also be implemented easily on TruffleRuby.
The worse is the mix of both which exposes internal details of the StringScanner implementation.
Returning nil looks consistent with handling a Regexp pattern. But is inconsistent with handling a String pattern after successful matching (when IndexError is raised).
There is always a "result of the last StringScanner match" (some kind of MatchData internally or nil) whether matched against String pattern or Regexp pattern
If the last match failed, the last result is nil, then [] returns nil for any group
If the last match succeeded, then use the semantics of MatchData#[] or equivalent, which raises IndexError on missing group. For a String pattern there are of course no named groups.
Activity
eregon commentedon Feb 18, 2025
Given the Regexp-matching methods predate the String ones, I think it would be best to have IndexError in both cases.
Using a String pattern should be equivalent to a Regexp in StringScanner & transparent.
Though returning
nilfor both cases can also be implemented easily on TruffleRuby.The worse is the mix of both which exposes internal details of the StringScanner implementation.
BTW this inconsistency seems to have been introduced in a0ff956 which is ruby/ruby@90311f3 and https://bugs.ruby-lang.org/issues/13759
kou commentedon Feb 19, 2025
OK. Let's use
IndexErrorforStringtoo.@naitoh Do you want to work on this?
naitoh commentedon Feb 19, 2025
Yes, I would like to try this issue.
andrykonchin commentedon Feb 21, 2025
Wondering how
#[name]should behave when a String pattern is used but matching failed - to raiseIndexErroror to returnnil.When a Regexp pattern is used
#[name]returnsnil:Returning
nillooks consistent with handling a Regexp pattern. But is inconsistent with handling a String pattern after successful matching (whenIndexErroris raised).eregon commentedon Feb 21, 2025
I think the logic should be:
nil) whether matched against String pattern or Regexp pattern[]returnsnilfor any groupMatchData#[]or equivalent, which raisesIndexErroron missing group. For a String pattern there are of course no named groups.kou commentedon Feb 22, 2025
Let's use the same behavior as a Regexp pattern.
Fix a bug that inconsistency of IndexError vs nil for unknown capture…
Fix a bug that inconsistency of IndexError vs nil for unknown capture…
Fix a bug that inconsistency of IndexError vs nil for unknown capture…
Fix a bug that inconsistency of IndexError vs nil for unknown capture…
Fix a bug that inconsistency of IndexError vs nil for unknown capture…
[ruby/strscan] Fix a bug that inconsistency of IndexError vs nil for
[ruby/strscan] Fix a bug that inconsistency of IndexError vs nil for