Skip to content

Commit 7c82bfa

Browse files
committed
mruby-sprintf to use mrb_int formatting macros; ref #3076
1 parent 3cf5af6 commit 7c82bfa

3 files changed

Lines changed: 30 additions & 17 deletions

File tree

include/mruby/value.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ struct mrb_state;
2222
# error "You can't define MRB_INT16 and MRB_INT64 at the same time."
2323
#endif
2424

25+
#include <inttypes.h>
26+
2527
#if defined(MRB_INT64)
2628
typedef int64_t mrb_int;
2729
# define MRB_INT_BIT 64

mrbgems/mruby-eval/src/eval.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ create_proc_from_string(mrb_state *mrb, char *s, int len, mrb_value binding, cha
150150
}
151151
cxt->capture_errors = TRUE;
152152
cxt->no_optimize = TRUE;
153+
// cxt->dump_result = TRUE;
153154

154155
p = mrb_parse_nstring(mrb, s, len, cxt);
155156

mrbgems/mruby-sprintf/src/sprintf.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
759759
case 'B':
760760
case 'u': {
761761
mrb_value val = GETARG();
762-
char fbuf[32], nbuf[68], *s;
762+
char nbuf[68], *s;
763763
const char *prefix = NULL;
764764
int sign = 0, dots = 0;
765765
char sc = 0;
@@ -836,8 +836,6 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
836836
}
837837
}
838838
if (sign) {
839-
char c = *p;
840-
if (c == 'i') c = 'd'; /* %d and %i are identical */
841839
if (v < 0) {
842840
v = -v;
843841
sc = '-';
@@ -851,28 +849,40 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
851849
sc = ' ';
852850
width--;
853851
}
854-
if (base == 2) {
855-
snprintf(nbuf, sizeof(nbuf), "%s", RSTRING_PTR(val));
856-
}
857-
else {
858-
snprintf(fbuf, sizeof(fbuf), "%%l%c", c);
859-
snprintf(nbuf, sizeof(nbuf), fbuf, v);
852+
switch (base) {
853+
case 2:
854+
strncpy(nbuf, RSTRING_PTR(val), sizeof(nbuf));
855+
break;
856+
case 8:
857+
snprintf(nbuf, sizeof(nbuf), "%"MRB_PRIo, v);
858+
break;
859+
case 10:
860+
snprintf(nbuf, sizeof(nbuf), "%"MRB_PRId, v);
861+
break;
862+
case 16:
863+
snprintf(nbuf, sizeof(nbuf), "%"MRB_PRIx, v);
864+
break;
860865
}
861866
s = nbuf;
862867
}
863868
else {
864-
char c = *p;
865-
if (c == 'X') c = 'x';
866869
s = nbuf;
867870
if (v < 0) {
868871
dots = 1;
869872
}
870-
if (base == 2) {
871-
snprintf(++s, sizeof(nbuf) - 1, "%s", RSTRING_PTR(val));
872-
}
873-
else {
874-
snprintf(fbuf, sizeof(fbuf), "%%l%c", c);
875-
snprintf(++s, sizeof(nbuf) - 1, fbuf, v);
873+
switch (base) {
874+
case 2:
875+
strncpy(++s, RSTRING_PTR(val), sizeof(nbuf)-1);
876+
break;
877+
case 8:
878+
snprintf(++s, sizeof(nbuf)-1, "%"MRB_PRIo, v);
879+
break;
880+
case 10:
881+
snprintf(++s, sizeof(nbuf)-1, "%"MRB_PRId, v);
882+
break;
883+
case 16:
884+
snprintf(++s, sizeof(nbuf)-1, "%"MRB_PRIx, v);
885+
break;
876886
}
877887
if (v < 0) {
878888
char d;

0 commit comments

Comments
 (0)