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 /src | |
parent | 3a0e9450e8c4f3667477b3c77f3ebc9c429963e4 (diff) |
Properly padding infinity/NaN in _formatString/printf.
Diffstat (limited to 'src')
-rw-r--r-- | src/library.js | 24 |
1 files changed, 15 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)); }); |