summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Meyerhoff <[email protected]>2025-02-04 18:00:44 +0100
committergit <[email protected]>2025-05-01 17:50:13 +0000
commitbd1d6e8cd725e84addfac8583634458a624929ae ()
tree1fd5bc9ac5d510a47e1fececdb3bba6d28c1060d
parent67b91e780798b80038dbfb39a06831918a75259f (diff)
[ruby/psych] 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. https://.com/ruby/psych/commit/f4dd8dadad
-rw-r--r--ext/psych/lib/psych/visitors/to_ruby.rb6
-rw-r--r--test/psych/test_yaml.rb4
2 files changed, 7 insertions, 3 deletions
@@ -96,11 +96,11 @@ module Psych
Float(@ss.tokenize(o.value))
when "!ruby/regexp"
klass = class_loader.regexp
- o.value =~ /^\/(.*)\/([mixn]*)$/m
- source = $1
options = 0
lang = nil
- $2&.each_char do |option|
case option
when 'x' then options |= Regexp::EXTENDED
when 'i' then options |= Regexp::IGNORECASE
@@ -35,6 +35,10 @@ class Psych_Unit_Tests < Psych::TestCase
assert_cycle(Regexp.new("foo\nbar"))
end
# [ruby-core:34969]
def test_regexp_with_n
assert_cycle(Regexp.new('',Regexp::NOENCODING))