From 101ac364a54b10a24e2351c2138b59a819665540 Mon Sep 17 00:00:00 2001 From: Tim Morgan Date: Fri, 15 Sep 2023 11:26:13 -0500 Subject: [PATCH] [ruby/yarp] Return Regexp options that match MRI for e, u, s, and n https://github.com/ruby/yarp/commit/17dbf4ec46 --- lib/yarp.rb | 22 ++++++++++++++-------- test/yarp/regexp_test.rb | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/lib/yarp.rb b/lib/yarp.rb index c1043c3232..a2940a2eaa 100644 --- a/lib/yarp.rb +++ b/lib/yarp.rb @@ -587,11 +587,14 @@ module YARP class InterpolatedRegularExpressionNode < Node # Returns a numeric value that represents the flags that were used to create - # the regular expression. This mirrors the Regexp#options method in Ruby. - # Note that this is effectively masking only the three common flags that are - # used in Ruby, and does not include the full set of flags like encoding. + # the regular expression. def options - flags & 0b111 + o = flags & 0b111 + o |= Regexp::FIXEDENCODING if flags & 8 != 0 # 'e' + o |= Regexp::FIXEDENCODING if flags & 32 != 0 # 's' + o |= Regexp::FIXEDENCODING if flags & 64 != 0 # 'u' + o |= Regexp::NOENCODING if flags & 16 != 0 # 'n' + o end end @@ -604,11 +607,14 @@ module YARP class RegularExpressionNode < Node # Returns a numeric value that represents the flags that were used to create - # the regular expression. This mirrors the Regexp#options method in Ruby. - # Note that this is effectively masking only the three common flags that are - # used in Ruby, and does not include the full set of flags like encoding. + # the regular expression. def options - flags & 0b111 + o = flags & 0b111 + o |= Regexp::FIXEDENCODING if flags & 8 != 0 # 'e' + o |= Regexp::FIXEDENCODING if flags & 32 != 0 # 's' + o |= Regexp::FIXEDENCODING if flags & 64 != 0 # 'u' + o |= Regexp::NOENCODING if flags & 16 != 0 # 'n' + o end end end diff --git a/test/yarp/regexp_test.rb b/test/yarp/regexp_test.rb index c2f92e2bd1..865c70a2c8 100644 --- a/test/yarp/regexp_test.rb +++ b/test/yarp/regexp_test.rb @@ -208,6 +208,20 @@ module YARP assert_equal(Regexp::MULTILINE, options("m")) end + def test_flag_fixedencoding + assert_equal(Regexp::FIXEDENCODING, options("e")) + assert_equal(Regexp::FIXEDENCODING, options("u")) + assert_equal(Regexp::FIXEDENCODING, options("s")) + end + + def test_flag_noencoding + assert_equal(Regexp::NOENCODING, options("n")) + end + + def test_flag_once + assert_equal(0, options("o")) + end + def test_flag_combined value = Regexp::IGNORECASE | Regexp::MULTILINE | Regexp::EXTENDED assert_equal(value, options("mix"))