mirror of
https://github.com/ruby/ruby.git
synced 2025-08-25 14:05:02 +02:00
* re.c (match_values_at): MatchData#values_at supports named captures
[Feature #9179] * re.c (namev_to_backref_number): separeted. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55036 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0f621628a9
commit
92f8d74a3e
4 changed files with 59 additions and 30 deletions
|
@ -1,3 +1,10 @@
|
||||||
|
Wed May 18 01:57:43 2016 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
|
* re.c (match_values_at): MatchData#values_at supports named captures
|
||||||
|
[Feature #9179]
|
||||||
|
|
||||||
|
* re.c (namev_to_backref_number): separeted.
|
||||||
|
|
||||||
Tue May 18 00:05:00 2016 Kenta Murata <mrkn@mrkn.jp>
|
Tue May 18 00:05:00 2016 Kenta Murata <mrkn@mrkn.jp>
|
||||||
|
|
||||||
* enum.c (enum_sum): Optimize for a range from int to int.
|
* enum.c (enum_sum): Optimize for a range from int to int.
|
||||||
|
|
1
NEWS
1
NEWS
|
@ -59,6 +59,7 @@ with all sufficient information, see the ChangeLog file or Redmine
|
||||||
* MatchData
|
* MatchData
|
||||||
|
|
||||||
* MatchData#named_captures [Feature #11999]
|
* MatchData#named_captures [Feature #11999]
|
||||||
|
* MatchData#values_at supports named captures[Feature #9179]
|
||||||
|
|
||||||
=== Stdlib updates (outstanding ones only)
|
=== Stdlib updates (outstanding ones only)
|
||||||
|
|
||||||
|
|
74
re.c
74
re.c
|
@ -1829,6 +1829,28 @@ name_to_backref_error(VALUE name)
|
||||||
name);
|
name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
namev_to_backref_number(struct re_registers *regs, VALUE re, VALUE name)
|
||||||
|
{
|
||||||
|
int num;
|
||||||
|
|
||||||
|
switch (TYPE(name)) {
|
||||||
|
case T_SYMBOL:
|
||||||
|
name = rb_sym2str(name);
|
||||||
|
/* fall through */
|
||||||
|
case T_STRING:
|
||||||
|
if (NIL_P(re) || !rb_enc_compatible(RREGEXP_SRC(re), name) ||
|
||||||
|
(num = name_to_backref_number(regs, re,
|
||||||
|
RSTRING_PTR(name), RSTRING_END(name))) < 1) {
|
||||||
|
name_to_backref_error(name);
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* mtch[i] -> str or nil
|
* mtch[i] -> str or nil
|
||||||
|
@ -1859,7 +1881,7 @@ name_to_backref_error(VALUE name)
|
||||||
static VALUE
|
static VALUE
|
||||||
match_aref(int argc, VALUE *argv, VALUE match)
|
match_aref(int argc, VALUE *argv, VALUE match)
|
||||||
{
|
{
|
||||||
VALUE idx, rest, re;
|
VALUE idx, rest;
|
||||||
|
|
||||||
match_check(match);
|
match_check(match);
|
||||||
rb_scan_args(argc, argv, "11", &idx, &rest);
|
rb_scan_args(argc, argv, "11", &idx, &rest);
|
||||||
|
@ -1871,25 +1893,9 @@ match_aref(int argc, VALUE *argv, VALUE match)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const char *p;
|
int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, idx);
|
||||||
int num;
|
if (num >= 0) {
|
||||||
|
|
||||||
switch (TYPE(idx)) {
|
|
||||||
case T_SYMBOL:
|
|
||||||
idx = rb_sym2str(idx);
|
|
||||||
/* fall through */
|
|
||||||
case T_STRING:
|
|
||||||
p = StringValuePtr(idx);
|
|
||||||
re = RMATCH(match)->regexp;
|
|
||||||
if (NIL_P(re) || !rb_enc_compatible(RREGEXP_SRC(re), 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);
|
return rb_reg_nth_match(num, match);
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1897,14 +1903,6 @@ match_aref(int argc, VALUE *argv, VALUE match)
|
||||||
return rb_ary_aref(argc, argv, match_to_a(match));
|
return rb_ary_aref(argc, argv, match_to_a(match));
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
|
||||||
match_entry(VALUE match, long n)
|
|
||||||
{
|
|
||||||
/* n should not exceed num_regs */
|
|
||||||
return rb_reg_nth_match((int)n, match);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
*
|
*
|
||||||
|
@ -1916,16 +1914,32 @@ match_entry(VALUE match, long n)
|
||||||
* m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
|
* m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
|
||||||
* m.to_a #=> ["HX1138", "H", "X", "113", "8"]
|
* m.to_a #=> ["HX1138", "H", "X", "113", "8"]
|
||||||
* m.values_at(0, 2, -2) #=> ["HX1138", "X", "113"]
|
* m.values_at(0, 2, -2) #=> ["HX1138", "X", "113"]
|
||||||
|
*
|
||||||
|
* m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2")
|
||||||
|
* m.to_a #=> ["1 + 2", "1", "+", "2"]
|
||||||
|
* m.values_at(:a, :b, :op) #=> ["1", "2", "+"]
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
match_values_at(int argc, VALUE *argv, VALUE match)
|
match_values_at(int argc, VALUE *argv, VALUE match)
|
||||||
{
|
{
|
||||||
struct re_registers *regs;
|
VALUE result;
|
||||||
|
int i;
|
||||||
|
|
||||||
match_check(match);
|
match_check(match);
|
||||||
regs = RMATCH_REGS(match);
|
result = rb_ary_new2(argc);
|
||||||
return rb_get_values_at(match, regs->num_regs, argc, argv, match_entry);
|
|
||||||
|
for (i=0; i<argc; i++) {
|
||||||
|
if (FIXNUM_P(argv[i])) {
|
||||||
|
rb_ary_push(result, rb_reg_nth_match(FIX2INT(argv[i]), match));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, argv[i]);
|
||||||
|
if (num < 0) num = NUM2INT(argv[i]);
|
||||||
|
rb_ary_push(result, rb_reg_nth_match(num, match));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -383,6 +383,13 @@ class TestRegexp < Test::Unit::TestCase
|
||||||
def test_match_values_at
|
def test_match_values_at
|
||||||
m = /(...)(...)(...)(...)?/.match("foobarbaz")
|
m = /(...)(...)(...)(...)?/.match("foobarbaz")
|
||||||
assert_equal(["foo", "bar", "baz"], m.values_at(1, 2, 3))
|
assert_equal(["foo", "bar", "baz"], m.values_at(1, 2, 3))
|
||||||
|
|
||||||
|
m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2")
|
||||||
|
assert_equal(["1", "2", "+"], m.values_at(:a, 'b', :op))
|
||||||
|
idx = Object.new
|
||||||
|
def idx.to_int; 2; end
|
||||||
|
assert_equal(["+"], m.values_at(idx))
|
||||||
|
assert_raise(TypeError){ m.values_at(nil) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_match_string
|
def test_match_string
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue