Permalink
Browse files

Use `FILL` and `PUSH`

Instead of loop or `memcpy` after `CHECK`, use dedicated macros,
`FILL` and `PUSH`, respectively.
  • Loading branch information...
nobu committed Apr 29, 2017
1 parent 3c10783 commit e05c30738ead860f7d59fe1de7cd14cad2b36cc0
Showing with 10 additions and 35 deletions.
  1. +10 −35 mrbgems/mruby-sprintf/src/sprintf.c
@@ -716,21 +716,15 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
c = RSTRING_PTR(tmp);
n = RSTRING_LEN(tmp);
if (!(flags & FWIDTH)) {
CHECK(n);
memcpy(buf+blen, c, n);
blen += n;
PUSH(c, n);
}
else if ((flags & FMINUS)) {
CHECK(n);
memcpy(buf+blen, c, n);
blen += n;
PUSH(c, n);
if (width>0) FILL(' ', width-1);
}
else {
if (width>0) FILL(' ', width-1);
CHECK(n);
memcpy(buf+blen, c, n);
blen += n;
PUSH(c, n);
}
}
break;
@@ -768,19 +762,11 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
if ((flags&FWIDTH) && (width > slen)) {
width -= (int)slen;
if (!(flags&FMINUS)) {
CHECK(width);
while (width-- > 0) {
buf[blen++] = ' ';
}
FILL(' ', width);
}
CHECK(len);
memcpy(&buf[blen], RSTRING_PTR(str), len);
blen += len;
PUSH(RSTRING_PTR(str), len);
if (flags&FMINUS) {
CHECK(width);
while (width-- > 0) {
buf[blen++] = ' ';
}
FILL(' ', width);
}
break;
}
@@ -986,10 +972,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
}
if (!(flags&FMINUS) && width > 0) {
CHECK(width);
while (width-- > 0) {
buf[blen++] = ' ';
}
FILL(' ', width);
}
if (sc) PUSH(&sc, 1);
@@ -998,28 +981,20 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
int plen = (int)strlen(prefix);
PUSH(prefix, plen);
}
CHECK(prec - len);
if (dots) PUSH("..", 2);
if (v < 0) {
char c = sign_bits(base, p);
while (len < prec--) {
buf[blen++] = c;
}
FILL(c, prec - len);
}
else if ((flags & (FMINUS|FPREC)) != FMINUS) {
char c = '0';
while (len < prec--) {
buf[blen++] = c;
}
FILL(c, prec - len);
}
PUSH(s, len);
if (width > 0) {
CHECK(width);
while (width-- > 0) {
buf[blen++] = ' ';
}
FILL(' ', width);
}
}
break;

0 comments on commit e05c307

Please sign in to comment.