Skip to content

Commit a4e5318

Browse files
andrykonchineregon
authored andcommitted
[GR-18163] Implement rb_enc_interned_str (#3706)
PullRequest: truffleruby/4393
2 parents 15845e8 + ee9b6cb commit a4e5318

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Compatibility:
1818
* Support the index/length arguments for the string argument to `String#bytesplice` added in 3.3 (#3656, @rwstauner).
1919
* Implement `rb_str_strlen()` (#3697, @Th3-M4jor).
2020
* Support `Time.new` with String argument and error when invalid (#3693, @rwstauner).
21+
* Implement `rb_enc_interned_str()` (#3703, @Th3-M4jor).
2122

2223
Performance:
2324

lib/cext/include/truffleruby/truffleruby-abi-version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
// $RUBY_VERSION must be the same as TruffleRuby.LANGUAGE_VERSION.
2121
// $ABI_NUMBER starts at 1 and is incremented for every ABI-incompatible change.
2222

23-
#define TRUFFLERUBY_ABI_VERSION "3.2.4.5"
23+
#define TRUFFLERUBY_ABI_VERSION "3.2.4.6"
2424

2525
#endif

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,11 @@ static VALUE string_spec_rb_enc_interned_str_cstr(VALUE self, VALUE str, VALUE e
581581
return rb_enc_interned_str_cstr(RSTRING_PTR(str), e);
582582
}
583583

584+
static VALUE string_spec_rb_enc_interned_str(VALUE self, VALUE str, VALUE len, VALUE enc) {
585+
rb_encoding *e = NIL_P(enc) ? 0 : rb_to_encoding(enc);
586+
return rb_enc_interned_str(RSTRING_PTR(str), FIX2LONG(len), e);
587+
}
588+
584589
static VALUE string_spec_rb_str_to_interned_str(VALUE self, VALUE str) {
585590
return rb_str_to_interned_str(str);
586591
}
@@ -687,6 +692,7 @@ void Init_string_spec(void) {
687692
rb_define_method(cls, "rb_str_locktmp", string_spec_rb_str_locktmp, 1);
688693
rb_define_method(cls, "rb_str_unlocktmp", string_spec_rb_str_unlocktmp, 1);
689694
rb_define_method(cls, "rb_enc_interned_str_cstr", string_spec_rb_enc_interned_str_cstr, 2);
695+
rb_define_method(cls, "rb_enc_interned_str", string_spec_rb_enc_interned_str, 3);
690696
rb_define_method(cls, "rb_str_to_interned_str", string_spec_rb_str_to_interned_str, 1);
691697
}
692698

spec/ruby/optional/capi/string_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,51 @@ def inspect
12611261
end
12621262
end
12631263

1264+
describe "rb_enc_interned_str" do
1265+
it "returns a frozen string" do
1266+
str = "hello"
1267+
val = @s.rb_enc_interned_str(str, str.bytesize, Encoding::US_ASCII)
1268+
1269+
val.should.is_a?(String)
1270+
val.encoding.should == Encoding::US_ASCII
1271+
val.should.frozen?
1272+
end
1273+
1274+
it "returns the same frozen string" do
1275+
str = "hello"
1276+
result1 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::US_ASCII)
1277+
result2 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::US_ASCII)
1278+
result1.should.equal?(result2)
1279+
end
1280+
1281+
it "returns different frozen strings for different encodings" do
1282+
str = "hello"
1283+
result1 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::US_ASCII)
1284+
result2 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::UTF_8)
1285+
result1.should_not.equal?(result2)
1286+
end
1287+
1288+
it 'returns the same string when using non-ascii characters' do
1289+
str = 'こんにちは'
1290+
result1 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::UTF_8)
1291+
result2 = @s.rb_enc_interned_str(str, str.bytesize, Encoding::UTF_8)
1292+
result1.should.equal?(result2)
1293+
end
1294+
1295+
it "returns the same string as String#-@" do
1296+
str = "hello"
1297+
@s.rb_enc_interned_str(str, str.bytesize, Encoding::UTF_8).should.equal?(-str)
1298+
end
1299+
1300+
ruby_bug "#20322", ""..."3.4" do
1301+
it "uses the default encoding if encoding is null" do
1302+
str = "hello"
1303+
val = @s.rb_enc_interned_str(str, str.bytesize, nil)
1304+
val.encoding.should == Encoding::ASCII_8BIT
1305+
end
1306+
end
1307+
end
1308+
12641309
describe "rb_str_to_interned_str" do
12651310
it "returns a frozen string" do
12661311
str = "hello"

src/main/c/cext/string.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@ VALUE rb_enc_interned_str_cstr(const char *ptr, rb_encoding *enc) {
437437
return rb_str_to_interned_str(str);
438438
}
439439

440+
VALUE rb_enc_interned_str(const char *ptr, long len, rb_encoding *enc) {
441+
VALUE str = rb_enc_str_new(ptr, len, enc ? enc : rb_ascii8bit_encoding());
442+
return rb_str_to_interned_str(str);
443+
}
444+
440445
VALUE rb_str_to_interned_str(VALUE str) {
441446
return RUBY_INVOKE(str, "-@");
442447
}

0 commit comments

Comments
 (0)