summaryrefslogtreecommitdiff
path: root/sprintf.c
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-07-16 02:20:00 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-07-16 02:20:00 +0000
commita12e0a991b6522b06ce955982dec6db47bb127ff (patch)
treed369d09ea9f001d1a4402a23e26875b6c6c3f8ef /sprintf.c
parent797aea65cbe0580f8c27dd05212777aab8489c83 (diff)
* sprintf.c (rb_f_sprintf): fix output of NaN, Inf and -Inf with
"%f" or etc on MSVCRT platforms. (backported from HEAD) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@6645 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sprintf.c')
-rw-r--r--sprintf.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/sprintf.c b/sprintf.c
index 405f24dd44..c3ba21b8a4 100644
--- a/sprintf.c
+++ b/sprintf.c
@@ -690,6 +690,63 @@ rb_f_sprintf(argc, argv)
char fbuf[32];
fval = RFLOAT(rb_Float(val))->value;
+#if defined(_WIN32) && !defined(__BORLANDC__)
+ if (isnan(fval) || isinf(fval)) {
+ char *expr;
+
+ if (isnan(fval)) {
+ expr = "NaN";
+ }
+ else {
+ expr = "Inf";
+ }
+ need = strlen(expr);
+ if ((!isnan(fval) && fval < 0.0) || (flags & FPLUS))
+ need++;
+ if ((flags & FWIDTH) && need < width)
+ need = width;
+
+ CHECK(need);
+ sprintf(&buf[blen], "%*s", need, "");
+ if (flags & FMINUS) {
+ if (!isnan(fval) && fval < 0.0)
+ buf[blen++] = '-';
+ else if (flags & FPLUS)
+ buf[blen++] = '+';
+ else if (flags & FSPACE)
+ blen++;
+ strncpy(&buf[blen], expr, strlen(expr));
+ }
+ else if (flags & FZERO) {
+ if (!isnan(fval) && fval < 0.0) {
+ buf[blen++] = '-';
+ need--;
+ }
+ else if (flags & FPLUS) {
+ buf[blen++] = '+';
+ need--;
+ }
+ else if (flags & FSPACE) {
+ blen++;
+ need--;
+ }
+ while (need-- - strlen(expr) > 0) {
+ buf[blen++] = '0';
+ }
+ strncpy(&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] = '+';
+ strncpy(&buf[blen + need - strlen(expr)], expr,
+ strlen(expr));
+ }
+ blen += strlen(&buf[blen]);
+ break;
+ }
+#endif /* defined(_WIN32) && !defined(__BORLANDC__) */
fmt_setup(fbuf, *p, flags, width, prec);
need = 0;
if (*p != 'e' && *p != 'E') {