Permalink
Browse files

mruby-sprintf to use mrb_int formatting macros; ref #3076

  • Loading branch information...
matz committed Jan 7, 2016
1 parent 3cf5af6 commit 7c82bfa7fe17ef7b44d55f058bb4be14eb7e62e0
Showing with 30 additions and 17 deletions.
  1. +2 −0 include/mruby/value.h
  2. +1 −0 mrbgems/mruby-eval/src/eval.c
  3. +27 −17 mrbgems/mruby-sprintf/src/sprintf.c
View
@@ -22,6 +22,8 @@ struct mrb_state;
# error "You can't define MRB_INT16 and MRB_INT64 at the same time."
#endif
#include <inttypes.h>
#if defined(MRB_INT64)
typedef int64_t mrb_int;
# define MRB_INT_BIT 64
@@ -150,6 +150,7 @@ create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, cha
}
cxt->capture_errors = TRUE;
cxt->no_optimize = TRUE;
// cxt->dump_result = TRUE;
p = mrb_parse_nstring(mrb, s, len, cxt);
@@ -759,7 +759,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
case 'B':
case 'u': {
mrb_value val = GETARG();
char fbuf[32], nbuf[68], *s;
char nbuf[68], *s;
const char *prefix = NULL;
int sign = 0, dots = 0;
char sc = 0;
@@ -836,8 +836,6 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
}
}
if (sign) {
char c = *p;
if (c == 'i') c = 'd'; /* %d and %i are identical */
if (v < 0) {
v = -v;
sc = '-';
@@ -851,28 +849,40 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
sc = ' ';
width--;
}
if (base == 2) {
snprintf(nbuf, sizeof(nbuf), "%s", RSTRING_PTR(val));
}
else {
snprintf(fbuf, sizeof(fbuf), "%%l%c", c);
snprintf(nbuf, sizeof(nbuf), fbuf, v);
switch (base) {
case 2:
strncpy(nbuf, RSTRING_PTR(val), sizeof(nbuf));
break;
case 8:
snprintf(nbuf, sizeof(nbuf), "%"MRB_PRIo, v);
break;
case 10:
snprintf(nbuf, sizeof(nbuf), "%"MRB_PRId, v);
break;
case 16:
snprintf(nbuf, sizeof(nbuf), "%"MRB_PRIx, v);
break;
}
s = nbuf;
}
else {
char c = *p;
if (c == 'X') c = 'x';
s = nbuf;
if (v < 0) {
dots = 1;
}
if (base == 2) {
snprintf(++s, sizeof(nbuf) - 1, "%s", RSTRING_PTR(val));
}
else {
snprintf(fbuf, sizeof(fbuf), "%%l%c", c);
snprintf(++s, sizeof(nbuf) - 1, fbuf, v);
switch (base) {
case 2:
strncpy(++s, RSTRING_PTR(val), sizeof(nbuf)-1);
break;
case 8:
snprintf(++s, sizeof(nbuf)-1, "%"MRB_PRIo, v);
break;
case 10:
snprintf(++s, sizeof(nbuf)-1, "%"MRB_PRId, v);
break;
case 16:
snprintf(++s, sizeof(nbuf)-1, "%"MRB_PRIx, v);
break;
}
if (v < 0) {
char d;

0 comments on commit 7c82bfa

Please sign in to comment.