diff options
author | max99x <max99x@gmail.com> | 2011-06-28 09:03:24 +0300 |
---|---|---|
committer | max99x <max99x@gmail.com> | 2011-06-28 09:03:24 +0300 |
commit | bef407f174543e926033a25ee1faf69e3bdad296 (patch) | |
tree | 7de3ac62582148ec3f9771efb42ea434249842b2 | |
parent | 3a0e9450e8c4f3667477b3c77f3ebc9c429963e4 (diff) |
Properly padding infinity/NaN in _formatString/printf.
-rw-r--r-- | src/library.js | 24 | ||||
-rw-r--r-- | tests/printf/output.txt | 6 | ||||
-rw-r--r-- | tests/printf/test.c | 6 |
3 files changed, 27 insertions, 9 deletions
diff --git a/src/library.js b/src/library.js index b2c3d13c..a9ce8c33 100644 --- a/src/library.js +++ b/src/library.js @@ -242,8 +242,10 @@ var Library = { if (isNaN(currArg)) { argText = 'nan'; + flagZeroPad = false; } else if (!isFinite(currArg)) { argText = (currArg < 0 ? '-' : '') + 'inf'; + flagZeroPad = false; } else { var isGeneral = false; var effectivePrecision = Math.min(precision, 20); @@ -295,21 +297,25 @@ var Library = { if (flagAlwaysSigned && currArg >= 0) { argText = '+' + argText; } + } - // Add padding. - while (argText.length < width) { - if (flagLeftAlign) { - argText += ' '; + // Add padding. + while (argText.length < width) { + if (flagLeftAlign) { + argText += ' '; + } else { + if (flagZeroPad && (argText[0] == '-' || argText[0] == '+')) { + argText = argText[0] + '0' + argText.slice(1); } else { - if (flagZeroPad && (argText[0] == '-' || argText[0] == '+')) { - argText = argText[0] + '0' + argText.slice(1); - } else { - argText = (flagZeroPad ? '0' : ' ') + argText; - } + argText = (flagZeroPad ? '0' : ' ') + argText; } } } + + // Adjust case. if (next < 'a'.charCodeAt(0)) argText = argText.toUpperCase(); + + // Insert the result into the buffer. argText.split('').forEach(function(chr) { ret.push(chr.charCodeAt(0)); }); diff --git a/tests/printf/output.txt b/tests/printf/output.txt index be93561b..3dd4d972 100644 --- a/tests/printf/output.txt +++ b/tests/printf/output.txt @@ -15,6 +15,12 @@ INF -INF nan NAN + nan +nan + nan +nan + inf +-inf 0 0 0.0 diff --git a/tests/printf/test.c b/tests/printf/test.c index e388ee9c..552c7e1c 100644 --- a/tests/printf/test.c +++ b/tests/printf/test.c @@ -20,6 +20,12 @@ int main() { printf("%lF\n", -INFINITY); printf("%lf\n", NAN); printf("%lF\n", NAN); + printf("%10f\n", NAN); + printf("%-10f\n", NAN); + printf("%010.2f\n", NAN); + printf("%-010.2f\n", NAN); + printf("%10.f\n", INFINITY); + printf("%-10.f\n", -INFINITY); printf("%.f\n", 0.0f); printf("%.0f\n", 0.0f); printf("%.1f\n", 0.0f); |