Skip to content

Commit 3b351ee

Browse files
committed
String#slice! should clear the coderange when truncating
[Bug #19739] This bug was incidentally fixed in Ruby 3.2 via b0b9f72 but remains on 3.1 and older.
1 parent 504f37a commit 3b351ee

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

string.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5435,6 +5435,7 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
54355435
slen -= len;
54365436
STR_SET_LEN(str, slen);
54375437
TERM_FILL(&sptr[slen], TERM_LEN(str));
5438+
ENC_CODERANGE_CLEAR(str);
54385439
}
54395440
}
54405441
return result;

test/ruby/test_string.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,6 +3232,12 @@ def test_substr_code_range
32323232
assert_not_predicate(data, :valid_encoding?)
32333233
assert_predicate(data[100..-1], :valid_encoding?)
32343234
end
3235+
3236+
def test_slice_bang_code_range
3237+
str = "[Bug #19739] ABC OÜ"
3238+
str.slice!(/ oü$/i)
3239+
assert_predicate str, :ascii_only?
3240+
end
32353241
end
32363242

32373243
class TestString2 < TestString

0 commit comments

Comments
 (0)