Skip to content

Commit

Permalink
String#slice! should clear the coderange when truncating
Browse files Browse the repository at this point in the history
[Bug #19739]

This bug was incidentally fixed in Ruby 3.2 via b0b9f72
but remains on 3.1 and older.
  • Loading branch information
byroot committed Jun 21, 2023
1 parent 504f37a commit 3b351ee
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -5435,6 +5435,7 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
slen -= len;
STR_SET_LEN(str, slen);
TERM_FILL(&sptr[slen], TERM_LEN(str));
ENC_CODERANGE_CLEAR(str);
}
}
return result;
Expand Down
6 changes: 6 additions & 0 deletions test/ruby/test_string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3232,6 +3232,12 @@ def test_substr_code_range
assert_not_predicate(data, :valid_encoding?)
assert_predicate(data[100..-1], :valid_encoding?)
end

def test_slice_bang_code_range
str = "[Bug #19739] ABC OÜ"
str.slice!(/ oü$/i)
assert_predicate str, :ascii_only?
end
end

class TestString2 < TestString
Expand Down

0 comments on commit 3b351ee

Please sign in to comment.