mirror of
https://github.com/ruby/ruby.git
synced 2025-09-15 08:33:58 +02:00
* 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
This commit is contained in:
parent
8a91c99905
commit
a59c599209
10 changed files with 203 additions and 6 deletions
29
re.c
29
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;
|
||||
}
|
||||
|
@ -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; 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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue