diff options
author | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-05-18 15:02:36 +0000 |
---|---|---|
committer | knu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-05-18 15:02:36 +0000 |
commit | 8480bcc8d5c72b61055cfa98e80f37fd62ae7ad4 (patch) | |
tree | f404254dbcee2a59866dbb3f7baab4df8e4b23ce /re.c | |
parent | 32378c5abea38a8278dae28eae9abcd547ac8a95 (diff) |
Merge -r16241:16456 from ruby_1_8.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_7@16458 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r-- | re.c | 44 |
1 files changed, 42 insertions, 2 deletions
@@ -927,6 +927,7 @@ rb_reg_search(re, str, pos, reverse) } if (result < 0) { + re_free_registers(®s); rb_backref_set(Qnil); return result; } @@ -943,6 +944,7 @@ rb_reg_search(re, str, pos, reverse) } re_copy_registers(RMATCH(match)->regs, ®s); + re_free_registers(®s); RMATCH(match)->str = rb_str_new4(str); rb_backref_set(match); @@ -1219,7 +1221,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 +1240,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 +2366,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); |