diff options
-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); |