Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions optional/capi/ext/string_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,14 @@ static VALUE string_spec_rb_str_to_interned_str(VALUE self, VALUE str) {
return rb_str_to_interned_str(str);
}

static VALUE string_spec_rb_interned_str(VALUE self, VALUE str, VALUE len) {
return rb_interned_str(RSTRING_PTR(str), FIX2LONG(len));
}

static VALUE string_spec_rb_interned_str_cstr(VALUE self, VALUE str) {
return rb_interned_str_cstr(RSTRING_PTR(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 @@ -681,6 +689,8 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_enc_interned_str_cstr", string_spec_rb_enc_interned_str_cstr, 2);
rb_define_method(cls, "rb_enc_interned_str", string_spec_rb_enc_interned_str, 3);
rb_define_method(cls, "rb_str_to_interned_str", string_spec_rb_str_to_interned_str, 1);
rb_define_method(cls, "rb_interned_str", string_spec_rb_interned_str, 2);
rb_define_method(cls, "rb_interned_str_cstr", string_spec_rb_interned_str_cstr, 1);
}

#ifdef __cplusplus
Expand Down
125 changes: 125 additions & 0 deletions optional/capi/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1369,8 +1369,133 @@ def inspect
result1.should_not.equal?(result2)
end

it "preserves the encoding of the original string" do
result1 = @s.rb_str_to_interned_str("hello".dup.force_encoding(Encoding::US_ASCII))
result2 = @s.rb_str_to_interned_str("hello".dup.force_encoding(Encoding::UTF_8))
result1.encoding.should == Encoding::US_ASCII
result2.encoding.should == Encoding::UTF_8
end

it "returns the same string as String#-@" do
@s.rb_str_to_interned_str("hello").should.equal?(-"hello")
end
end

describe "rb_interned_str" do
it "returns a frozen string" do
str = "hello"
result = @s.rb_interned_str(str, str.bytesize)
result.should.is_a?(String)
result.should.frozen?
result.encoding.should == Encoding::US_ASCII
end

it "returns the same frozen string" do
str = "hello"
result1 = @s.rb_interned_str(str, str.bytesize)
result2 = @s.rb_interned_str(str, str.bytesize)
result1.should.equal?(result2)
end

it "supports strings with embedded null bytes" do
str = "foo\x00bar\x00baz".b
result = @s.rb_interned_str(str, str.bytesize)
result.should == str
end

it "return US_ASCII encoding for an empty string" do
result = @s.rb_interned_str("", 0)
result.should == ""
result.encoding.should == Encoding::US_ASCII
end

it "returns US_ASCII encoding for strings of only 7 bit ASCII" do
0x00.upto(0x7f).each do |char|
result = @s.rb_interned_str(char.chr, 1)
result.encoding.should == Encoding::US_ASCII
end
end

ruby_bug "21842", ""..."4.1" do
it "returns BINARY encoding for strings that use the 8th bit" do
0x80.upto(0xff) do |char|
result = @s.rb_interned_str(char.chr, 1)
result.encoding.should == Encoding::BINARY
end
end
end

it 'returns the same string when using non-ascii characters' do
str = 'こんにちは'
result1 = @s.rb_interned_str(str, str.bytesize)
result2 = @s.rb_interned_str(str, str.bytesize)
result1.should.equal?(result2)
end

ruby_bug "21842", ""..."4.1" do
it "returns the same string as String#-@" do
str = "hello".dup.force_encoding(Encoding::US_ASCII)
@s.rb_interned_str(str, str.bytesize).should.equal?(-str)
end
end
end

describe "rb_interned_str_cstr" do
it "returns a frozen string" do
str = "hello"
result = @s.rb_interned_str_cstr(str)
result.should.is_a?(String)
result.should.frozen?
result.encoding.should == Encoding::US_ASCII
end

it "returns the same frozen string" do
str = "hello"
result1 = @s.rb_interned_str_cstr(str)
result2 = @s.rb_interned_str_cstr(str)
result1.should.equal?(result2)
end

it "does not support strings with embedded null bytes" do
str = "foo\x00bar\x00baz".b
result = @s.rb_interned_str_cstr(str)
result.should == "foo"
end

it "return US_ASCII encoding for an empty string" do
result = @s.rb_interned_str_cstr("")
result.should == ""
result.encoding.should == Encoding::US_ASCII
end

it "returns US_ASCII encoding for strings of only 7 bit ASCII" do
0x01.upto(0x7f).each do |char|
result = @s.rb_interned_str_cstr(char.chr)
result.encoding.should == Encoding::US_ASCII
end
end

ruby_bug "21842", ""..."4.1" do
it "returns BINARY encoding for strings that use the 8th bit" do
0x80.upto(0xff) do |char|
result = @s.rb_interned_str_cstr(char.chr)
result.encoding.should == Encoding::BINARY
end
end
end

it 'returns the same string when using non-ascii characters' do
str = 'こんにちは'
result1 = @s.rb_interned_str_cstr(str)
result2 = @s.rb_interned_str_cstr(str)
result1.should.equal?(result2)
end

ruby_bug "21842", ""..."4.1" do
it "returns the same string as String#-@" do
str = "hello".dup.force_encoding(Encoding::US_ASCII)
@s.rb_interned_str_cstr(str).should.equal?(-str)
end
end
end
end