Skip to content

Commit

Permalink
Implement rb_str_strlen
Browse files Browse the repository at this point in the history
  • Loading branch information
Th3-M4jor authored and headius committed Nov 6, 2024
1 parent 3e37d57 commit 2b3ace1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions 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 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

0 comments on commit 2b3ace1

Please sign in to comment.