Skip to content

Commit

Permalink
Basic support for regexp in Marshal.dump
Browse files Browse the repository at this point in the history
We forgot to output the `/` byte to mark it as a regexp type.
  • Loading branch information
herwinw committed Sep 29, 2024
1 parent 1668e40 commit 563a05c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
8 changes: 2 additions & 6 deletions spec/core/marshal/dump_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -390,15 +390,11 @@ def _dump(level)

describe "with a Regexp" do
it "dumps a Regexp" do
NATFIXME 'dumps a Regexp', exception: SpecFailedException do
Marshal.dump(/\A.\Z/).should == "\x04\bI/\n\\A.\\Z\x00\x06:\x06EF"
end
Marshal.dump(/\A.\Z/).should == "\x04\bI/\n\\A.\\Z\x00\x06:\x06EF"
end

it "dumps a Regexp with flags" do
NATFIXME 'dumps a Regexp with flags', exception: SpecFailedException do
Marshal.dump(//im).should == "\x04\bI/\x00\x05\x06:\x06EF"
end
Marshal.dump(//im).should == "\x04\bI/\x00\x05\x06:\x06EF"
end

it "dumps a Regexp with instance variables" do
Expand Down
12 changes: 5 additions & 7 deletions spec/core/marshal/shared/load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ def io.binmode; raise "binmode"; end

it "preserves Regexp encoding" do
source_object = Regexp.new("a".encode("utf-32le"))
NATFIXME 'Support regexp dump', exception: ArgumentError, message: 'dump format error' do
NATFIXME 'Support encoding in regexp dump', exception: SpecFailedException do
regexp = Marshal.send(@method, Marshal.dump(source_object))

regexp.encoding.should == Encoding::UTF_32LE
Expand All @@ -1077,13 +1077,11 @@ def io.binmode; raise "binmode"; end
end

it "raises ArgumentError when end of byte sequence reached before source string end" do
NATFIXME 'raises ArgumentError when end of byte sequence reached before source string end', exception: SpecFailedException do
Marshal.dump(/hello world/).should == "\x04\bI/\x10hello world\x00\x06:\x06EF"
Marshal.dump(/hello world/).should == "\x04\bI/\x10hello world\x00\x06:\x06EF"

-> {
Marshal.send(@method, "\x04\bI/\x10hel")
}.should raise_error(ArgumentError, "marshal data too short")
end
-> {
Marshal.send(@method, "\x04\bI/\x10hel")
}.should raise_error(ArgumentError, "marshal data too short")
end
end

Expand Down
1 change: 1 addition & 0 deletions src/marshal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def write_module(value)

def write_regexp(value)
write_char('I')
write_char('/')
write_string_bytes(value.source)
write_byte(value.options)
write_encoding_bytes(value)
Expand Down

0 comments on commit 563a05c

Please sign in to comment.