summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-12-11 03:48:08 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-12-11 03:48:08 +0000
commita59c599209a11d4ab0dc0d7626ab3d5ca43a78c2 (patch)
treef4c4400099a7feb51a7b303cbedc0e1f04714f43 /re.c
parent8a91c99905c1bfbf441ec890161538acc8e34120 (diff)
* 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
Diffstat (limited to 're.c')
-rw-r--r--re.c29
1 files changed, 28 insertions, 1 deletions
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; i<regs->num_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;
}
@@ -807,6 +807,32 @@ match_aref(argc, argv, 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; i<argc; i++) {
+ idx = NUM2LONG(argv[i]);
+ if (idx < 0) idx += regs->num_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);