Add a %B sprintf format
authorNicholas Clark <nick@ccl4.org>
Mon, 23 Oct 2006 18:19:51 +0000 (19:19 +0100)
committerRafael Garcia-Suarez <rgarciasuarez@gmail.com>
Wed, 25 Oct 2006 10:59:55 +0000 (10:59 +0000)
Subject: Re: [perl #40583] sprintf "%#04X" also uppercases the 0x-prefix
Message-ID: <20061023171951.GA3262@plum.flirble.org>
(plus docs)

p4raw-id: //depot/perl@29104

pod/perlfunc.pod
sv.c
t/op/sprintf.t

index 3920153..ab3b0e1 100644 (file)
@@ -5516,6 +5516,7 @@ In addition, Perl permits the following widely-supported conversions:
    %E  like %e, but using an upper-case "E"
    %G  like %g, but with an upper-case "E" (if applicable)
    %b  an unsigned integer, in binary
+   %B  like %x, but using an upper-case "B" with the # flag
    %p  a pointer (outputs the Perl value's address in hexadecimal)
    %n  special: *stores* the number of characters output so far
         into the next variable in the parameter list
@@ -5558,7 +5559,7 @@ one or more of:
    -       left-justify within the field
    0       use zeros, not spaces, to right-justify
    #       prefix non-zero octal with "0", non-zero hex with "0x"
-           or "0X", non-zero binary with "0b"
+           or "0X", non-zero binary with "0b" or "OB"
 
 For example:
 
diff --git a/sv.c b/sv.c
index f5d9cb6..e38d4b7 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -9039,6 +9039,7 @@ Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV
            base = 10;
            goto uns_integer;
 
+       case 'B':
        case 'b':
            base = 2;
            goto uns_integer;
@@ -9132,7 +9133,7 @@ Perl_sv_vcatpvfn(pTHX_ SV *sv, const char *pat, STRLEN patlen, va_list *args, SV
                    } while (uv >>= 1);
                    if (tempalt) {
                        esignbuf[esignlen++] = '0';
-                       esignbuf[esignlen++] = 'b';
+                       esignbuf[esignlen++] = c;
                    }
                    break;
                default:                /* it had better be ten or less */
index 11eeee4..6751a86 100755 (executable)
@@ -171,7 +171,9 @@ __END__
 >%6 .6s<    >''<          >%6 .6s INVALID<
 >%6.6 s<    >''<          >%6.6 s INVALID<
 >%A<        >''<          >%A INVALID<
->%B<        >''<          >%B INVALID<
+>%B<        >2**32-1<     >11111111111111111111111111111111<
+>%+B<       >2**32-1<     >11111111111111111111111111111111<
+>%#B<       >2**32-1<     >0B11111111111111111111111111111111<
 >%C<        >''<          >%C INVALID<
 >%D<        >0x7fffffff<  >2147483647<     >Synonym for %ld<
 >%E<        >123456.789<  >1.234568E+05<   >Like %e, but using upper-case "E"<