Skip to content

Commit f4dd8da

Browse files
committed
Fix loading/parsing regular expressions
This fixes the issue where regular expression would come back slightly different after going through a YAML load/dump cycle. Because we're used to having to escape forward slashes in regular expression literals (because the literal is delimited by slashes), but the deserializer takes the literal output from `Regexp#inspect` and feeds it as a string into `Regexp.new`, which expects a string, not a Regexp literal, cycling did not properly work before this commit. I've also changed the code to be a bit more readable, I hope this doesn't affect performance.
1 parent 5605134 commit f4dd8da

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

lib/psych/visitors/to_ruby.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ def deserialize o
9696
Float(@ss.tokenize(o.value))
9797
when "!ruby/regexp"
9898
klass = class_loader.regexp
99-
o.value =~ /^\/(.*)\/([mixn]*)$/m
100-
source = $1
99+
matches = /^\/(?<string>.*)\/(?<options>[mixn]*)$/m.match(o.value)
100+
source = matches[:string].gsub('\/', '/')
101101
options = 0
102102
lang = nil
103-
$2&.each_char do |option|
103+
matches[:options].each_char do |option|
104104
case option
105105
when 'x' then options |= Regexp::EXTENDED
106106
when 'i' then options |= Regexp::IGNORECASE

test/psych/test_yaml.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def test_multiline_regexp
3535
assert_cycle(Regexp.new("foo\nbar"))
3636
end
3737

38+
def test_regexp_with_slash
39+
assert_cycle(Regexp.new('/'))
40+
end
41+
3842
# [ruby-core:34969]
3943
def test_regexp_with_n
4044
assert_cycle(Regexp.new('',Regexp::NOENCODING))

0 commit comments

Comments
 (0)