linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
@ 2012-05-05 14:32 Pierre Carrier
  2012-05-07  0:19 ` Stephen Rothwell
  0 siblings, 1 reply; 7+ messages in thread
From: Pierre Carrier @ 2012-05-05 14:32 UTC (permalink / raw)
  To: Stephen Rothwell, Andrew Morton; +Cc: linux-kernel, 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

After:
Number\Format  %o    %#o  %x    %#x
            0     0    0    0   0x0
            1     1   01    1   0x1
           16    20  020   10  0x10

Signed-off-by: Pierre Carrier <pierre@spotify.com>
---
 lib/vsprintf.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index abbabec..7129383 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -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);
-- 
1.7.10.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
  2012-05-05 14:32 [PATCH 1/1] lib/vsprintf.c: "%#o",0 becomes '0' instead of '00' Pierre Carrier
@ 2012-05-07  0:19 ` Stephen Rothwell
  2012-05-07  1:20   ` Pierre Carrier
  0 siblings, 1 reply; 7+ messages in thread
From: Stephen Rothwell @ 2012-05-07  0:19 UTC (permalink / raw)
  To: Pierre Carrier; +Cc: Andrew Morton, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1336 bytes --]

Hi Pierre,

Thanks for doing this.  Comments below.

On Sat,  5 May 2012 16:32:30 +0200 Pierre Carrier <pierre@spotify.com> wrote:
>
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index abbabec..7129383 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -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).

>  	/* 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);

There is also another earlier section that tests "need_pfx" that reduces
the field width.  You should fix that up as well.

-- 
Cheers,
Stephen Rothwell                    sfr@canb.auug.org.au
http://www.canb.auug.org.au/~sfr/

[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/1] lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
  2012-05-07  0:19 ` Stephen Rothwell
@ 2012-05-07  1:20   ` Pierre Carrier
  2012-05-07  1:59     ` Joe Perches
  2012-05-07  3:56     ` Stephen Rothwell
  0 siblings, 2 replies; 7+ messages in thread
From: Pierre Carrier @ 2012-05-07  1:20 UTC (permalink / raw)
  To: Stephen Rothwell, Andrew Morton; +Cc: linux-kernel, 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

After:
Number\Format  %o    %#o  %x    %#x
            0     0    0    0   0x0
            1     1   01    1   0x1
           16    20  020   10  0x10

Signed-off-by: Pierre Carrier <pierre@spotify.com>
---
 lib/vsprintf.c |   12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index abbabec..bc8f319 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -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);
-- 
1.7.10.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
  2012-05-07  1:20   ` Pierre Carrier
@ 2012-05-07  1:59     ` Joe Perches
  2012-05-07  2:20       ` Pierre Carrier
  2012-05-07  3:56     ` Stephen Rothwell
  1 sibling, 1 reply; 7+ messages in thread
From: Joe Perches @ 2012-05-07  1:59 UTC (permalink / raw)
  To: Pierre Carrier; +Cc: Stephen Rothwell, Andrew Morton, linux-kernel

On Mon, 2012-05-07 at 03:20 +0200, Pierre Carrier 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
> 
> 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?



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
  2012-05-07  1:59     ` Joe Perches
@ 2012-05-07  2:20       ` Pierre Carrier
  2012-05-07  3:55         ` Stephen Rothwell
  0 siblings, 1 reply; 7+ messages in thread
From: Pierre Carrier @ 2012-05-07  2:20 UTC (permalink / raw)
  To: Joe Perches; +Cc: Stephen Rothwell, Andrew Morton, linux-kernel

On Mon, May 7, 2012 at 3:59 AM, Joe Perches <joe@perches.com> wrote:
> 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

-- 
Pierre

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
  2012-05-07  2:20       ` Pierre Carrier
@ 2012-05-07  3:55         ` Stephen Rothwell
  0 siblings, 0 replies; 7+ messages in thread
From: Stephen Rothwell @ 2012-05-07  3:55 UTC (permalink / raw)
  To: Pierre Carrier; +Cc: Joe Perches, Andrew Morton, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1231 bytes --]

Hi Pierre,

On Mon, 7 May 2012 04:20:18 +0200 Pierre Carrier <pierre@spotify.com> wrote:
>
> On Mon, May 7, 2012 at 3:59 AM, Joe Perches <joe@perches.com> wrote:
> > 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

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.

-- 
Cheers,
Stephen Rothwell                    sfr@canb.auug.org.au
http://www.canb.auug.org.au/~sfr/

[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/1] lib/vsprintf.c: "%#o",0 becomes '0' instead of '00'
  2012-05-07  1:20   ` Pierre Carrier
  2012-05-07  1:59     ` Joe Perches
@ 2012-05-07  3:56     ` Stephen Rothwell
  1 sibling, 0 replies; 7+ messages in thread
From: Stephen Rothwell @ 2012-05-07  3:56 UTC (permalink / raw)
  To: Pierre Carrier; +Cc: Andrew Morton, linux-kernel, Joe Perches

[-- Attachment #1: Type: text/plain, Size: 725 bytes --]

On Mon,  7 May 2012 03:20:14 +0200 Pierre Carrier <pierre@spotify.com> 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
> 
> After:
> Number\Format  %o    %#o  %x    %#x
>             0     0    0    0   0x0
>             1     1   01    1   0x1
>            16    20  020   10  0x10
> 
> Signed-off-by: Pierre Carrier <pierre@spotify.com>

Acked-by: Stephen Rothwell <sfr@canb.auug.org.au>

-- 
Cheers,
Stephen Rothwell                    sfr@canb.auug.org.au

[-- Attachment #2: Type: application/pgp-signature, Size: 836 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-05-07  3:56 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-05 14:32 [PATCH 1/1] lib/vsprintf.c: "%#o",0 becomes '0' instead of '00' Pierre Carrier
2012-05-07  0:19 ` Stephen Rothwell
2012-05-07  1:20   ` Pierre Carrier
2012-05-07  1:59     ` Joe Perches
2012-05-07  2:20       ` Pierre Carrier
2012-05-07  3:55         ` Stephen Rothwell
2012-05-07  3:56     ` Stephen Rothwell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).