Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement rb_utf8_str_new etc #1788

Merged
merged 2 commits into from Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions spec/ruby/optional/capi/ext/string_spec.c
Expand Up @@ -411,6 +411,18 @@ static VALUE string_spec_rb_str_modify(VALUE self, VALUE str) {
return str;
}

static VALUE string_spec_rb_utf8_str_new_static(VALUE self) {
return rb_utf8_str_new_static("nokogiri", 8);
}

static VALUE string_spec_rb_utf8_str_new(VALUE self) {
return rb_utf8_str_new("nokogiri", 8);
}

static VALUE string_spec_rb_utf8_str_new_cstr(VALUE self) {
return rb_utf8_str_new_cstr("nokogiri");
}

void Init_string_spec(void) {
VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject);
rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2);
Expand Down Expand Up @@ -485,6 +497,9 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_String", string_spec_rb_String, 1);
rb_define_method(cls, "rb_string_value_cstr", string_spec_rb_string_value_cstr, 1);
rb_define_method(cls, "rb_str_modify", string_spec_rb_str_modify, 1);
rb_define_method(cls, "rb_utf8_str_new_static", string_spec_rb_utf8_str_new_static, 0);
rb_define_method(cls, "rb_utf8_str_new", string_spec_rb_utf8_str_new, 0);
rb_define_method(cls, "rb_utf8_str_new_cstr", string_spec_rb_utf8_str_new_cstr, 0);
}

#ifdef __cplusplus
Expand Down
24 changes: 24 additions & 0 deletions spec/ruby/optional/capi/string_spec.rb
Expand Up @@ -997,4 +997,28 @@ def inspect
str.should == "2345678".encode("UTF-32LE")
end
end

describe "rb_utf8_str_new_static" do
it "returns a UTF-8 string of the correct characters and length" do
str = @s.rb_utf8_str_new_static
str.should == "nokogiri"
str.encoding.should == Encoding::UTF_8
end
end

describe "rb_utf8_str_new" do
it "returns a UTF-8 string of the correct characters and length" do
str = @s.rb_utf8_str_new
str.should == "nokogiri"
str.encoding.should == Encoding::UTF_8
end
end

describe "rb_utf8_str_new_cstr" do
it "returns a UTF-8 string of the correct characters and length" do
str = @s.rb_utf8_str_new_cstr
str.should == "nokogiri"
str.encoding.should == Encoding::UTF_8
end
end
end
7 changes: 4 additions & 3 deletions src/main/c/cext/ruby.c
Expand Up @@ -4276,12 +4276,13 @@ VALUE rb_str_tmp_new(long len) {

#undef rb_utf8_str_new
VALUE rb_utf8_str_new(const char *ptr, long len) {
rb_tr_error("rb_utf8_str_new not implemented");
return rb_enc_str_new(ptr, len, rb_utf8_encoding());
}

#undef rb_utf8_str_new_cstr
VALUE rb_utf8_str_new_cstr(const char *ptr) {
rb_tr_error("rb_utf8_str_new_cstr not implemented");
// TODO CS 11-Oct-19 would be nice to read in one go rather than strlen followed by read
return rb_utf8_str_new(ptr, strlen(ptr));
}

VALUE rb_str_new_static(const char *ptr, long len) {
Expand All @@ -4293,7 +4294,7 @@ VALUE rb_usascii_str_new_static(const char *ptr, long len) {
}

VALUE rb_utf8_str_new_static(const char *ptr, long len) {
rb_tr_error("rb_utf8_str_new_static not implemented");
return rb_utf8_str_new(ptr, len);
}

void rb_str_shared_replace(VALUE str, VALUE str2) {
Expand Down