diff options
author | NAITOH Jun <[email protected]> | 2025-02-23 16:52:32 +0900 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2025-02-25 15:36:46 +0900 |
commit | 018943ba0557cb78d429caf4a6d536faa94a95ca (patch) | |
tree | ba45094470610ff0f8d1d9c868e259162ae82f7f | |
parent | 36ab247e4d2fa7fa82142d9f70b340b28da05274 (diff) |
[ruby/strscan] Fix a bug that inconsistency of IndexError vs nil for
unknown capture group
(https://github.com/ruby/strscan/pull/143)
Fix https://github.com/ruby/strscan/pull/139
Reported by Benoit Daloze. Thanks!!!
https://github.com/ruby/strscan/commit/bc8a0d2623
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/12804
-rw-r--r-- | ext/strscan/strscan.c | 23 | ||||
-rw-r--r-- | test/strscan/test_stringscanner.rb | 34 |
2 files changed, 39 insertions, 18 deletions
diff --git a/ext/strscan/strscan.c b/ext/strscan/strscan.c index 86a60280ca..2824eeb4d9 100644 --- a/ext/strscan/strscan.c +++ b/ext/strscan/strscan.c @@ -1667,19 +1667,17 @@ strscan_matched_size(VALUE self) static int name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end, rb_encoding *enc) { - int num; - - num = onig_name_to_backref_number(RREGEXP_PTR(regexp), - (const unsigned char* )name, (const unsigned char* )name_end, regs); - if (num >= 1) { - return num; - } - else { - rb_enc_raise(enc, rb_eIndexError, "undefined group name reference: %.*s", - rb_long2int(name_end - name), name); + if (RTEST(regexp)) { + int num = onig_name_to_backref_number(RREGEXP_PTR(regexp), + (const unsigned char* )name, + (const unsigned char* )name_end, + regs); + if (num >= 1) { + return num; + } } - - UNREACHABLE; + rb_enc_raise(enc, rb_eIndexError, "undefined group name reference: %.*s", + rb_long2int(name_end - name), name); } /* @@ -1768,7 +1766,6 @@ strscan_aref(VALUE self, VALUE idx) idx = rb_sym2str(idx); /* fall through */ case T_STRING: - if (!RTEST(p->regex)) return Qnil; RSTRING_GETMEM(idx, name, i); i = name_to_backref_number(&(p->regs), p->regex, name, name + i, rb_enc_get(idx)); break; diff --git a/test/strscan/test_stringscanner.rb b/test/strscan/test_stringscanner.rb index ece6ceef6f..93bc9bba8d 100644 --- a/test/strscan/test_stringscanner.rb +++ b/test/strscan/test_stringscanner.rb @@ -458,7 +458,31 @@ module StringScannerTests def test_AREF s = create_string_scanner('stra strb strc') - s.scan(/\w+/) + s.scan(/\s+/) + assert_nil( s[-2]) + assert_nil( s[-1]) + assert_nil( s[0]) + assert_nil( s[1]) + assert_nil( s[:c]) + assert_nil( s['c']) + + s.scan("not match") + assert_nil( s[-2]) + assert_nil( s[-1]) + assert_nil( s[0]) + assert_nil( s[1]) + assert_nil( s[:c]) + assert_nil( s['c']) + + s.check(/\w+/) + assert_nil( s[-2]) + assert_equal('stra', s[-1]) + assert_equal('stra', s[0]) + assert_nil( s[1]) + assert_raise(IndexError) { s[:c] } + assert_raise(IndexError) { s['c'] } + + s.scan("stra") assert_nil( s[-2]) assert_equal('stra', s[-1]) assert_equal('stra', s[0]) @@ -903,11 +927,11 @@ module StringScannerTests s = create_string_scanner('abc') s.get_byte - assert_nil(s[:c]) - assert_nil(s["c"]) + assert_raise(IndexError) { s[:c] } + assert_raise(IndexError) { s['c'] } s.getch - assert_nil(s[:c]) - assert_nil(s["c"]) + assert_raise(IndexError) { s[:c] } + assert_raise(IndexError) { s['c'] } end def test_size |