From a59c599209a11d4ab0dc0d7626ab3d5ca43a78c2 Mon Sep 17 00:00:00 2001 From: matz Date: Tue, 11 Dec 2001 03:48:08 +0000 Subject: * string.c (rb_str_match_m): should convert an argument into regexp if it's a string. * array.c (rb_ary_select): Array#select(n,m,...) now works like Array#indexes(n,m,..). [new, experimental] * hash.c (rb_hash_select): ditto. * hash.c (env_select): ditto. * re.c (match_select): ditto. * struct.c (rb_struct_select): ditto. * gc.c (STR_ASSOC): use FL_USER3 instead of FL_USER2. * parse.y (str_extend): make up pushback call. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1905 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- re.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 're.c') diff --git a/re.c b/re.c index f2e2dee4dc..176c3bc8ec 100644 --- a/re.c +++ b/re.c @@ -785,7 +785,7 @@ match_to_a(match) for (i=0; inum_regs; i++) { if (regs->beg[i] == -1) rb_ary_push(ary, Qnil); else rb_ary_push(ary, rb_str_new(ptr+regs->beg[i], - regs->end[i]-regs->beg[i])); + regs->end[i]-regs->beg[i])); } return ary; } @@ -806,6 +806,32 @@ match_aref(argc, argv, match) return rb_reg_nth_match(FIX2INT(idx), match); } +static VALUE +match_select(argc, argv, match) + int argc; + VALUE *argv; + VALUE match; +{ + struct re_registers *regs = RMATCH(match)->regs; + char *ptr = RSTRING(RMATCH(match)->str)->ptr; + VALUE result = rb_ary_new(); + int i; + long idx; + + for (i=0; inum_regs; + if (idx < 0 || regs->num_regs <= idx) { + rb_ary_push(result, Qnil); + } + else { + rb_ary_push(result, rb_str_new(ptr+regs->beg[idx], + regs->end[idx]-regs->beg[idx])); + } + } + return result; +} + static VALUE match_to_s(match) VALUE match; @@ -1436,6 +1462,7 @@ Init_Regexp() rb_define_method(rb_cMatch, "to_a", match_to_a, 0); rb_define_method(rb_cMatch, "to_ary", match_to_a, 0); rb_define_method(rb_cMatch, "[]", match_aref, -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); -- cgit v1.2.3