Skip to content

Commit

Permalink
rb_id2str() and rb_id2name() treat (ID) 0 specially
Browse files Browse the repository at this point in the history
  • Loading branch information
eregon committed Mar 28, 2022
1 parent 570fb2d commit b721b62
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions optional/capi/ext/symbol_spec.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,19 @@ VALUE symbol_spec_rb_id2name(VALUE self, VALUE symbol) {
return rb_str_new(c_str, strlen(c_str));
}

VALUE symbol_spec_rb_id2name_id_zero(VALUE self) {
const char* c_str = rb_id2name((ID) 0);
return c_str ? rb_str_new(c_str, strlen(c_str)) : Qnil;
}

VALUE symbol_spec_rb_id2str(VALUE self, VALUE symbol) {
return rb_id2str(SYM2ID(symbol));
}

VALUE symbol_spec_rb_id2str_id_zero(VALUE self) {
return rb_id2str((ID) 0);
}

VALUE symbol_spec_rb_intern_str(VALUE self, VALUE str) {
return ID2SYM(rb_intern_str(str));
}
Expand Down Expand Up @@ -90,7 +99,9 @@ void Init_symbol_spec(void) {
rb_define_method(cls, "rb_intern3", symbol_spec_rb_intern3, 3);
rb_define_method(cls, "rb_intern3_c_compare", symbol_spec_rb_intern3_c_compare, 4);
rb_define_method(cls, "rb_id2name", symbol_spec_rb_id2name, 1);
rb_define_method(cls, "rb_id2name_id_zero", symbol_spec_rb_id2name_id_zero, 0);
rb_define_method(cls, "rb_id2str", symbol_spec_rb_id2str, 1);
rb_define_method(cls, "rb_id2str_id_zero", symbol_spec_rb_id2str_id_zero, 0);
rb_define_method(cls, "rb_intern_str", symbol_spec_rb_intern_str, 1);
rb_define_method(cls, "rb_check_symbol_cstr", symbol_spec_rb_check_symbol_cstr, 1);
rb_define_method(cls, "rb_is_class_id", symbol_spec_rb_is_class_id, 1);
Expand Down
8 changes: 8 additions & 0 deletions optional/capi/symbol_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
it "converts a symbol to a C char array" do
@s.rb_id2name(:test_symbol).should == "test_symbol"
end

it "returns (char*) NULL for (ID) 0" do
@s.rb_id2name_id_zero.should == nil
end
end

describe "rb_id2str" do
Expand All @@ -72,6 +76,10 @@
str = "test_symbol".encode(Encoding::UTF_16LE)
@s.rb_id2str(str.to_sym).encoding.should == Encoding::UTF_16LE
end

it "returns (VALUE) 0 = Qfalse for (ID) 0" do
@s.rb_id2str_id_zero.should == false
end
end

describe "rb_intern_str" do
Expand Down

0 comments on commit b721b62

Please sign in to comment.