Skip to content

Commit 1d1556d

Browse files
committed
[GR-19220] Support null encoding for rb_enc_interned_str_cstr (#3480)
PullRequest: truffleruby/4214
2 parents 7212200 + 2dea520 commit 1d1556d

File tree

5 files changed

+13
-3
lines changed

5 files changed

+13
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Compatibility:
1414
* Fix evaluation order for multi-assignment and evaluate left-hand-side before right-hand-side (@andrykonchin).
1515
* Add `Regexp.linear_time?` method (#3039, @andrykonchin).
1616

17+
* Allow null encoding pointer in `rb_enc_interned_str_cstr` (@thomasmarshall).
18+
1719
Performance:
1820

1921
Changes:

lib/cext/ABI_check.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13
1+
14

spec/ruby/optional/capi/ext/string_spec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ static VALUE string_spec_rb_str_unlocktmp(VALUE self, VALUE str) {
573573
}
574574

575575
static VALUE string_spec_rb_enc_interned_str_cstr(VALUE self, VALUE str, VALUE enc) {
576-
rb_encoding *e = rb_to_encoding(enc);
576+
rb_encoding *e = NIL_P(enc) ? 0 : rb_to_encoding(enc);
577577
return rb_enc_interned_str_cstr(RSTRING_PTR(str), e);
578578
}
579579

spec/ruby/optional/capi/string_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,14 @@ def inspect
12361236
it "returns the same string as String#-@" do
12371237
@s.rb_enc_interned_str_cstr("hello", Encoding::UTF_8).should.equal?(-"hello")
12381238
end
1239+
1240+
ruby_bug "#20322", ""..."3.4" do
1241+
it "uses the default encoding if encoding is null" do
1242+
str = "hello"
1243+
val = @s.rb_enc_interned_str_cstr(str, nil)
1244+
val.encoding.should == Encoding::ASCII_8BIT
1245+
end
1246+
end
12391247
end
12401248

12411249
describe "rb_str_to_interned_str" do

src/main/c/cext/string.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ long rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding
439439
}
440440

441441
VALUE rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc) {
442-
VALUE str = rb_enc_str_new_cstr(ptr, enc);
442+
VALUE str = rb_enc_str_new_cstr(ptr, enc ? enc : rb_ascii8bit_encoding());
443443
return rb_str_to_interned_str(str);
444444
}
445445

0 commit comments

Comments
 (0)