diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-05-02 04:51:58 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-05-02 04:51:58 +0000 |
commit | eb416323bf3e964ebba71212a16e142bfd643a04 (patch) | |
tree | 5858385ba1a05e1a4ad9b3adb0063addc864f588 /re.c | |
parent | 4f811857fa221811474d7a84569ec5a3c959f90d (diff) |
* re.c (match_select): restore previous behavior of MatchData#select.
RDoc updated as well, mentioning the plan to remove this method
in the future. [ruby-dev:34556]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16263 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r-- | re.c | 42 |
1 files changed, 40 insertions, 2 deletions
@@ -1219,7 +1219,6 @@ match_entry(match, n) /* * call-seq: * mtch.values_at([index]*) => array - * mtch.select([index]*) => array * * Uses each <i>index</i> to access the matching values, returning an array of * the corresponding matches. @@ -1239,6 +1238,45 @@ match_values_at(argc, argv, match) } +/* + * call-seq: + * mtch.select{|obj| block} => array + * + * Returns an array containing match strings for which <em>block</em> + * gives <code>true</code>. MatchData#select will be removed from Ruby 1.9. + * + * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie") + * p m.select{|x| /X/ =~ x} #=> ["HX1138", "X"] + */ + +static VALUE +match_select(argc, argv, match) + int argc; + VALUE *argv; + VALUE match; +{ + if (argc > 0) { + rb_raise(rb_eArgError, "wrong number of arguments (%d for 0)", argc); + } + else { + struct re_registers *regs = RMATCH(match)->regs; + VALUE target = RMATCH(match)->str; + VALUE result = rb_ary_new(); + int i; + int taint = OBJ_TAINTED(match); + + for (i=0; i<regs->num_regs; i++) { + VALUE str = rb_str_substr(target, regs->beg[i], regs->end[i]-regs->beg[i]); + if (taint) OBJ_TAINT(str); + if (RTEST(rb_yield(str))) { + rb_ary_push(result, str); + } + } + return result; + } +} + + /* * call-seq: @@ -2326,7 +2364,7 @@ Init_Regexp() rb_define_method(rb_cMatch, "[]", match_aref, -1); rb_define_method(rb_cMatch, "captures", match_captures, 0); rb_define_method(rb_cMatch, "values_at", match_values_at, -1); - rb_define_method(rb_cMatch, "select", match_values_at, -1); + rb_define_method(rb_cMatch, "select", match_select, -1); rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0); rb_define_method(rb_cMatch, "post_match", rb_reg_match_post, 0); rb_define_method(rb_cMatch, "to_s", match_to_s, 0); |