Skip to content

Commit

Permalink
Add specs for rb_enc_left_char_head()
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Sep 20, 2023
1 parent 9f8ff77 commit 124a255
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spec/ruby/optional/capi/encoding_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,22 @@
end
end

describe "rb_enc_left_char_head" do
it 'returns the head position of a character' do
@s.rb_enc_left_char_head("é", 1).should == 0
@s.rb_enc_left_char_head("éééé", 7).should == 6

@s.rb_enc_left_char_head("a", 0).should == 0

# unclear if this is intended to work
@s.rb_enc_left_char_head("a", 1).should == 1

# Works because for single-byte encodings rb_enc_left_char_head() just returns the pointer
@s.rb_enc_left_char_head("a".force_encoding(Encoding::US_ASCII), 88).should == 88
@s.rb_enc_left_char_head("a".b, 88).should == 88
end
end

describe "ONIGENC_MBC_CASE_FOLD" do
it "returns the correct case fold for the given string" do
@s.ONIGENC_MBC_CASE_FOLD("lower").should == ["l", 1]
Expand Down
7 changes: 7 additions & 0 deletions spec/ruby/optional/capi/ext/encoding_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ static VALUE encoding_spec_rb_enc_strlen(VALUE self, VALUE str, VALUE length, VA
return LONG2FIX(rb_enc_strlen(p, e, rb_to_encoding(encoding)));
}

static VALUE encoding_spec_rb_enc_left_char_head(VALUE self, VALUE str, VALUE offset) {
char *ptr = RSTRING_PTR(str);
char *result = rb_enc_left_char_head(ptr, ptr + NUM2INT(offset), RSTRING_END(str), rb_enc_get(str));
return LONG2NUM(result - ptr);
}

void Init_encoding_spec(void) {
VALUE cls;
native_rb_encoding_pointer = (rb_encoding**) malloc(sizeof(rb_encoding*));
Expand Down Expand Up @@ -364,6 +370,7 @@ void Init_encoding_spec(void) {
rb_define_method(cls, "rb_enc_str_asciionly_p", encoding_spec_rb_enc_str_asciionly_p, 1);
rb_define_method(cls, "rb_uv_to_utf8", encoding_spec_rb_uv_to_utf8, 2);
rb_define_method(cls, "ONIGENC_MBC_CASE_FOLD", encoding_spec_ONIGENC_MBC_CASE_FOLD, 1);
rb_define_method(cls, "rb_enc_left_char_head", encoding_spec_rb_enc_left_char_head, 2);
}

#ifdef __cplusplus
Expand Down

0 comments on commit 124a255

Please sign in to comment.