Skip to content

Commit

Permalink
Use rb_String() instead of rb_funcall() (#341)
Browse files Browse the repository at this point in the history
rb_String() will convert to String object using #to_s method if needed.

https://github.com/ruby/ruby/blob/8b2884c0b51e4733c09ecbb388ca1b236bad738b/object.c#L3825-L3831
  • Loading branch information
Watson1978 authored May 6, 2023
1 parent e551b61 commit 09d4dde
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 17 deletions.
12 changes: 3 additions & 9 deletions ext/ox/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,7 @@ static VALUE builder_text(int argc, VALUE *argv, VALUE self) {
strip_invalid_chars = Qfalse;
}

if (T_STRING != rb_type(v)) {
v = rb_funcall(v, ox_to_s_id, 0);
}
v = rb_String(v);
i_am_a_child(b, true);
append_string(b, StringValuePtr(v), RSTRING_LEN(v), xml_element_chars, RTEST(strip_invalid_chars));

Expand All @@ -730,9 +728,7 @@ static VALUE builder_cdata(VALUE self, VALUE data) {
const char *end;
int len;

if (T_STRING != rb_type(v)) {
v = rb_funcall(v, ox_to_s_id, 0);
}
v = rb_String(v);
str = StringValuePtr(v);
len = (int)RSTRING_LEN(v);
s = str;
Expand Down Expand Up @@ -772,9 +768,7 @@ static VALUE builder_raw(VALUE self, VALUE text) {
const char *end;
int len;

if (T_STRING != rb_type(v)) {
v = rb_funcall(v, ox_to_s_id, 0);
}
v = rb_String(v);
str = StringValuePtr(v);
len = (int)RSTRING_LEN(v);
s = str;
Expand Down
2 changes: 1 addition & 1 deletion ext/ox/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ static void dump_obj(ID aid, VALUE obj, int depth, Out out) {
e.indent = -1;
out->w_end(out, &e);
} else if (0 == strcmp("BigDecimal", classname)) {
volatile VALUE rs = rb_funcall(obj, ox_to_s_id, 0);
volatile VALUE rs = rb_String(obj);

e.type = BigDecimalCode;
out->w_start(out, &e);
Expand Down
2 changes: 1 addition & 1 deletion ext/ox/obj_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ static void debug_stack(PInfo pi, const char *comment) {
if (HashCode == h->type) {
VALUE v;

v = rb_funcall2(h->var, rb_intern("to_s"), 0, 0);
v = rb_String(h->var);
key = StringValuePtr(v);
} else if (ObjectCode == (h - 1)->type || ExceptionCode == (h - 1)->type ||
RangeCode == (h - 1)->type || StructCode == (h - 1)->type) {
Expand Down
2 changes: 0 additions & 2 deletions ext/ox/ox.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ ID ox_start_element_id;
ID ox_string_id;
ID ox_text_id;
ID ox_to_c_id;
ID ox_to_s_id;
ID ox_value_id;

VALUE ox_encoding_sym;
Expand Down Expand Up @@ -1445,7 +1444,6 @@ void Init_ox() {
ox_string_id = rb_intern("string");
ox_text_id = rb_intern("text");
ox_to_c_id = rb_intern("to_c");
ox_to_s_id = rb_intern("to_s");
ox_value_id = rb_intern("value");

encoding_id = rb_intern("encoding");
Expand Down
1 change: 0 additions & 1 deletion ext/ox/ox.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ extern ID ox_start_element_id;
extern ID ox_string_id;
extern ID ox_text_id;
extern ID ox_to_c_id;
extern ID ox_to_s_id;
extern ID ox_value_id;

extern rb_encoding *ox_utf8_encoding;
Expand Down
2 changes: 1 addition & 1 deletion ext/ox/slotcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static void slot_print(SlotCache c, unsigned int depth) {
vs = "undefined";
clas = "";
} else {
VALUE rs = rb_funcall2((*cp)->value, rb_intern("to_s"), 0, 0);
VALUE rs = rb_String((*cp)->value);

vs = StringValuePtr(rs);
clas = rb_class2name(rb_obj_class((*cp)->value));
Expand Down
2 changes: 1 addition & 1 deletion test/c/cache16.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static void slot_print(Cache16 c, unsigned int depth) {
}

static void v2s(VALUE v, char *buf, unsigned long len) {
VALUE rs = rb_funcall2(v, rb_intern("to_s"), 0, 0);
VALUE rs = rb_String(v);

snprintf(buf, len, "%s", StringValuePtr(rs));
}
2 changes: 1 addition & 1 deletion test/c/cache_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void ox_cache_test() {
*slot = v;
}
} else {
VALUE rs = rb_funcall2(v, rb_intern("to_s"), 0, 0);
VALUE rs = rb_String(v);

printf("*** get on '%s' returned '%s' (%s)\n", *d, StringValuePtr(rs), rb_class2name(rb_obj_class(v)));
}
Expand Down

0 comments on commit 09d4dde

Please sign in to comment.