From 1e9939dae24db232d6f3693630fa37a382e1a6d7 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Thu, 16 Jun 2022 18:53:35 +0900 Subject: [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. --- re.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 're.c') 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; } -- cgit v1.2.1