diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-12-11 03:48:08 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-12-11 03:48:08 +0000 |
commit | 2cfcc42a17d7de808fad5468cf93e1a79b839ab0 (patch) | |
tree | f4c4400099a7feb51a7b303cbedc0e1f04714f43 /re.c | |
parent | 2adf4008deab55a039d73c0cc667a53bdd94c728 (diff) | |
download | ruby-2cfcc42a17d7de808fad5468cf93e1a79b839ab0.tar.gz ruby-2cfcc42a17d7de808fad5468cf93e1a79b839ab0.tar.xz ruby-2cfcc42a17d7de808fad5468cf93e1a79b839ab0.zip |
* 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: http://svn.ruby-lang.org/repos/ruby/trunk@1905 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r-- | re.c | 29 |
1 files changed, 28 insertions, 1 deletions
@@ -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); |