Skip to content

Commit 018943b

Browse files
naitohhsbt
authored andcommitted
[ruby/strscan] Fix a bug that inconsistency of IndexError vs nil for
unknown capture group (ruby/strscan#143) Fix ruby/strscan#139 Reported by Benoit Daloze. Thanks!!! ruby/strscan@bc8a0d2623
1 parent 36ab247 commit 018943b

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

ext/strscan/strscan.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,19 +1667,17 @@ strscan_matched_size(VALUE self)
16671667
static int
16681668
name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end, rb_encoding *enc)
16691669
{
1670-
int num;
1671-
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);
1670+
if (RTEST(regexp)) {
1671+
int num = onig_name_to_backref_number(RREGEXP_PTR(regexp),
1672+
(const unsigned char* )name,
1673+
(const unsigned char* )name_end,
1674+
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)