Implement missing 'space' formatting flag

This flag causes space (padding) to be reserved for the sign even if the
number is positive. It is basically the same as the 'plus' flag except that
a space is displayed instead of a plus sign. The 'plus' flag takes precedence.
This commit is contained in:
Ranger Harke 2013-09-19 17:24:31 -04:00
Родитель b72b6d8972
Коммит 61c9d6ede1
4 изменённых файлов: 25 добавлений и 4 удалений

Просмотреть файл

@ -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,8 +2162,12 @@ LibraryManager.library = {
}
// Add sign if needed
if (flagAlwaysSigned && currArg >= 0) {
if (currArg >= 0) {
if (flagAlwaysSigned) {
prefix = '+' + prefix;
} else if (flagPadSign) {
prefix = ' ' + prefix;
}
}
// Move sign to prefix so we zero-pad after the sign
@ -2250,8 +2258,12 @@ LibraryManager.library = {
if (next == {{{ charCode('E') }}}) argText = argText.toUpperCase();
// Add sign.
if (flagAlwaysSigned && currArg >= 0) {
if (currArg >= 0) {
if (flagAlwaysSigned) {
argText = '+' + argText;
} else if (flagPadSign) {
argText = ' ' + argText;
}
}
}

Просмотреть файл

@ -6,9 +6,12 @@ Decimals: 1977 650000 12 4
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 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)

Просмотреть файл

@ -6,9 +6,12 @@ Decimals: 1977 650000 12 4
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 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)

Просмотреть файл

@ -11,9 +11,12 @@ int main() {
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 %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);