From: nobu.nakada@... Date: 2003-07-25T12:49:50+09:00 Subject: [ruby-dev:20890] Re: third argument for Regexp.new なかだです。 At Fri, 25 Jul 2003 11:48:56 +0900, Tanaka Akira wrote: > % ruby -e 'Regexp.new("", 0, nil)' > -e:1:in `initialize': cannot convert nil into String (TypeError) > from -e:1:in `new' > from -e:1 > > というように、Regexp.new の第3引数に nil を渡すとエラーになるため、 > > % ruby -e 'r = // > Regexp.new(r.source, r.options, r.kcode)' > -e:2:in `initialize': cannot convert nil into String (TypeError) > from -e:2:in `new' > from -e:2 > > というように Regexp を再生成するのに失敗するのですが、意図的でしょうか? Regexp#kcodeがnilを返すことがある以上、無視したほうがいいでしょ うね。 > というように、"euc" を渡したはずが反映されないというのもあるのですが、 > 意図的でしょうか? Regexp#optionsがregex.cの内部的なフラグをそのまま返してること自 体がよくなさそうです。 Index: re.c =================================================================== RCS file: /cvs/ruby/src/ruby/re.c,v retrieving revision 1.108 diff -u -2 -p -r1.108 re.c --- re.c 17 Jul 2003 09:53:16 -0000 1.108 +++ re.c 25 Jul 2003 03:47:05 -0000 @@ -503,6 +503,6 @@ rb_reg_options_m(re) VALUE re; { - rb_reg_check(re); - return INT2NUM(RREGEXP(re)->ptr->options); + int options = rb_reg_options(re); + return INT2NUM(options); } @@ -1304,7 +1304,8 @@ rb_reg_initialize_m(argc, argv, self) else if (RTEST(argv[1])) flags = RE_OPTION_IGNORECASE; } - if (argc == 3) { + if (argc == 3 && !NIL_P(argv[2])) { char *kcode = StringValuePtr(argv[2]); + flags &= ~0x70; switch (kcode[0]) { case 'n': case 'N': @@ -1475,10 +1476,6 @@ rb_reg_options(re) rb_reg_check(re); - if (RREGEXP(re)->ptr->options & RE_OPTION_IGNORECASE) - options |= RE_OPTION_IGNORECASE; - if (RREGEXP(re)->ptr->options & RE_OPTION_MULTILINE) - options |= RE_OPTION_MULTILINE; - if (RREGEXP(re)->ptr->options & RE_OPTION_EXTENDED) - options |= RE_OPTION_EXTENDED; + options = RREGEXP(re)->ptr->options & + (RE_OPTION_IGNORECASE|RE_OPTION_MULTILINE|RE_OPTION_EXTENDED); if (FL_TEST(re, KCODE_FIXED)) { options |= rb_reg_get_kcode(re); -- --- 僕の前にBugはない。 --- 僕の後ろにBugはできる。 中田 伸悦