1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/strscan/strscan.c (strscan_aref): raise error if given

name reference is not found.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40912 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
naruse 2013-05-24 07:32:45 +00:00
parent 755f669316
commit c43dd625b6
3 changed files with 32 additions and 7 deletions

View file

@ -1,3 +1,8 @@
Fri May 24 16:31:23 2013 NARUSE, Yui <naruse@ruby-lang.org>
* ext/strscan/strscan.c (strscan_aref): raise error if given
name reference is not found.
Fri May 24 15:48:18 2013 Koichi Sasada <ko1@atdot.net> Fri May 24 15:48:18 2013 Koichi Sasada <ko1@atdot.net>
* gc.c (after_gc_sweep, garbage_collect_body): do major GC (full GC) * gc.c (after_gc_sweep, garbage_collect_body): do major GC (full GC)

View file

@ -975,6 +975,25 @@ strscan_matched_size(VALUE self)
return INT2NUM(p->regs.end[0] - p->regs.beg[0]); return INT2NUM(p->regs.end[0] - p->regs.beg[0]);
} }
static int
name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
{
int num;
num = onig_name_to_backref_number(RREGEXP(regexp)->ptr,
(const unsigned char* )name, (const unsigned char* )name_end, regs);
if (num >= 1) {
return num;
}
else {
VALUE s = rb_str_new(name, (long )(name_end - name));
rb_raise(rb_eIndexError, "undefined group name reference: %s",
StringValuePtr(s));
}
UNREACHABLE;
}
/* /*
* call-seq: [](n) * call-seq: [](n)
* *
@ -1019,9 +1038,7 @@ strscan_aref(VALUE self, VALUE idx)
case T_STRING: case T_STRING:
name = StringValuePtr(idx); name = StringValuePtr(idx);
name_to_backref: name_to_backref:
name_end = name + strlen(name); i = name_to_backref_number(&(p->regs), p->regex, name, name + strlen(name));
i = onig_name_to_backref_number(RREGEXP(p->regex)->ptr,
(const unsigned char* )name, (const unsigned char* )name_end, &(p->regs));
break; break;
default: default:
i = NUM2LONG(idx); i = NUM2LONG(idx);

View file

@ -402,6 +402,8 @@ class TestStringScanner < Test::Unit::TestCase
assert_equal 'stra', s[-1] assert_equal 'stra', s[-1]
assert_equal 'stra', s[0] assert_equal 'stra', s[0]
assert_nil s[1] assert_nil s[1]
assert_raise(IndexError) { s[:c] }
assert_raise(IndexError) { s['c'] }
assert_equal false, s[-1].tainted? assert_equal false, s[-1].tainted?
assert_equal false, s[0].tainted? assert_equal false, s[0].tainted?
@ -458,16 +460,17 @@ class TestStringScanner < Test::Unit::TestCase
assert_equal true, s[3].tainted? assert_equal true, s[3].tainted?
assert_equal true, s[4].tainted? assert_equal true, s[4].tainted?
s = StringScanner.new("foo bar") s = StringScanner.new("foo bar baz")
s.scan /(?<a>(\w+)) (?<b>(\w+))/ s.scan /(?<a>\w+) (?<b>\w+) (\w+)/
assert_equal 'foo', s[1] assert_equal 'foo', s[1]
assert_equal 'bar', s[2] assert_equal 'bar', s[2]
assert_nil s[3]
assert_equal 'foo', s[:a] assert_equal 'foo', s[:a]
assert_equal 'bar', s[:b] assert_equal 'bar', s[:b]
assert_equal nil, s[:c] assert_raise(IndexError) { s[:c] }
assert_equal 'foo', s['a'] assert_equal 'foo', s['a']
assert_equal 'bar', s['b'] assert_equal 'bar', s['b']
assert_equal nil, s['c'] assert_raise(IndexError) { s['c'] }
end end
def test_pre_match def test_pre_match