Skip to content

Commit 17dbf4e

Browse files
committed
Return Regexp options that match MRI for e, u, s, and n
1 parent 9d0af98 commit 17dbf4e

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

lib/yarp.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,14 @@ def value
587587

588588
class InterpolatedRegularExpressionNode < Node
589589
# Returns a numeric value that represents the flags that were used to create
590-
# the regular expression. This mirrors the Regexp#options method in Ruby.
591-
# Note that this is effectively masking only the three common flags that are
592-
# used in Ruby, and does not include the full set of flags like encoding.
590+
# the regular expression.
593591
def options
594-
flags & 0b111
592+
o = flags & 0b111
593+
o |= Regexp::FIXEDENCODING if flags & 8 != 0 # 'e'
594+
o |= Regexp::FIXEDENCODING if flags & 32 != 0 # 's'
595+
o |= Regexp::FIXEDENCODING if flags & 64 != 0 # 'u'
596+
o |= Regexp::NOENCODING if flags & 16 != 0 # 'n'
597+
o
595598
end
596599
end
597600

@@ -604,11 +607,14 @@ def value
604607

605608
class RegularExpressionNode < Node
606609
# Returns a numeric value that represents the flags that were used to create
607-
# the regular expression. This mirrors the Regexp#options method in Ruby.
608-
# Note that this is effectively masking only the three common flags that are
609-
# used in Ruby, and does not include the full set of flags like encoding.
610+
# the regular expression.
610611
def options
611-
flags & 0b111
612+
o = flags & 0b111
613+
o |= Regexp::FIXEDENCODING if flags & 8 != 0 # 'e'
614+
o |= Regexp::FIXEDENCODING if flags & 32 != 0 # 's'
615+
o |= Regexp::FIXEDENCODING if flags & 64 != 0 # 'u'
616+
o |= Regexp::NOENCODING if flags & 16 != 0 # 'n'
617+
o
612618
end
613619
end
614620
end

test/yarp/regexp_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,20 @@ def test_flag_multiline
208208
assert_equal(Regexp::MULTILINE, options("m"))
209209
end
210210

211+
def test_flag_fixedencoding
212+
assert_equal(Regexp::FIXEDENCODING, options("e"))
213+
assert_equal(Regexp::FIXEDENCODING, options("u"))
214+
assert_equal(Regexp::FIXEDENCODING, options("s"))
215+
end
216+
217+
def test_flag_noencoding
218+
assert_equal(Regexp::NOENCODING, options("n"))
219+
end
220+
221+
def test_flag_once
222+
assert_equal(0, options("o"))
223+
end
224+
211225
def test_flag_combined
212226
value = Regexp::IGNORECASE | Regexp::MULTILINE | Regexp::EXTENDED
213227
assert_equal(value, options("mix"))

0 commit comments

Comments
 (0)