ruby-changes:28860
From: naruse <ko1@a...>
Date: Fri, 24 May 2013 16:32:57 +0900 (JST)
Subject: [ruby-changes:28860] naruse:r40912 (trunk): * ext/strscan/strscan.c (strscan_aref): raise error if given
naruse 2013-05-24 16:32:45 +0900 (Fri, 24 May 2013) New Revision: 40912 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40912 Log: * ext/strscan/strscan.c (strscan_aref): raise error if given name reference is not found. Modified files: trunk/ChangeLog trunk/ext/strscan/strscan.c trunk/test/strscan/test_stringscanner.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 40911) +++ ChangeLog (revision 40912) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri May 24 16:31:23 2013 NARUSE, Yui <naruse@r...> + + * 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@a...> * gc.c (after_gc_sweep, garbage_collect_body): do major GC (full GC) Index: ext/strscan/strscan.c =================================================================== --- ext/strscan/strscan.c (revision 40911) +++ ext/strscan/strscan.c (revision 40912) @@ -975,6 +975,25 @@ strscan_matched_size(VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L975 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) * @@ -1019,9 +1038,7 @@ strscan_aref(VALUE self, VALUE idx) https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L1038 case T_STRING: name = StringValuePtr(idx); name_to_backref: - name_end = name + strlen(name); - i = onig_name_to_backref_number(RREGEXP(p->regex)->ptr, - (const unsigned char* )name, (const unsigned char* )name_end, &(p->regs)); + i = name_to_backref_number(&(p->regs), p->regex, name, name + strlen(name)); break; default: i = NUM2LONG(idx); Index: test/strscan/test_stringscanner.rb =================================================================== --- test/strscan/test_stringscanner.rb (revision 40911) +++ test/strscan/test_stringscanner.rb (revision 40912) @@ -402,6 +402,8 @@ class TestStringScanner < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/strscan/test_stringscanner.rb#L402 assert_equal 'stra', s[-1] assert_equal 'stra', s[0] assert_nil s[1] + assert_raise(IndexError) { s[:c] } + assert_raise(IndexError) { s['c'] } assert_equal false, s[-1].tainted? assert_equal false, s[0].tainted? @@ -458,16 +460,17 @@ class TestStringScanner < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/strscan/test_stringscanner.rb#L460 assert_equal true, s[3].tainted? assert_equal true, s[4].tainted? - s = StringScanner.new("foo bar") - s.scan /(?<a>(\w+)) (?<b>(\w+))/ + s = StringScanner.new("foo bar baz") + s.scan /(?<a>\w+) (?<b>\w+) (\w+)/ assert_equal 'foo', s[1] assert_equal 'bar', s[2] + assert_nil s[3] assert_equal 'foo', s[:a] assert_equal 'bar', s[:b] - assert_equal nil, s[:c] + assert_raise(IndexError) { s[:c] } assert_equal 'foo', s['a'] assert_equal 'bar', s['b'] - assert_equal nil, s['c'] + assert_raise(IndexError) { s['c'] } end def test_pre_match -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/