Skip to content

Commit

Permalink
[GR-18163] Make rpartition compatible with Ruby 2.7 (#2320)
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/2557
  • Loading branch information
eregon committed Apr 15, 2021
2 parents 93ff961 + 874b84c commit b0e18a2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Compatibility:
* Make interpolated strings frozen for compatibility with Ruby 2.7 (#2304, @kirs).
* `require 'socket'` now also requires `'io/wait'` like CRuby (#2326).
* Support precision when formatting strings (#2281, @kirs).
* Make rpartition compatible with Ruby 2.7 (#2320, @gogainda).

Performance:

Expand Down
13 changes: 13 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,19 @@
"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
str = "hello"
str.rpartition("/no_match/").last.should_not.equal?(str)
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]
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

0 comments on commit b0e18a2

Please sign in to comment.