Skip to content

Commit 40c6850

Browse files
Add C API rb_str_to_interned_str function
1 parent 6e48998 commit 40c6850

File tree

5 files changed

+27
-1
lines changed

5 files changed

+27
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Compatibility:
4545
* Support passing anonymous * and ** parameters as method call arguments (#3039, @andrykonchin).
4646
* Handle either positional or keywords arguments by default in `Struct.new` (#3039, @rwstauner).
4747
* Add `rb_enc_interned_str_cstr` function (#3408, @goyox86, @thomasmarshall).
48+
* Add `rb_str_to_interned_str` function (#3408, @thomasmarshall).
4849

4950
Performance:
5051

lib/cext/ABI_check.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
11
1+
12

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,10 @@ static VALUE string_spec_rb_enc_interned_str_cstr(VALUE self, VALUE str, VALUE e
589589
return rb_enc_interned_str_cstr(RSTRING_PTR(str), e);
590590
}
591591

592+
static VALUE string_spec_rb_str_to_interned_str(VALUE self, VALUE str) {
593+
return rb_str_to_interned_str(str);
594+
}
595+
592596
void Init_string_spec(void) {
593597
VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject);
594598
rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2);
@@ -691,6 +695,7 @@ void Init_string_spec(void) {
691695
rb_define_method(cls, "rb_str_locktmp", string_spec_rb_str_locktmp, 1);
692696
rb_define_method(cls, "rb_str_unlocktmp", string_spec_rb_str_unlocktmp, 1);
693697
rb_define_method(cls, "rb_enc_interned_str_cstr", string_spec_rb_enc_interned_str_cstr, 2);
698+
rb_define_method(cls, "rb_str_to_interned_str", string_spec_rb_str_to_interned_str, 1);
694699
}
695700

696701
#ifdef __cplusplus

spec/ruby/optional/capi/string_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,4 +1252,20 @@ def inspect
12521252
result1.should_not.equal?(result2)
12531253
end
12541254
end
1255+
1256+
describe "rb_str_to_interned_str" do
1257+
it "returns a frozen string" do
1258+
str = "hello"
1259+
result = @s.rb_str_to_interned_str(str)
1260+
result.should.is_a?(String)
1261+
result.should.frozen?
1262+
end
1263+
1264+
it "returns the same frozen string" do
1265+
str = "hello"
1266+
result1 = @s.rb_str_to_interned_str(str)
1267+
result2 = @s.rb_str_to_interned_str(str)
1268+
result1.should.equal?(result2)
1269+
end
1270+
end
12551271
end

src/main/c/cext/string.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,3 +442,7 @@ VALUE rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc) {
442442
VALUE str = rb_enc_str_new_cstr(ptr, enc);
443443
return rb_fstring(str);
444444
}
445+
446+
VALUE rb_str_to_interned_str(VALUE str) {
447+
return rb_fstring(str);
448+
}

0 commit comments

Comments
 (0)