summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-03-19 12:46:31 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-03-19 12:46:31 (GMT)
commitf30ed02b8ab673c23d16d081b1dc581baa5ef743 (patch)
treedc337719851b4f6f9c0de5f69f64dfa8833bf8af /sprintf.c
parentd8bf1785316c55e3befac9db0981180bd1432b49 (diff)
sprintf.c: use FILL
* sprintf.c (rb_str_format): use FILL and FILL_ macros instead of while loop. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58021 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sprintf.c')
-rw-r--r--sprintf.c37
1 files changed, 13 insertions, 24 deletions
diff --git a/sprintf.c b/sprintf.c
index 117c568..cdc2d2d 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -74,6 +74,7 @@ sign_bits(int base, const char *p)
} while (0)
#define FILL(c, l) do { \
+ if ((l) <= 0) break;\
CHECK(l);\
FILL_(c, l);\
} while (0)
@@ -758,20 +759,15 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
if ((flags&FWIDTH) && (width > slen)) {
width -= (int)slen;
if (!(flags&FMINUS)) {
- CHECK(width);
- while (width--) {
- buf[blen++] = ' ';
- }
+ FILL(' ', width);
+ width = 0;
}
CHECK(len);
memcpy(&buf[blen], RSTRING_PTR(str), len);
RB_GC_GUARD(str);
blen += len;
if (flags&FMINUS) {
- CHECK(width);
- while (width--) {
- buf[blen++] = ' ';
- }
+ FILL(' ', width);
}
rb_enc_associate(result, enc);
break;
@@ -1009,10 +1005,8 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
width -= prec;
}
if (!(flags&FMINUS)) {
- CHECK(width);
- while (width-- > 0) {
- buf[blen++] = ' ';
- }
+ FILL(' ', width);
+ width = 0;
}
if (sc) PUSH(&sc, 1);
if (prefix) {
@@ -1021,23 +1015,18 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
}
CHECK(prec - len);
if (dots) PUSH("..", 2);
- if (!sign && valsign < 0) {
- char c = sign_bits(base, p);
- while (len < prec--) {
- buf[blen++] = c;
+ if (prec > len) {
+ if (!sign && valsign < 0) {
+ char c = sign_bits(base, p);
+ FILL_(c, prec - len);
}
- }
- else if ((flags & (FMINUS|FPREC)) != FMINUS) {
- while (len < prec--) {
- buf[blen++] = '0';
+ else if ((flags & (FMINUS|FPREC)) != FMINUS) {
+ FILL_('0', prec - len);
}
}
PUSH(s, len);
RB_GC_GUARD(tmp);
- CHECK(width);
- while (width-- > 0) {
- buf[blen++] = ' ';
- }
+ FILL(' ', width);
}
break;