summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-04-23 01:04:40 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-04-23 01:04:40 +0000
commitbe7309ddf6a24751ccc242346586c3feaff82f19 ()
tree0dce0179fdaeeb01b985c88a2fe914ff8269b0aa /sprintf.c
parent6f8f950cc3aec12826c72e7e67bf54022444aacc (diff)
Refactor "%f" % Inf/NaN
* sprintf.c (rb_str_format): as for non-finite float, calculate the exact needed size with the space flag. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58454 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--sprintf.c35
1 files changed, 17 insertions, 18 deletions
@@ -1128,6 +1128,8 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
fval = RFLOAT_VALUE(rb_Float(val));
if (isnan(fval) || isinf(fval)) {
const char *expr;
if (isnan(fval)) {
expr = "NaN";
@@ -1136,31 +1138,28 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
expr = "Inf";
}
need = (int)strlen(expr);
- if ((!isnan(fval) && fval < 0.0) || (flags & (FPLUS|FSPACE)))
- need++;
if ((flags & FWIDTH) && need < width)
need = width;
- CHECK(need + 1);
- snprintf(&buf[blen], need + 1, "%*s", need, "");
if (flags & FMINUS) {
- if (!isnan(fval) && fval < 0.0)
- buf[blen++] = '-';
- else if (flags & FPLUS)
- buf[blen++] = '+';
- else if (flags & FSPACE)
- blen++;
- memcpy(&buf[blen], expr, strlen(expr));
}
else {
- if (!isnan(fval) && fval < 0.0)
- buf[blen + need - strlen(expr) - 1] = '-';
- else if (flags & FPLUS)
- buf[blen + need - strlen(expr) - 1] = '+';
- memcpy(&buf[blen + need - strlen(expr)], expr,
- strlen(expr));
}
- blen += strlen(&buf[blen]);
break;
}