Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rpartition compatible with Ruby 2.7 #2320

Merged
merged 2 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
12 changes: 12 additions & 0 deletions spec/ruby/core/string/rpartition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@
"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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.last.should_not.equal?("hello") is better here, I'll change to that.
Also we need to store the first "hello" in a variable because this file does not use frozen string literals.

end

it "handles multibyte string correctly" do
"ユーザ@ドメイン".rpartition(/@/).should == ["ユーザ", "@", "ドメイン"]
end

it "accepts regexp" do
"hello!".rpartition(/l./).should == ["hel", "lo", "!"]
end
Expand Down
12 changes: 6 additions & 6 deletions src/main/ruby/truffleruby/core/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ 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)
[m.pre_match, m[0], m.post_match]
return [m.pre_match, m[0], m.post_match]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm was this always effectively a typo then? A missing return?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be, yes

end
else
pattern = StringValue(pattern)
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion test/mri/excludes/TestString.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down