From 09d4dde9f6a758cee89505d8457c6730806b27f9 Mon Sep 17 00:00:00 2001 From: Watson Date: Sun, 7 May 2023 08:34:09 +0900 Subject: [PATCH] Use rb_String() instead of rb_funcall() (#341) rb_String() will convert to String object using #to_s method if needed. https://github.com/ruby/ruby/blob/8b2884c0b51e4733c09ecbb388ca1b236bad738b/object.c#L3825-L3831 --- ext/ox/builder.c | 12 +++--------- ext/ox/dump.c | 2 +- ext/ox/obj_load.c | 2 +- ext/ox/ox.c | 2 -- ext/ox/ox.h | 1 - ext/ox/slotcache.c | 2 +- test/c/cache16.c | 2 +- test/c/cache_test.c | 2 +- 8 files changed, 8 insertions(+), 17 deletions(-) diff --git a/ext/ox/builder.c b/ext/ox/builder.c index e14df3f3..97342cde 100644 --- a/ext/ox/builder.c +++ b/ext/ox/builder.c @@ -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)); @@ -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; @@ -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; diff --git a/ext/ox/dump.c b/ext/ox/dump.c index 5d520d37..44dfa205 100644 --- a/ext/ox/dump.c +++ b/ext/ox/dump.c @@ -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); diff --git a/ext/ox/obj_load.c b/ext/ox/obj_load.c index 6a3d31f6..fff12f55 100644 --- a/ext/ox/obj_load.c +++ b/ext/ox/obj_load.c @@ -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) { diff --git a/ext/ox/ox.c b/ext/ox/ox.c index d004664a..76b8b5f6 100644 --- a/ext/ox/ox.c +++ b/ext/ox/ox.c @@ -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; @@ -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"); diff --git a/ext/ox/ox.h b/ext/ox/ox.h index cbfaf06e..aa359cb4 100644 --- a/ext/ox/ox.h +++ b/ext/ox/ox.h @@ -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; diff --git a/ext/ox/slotcache.c b/ext/ox/slotcache.c index fbd3298f..2680fb81 100644 --- a/ext/ox/slotcache.c +++ b/ext/ox/slotcache.c @@ -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)); diff --git a/test/c/cache16.c b/test/c/cache16.c index 1c5a4e5c..5bbee1f1 100644 --- a/test/c/cache16.c +++ b/test/c/cache16.c @@ -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)); } diff --git a/test/c/cache_test.c b/test/c/cache_test.c index 9657432a..fa2d6cbd 100644 --- a/test/c/cache_test.c +++ b/test/c/cache_test.c @@ -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))); }