summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-07-03 11:02:53 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2003-07-03 11:02:53 +0000
commit9d22a06ea06e9bec5030fd6e2c8481958fcd30d9 (patch)
tree58b15b4dc85acca1c806388ff3848a3c85fd0ebf /re.c
parenta11ab83884c47f729a8630ab5ef21c7522b48386 (diff)
* array.c (rb_values_at): extract common procedure from
rb_ary_values_at. follow DRY principle. * re.c (match_values_at): values_at should understand ranges. * struct.c (rb_struct_values_at): ditto. * struct.c (inspect_struct): inspect format changed; add "struct " at the top. * sprintf.c (rb_f_sprintf): "%p" specifier for inspect output. (RCR#68) * eval.c (rb_mod_undef_method): allow "undef_method" to accept multiple arguments. (RCR#146) * lib/timeout.rb: put timeout in Timeout module. (RCR#121) [ruby-talk:61028] * re.c (match_groups): new method added. (RCR#139) * variable.c (rb_mod_const_of): should exclude constant defined in Object, unless retrieving constants of Object. * string.c (rb_str_new4): do not allocate new string if original is frozen or already have copy-on-write entry. [ruby-talk:74940] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4031 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r--re.c49
1 files changed, 27 insertions, 22 deletions
diff --git a/re.c b/re.c
index dd18000..313486c 100644
--- a/re.c
+++ b/re.c
@@ -914,8 +914,9 @@ last_paren_match_getter()
}
static VALUE
-match_to_a(match)
+match_array(match, start)
VALUE match;
+ int start;
{
struct re_registers *regs = RMATCH(match)->regs;
VALUE ary = rb_ary_new2(regs->num_regs);
@@ -923,7 +924,7 @@ match_to_a(match)
int i;
int taint = OBJ_TAINTED(match);
- for (i=0; i<regs->num_regs; i++) {
+ for (i=start; i<regs->num_regs; i++) {
if (regs->beg[i] == -1) {
rb_ary_push(ary, Qnil);
}
@@ -937,6 +938,20 @@ match_to_a(match)
}
static VALUE
+match_to_a(match)
+ VALUE match;
+{
+ return match_array(match, 0);
+}
+
+static VALUE
+match_groups(match)
+ VALUE match;
+{
+ return match_array(match, 1);
+}
+
+static VALUE
match_aref(argc, argv, match)
int argc;
VALUE *argv;
@@ -953,31 +968,20 @@ match_aref(argc, argv, match)
}
static VALUE
+match_entry(match, n)
+ VALUE match;
+ long n;
+{
+ return rb_reg_nth_match(n, match);
+}
+
+static VALUE
match_values_at(argc, argv, match)
int argc;
VALUE *argv;
VALUE match;
{
- struct re_registers *regs = RMATCH(match)->regs;
- VALUE target = RMATCH(match)->str;
- VALUE result = rb_ary_new();
- int i;
- long idx;
- int taint = OBJ_TAINTED(match);
-
- 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 {
- VALUE str = rb_str_substr(target, regs->beg[idx], regs->end[idx]-regs->beg[idx]);
- if (taint) OBJ_TAINT(str);
- rb_ary_push(result, str);
- }
- }
- return result;
+ return rb_values_at(match, RMATCH(match)->regs->num_regs, argc, argv, match_entry);
}
static VALUE
@@ -1766,6 +1770,7 @@ Init_Regexp()
rb_define_method(rb_cMatch, "end", match_end, 1);
rb_define_method(rb_cMatch, "to_a", match_to_a, 0);
rb_define_method(rb_cMatch, "[]", match_aref, -1);
+ rb_define_method(rb_cMatch, "groups", match_groups, 0);
rb_define_method(rb_cMatch, "select", match_select, -1);
rb_define_method(rb_cMatch, "values_at", match_values_at, -1);
rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0);