From 041c59e7ebf99dc4bcff4b8f94142a04d99e4ed2 Mon Sep 17 00:00:00 2001 From: Igor Novokshonov Date: Thu, 8 Apr 2021 21:31:31 +0200 Subject: [PATCH 1/2] Fix rpartition result when regexp doesn't match --- spec/ruby/core/string/rpartition_spec.rb | 8 ++++++++ src/main/ruby/truffleruby/core/string.rb | 10 +++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/spec/ruby/core/string/rpartition_spec.rb b/spec/ruby/core/string/rpartition_spec.rb index c6428636f6a1..6979114d5632 100644 --- a/spec/ruby/core/string/rpartition_spec.rb +++ b/spec/ruby/core/string/rpartition_spec.rb @@ -11,6 +11,14 @@ "hello".rpartition("hello").should == ["", "hello", ""] end + it "returns original string if regexp doesn't match" do + "hello".rpartition("/x/").should == ["", "", "hello"] + end + + it "returns new object if doesn't match" do + "hello".rpartition("/no_match/").last.object_id.should_not eql("hello".object_id) + end + it "accepts regexp" do "hello!".rpartition(/l./).should == ["hel", "lo", "!"] end diff --git a/src/main/ruby/truffleruby/core/string.rb b/src/main/ruby/truffleruby/core/string.rb index 883d4c2ed2a9..bdf489d527cf 100644 --- a/src/main/ruby/truffleruby/core/string.rb +++ b/src/main/ruby/truffleruby/core/string.rb @@ -205,7 +205,7 @@ def rpartition(pattern) if pattern.kind_of? Regexp if m = Truffle::RegexpOperations.search_region(pattern, self, 0, size, false) Primitive.regexp_last_match_set(Primitive.caller_special_variables, m) - [m.pre_match, m[0], m.post_match] + return [m.pre_match, m[0], m.post_match] end else pattern = StringValue(pattern) @@ -217,11 +217,11 @@ def rpartition(pattern) pattern.dup, Primitive.string_substring(self, post_start, post_len)] end - - # Nothing worked out, this is the default. - empty = String.new(encoding: encoding) - [empty, empty.dup, self] end + + # Nothing worked out, this is the default. + empty = String.new(encoding: encoding) + [empty, empty.dup, self.dup] end def rstrip From abfe81768787e5d8c40ec83d3b6f223369fb31e3 Mon Sep 17 00:00:00 2001 From: Igor Novokshonov Date: Fri, 9 Apr 2021 17:39:48 +0200 Subject: [PATCH 2/2] Fix multibyte handling --- CHANGELOG.md | 1 + spec/ruby/core/string/rpartition_spec.rb | 4 ++++ src/main/ruby/truffleruby/core/string.rb | 2 +- test/mri/excludes/TestString.rb | 1 - 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f459df230003..6d9f7c19b3d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Bug fixes: Compatibility: * Make interpolated strings frozen for compatibility with Ruby 2.7 (#2304, @kirs). +* Make rpartition compatible with Ruby 2.7 (#2320, @gogainda). Performance: diff --git a/spec/ruby/core/string/rpartition_spec.rb b/spec/ruby/core/string/rpartition_spec.rb index 6979114d5632..b2f5e54ee776 100644 --- a/spec/ruby/core/string/rpartition_spec.rb +++ b/spec/ruby/core/string/rpartition_spec.rb @@ -19,6 +19,10 @@ "hello".rpartition("/no_match/").last.object_id.should_not eql("hello".object_id) end + it "handles multibyte string correctly" do + "ユーザ@ドメイン".rpartition(/@/).should == ["ユーザ", "@", "ドメイン"] + end + it "accepts regexp" do "hello!".rpartition(/l./).should == ["hel", "lo", "!"] end diff --git a/src/main/ruby/truffleruby/core/string.rb b/src/main/ruby/truffleruby/core/string.rb index bdf489d527cf..c8622599d600 100644 --- a/src/main/ruby/truffleruby/core/string.rb +++ b/src/main/ruby/truffleruby/core/string.rb @@ -203,7 +203,7 @@ def partition(pattern=nil) def rpartition(pattern) if pattern.kind_of? Regexp - if m = Truffle::RegexpOperations.search_region(pattern, self, 0, size, false) + if m = Truffle::RegexpOperations.search_region(pattern, self, 0, bytesize, false) Primitive.regexp_last_match_set(Primitive.caller_special_variables, m) return [m.pre_match, m[0], m.post_match] end diff --git a/test/mri/excludes/TestString.rb b/test/mri/excludes/TestString.rb index 2ca455e5d399..5c754197c3e8 100644 --- a/test/mri/excludes/TestString.rb +++ b/test/mri/excludes/TestString.rb @@ -14,7 +14,6 @@ exclude :test_lines, "needs investigation" exclude :test_partition, "needs investigation" exclude :test_respond_to, "needs investigation" -exclude :test_rpartition, "needs investigation" exclude :test_rstrip, "needs investigation" exclude :test_rstrip_bang, "needs investigation" exclude :test_setter, "needs investigation"