Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ext/jruby/org/jruby/ext/strscan/RubyStringScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -751,12 +751,14 @@ public IRubyObject op_aref(ThreadContext context, IRubyObject idx) {
return context.nil;
}

Ruby runtime = context.runtime;

if (idx instanceof RubySymbol || idx instanceof RubyString) {
if (pattern == null) return context.nil;
if (pattern == null) {
throw runtime.newRaiseException((RubyClass) getMetaClass().getConstant("IndexError"), "undefined group name reference: " + idx);
}
}

Ruby runtime = context.runtime;

int i = RubyMatchData.backrefNumber(runtime, pattern, regs, idx);

return extractRegion(context, i);
Expand Down
23 changes: 10 additions & 13 deletions ext/strscan/strscan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 29 additions & 5 deletions test/strscan/test_stringscanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,31 @@ def test_matched_string
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])
Expand Down Expand Up @@ -903,11 +927,11 @@ def test_aref_without_regex

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
Expand Down