Skip to content

Commit 2dea520

Browse files
Support null encoding for rb_enc_interned_str_cstr
This commit adds support for a null pointer to be passed as the encoding argument to rb_enc_interned_str_cstr. When we implemented this function originally, we noticed that the CRuby behavior did not match the header documentation, so we opted not to support it. However, this has now been fixed in CRuby [1], so we can fix it here as well. [1]: ruby/ruby#10169
1 parent 88e89c3 commit 2dea520

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
@@ -9,6 +9,8 @@ Bug fixes:
99

1010
Compatibility:
1111

12+
* Allow null encoding pointer in `rb_enc_interned_str_cstr` (@thomasmarshall).
13+
1214
Performance:
1315

1416
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)