Skip to content

Commit 48a7359

Browse files
committed
Fix a bug that inconsistency of IndexError vs nil for unknown capture group
Fix GH-139
1 parent b957443 commit 48a7359

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,12 +751,14 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject idx) {
751751
return context.nil;
752752
}
753753

754+
Ruby runtime = context.runtime;
755+
754756
if (idx instanceof RubySymbol || idx instanceof RubyString) {
755-
if (pattern == null) return context.nil;
757+
if (pattern == null) {
758+
throw runtime.newRaiseException((RubyClass) getMetaClass().getConstant("IndexError"), "undefined group name reference: " + idx);
759+
}
756760
}
757761

758-
Ruby runtime = context.runtime;
759-
760762
int i = RubyMatchData.backrefNumber(runtime, pattern, regs, idx);
761763

762764
return extractRegion(context, i);

ext/strscan/strscan.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,17 +1669,15 @@ name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name
16691669
{
16701670
int num;
16711671

1672-
num = onig_name_to_backref_number(RREGEXP_PTR(regexp),
1673-
(const unsigned char* )name, (const unsigned char* )name_end, regs);
1674-
if (num >= 1) {
1675-
return num;
1676-
}
1677-
else {
1678-
rb_enc_raise(enc, rb_eIndexError, "undefined group name reference: %.*s",
1679-
rb_long2int(name_end - name), name);
1672+
if (RTEST(regexp)){
1673+
num = onig_name_to_backref_number(RREGEXP_PTR(regexp),
1674+
(const unsigned char* )name, (const unsigned char* )name_end, regs);
1675+
if (num >= 1) {
1676+
return num;
1677+
}
16801678
}
1681-
1682-
UNREACHABLE;
1679+
rb_enc_raise(enc, rb_eIndexError, "undefined group name reference: %.*s",
1680+
rb_long2int(name_end - name), name);
16831681
}
16841682

16851683
/*
@@ -1768,7 +1766,6 @@ strscan_aref(VALUE self, VALUE idx)
17681766
idx = rb_sym2str(idx);
17691767
/* fall through */
17701768
case T_STRING:
1771-
if (!RTEST(p->regex)) return Qnil;
17721769
RSTRING_GETMEM(idx, name, i);
17731770
i = name_to_backref_number(&(p->regs), p->regex, name, name + i, rb_enc_get(idx));
17741771
break;

test/strscan/test_stringscanner.rb

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,31 @@ def test_matched_string
458458
def test_AREF
459459
s = create_string_scanner('stra strb strc')
460460

461-
s.scan(/\w+/)
461+
s.scan(/\s+/)
462+
assert_nil( s[-2])
463+
assert_nil( s[-1])
464+
assert_nil( s[0])
465+
assert_nil( s[1])
466+
assert_nil( s[:c])
467+
assert_nil( s['c'])
468+
469+
s.scan("not match")
470+
assert_nil( s[-2])
471+
assert_nil( s[-1])
472+
assert_nil( s[0])
473+
assert_nil( s[1])
474+
assert_nil( s[:c])
475+
assert_nil( s['c'])
476+
477+
s.check(/\w+/)
478+
assert_nil( s[-2])
479+
assert_equal('stra', s[-1])
480+
assert_equal('stra', s[0])
481+
assert_nil( s[1])
482+
assert_raise(IndexError) { s[:c] }
483+
assert_raise(IndexError) { s['c'] }
484+
485+
s.scan("stra")
462486
assert_nil( s[-2])
463487
assert_equal('stra', s[-1])
464488
assert_equal('stra', s[0])
@@ -903,11 +927,11 @@ def test_aref_without_regex
903927

904928
s = create_string_scanner('abc')
905929
s.get_byte
906-
assert_nil(s[:c])
907-
assert_nil(s["c"])
930+
assert_raise(IndexError) { s[:c] }
931+
assert_raise(IndexError) { s['c'] }
908932
s.getch
909-
assert_nil(s[:c])
910-
assert_nil(s["c"])
933+
assert_raise(IndexError) { s[:c] }
934+
assert_raise(IndexError) { s['c'] }
911935
end
912936

913937
def test_size

0 commit comments

Comments
 (0)