named capture e$B$^$o$j$r$=$m$($F$$$F!"$d$O$jL>A0$N%j%9%H$rF@$ke(B
e$B%a%=%C%I$,I,MW$@$m$&$H$$$&$3$H$G!“e(BRegexp#names e$B$He(B
MatchData#names e$B$r<BAu$7$F$_$^$7$?$,!”$I$&$G$7$g$&$+!#e(B
e$B$"$H!“e(Bmatch.names e$B$Oe(B match.regexp.names
e$B$HF1$8!”$H%I%-%e%a%se(B
e$B%H$K=q$3$&$H$7$?$N$G$9$,e(B MatchData#regexp e$B$,$J$+$C$?$N$G$3$le(B
e$B$b$D$1$^$7$?!#e(B
Index: re.c
— re.c (revision 14159)
+++ re.c (working copy)
@@ -513,6 +513,38 @@ rb_reg_options_m(VALUE re)
return INT2NUM(options);
}
+static int
+reg_names_iter(const OnigUChar *name, const OnigUChar *name_end,
-
int back_num, int *back_refs, OnigRegex regex, void *arg)
+{
- VALUE ary = (VALUE)arg;
- rb_ary_push(ary, rb_str_new((const char *)name, name_end-name));
- return 0;
+}
+/*
-
- call-seq:
-
- rxp.names => [name1, name2, …]
-
-
- Returns an array of names of named captures.
-
-
-
/(?<foo>.)(?<bar>.)(?<baz>.)/.names
-
-
-
#=> ["foo", "bar", "baz"]
-
-
-
-
/(?<foo>.)(?<foo>.)/.names
-
-
-
#=> ["foo"]
-
-
-
-
/(.)(.)/.names'
-
-
-
#=> []
-
- */
+static VALUE
+rb_reg_names(VALUE re)
+{
- VALUE ary = rb_ary_new();
- onig_foreach_name(RREGEXP(re)->ptr, reg_names_iter, (void*)ary);
- return ary;
+}
static Regexp*
make_regexp(const char *s, long len, rb_encoding *enc, int flags,
onig_errmsg_buffer err)
@@ -595,6 +627,42 @@ match_init_copy(VALUE obj, VALUE orig)
/*
-
- call-seq:
-
- mtch.regexp => regexp
-
-
- Returns the regexp.
-
-
-
m = /a.*b/.match("abc")
-
-
-
m.regexp #=> /a.*b/
-
- */
+static VALUE
+match_regexp(VALUE match)
+{
- return RMATCH(match)->regexp;
+}
+/*
-
- call-seq:
-
- mtch.names => [name1, name2, …]
-
-
- Returns an array of names of named captures.
-
- It is same as mtch.regexp.names.
-
-
-
/(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").names
-
-
-
#=> ["foo", "bar", "baz"]
-
-
-
-
m = /(?<x>.)(?<y>.)?/.match("a") #=> #<MatchData "a" x:"a"
-
y:nil>
-
-
m.names #=> ["x", "y"]
-
- */
+static VALUE
+match_names(VALUE match)
+{
- return rb_reg_names(RMATCH(match)->regexp);
+}
+/*
- call-seq:
-
mtch.length => integer
-
mtch.size => integer
@@ -2754,6 +2822,7 @@ Init_Regexp(void)
rb_define_method(rb_cRegexp, “options”, rb_reg_options_m, 0);
rb_define_method(rb_cRegexp, “encoding”, rb_obj_encoding, 0); /* in
encoding.c */
rb_define_method(rb_cRegexp, “fixed_encoding?”,
rb_reg_fixed_encoding_p, 0);
-
rb_define_method(rb_cRegexp, “names”, rb_reg_names, 0);
rb_define_const(rb_cRegexp, “IGNORECASE”,
INT2FIX(ONIG_OPTION_IGNORECASE));
rb_define_const(rb_cRegexp, “EXTENDED”,
INT2FIX(ONIG_OPTION_EXTEND));
@@ -2766,6 +2835,8 @@ Init_Regexp(void)
rb_undef_method(CLASS_OF(rb_cMatch), “new”);rb_define_method(rb_cMatch, “initialize_copy”, match_init_copy, 1);
-
rb_define_method(rb_cMatch, “regexp”, match_regexp, 0);
-
rb_define_method(rb_cMatch, “names”, match_names, 0);
rb_define_method(rb_cMatch, “size”, match_size, 0);
rb_define_method(rb_cMatch, “length”, match_size, 0);
rb_define_method(rb_cMatch, “offset”, match_offset, 1);