aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRanger Harke <ranger.harke@autodesk.com>2013-09-19 16:03:15 -0400
committerRanger Harke <ranger.harke@autodesk.com>2013-09-19 16:03:15 -0400
commite4678bed92f20ea0b5cd6fa2467cc2b0444511e0 (patch)
treeacc9652cb675f76a8a1d1e1d37241beeea3e2251
parent45f8f9c948c5548665438827e3d6b39a40eca06e (diff)
Fix bug with zero-padded negative integers
The zero padding was before the sign. It should be after.
-rw-r--r--src/library.js6
-rw-r--r--tests/printf/output.txt7
-rw-r--r--tests/printf/output_i64_1.txt7
-rw-r--r--tests/printf/test.c7
4 files changed, 18 insertions, 9 deletions
diff --git a/src/library.js b/src/library.js
index 5c2c858d..0aaef0b2 100644
--- a/src/library.js
+++ b/src/library.js
@@ -2166,6 +2166,12 @@ LibraryManager.library = {
}
}
+ // 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) {
diff --git a/tests/printf/output.txt b/tests/printf/output.txt
index 19a6c1c2..0f59d9fc 100644
--- a/tests/printf/output.txt
+++ b/tests/printf/output.txt
@@ -3,10 +3,11 @@ 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
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
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..ca03386f 100644
--- a/tests/printf/output_i64_1.txt
+++ b/tests/printf/output_i64_1.txt
@@ -3,10 +3,11 @@ 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
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
Width trick: 10
A string %
Null string: (null)
diff --git a/tests/printf/test.c b/tests/printf/test.c
index d05ba096..c5f159f1 100644
--- a/tests/printf/test.c
+++ b/tests/printf/test.c
@@ -8,10 +8,11 @@ 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("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("Width trick: %*d\n", 5, 10);
printf("%s %%\n", "A string");
printf("Null string: %7s\n", NULL);