summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2022-06-16 18:53:35 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2022-06-20 19:35:12 +0900
commit1e9939dae24db232d6f3693630fa37a382e1a6d7 (patch)
tree7bb1660444a068ce185c4ef636467861b40cae6c /re.c
parent39dc455b511614ee8a1911c0ba6445a0307d5e4f (diff)
downloadruby-1e9939dae24db232d6f3693630fa37a382e1a6d7.tar.gz
[Feature #18788] Support options as `String` to `Regexp.new`
`Regexp.new` now supports passing the regexp flags not only as an `Integer`, but also as a `String. Unknown flags raise errors.
Diffstat (limited to 're.c')
-rw-r--r--re.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/re.c b/re.c
index f225a413d5..39d4fc046f 100644
--- a/re.c
+++ b/re.c
@@ -3632,6 +3632,25 @@ rb_reg_match_p(VALUE re, VALUE str, long pos)
* Alias for Regexp.new
*/
+static int
+str_to_option(VALUE str)
+{
+ int flag = 0;
+ const char *ptr;
+ long len;
+ str = rb_check_string_type(str);
+ if (NIL_P(str)) return -1;
+ RSTRING_GETMEM(str, ptr, len);
+ for (long i = 0; i < len; ++i) {
+ int f = char_to_option(ptr[i]);
+ if (!f) {
+ rb_raise(rb_eArgError, "unknown regexp option: %"PRIsVALUE, str);
+ }
+ flag |= f;
+ }
+ return flag;
+}
+
/*
* call-seq:
* Regexp.new(string, options = 0, n_flag = nil, timeout: nil) -> regexp
@@ -3716,7 +3735,9 @@ rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
}
else {
if (opts != Qundef) {
+ int f;
if (FIXNUM_P(opts)) flags = FIX2INT(opts);
+ else if ((f = str_to_option(opts)) >= 0) flags = f;
else if (!NIL_P(opts) && rb_bool_expected(opts, "ignorecase", FALSE))
flags = ONIG_OPTION_IGNORECASE;
}