Skip to content

Commit

Permalink
[GR-18163] Implement rb_str_strlen() (#3699)
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4384
  • Loading branch information
eregon committed Oct 29, 2024
2 parents b359f4c + 414e9a3 commit fe19a02
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Compatibility:
* Add `MAJOR`, `MINOR`, `TEENY`, `PATCHLEVEL`, `RUBY_API_VERSION`, and `RUBY_PROGRAM_VERSION` to `RbConfig::CONFIG` (#3396, @rwstauner).
* Set `RbConfig::CONFIG['archincludedir']` (#3396, @andrykonchin).
* 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).

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.3"
#define TRUFFLERUBY_ABI_VERSION "3.2.4.4"

#endif
5 changes: 5 additions & 0 deletions spec/ruby/optional/capi/ext/string_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ VALUE string_spec_rb_str_cmp(VALUE self, VALUE str1, VALUE str2) {
return INT2NUM(rb_str_cmp(str1, str2));
}

VALUE string_spec_rb_str_strlen(VALUE self, VALUE str) {
return LONG2NUM(rb_str_strlen(str));
}

VALUE string_spec_rb_str_conv_enc(VALUE self, VALUE str, VALUE from, VALUE to) {
rb_encoding* from_enc;
rb_encoding* to_enc;
Expand Down Expand Up @@ -600,6 +604,7 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_str_cat_cstr", string_spec_rb_str_cat_cstr, 2);
rb_define_method(cls, "rb_str_cat_cstr_constant", string_spec_rb_str_cat_cstr_constant, 1);
rb_define_method(cls, "rb_str_cmp", string_spec_rb_str_cmp, 2);
rb_define_method(cls, "rb_str_strlen", string_spec_rb_str_strlen, 1);
rb_define_method(cls, "rb_str_conv_enc", string_spec_rb_str_conv_enc, 3);
rb_define_method(cls, "rb_str_conv_enc_opts", string_spec_rb_str_conv_enc_opts, 5);
rb_define_method(cls, "rb_str_drop_bytes", string_spec_rb_str_drop_bytes, 2);
Expand Down
14 changes: 14 additions & 0 deletions spec/ruby/optional/capi/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,20 @@ def inspect
end
end

describe "rb_str_strlen" do
it 'returns 0 as the length of an empty string' do
@s.rb_str_strlen('').should == 0
end

it 'returns the number of characters in a string' do
@s.rb_str_strlen('hello').should == 5
end

it 'returns the number of characters in a string with multi-byte characters' do
@s.rb_str_strlen('こんにちは').should == 5
end
end

describe "rb_str_split" do
it "splits strings over a splitter" do
@s.rb_str_split("Hello,Goodbye").should == ["Hello", "Goodbye"]
Expand Down
4 changes: 4 additions & 0 deletions src/main/c/cext/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ int rb_str_cmp(VALUE a, VALUE b) {
return polyglot_as_i32(RUBY_INVOKE_NO_WRAP(a, "<=>", b));
}

long rb_str_strlen(VALUE string) {
return polyglot_as_i64(RUBY_INVOKE_NO_WRAP(string, "size"));
}

VALUE rb_str_conv_enc(VALUE string, rb_encoding *from, rb_encoding *to) {
return rb_str_conv_enc_opts(string, from, to, 0, Qnil);
}
Expand Down

0 comments on commit fe19a02

Please sign in to comment.