Skip to content
Permalink
Browse files
Refactor sprintf() code.
  • Loading branch information
matz committed Jul 3, 2017
1 parent 31bc2b0 commit e9cdc880082afd037342a81b3aa31a1924f98734
Showing 1 changed file with 20 additions and 40 deletions.
@@ -801,13 +801,6 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
int base;
mrb_int len;

switch (*p) {
case 'd':
case 'i':
sign = 1; break;
default:
break;
}
if (flags & FSHARP) {
switch (*p) {
case 'o': prefix = "0"; break;
@@ -845,22 +838,14 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
case 'b':
case 'B':
base = 2; break;
case 'u':
case 'd':
case 'i':
sign = 1;
case 'u':
default:
base = 10; break;
}

if (base == 2) {
if (v < 0 && !sign) {
val = mrb_fix2binstr(mrb, mrb_fixnum_value(v), base);
dots = 1;
}
else {
val = mrb_fixnum_to_str(mrb, mrb_fixnum_value(v), base);
}
}
if (sign) {
if (v > 0) {
if (flags & FPLUS) {
@@ -872,37 +857,33 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
width--;
}
}
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;
else {
sc = '-';
width--;
v = -v;
}
mrb_assert(base == 10);
snprintf(nbuf, sizeof(nbuf), "%" MRB_PRId, v);
s = nbuf;
}
else {
s = nbuf;
if (base != 10 && v < 0) {
if (v < 0) {
dots = 1;
}
switch (base) {
case 2:
if (v < 0) {
val = mrb_fix2binstr(mrb, mrb_fixnum_value(v), base);
}
else {
val = mrb_fixnum_to_str(mrb, mrb_fixnum_value(v), base);
}
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;
@@ -930,11 +911,6 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
len = (mrb_int)size;
}

if (dots) {
prec -= 2;
width -= 2;
}

if (*p == 'X') {
char *pp = s;
int c;
@@ -990,7 +966,11 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
int plen = (int)strlen(prefix);
PUSH(prefix, plen);
}
if (dots) PUSH("..", 2);
if (dots) {
prec -= 2;
width -= 2;
PUSH("..", 2);
}

if (prec > len) {
CHECK(prec - len);

0 comments on commit e9cdc88

Please sign in to comment.