Skip to content

Commit

Permalink
Support null encoding for rb_enc_interned_str_cstr
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thomasmarshall committed Mar 13, 2024
1 parent 88e89c3 commit 2dea520
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Bug fixes:

Compatibility:

* Allow null encoding pointer in `rb_enc_interned_str_cstr` (@thomasmarshall).

Performance:

Changes:
Expand Down
2 changes: 1 addition & 1 deletion lib/cext/ABI_check.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
13
14
2 changes: 1 addition & 1 deletion spec/ruby/optional/capi/ext/string_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ static VALUE string_spec_rb_str_unlocktmp(VALUE self, VALUE str) {
}

static VALUE string_spec_rb_enc_interned_str_cstr(VALUE self, VALUE str, VALUE enc) {
rb_encoding *e = rb_to_encoding(enc);
rb_encoding *e = NIL_P(enc) ? 0 : rb_to_encoding(enc);
return rb_enc_interned_str_cstr(RSTRING_PTR(str), e);
}

Expand Down
8 changes: 8 additions & 0 deletions spec/ruby/optional/capi/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,14 @@ def inspect
it "returns the same string as String#-@" do
@s.rb_enc_interned_str_cstr("hello", Encoding::UTF_8).should.equal?(-"hello")
end

ruby_bug "#20322", ""..."3.4" do
it "uses the default encoding if encoding is null" do
str = "hello"
val = @s.rb_enc_interned_str_cstr(str, nil)
val.encoding.should == Encoding::ASCII_8BIT
end
end
end

describe "rb_str_to_interned_str" do
Expand Down
2 changes: 1 addition & 1 deletion src/main/c/cext/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ long rb_str_coderange_scan_restartable(const char *s, const char *e, rb_encoding
}

VALUE rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc) {
VALUE str = rb_enc_str_new_cstr(ptr, enc);
VALUE str = rb_enc_str_new_cstr(ptr, enc ? enc : rb_ascii8bit_encoding());
return rb_str_to_interned_str(str);
}

Expand Down

0 comments on commit 2dea520

Please sign in to comment.