aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-09-24 16:47:27 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-09-24 16:47:27 -0700
commitd597a4f280914b187a3d01bc6bb11fa3ae53be29 (patch)
treedb81811e9bb91c7ef1730754be0f01ab474925ee
parent061e6255ca15b31d002f22c2ca49eb1197d23f46 (diff)
parent61c9d6ede1fe9a91ae501dc019d2e5f97803943a (diff)
Merge pull request #1650 from rharkeadsk/dev/fix-printf-padding
Fix 3 issues with formatting (printf)
-rw-r--r--src/library.js26
-rw-r--r--tests/printf/output.txt11
-rw-r--r--tests/printf/output_i64_1.txt11
-rw-r--r--tests/printf/test.c11
4 files changed, 44 insertions, 15 deletions
diff --git a/src/library.js b/src/library.js
index 5c2c858d..2faa40d7 100644
--- a/src/library.js
+++ b/src/library.js
@@ -1974,6 +1974,7 @@ LibraryManager.library = {
var flagLeftAlign = false;
var flagAlternative = false;
var flagZeroPad = false;
+ var flagPadSign = false;
flagsLoop: while (1) {
switch (next) {
case {{{ charCode('+') }}}:
@@ -1992,6 +1993,9 @@ LibraryManager.library = {
flagZeroPad = true;
break;
}
+ case {{{ charCode(' ') }}}:
+ flagPadSign = true;
+ break;
default:
break flagsLoop;
}
@@ -2158,14 +2162,20 @@ LibraryManager.library = {
}
// Add sign if needed
- if (flagAlwaysSigned) {
- if (currArg < 0) {
- prefix = '-' + prefix;
- } else {
+ if (currArg >= 0) {
+ if (flagAlwaysSigned) {
prefix = '+' + prefix;
+ } else if (flagPadSign) {
+ prefix = ' ' + prefix;
}
}
+ // Move sign to prefix so we zero-pad after the sign
+ if (argText.charAt(0) == '-') {
+ prefix = '-' + prefix;
+ argText = argText.substr(1);
+ }
+
// Add padding.
while (prefix.length + argText.length < width) {
if (flagLeftAlign) {
@@ -2248,8 +2258,12 @@ LibraryManager.library = {
if (next == {{{ charCode('E') }}}) argText = argText.toUpperCase();
// Add sign.
- if (flagAlwaysSigned && currArg >= 0) {
- argText = '+' + argText;
+ if (currArg >= 0) {
+ if (flagAlwaysSigned) {
+ argText = '+' + argText;
+ } else if (flagPadSign) {
+ argText = ' ' + argText;
+ }
}
}
diff --git a/tests/printf/output.txt b/tests/printf/output.txt
index 19a6c1c2..0155f0da 100644
--- a/tests/printf/output.txt
+++ b/tests/printf/output.txt
@@ -3,10 +3,15 @@ n=7
Characters: a A
Decimals: 1977 650000 12 4
-Preceding with blanks: 1977
-Preceding with zeros: 0000001977
+Preceding with blanks: 1977 -1977
+Preceding with zeros: 0000001977 -000001977
+Force sign: +1977 -1977 +1977 -1977
+Force sign or space: 1977 -1977 1977 -1977
+Sign overrides space: +1977 -1977 +1977 -1977
Some different radixes: 100 64 144 0x64 0144
-floats: 3.14 +3e+00 3.141600E+00
+floats: 3.14 +3e+00 3.141600E+00 00003.14
+negative floats: -3.14 -3e+00 -3.141600E+00 -0003.14
+Force sign or space: 3.14 -3.14 3.14 -3.14
Width trick: 10
A string %
Null string: (null)
diff --git a/tests/printf/output_i64_1.txt b/tests/printf/output_i64_1.txt
index 775f3f8d..e38fb78f 100644
--- a/tests/printf/output_i64_1.txt
+++ b/tests/printf/output_i64_1.txt
@@ -3,10 +3,15 @@ n=7
Characters: a A
Decimals: 1977 650000 12 4
-Preceding with blanks: 1977
-Preceding with zeros: 0000001977
+Preceding with blanks: 1977 -1977
+Preceding with zeros: 0000001977 -000001977
+Force sign: +1977 -1977 +1977 -1977
+Force sign or space: 1977 -1977 1977 -1977
+Sign overrides space: +1977 -1977 +1977 -1977
Some different radixes: 100 64 144 0x64 0144
-floats: 3.14 +3e+00 3.141600E+00
+floats: 3.14 +3e+00 3.141600E+00 00003.14
+negative floats: -3.14 -3e+00 -3.141600E+00 -0003.14
+Force sign or space: 3.14 -3.14 3.14 -3.14
Width trick: 10
A string %
Null string: (null)
diff --git a/tests/printf/test.c b/tests/printf/test.c
index d05ba096..1c8ad9f7 100644
--- a/tests/printf/test.c
+++ b/tests/printf/test.c
@@ -8,10 +8,15 @@ int main() {
printf("\n");
printf("Characters: %c %c\n", 'a', 65);
printf("Decimals: %d %ld %lld %d\n", 1977, 650000L, 12LL, 4);
- printf("Preceding with blanks: %10d\n", 1977);
- printf("Preceding with zeros: %010d\n", 1977);
+ printf("Preceding with blanks: %10d %10d\n", 1977, -1977);
+ printf("Preceding with zeros: %010d %010d\n", 1977, -1977);
+ printf("Force sign: %+d %+d %+6d %+6d\n", 1977, -1977, 1977, -1977);
+ printf("Force sign or space: % d % d % 6d % 6d\n", 1977, -1977, 1977, -1977);
+ printf("Sign overrides space: % +d % +d % +6d % +6d\n", 1977, -1977, 1977, -1977);
printf("Some different radixes: %d %x %o %#x %#o\n", 100, 100, 100, 100, 100);
- printf("floats: %4.2f %+.0e %E\n", 3.1416, 3.1416, 3.1416);
+ printf("floats: %4.2f %+.0e %E %08.2f\n", 3.1416, 3.1416, 3.1416, 3.1416);
+ printf("negative floats: %4.2f %+.0e %E %08.2f\n", -3.1416, -3.1416, -3.1416, -3.1416);
+ printf("Force sign or space: % .2f % .2f % 6.2f % 6.2f\n", 3.1416, -3.1416, 3.1416, -3.1416);
printf("Width trick: %*d\n", 5, 10);
printf("%s %%\n", "A string");
printf("Null string: %7s\n", NULL);