mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
merge revision(s) r46345,r46346: [Backport #9903]
re.c: reduce new strings * re.c (match_aref, rb_reg_regsub): reduce new strings creation for exceptions. * re.c (match_aref, rb_reg_regsub): consider encoding of captured names, encoding-incompatible should not match. [ruby-dev:48278] [Bug #9903] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_1@47052 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
84091a71e5
commit
3aefb61f15
4 changed files with 35 additions and 16 deletions
|
@ -1,3 +1,9 @@
|
|||
Mon Aug 4 01:11:07 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* re.c (match_aref, rb_reg_regsub): consider encoding of captured
|
||||
names, encoding-incompatible should not match.
|
||||
[ruby-dev:48278] [Bug #9903]
|
||||
|
||||
Mon Aug 4 00:52:42 2014 Koichi Sasada <ko1@atdot.net>
|
||||
|
||||
* vm_eval.c (rb_catch_protect): fix same problem of [Bug #9961].
|
||||
|
|
34
re.c
34
re.c
|
@ -1691,20 +1691,16 @@ match_captures(VALUE match)
|
|||
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,
|
||||
return 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;
|
||||
NORETURN(static void name_to_backref_error(VALUE name));
|
||||
static void
|
||||
name_to_backref_error(VALUE name)
|
||||
{
|
||||
rb_raise(rb_eIndexError, "undefined group name reference: % "PRIsVALUE,
|
||||
name);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -1758,8 +1754,11 @@ match_aref(int argc, VALUE *argv, VALUE match)
|
|||
/* fall through */
|
||||
case T_STRING:
|
||||
p = StringValuePtr(idx);
|
||||
num = name_to_backref_number(RMATCH_REGS(match),
|
||||
RMATCH(match)->regexp, p, p + RSTRING_LEN(idx));
|
||||
if (!rb_enc_compatible(RREGEXP(RMATCH(match)->regexp)->src, idx) ||
|
||||
(num = name_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp,
|
||||
p, p + RSTRING_LEN(idx))) < 1) {
|
||||
name_to_backref_error(idx);
|
||||
}
|
||||
return rb_reg_nth_match(num, match);
|
||||
|
||||
default:
|
||||
|
@ -3369,7 +3368,12 @@ rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
|
|||
name_end += c == -1 ? mbclen(name_end, e, str_enc) : clen;
|
||||
}
|
||||
if (name_end < e) {
|
||||
no = name_to_backref_number(regs, regexp, name, name_end);
|
||||
VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)),
|
||||
(long)(name_end - name));
|
||||
if (!rb_enc_compatible(RREGEXP(regexp)->src, n) ||
|
||||
(no = name_to_backref_number(regs, regexp, name, name_end)) < 1) {
|
||||
name_to_backref_error(n);
|
||||
}
|
||||
p = s = name_end + clen;
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -158,6 +158,15 @@ class TestRegexp < Test::Unit::TestCase
|
|||
}
|
||||
end
|
||||
|
||||
def test_named_capture_nonascii
|
||||
bug9903 = '[ruby-dev:48278] [Bug #9903]'
|
||||
|
||||
key = "\xb1\xb2".force_encoding(Encoding::EUC_JP)
|
||||
m = /(?<#{key}>.*)/.match("xxx")
|
||||
assert_equal("xxx", m[key])
|
||||
assert_raise(IndexError, bug9903) {m[key.dup.force_encoding(Encoding::Shift_JIS)]}
|
||||
end
|
||||
|
||||
def test_assign_named_capture
|
||||
assert_equal("a", eval('/(?<foo>.)/ =~ "a"; foo'))
|
||||
assert_equal("a", eval('foo = 1; /(?<foo>.)/ =~ "a"; foo'))
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#define RUBY_VERSION "2.1.2"
|
||||
#define RUBY_RELEASE_DATE "2014-08-04"
|
||||
#define RUBY_PATCHLEVEL 190
|
||||
#define RUBY_PATCHLEVEL 191
|
||||
|
||||
#define RUBY_RELEASE_YEAR 2014
|
||||
#define RUBY_RELEASE_MONTH 8
|
||||
|
|
Loading…
Add table
Reference in a new issue