Skip to content

Commit f89d745

Browse files
committed
Add specs for rb_interned_str and rb_interned_str_cstr
1 parent b833642 commit f89d745

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

optional/capi/ext/string_spec.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,14 @@ static VALUE string_spec_rb_str_to_interned_str(VALUE self, VALUE str) {
581581
return rb_str_to_interned_str(str);
582582
}
583583

584+
static VALUE string_spec_rb_interned_str(VALUE self, VALUE str, VALUE len) {
585+
return rb_interned_str(RSTRING_PTR(str), FIX2LONG(len));
586+
}
587+
588+
static VALUE string_spec_rb_interned_str_cstr(VALUE self, VALUE str) {
589+
return rb_interned_str_cstr(RSTRING_PTR(str));
590+
}
591+
584592
void Init_string_spec(void) {
585593
VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject);
586594
rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2);
@@ -681,6 +689,8 @@ void Init_string_spec(void) {
681689
rb_define_method(cls, "rb_enc_interned_str_cstr", string_spec_rb_enc_interned_str_cstr, 2);
682690
rb_define_method(cls, "rb_enc_interned_str", string_spec_rb_enc_interned_str, 3);
683691
rb_define_method(cls, "rb_str_to_interned_str", string_spec_rb_str_to_interned_str, 1);
692+
rb_define_method(cls, "rb_interned_str", string_spec_rb_interned_str, 2);
693+
rb_define_method(cls, "rb_interned_str_cstr", string_spec_rb_interned_str_cstr, 1);
684694
}
685695

686696
#ifdef __cplusplus

optional/capi/string_spec.rb

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,4 +1373,106 @@ def inspect
13731373
@s.rb_str_to_interned_str("hello").should.equal?(-"hello")
13741374
end
13751375
end
1376+
1377+
describe "rb_interned_str" do
1378+
it "returns a frozen string" do
1379+
str = "hello"
1380+
result = @s.rb_interned_str(str, str.bytesize)
1381+
result.should.is_a?(String)
1382+
result.should.frozen?
1383+
result.encoding.should == Encoding::US_ASCII
1384+
end
1385+
1386+
it "returns the same frozen string" do
1387+
str = "hello"
1388+
result1 = @s.rb_interned_str(str, str.bytesize)
1389+
result2 = @s.rb_interned_str(str, str.bytesize)
1390+
result1.should.equal?(result2)
1391+
end
1392+
1393+
it "supports strings with embedded null bytes" do
1394+
str = "foo\x00bar\x00baz".b
1395+
result = @s.rb_interned_str(str, str.bytesize)
1396+
result.should == str
1397+
end
1398+
1399+
it "support binary strings that are invalid in ASCII encoding" do
1400+
str = "foo\x81bar\x82baz".b
1401+
result = @s.rb_interned_str(str, str.bytesize)
1402+
result.encoding.should == Encoding::US_ASCII
1403+
result.should == str.dup.force_encoding(Encoding::US_ASCII)
1404+
result.should_not.valid_encoding?
1405+
end
1406+
1407+
it "returns the same frozen strings for different encodings" do
1408+
str1 = "hello".dup.force_encoding(Encoding::US_ASCII)
1409+
str2 = "hello".dup.force_encoding(Encoding::UTF_8)
1410+
result1 = @s.rb_interned_str(str1, str1.bytesize)
1411+
result2 = @s.rb_interned_str(str2, str2.bytesize)
1412+
result1.should.equal?(result2)
1413+
end
1414+
1415+
it 'returns the same string when using non-ascii characters' do
1416+
str = 'こんにちは'
1417+
result1 = @s.rb_interned_str(str, str.bytesize)
1418+
result2 = @s.rb_interned_str(str, str.bytesize)
1419+
result1.should.equal?(result2)
1420+
end
1421+
1422+
it "returns the same string as String#-@" do
1423+
str = "hello".dup.force_encoding(Encoding::US_ASCII)
1424+
@s.rb_interned_str(str, str.bytesize).should.equal?(-str)
1425+
end
1426+
end
1427+
1428+
describe "rb_interned_str_cstr" do
1429+
it "returns a frozen string" do
1430+
str = "hello"
1431+
result = @s.rb_interned_str_cstr(str)
1432+
result.should.is_a?(String)
1433+
result.should.frozen?
1434+
result.encoding.should == Encoding::US_ASCII
1435+
end
1436+
1437+
it "returns the same frozen string" do
1438+
str = "hello"
1439+
result1 = @s.rb_interned_str_cstr(str)
1440+
result2 = @s.rb_interned_str_cstr(str)
1441+
result1.should.equal?(result2)
1442+
end
1443+
1444+
it "does not support strings with embedded null bytes" do
1445+
str = "foo\x00bar\x00baz".b
1446+
result = @s.rb_interned_str_cstr(str)
1447+
result.should == "foo"
1448+
end
1449+
1450+
it "support binary strings that are invalid in ASCII encoding" do
1451+
str = "foo\x81bar\x82baz".b
1452+
result = @s.rb_interned_str_cstr(str)
1453+
result.encoding.should == Encoding::US_ASCII
1454+
result.should == str.dup.force_encoding(Encoding::US_ASCII)
1455+
result.should_not.valid_encoding?
1456+
end
1457+
1458+
it "returns the same frozen strings for different encodings" do
1459+
str1 = "hello".dup.force_encoding(Encoding::US_ASCII)
1460+
str2 = "hello".dup.force_encoding(Encoding::UTF_8)
1461+
result1 = @s.rb_interned_str_cstr(str1)
1462+
result2 = @s.rb_interned_str_cstr(str2)
1463+
result1.should.equal?(result2)
1464+
end
1465+
1466+
it 'returns the same string when using non-ascii characters' do
1467+
str = 'こんにちは'
1468+
result1 = @s.rb_interned_str_cstr(str)
1469+
result2 = @s.rb_interned_str_cstr(str)
1470+
result1.should.equal?(result2)
1471+
end
1472+
1473+
it "returns the same string as String#-@" do
1474+
str = "hello".dup.force_encoding(Encoding::US_ASCII)
1475+
@s.rb_interned_str_cstr(str).should.equal?(-str)
1476+
end
1477+
end
13761478
end

0 commit comments

Comments
 (0)