From dd6234974932382181ced7a813ad71d426d1e0ab Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Sat, 23 Feb 2013 21:21:15 +0900 Subject: Reduce sprintf() calls. Remove mrb_int_to_str() and MRB_INT_FORMAT. --- src/dump.c | 9 ++++++--- src/string.c | 28 ++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/dump.c b/src/dump.c index e8d96c570..b97b38dc2 100644 --- a/src/dump.c +++ b/src/dump.c @@ -10,6 +10,7 @@ #include "mruby/string.h" #include "mruby/irep.h" +#include "mruby/numeric.h" static const unsigned char def_rite_binary_header[] = RITE_FILE_IDENFIFIER @@ -241,8 +242,8 @@ get_pool_block_size(mrb_state *mrb, mrb_irep *irep, int type) switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: - len = mrb_int_to_str( buf, mrb_fixnum(irep->pool[pool_no])); - size += (uint32_t)len; + str = mrb_fix2str(mrb, irep->pool[pool_no], 10); + size += (uint32_t)RSTRING_LEN(str); break; case MRB_TT_FLOAT: len = mrb_float_to_str( buf, mrb_float(irep->pool[pool_no])); @@ -358,7 +359,9 @@ write_pool_block(mrb_state *mrb, mrb_irep *irep, char *buf, int type) switch (mrb_type(irep->pool[pool_no])) { case MRB_TT_FIXNUM: - len = mrb_int_to_str(char_buf, mrb_fixnum(irep->pool[pool_no])); + str = mrb_fix2str(mrb, irep->pool[pool_no], 10); + memcpy(char_buf, RSTRING_PTR(str), RSTRING_LEN(str)); + len = RSTRING_LEN(str); break; case MRB_TT_FLOAT: diff --git a/src/string.c b/src/string.c index 6afb4f6ca..6e0c8d1a2 100644 --- a/src/string.c +++ b/src/string.c @@ -14,6 +14,7 @@ #include "mruby/range.h" #include "mruby/array.h" #include "mruby/class.h" +#include "mruby/numeric.h" #include #include "re.h" @@ -2552,9 +2553,16 @@ mrb_str_dump(mrb_state *mrb, mrb_value str) *q++ = c; } else { - *q++ = '\\'; - sprintf(q, "%03o", c&0xff); - q += 3; + mrb_value octstr; + mrb_value chr; + const char *ptr; + int len; + chr = mrb_fixnum_value(c & 0xff); + octstr = mrb_fix2str(mrb, chr, 8); + ptr = mrb_str_body(octstr, &len); + memcpy(q, "\\000", 4); + memcpy(q + 4 - len, ptr, len); + q += 4; } } *q++ = '"'; @@ -2637,9 +2645,17 @@ mrb_str_inspect(mrb_state *mrb, mrb_value str) continue; } else { - int n = sprintf(buf, "\\%03o", c & 0377); - mrb_str_buf_cat(mrb, result, buf, n); - continue; + mrb_value octstr; + mrb_value chr; + const char *ptr; + int len; + chr = mrb_fixnum_value(c & 0xff); + octstr = mrb_fix2str(mrb, chr, 8); + ptr = mrb_str_body(octstr, &len); + memcpy(buf, "\\000", 4); + memcpy(buf + 4 - len, ptr, len); + mrb_str_buf_cat(mrb, result, buf, 4); + continue; } } mrb_str_buf_cat(mrb, result, "\"", 1); -- cgit v1.2.3