Skip to content

Implement rb_enc_interned_str #3706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Compatibility:
* Support the index/length arguments for the string argument to `String#bytesplice` added in 3.3 (#3656, @rwstauner).
* Implement `rb_str_strlen()` (#3697, @Th3-M4jor).
* Support `Time.new` with String argument and error when invalid (#3693, @rwstauner).
* Implement `rb_enc_interned_str()` (#3703, @Th3-M4jor).

Performance:

Expand Down
2 changes: 1 addition & 1 deletion lib/cext/include/truffleruby/truffleruby-abi-version.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
// $RUBY_VERSION must be the same as TruffleRuby.LANGUAGE_VERSION.
// $ABI_NUMBER starts at 1 and is incremented for every ABI-incompatible change.

#define TRUFFLERUBY_ABI_VERSION "3.2.4.5"
#define TRUFFLERUBY_ABI_VERSION "3.2.4.6"

#endif
6 changes: 6 additions & 0 deletions spec/ruby/optional/capi/ext/string_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,11 @@ 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_enc_interned_str(VALUE self, VALUE str, VALUE len, VALUE enc) {
rb_encoding *e = NIL_P(enc) ? 0 : rb_to_encoding(enc);
return rb_enc_interned_str(RSTRING_PTR(str), FIX2LONG(len), e);
}

static VALUE string_spec_rb_str_to_interned_str(VALUE self, VALUE str) {
return rb_str_to_interned_str(str);
}
Expand Down Expand Up @@ -687,6 +692,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_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);
}

Expand Down
45 changes: 45 additions & 0 deletions spec/ruby/optional/capi/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,51 @@ def inspect
end
end

describe "rb_enc_interned_str" do
it "returns a frozen string" do
str = "hello"
val = @s.rb_enc_interned_str(str, str.bytesize, Encoding::US_ASCII)

val.should.is_a?(String)
val.encoding.should == Encoding::US_ASCII
val.should.frozen?
end

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

it "returns different frozen strings for different encodings" do
str = "hello"
result1 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::US_ASCII)
result2 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::UTF_8)
result1.should_not.equal?(result2)
end

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

it "returns the same string as String#-@" do
str = "hello"
@s.rb_enc_interned_str(str, str.bytesize, Encoding::UTF_8).should.equal?(-str)
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(str, str.bytesize, nil)
val.encoding.should == Encoding::ASCII_8BIT
end
end
end

describe "rb_str_to_interned_str" do
it "returns a frozen string" do
str = "hello"
Expand Down
5 changes: 5 additions & 0 deletions src/main/c/cext/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ VALUE rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc) {
return rb_str_to_interned_str(str);
}

VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc) {
VALUE str = rb_enc_str_new(ptr, len, enc ? enc : rb_ascii8bit_encoding());
return rb_str_to_interned_str(str);
}

VALUE rb_str_to_interned_str(VALUE str) {
return RUBY_INVOKE(str, "-@");
}
Loading