Skip to content
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
6 changes: 4 additions & 2 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -12724,10 +12724,11 @@ rb_str_partition(VALUE str, VALUE sep)
static VALUE
rb_str_rpartition(VALUE str, VALUE sep)
{
long pos = RSTRING_LEN(str);
long pos;

sep = get_pat_quoted(sep, 0);
if (RB_TYPE_P(sep, T_REGEXP)) {
pos = RSTRING_LEN(str);
if (rb_reg_search(sep, str, pos, 1) < 0) {
goto failed;
}
Expand All @@ -12737,7 +12738,8 @@ rb_str_rpartition(VALUE str, VALUE sep)
sep = rb_str_subseq(str, pos, RMATCH_END(match, 0) - pos);
}
else {
pos = rb_str_sublen(str, pos);
/* str may have been modified by #to_str above */
pos = rb_str_sublen(str, RSTRING_LEN(str));
pos = rb_str_rindex(str, sep, pos);
if (pos < 0) {
goto failed;
Expand Down
11 changes: 11 additions & 0 deletions test/ruby/test_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,17 @@ def (hyphen = Object.new).to_str; "-"; end
assert_equal(["", "", s], s.rpartition(sep))
end

def test_rpartition_string_modified
str = S("héllo" * 1000)
replacement = S("hé")
obj = Object.new
obj.define_singleton_method(:to_str) do
str.replace(replacement)
"-"
end
assert_equal([S(""), S(""), replacement], str.rpartition(obj))
end

def test_rs
return unless @cls == String

Expand Down