lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
Subsystems (2)
PATCH 1/1 lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
number()'s behaviour is slighly changed:
0 becomes "0" instead of "00" when using the flag SPECIAL and base 8.
Before:
Number\Format %o %#o %x %#x
0 0 00 0 0x0
1 1 01 1 0x1
16 20 020 10 0x10After:
Number\Format %o %#o %x %#x
0 0 0 0 0x0
1 1 01 1 0x1
16 20 020 10 0x10--- lib/vsprintf.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
lib/vsprintf.c +6 -3
@@ -284,6 +284,7 @@ char *number(char *buf, char *end, unsigned long long num,char locase;int need_pfx = ((spec.flags &SPECIAL ) && spec.base !=10 );int i; +bool is_null = num ==0LL ;/* locase = 0 or 0x20. ORing digits or letters with 'locase' * produces same digits or (maybe lowercased) letters */
@@ -353,9 +354,11 @@ char *number(char *buf, char *end, unsigned long long num, }/* "0x" / "0" prefix */ if (need_pfx) { -if (buf < end) - *buf ='0' ; - ++buf; +if (spec.base ==16 || !is_null) { +if (buf < end) + *buf ='0' ; + ++buf; + }if (spec.base ==16 ) {if (buf < end) *buf = ('X' | locase);
Signature · 1 line
1.7.10.1
Hi Pierre,
Thanks for doing this. Comments below.
lib/vsprintf.c char *number(char *buf, char *end, unsigned long long num, · line 287
Quoted 10 lines · On Sat, 5 May 2012 16:32:30 +0200 Pierre Carrier <[email protected]> wrote:
lib/vsprintf.c +1
-0@@ -284,6 +284,7 @@ char *number(char *buf, char *end, unsigned long long num,char locase;int need_pfx = ((spec.flags &SPECIAL ) && spec.base !=10 );int i; +bool is_null = num ==0LL ;
"num" is a number not a pointer, so I would call it "is_zero" (or just do the comparisons explicitly below).
Quoted 7 lines
lib/vsprintf.c +0
-1/* locase = 0 or 0x20. ORing digits or letters with 'locase' * produces same digits or (maybe lowercased) letters */ @@ -353,9 +354,11 @@ char *number(char *buf, char *end, unsigned long long num, }/* "0x" / "0" prefix */ if (need_pfx) { -if (buf < end)
lib/vsprintf.c char *number(char *buf, char *end, unsigned long long num, · line 364
Quoted 10 lines
lib/vsprintf.c +5
-2- *buf ='0' ; - ++buf; +if (spec.base ==16 || !is_null) { +if (buf < end) + *buf ='0' ; + ++buf; + }if (spec.base ==16 ) {if (buf < end) *buf = ('X' | locase);
There is also another earlier section that tests "need_pfx" that reduces the field width. You should fix that up as well.
Signature · 3 lines
Cheers, Stephen Rothwell [email protected] http://www.canb.auug.org.au/~sfr/
PATCH 1/1 lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
number()'s behaviour is slighly changed:
0 becomes "0" instead of "00" when using the flag SPECIAL and base 8.
Before:
Number\Format %o %#o %x %#x
0 0 00 0 0x0
1 1 01 1 0x1
16 20 020 10 0x10After:
Number\Format %o %#o %x %#x
0 0 0 0 0x0
1 1 01 1 0x1
16 20 020 10 0x10--- lib/vsprintf.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
lib/vsprintf.c +8 -4
@@ -284,6 +284,7 @@ char *number(char *buf, char *end, unsigned long long num,char locase;int need_pfx = ((spec.flags &SPECIAL ) && spec.base !=10 );int i; +bool is_zero = num ==0LL ;/* locase = 0 or 0x20. ORing digits or letters with 'locase' * produces same digits or (maybe lowercased) letters */
@@ -305,8 +306,9 @@ char *number(char *buf, char *end, unsigned long long num, } }if (need_pfx) { - spec.field_width--;if (spec.base ==16 ) + spec.field_width -=2 ; +else if (!is_zero) spec.field_width--; }
@@ -353,9 +355,11 @@ char *number(char *buf, char *end, unsigned long long num, }/* "0x" / "0" prefix */ if (need_pfx) { -if (buf < end) - *buf ='0' ; - ++buf; +if (spec.base ==16 || !is_zero) { +if (buf < end) + *buf ='0' ; + ++buf; + }if (spec.base ==16 ) {if (buf < end) *buf = ('X' | locase);
Signature · 1 line
1.7.10.1
Quoted 9 lines · Pierre Carrier
number()'s behaviour is slighly changed:
0 becomes "0" instead of "00" when using the flag SPECIAL and base 8.Before:
Number\Format %o %#o %x %#x 0 0 00 0 0x0 1 1 01 1 0x1 16 20 020 10 0x10
Quoted 5 lines
After:
Number\Format %o %#o %x %#x 0 0 0 0 0x0 1 1 01 1 0x1 16 20 020 10 0x10
If you do this, why not change 0-7 instead of just 0?
Quoted 1 line · Joe Perches
If you do this, why not change 0-7 instead of just 0?
Consistency with POSIX *printf.
"#: Specifies that the value is to be converted to an alternative form. For o conversion, it increases the precision (if necessary) to force the first digit of the result to be zero."
-- http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html
% cat octal.c;gcc -o octal octal.c;./octal
#include <stdio.h>
void main() {
int i;
for (i=0;i<10;i++) printf("%#o ", i);
puts("");
}0 01 02 03 04 05 06 07 010 011
Signature · 1 line
Pierre
Hi Pierre,
Quoted 11 lines · On Mon, 7 May 2012 04:20:18 +0200 Pierre Carrier <[email protected]> wrote:
Quoted 1 line · Joe Perches
If you do this, why not change 0-7 instead of just 0?
Consistency with POSIX *printf.
"#: Specifies that the value is to be converted to an alternative form. For o conversion, it increases the precision (if necessary) to force the first digit of the result to be zero."
-- http://pubs.opengroup.org/onlinepubs/009695399/functions/printf.html
Quoted 8 lines
% cat octal.c;gcc -o octal octal.c;./octal
#include <stdio.h>void main() { int i; for (i=0;i<10;i++) printf("%#o ", i); puts(""); }0 01 02 03 04 05 06 07 010 011
Yes, if we are going to follow anything, then it may as well be that.
However just to add more mess, the next part of that section of the standard says:
"For x or X conversion specifiers, a non-zero result shall have 0x (or 0X) prefixed to it."
so we should also do:
0 0x1 ...
However, I think we expect 0x0, so changing it is, I think, unnecessary.
Signature · 3 lines
Cheers, Stephen Rothwell [email protected] http://www.canb.auug.org.au/~sfr/
Quoted 10 lines · On Mon, 7 May 2012 03:20:14 +0200 Pierre Carrier <[email protected]> wrote:
number()'s behaviour is slighly changed:
0 becomes "0" instead of "00" when using the flag SPECIAL and base 8.Before:
Number\Format %o %#o %x %#x 0 0 00 0 0x0 1 1 01 1 0x1 16 20 020 10 0x10
Quoted 7 lines
After:
Number\Format %o %#o %x %#x 0 0 0 0 0x0 1 1 01 1 0x1 16 20 020 10 0x10Signed-off-by Pierre Carrier <[email protected]>
Signature · 2 lines
Cheers, Stephen Rothwell [email protected]