mirror of
https://github.com/ruby/ruby.git
synced 2025-09-21 03:24:00 +02:00
[ruby/yarp] Return Regexp options that match MRI for e, u, s, and n
17dbf4ec46
This commit is contained in:
parent
0996cf5593
commit
101ac364a5
2 changed files with 28 additions and 8 deletions
22
lib/yarp.rb
22
lib/yarp.rb
|
@ -587,11 +587,14 @@ module YARP
|
||||||
|
|
||||||
class InterpolatedRegularExpressionNode < Node
|
class InterpolatedRegularExpressionNode < Node
|
||||||
# Returns a numeric value that represents the flags that were used to create
|
# Returns a numeric value that represents the flags that were used to create
|
||||||
# the regular expression. This mirrors the Regexp#options method in Ruby.
|
# the regular expression.
|
||||||
# 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.
|
|
||||||
def options
|
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
|
end
|
||||||
|
|
||||||
|
@ -604,11 +607,14 @@ module YARP
|
||||||
|
|
||||||
class RegularExpressionNode < Node
|
class RegularExpressionNode < Node
|
||||||
# Returns a numeric value that represents the flags that were used to create
|
# Returns a numeric value that represents the flags that were used to create
|
||||||
# the regular expression. This mirrors the Regexp#options method in Ruby.
|
# the regular expression.
|
||||||
# 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.
|
|
||||||
def options
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -208,6 +208,20 @@ module YARP
|
||||||
assert_equal(Regexp::MULTILINE, options("m"))
|
assert_equal(Regexp::MULTILINE, options("m"))
|
||||||
end
|
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
|
def test_flag_combined
|
||||||
value = Regexp::IGNORECASE | Regexp::MULTILINE | Regexp::EXTENDED
|
value = Regexp::IGNORECASE | Regexp::MULTILINE | Regexp::EXTENDED
|
||||||
assert_equal(value, options("mix"))
|
assert_equal(value, options("mix"))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue