diff options
| author | Martin Meyerhoff <mamhoff@gmail.com> | 2025-02-04 18:00:44 +0100 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2025-05-01 17:50:13 +0000 |
| commit | bd1d6e8cd725e84addfac8583634458a624929ae (patch) | |
| tree | 1fd5bc9ac5d510a47e1fececdb3bba6d28c1060d | |
| parent | 67b91e780798b80038dbfb39a06831918a75259f (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://github.com/ruby/psych/commit/f4dd8dadad
| -rw-r--r-- | ext/psych/lib/psych/visitors/to_ruby.rb | 6 | ||||
| -rw-r--r-- | test/psych/test_yaml.rb | 4 |
2 files changed, 7 insertions, 3 deletions
diff --git a/ext/psych/lib/psych/visitors/to_ruby.rb b/ext/psych/lib/psych/visitors/to_ruby.rb index f0b4a94e45..d8a203993b 100644 --- a/ext/psych/lib/psych/visitors/to_ruby.rb +++ b/ext/psych/lib/psych/visitors/to_ruby.rb @@ -96,11 +96,11 @@ module Psych Float(@ss.tokenize(o.value)) when "!ruby/regexp" klass = class_loader.regexp - o.value =~ /^\/(.*)\/([mixn]*)$/m - source = $1 + matches = /^\/(?<string>.*)\/(?<options>[mixn]*)$/m.match(o.value) + source = matches[:string].gsub('\/', '/') options = 0 lang = nil - $2&.each_char do |option| + matches[:options].each_char do |option| case option when 'x' then options |= Regexp::EXTENDED when 'i' then options |= Regexp::IGNORECASE diff --git a/test/psych/test_yaml.rb b/test/psych/test_yaml.rb index 897a7c8935..bb9b25f250 100644 --- a/test/psych/test_yaml.rb +++ b/test/psych/test_yaml.rb @@ -35,6 +35,10 @@ class Psych_Unit_Tests < Psych::TestCase assert_cycle(Regexp.new("foo\nbar")) end + def test_regexp_with_slash + assert_cycle(Regexp.new('/')) + end + # [ruby-core:34969] def test_regexp_with_n assert_cycle(Regexp.new('',Regexp::NOENCODING)) |
