Skip to content

Commit

Permalink
[GR-18163] Implement rb_enc_interned_str (#3706)
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4393
  • Loading branch information
andrykonchin authored and eregon committed Nov 7, 2024
2 parents 15845e8 + ee9b6cb commit a4e5318
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
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, "-@");
}

0 comments on commit a4e5318

Please sign in to comment.