diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-02-23 21:31:39 -0800 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2013-02-23 21:31:39 -0800 |
| commit | bbd50cca2f78c642517beaeccd26400798b1ec46 (patch) | |
| tree | b88d311529528952a989d9315b097ad40fec2dee /src | |
| parent | a6d7de5dab594b0ba85c80246f59eb43774a707d (diff) | |
| parent | dd6234974932382181ced7a813ad71d426d1e0ab (diff) | |
| download | mruby-bbd50cca2f78c642517beaeccd26400798b1ec46.tar.gz mruby-bbd50cca2f78c642517beaeccd26400798b1ec46.zip | |
Merge pull request #878 from monaka/pr-reduce-sprintf
Reduce sprintf() calls. Remove mrb_int_to_str() and MRB_INT_FORMAT.
Diffstat (limited to 'src')
| -rw-r--r-- | src/dump.c | 9 | ||||
| -rw-r--r-- | src/string.c | 28 |
2 files changed, 28 insertions, 9 deletions
diff --git a/src/dump.c b/src/dump.c index b73e7104e..ba5a69a56 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 @@ -242,8 +243,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])); @@ -359,7 +360,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 30fe7c9ca..205c39876 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 <stdio.h> #include "re.h" @@ -2540,9 +2541,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++ = '"'; @@ -2625,9 +2633,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); |
