summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-10-08 16:04:07 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-10-08 16:04:07 +0000
commit398755267d6bb1e637248e0048cbc0cdf3ccf284 (patch)
tree25ff21232f08275d3405e5a30a07ac58f551f872 /re.c
parentb4b36c06faeb3a1586e4e3465354ab931bc19acf (diff)
* re.c (rb_reg_s_union_m): Regexp.union accepts single
argument which is an array of patterns. [ruby-list:44084] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@13659 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r--re.c60
1 files changed, 37 insertions, 23 deletions
diff --git a/re.c b/re.c
index 446ea4fa24..ba88cb5f69 100644
--- a/re.c
+++ b/re.c
@@ -1892,26 +1892,12 @@ rb_reg_options(re)
return options;
}
-
-/*
- * call-seq:
- * Regexp.union([pattern]*) => new_str
- *
- * Return a <code>Regexp</code> object that is the union of the given
- * <em>pattern</em>s, i.e., will match any of its parts. The <em>pattern</em>s
- * can be Regexp objects, in which case their options will be preserved, or
- * Strings. If no arguments are given, returns <code>/(?!)/</code>.
- *
- * Regexp.union #=> /(?!)/
- * Regexp.union("penzance") #=> /penzance/
- * Regexp.union("skiing", "sledding") #=> /skiing|sledding/
- * Regexp.union(/dogs/, /cats/i) #=> /(?-mix:dogs)|(?i-mx:cats)/
- */
static VALUE
-rb_reg_s_union(argc, argv)
- int argc;
- VALUE *argv;
+rb_reg_s_union(self, args0)
+ VALUE self;
+ VALUE args0;
{
+ long argc = RARRAY_LEN(args0);
if (argc == 0) {
VALUE args[1];
args[0] = rb_str_new2("(?!)");
@@ -1919,12 +1905,12 @@ rb_reg_s_union(argc, argv)
}
else if (argc == 1) {
VALUE v;
- v = rb_check_convert_type(argv[0], T_REGEXP, "Regexp", "to_regexp");
+ v = rb_check_convert_type(rb_ary_entry(args0, 0), T_REGEXP, "Regexp", "to_regexp");
if (!NIL_P(v))
return v;
else {
VALUE args[1];
- args[0] = rb_reg_s_quote(argc, argv);
+ args[0] = rb_reg_s_quote(RARRAY_LEN(args0), RARRAY_PTR(args0));
return rb_class_new_instance(1, args, rb_cRegexp);
}
}
@@ -1937,7 +1923,7 @@ rb_reg_s_union(argc, argv)
volatile VALUE v;
if (0 < i)
rb_str_buf_cat2(source, "|");
- v = rb_check_convert_type(argv[i], T_REGEXP, "Regexp", "to_regexp");
+ v = rb_check_convert_type(rb_ary_entry(args0, i), T_REGEXP, "Regexp", "to_regexp");
if (!NIL_P(v)) {
if (FL_TEST(v, KCODE_FIXED)) {
if (kcode == -1) {
@@ -1955,7 +1941,7 @@ rb_reg_s_union(argc, argv)
v = rb_reg_to_s(v);
}
else {
- args[0] = argv[i];
+ args[0] = rb_ary_entry(args0, i);
v = rb_reg_s_quote(1, args);
}
rb_str_buf_append(source, v);
@@ -1983,6 +1969,34 @@ rb_reg_s_union(argc, argv)
}
}
+/*
+ * call-seq:
+ * Regexp.union(pat1, pat2, ...) => new_regexp
+ * Regexp.union(pats_ary) => new_regexp
+ *
+ * Return a <code>Regexp</code> object that is the union of the given
+ * <em>pattern</em>s, i.e., will match any of its parts. The <em>pattern</em>s
+ * can be Regexp objects, in which case their options will be preserved, or
+ * Strings. If no patterns are given, returns <code>/(?!)/</code>.
+ *
+ * Regexp.union #=> /(?!)/
+ * Regexp.union("penzance") #=> /penzance/
+ * Regexp.union("a+b*c") #=> /a\+b\*c/
+ * Regexp.union("skiing", "sledding") #=> /skiing|sledding/
+ * Regexp.union(["skiing", "sledding"]) #=> /skiing|sledding/
+ * Regexp.union(/dogs/, /cats/i) #=> /(?-mix:dogs)|(?i-mx:cats)/
+ */
+static VALUE
+rb_reg_s_union_m(VALUE self, VALUE args)
+{
+ VALUE v;
+ if (RARRAY_LEN(args) == 1 &&
+ !NIL_P(v = rb_check_array_type(rb_ary_entry(args, 0)))) {
+ return rb_reg_s_union(self, v);
+ }
+ return rb_reg_s_union(self, args);
+}
+
/* :nodoc: */
static VALUE
rb_reg_init_copy(copy, re)
@@ -2260,7 +2274,7 @@ Init_Regexp()
rb_define_singleton_method(rb_cRegexp, "compile", rb_class_new_instance, -1);
rb_define_singleton_method(rb_cRegexp, "quote", rb_reg_s_quote, -1);
rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, -1);
- rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union, -1);
+ rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union_m, -2);
rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1);
rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1);