aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorDustin Brown <dbrown9@gmail.com>2023-12-06 19:25:29 -0800
committerGitHub <noreply@github.com>2023-12-06 19:25:29 -0800
commitd89280e8bf6496aa83326b5f9c293724bd1cc1e9 (patch)
treef9d0e1db3f273cf90f1c68ed7b20923c0586d52b /test
parent1ace218690d76496c5a54c45691515f8f6410a49 (diff)
downloadruby-d89280e8bf6496aa83326b5f9c293724bd1cc1e9.tar.gz
Copy encoding flags when copying a regex [Bug #20039]
* :bug: Fixes [Bug #20039](https://bugs.ruby-lang.org/issues/20039) When a Regexp is initialized with another Regexp, we simply copy the properties from the original. However, the flags on the original were not being copied correctly. This caused an issue when the original had multibyte characters and was being compared with an ASCII string. Without the forced encoding flag (`KCODE_FIXED`) transferred on to the new Regexp, the comparison would fail. See the included test for an example. Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_regexp.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb
index 7dff4c25f2..4d94702502 100644
--- a/test/ruby/test_regexp.rb
+++ b/test/ruby/test_regexp.rb
@@ -1936,6 +1936,16 @@ class TestRegexp < Test::Unit::TestCase
assert_equal("123456789".match(/(?:x?\dx?){2,}/)[0], "123456789")
end
+ def test_encoding_flags_are_preserved_when_initialized_with_another_regexp
+ re = Regexp.new("\u2018hello\u2019".encode("UTF-8"))
+ str = "".encode("US-ASCII")
+
+ assert_nothing_raised do
+ str.match?(re)
+ str.match?(Regexp.new(re))
+ end
+ end
+
def test_bug_19537 # [Bug #19537]
str = 'aac'
re = '^([ab]{1,3})(a?)*$'