Skip to content

Commit

Permalink
Add C API rb_str_to_interned_str function
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmarshall committed Feb 2, 2024
1 parent 6e48998 commit 40c6850
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Compatibility:
* Support passing anonymous * and ** parameters as method call arguments (#3039, @andrykonchin).
* Handle either positional or keywords arguments by default in `Struct.new` (#3039, @rwstauner).
* Add `rb_enc_interned_str_cstr` function (#3408, @goyox86, @thomasmarshall).
* Add `rb_str_to_interned_str` function (#3408, @thomasmarshall).

Performance:

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 @@
11
12
5 changes: 5 additions & 0 deletions spec/ruby/optional/capi/ext/string_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,10 @@ static VALUE string_spec_rb_enc_interned_str_cstr(VALUE self, VALUE str, VALUE e
return rb_enc_interned_str_cstr(RSTRING_PTR(str), e);
}

static VALUE string_spec_rb_str_to_interned_str(VALUE self, VALUE str) {
return rb_str_to_interned_str(str);
}

void Init_string_spec(void) {
VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject);
rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2);
Expand Down Expand Up @@ -691,6 +695,7 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_str_locktmp", string_spec_rb_str_locktmp, 1);
rb_define_method(cls, "rb_str_unlocktmp", string_spec_rb_str_unlocktmp, 1);
rb_define_method(cls, "rb_enc_interned_str_cstr", string_spec_rb_enc_interned_str_cstr, 2);
rb_define_method(cls, "rb_str_to_interned_str", string_spec_rb_str_to_interned_str, 1);
}

#ifdef __cplusplus
Expand Down
16 changes: 16 additions & 0 deletions spec/ruby/optional/capi/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1252,4 +1252,20 @@ def inspect
result1.should_not.equal?(result2)
end
end

describe "rb_str_to_interned_str" do
it "returns a frozen string" do
str = "hello"
result = @s.rb_str_to_interned_str(str)
result.should.is_a?(String)
result.should.frozen?
end

it "returns the same frozen string" do
str = "hello"
result1 = @s.rb_str_to_interned_str(str)
result2 = @s.rb_str_to_interned_str(str)
result1.should.equal?(result2)
end
end
end
4 changes: 4 additions & 0 deletions src/main/c/cext/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,7 @@ VALUE rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc) {
VALUE str = rb_enc_str_new_cstr(ptr, enc);
return rb_fstring(str);
}

VALUE rb_str_to_interned_str(VALUE str) {
return rb_fstring(str);
}

0 comments on commit 40c6850

Please sign in to comment.