Skip to content

Commit

Permalink
mruby-sprintf to use mrb_int formatting macros; ref #3076
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed Jan 7, 2016
1 parent 3cf5af6 commit 7c82bfa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
2 changes: 2 additions & 0 deletions include/mruby/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions mrbgems/mruby-eval/src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
44 changes: 27 additions & 17 deletions mrbgems/mruby-sprintf/src/sprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = '-';
Expand All @@ -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;
Expand Down

0 comments on commit 7c82bfa

Please sign in to comment.