Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #1047: Layout.negative.patch

File Layout.negative.patch, 4.7 kB (added by fvbommel, 5 months ago)

Proposed patch to fix this.

  • tango/text/convert/Layout.d

    old new  
    529529                            return (*cast(bool*) p) ? t : f; 
    530530 
    531531                       case TypeCode.BYTE: 
    532                             return integer (result, *cast(byte*) p, format); 
     532                            return integer (result, *cast(byte*) p, format, ubyte.max); 
    533533 
    534534                       case TypeCode.UBYTE: 
    535                             return integer (result, *cast(ubyte*) p, format, 'u'); 
     535                            return integer (result, *cast(ubyte*) p, format, ubyte.max, 'u'); 
    536536 
    537537                       case TypeCode.SHORT: 
    538                             return integer (result, *cast(short*) p, format); 
     538                            return integer (result, *cast(short*) p, format, ushort.max); 
    539539 
    540540                       case TypeCode.USHORT: 
    541                             return integer (result, *cast(ushort*) p, format, 'u'); 
     541                            return integer (result, *cast(ushort*) p, format, ushort.max, 'u'); 
    542542 
    543543                       case TypeCode.INT: 
    544                             return integer (result, *cast(int*) p, format); 
     544                            return integer (result, *cast(int*) p, format, uint.max); 
    545545 
    546546                       case TypeCode.UINT: 
    547                             return integer (result, *cast(uint*) p, format, 'u'); 
     547                            return integer (result, *cast(uint*) p, format, uint.max, 'u'); 
    548548 
    549549                       case TypeCode.ULONG: 
    550                             return integer (result, *cast(long*) p, format, 'u'); 
     550                            return integer (result, *cast(long*) p, format, ulong.max, 'u'); 
    551551 
    552552                       case TypeCode.LONG: 
    553                             return integer (result, *cast(long*) p, format); 
     553                            return integer (result, *cast(long*) p, format, ulong.max); 
    554554 
    555555                       case TypeCode.FLOAT: 
    556556                            return floater (result, *cast(float*) p, format); 
     
    571571                            return fromUtf32 ((cast(dchar*) p)[0..1], result); 
    572572 
    573573                       case TypeCode.POINTER: 
    574                             return integer (result, *cast(size_t*) p, format, 'x'); 
     574                            return integer (result, *cast(size_t*) p, format, size_t.max, 'x'); 
    575575 
    576576                       case TypeCode.CLASS: 
    577577                            auto c = *cast(Object*) p; 
     
    621621 
    622622        **********************************************************************/ 
    623623 
    624         protected T[] integer (T[] output, long v, T[] format, T style = 'd') 
     624        protected T[] integer (T[] output, long v, T[] format, ulong mask = ulong.max, T style = 'd') 
    625625        { 
    626626                Integer.Flags flags; 
    627627                uint          width = output.length; 
     
    632632                       output = output [0 .. width]; 
    633633                       flags |= flags.Zero; 
    634634                       } 
     635                if (style != 'd') 
     636                    v &= mask; 
    635637                return Integer.format (output, v, cast(Integer.Style) style, flags); 
    636638        } 
    637639 
     
    843845        // fragments before and after 
    844846        assert( Formatter( "d{0}d", "s" ) == "dsd" ); 
    845847 
     848        // Negative numbers in various bases 
     849        assert( Formatter( "{:b}", cast(byte) -1 ) == "11111111" ); 
     850        assert( Formatter( "{:b}", cast(short) -1 ) == "1111111111111111" ); 
     851        assert( Formatter( "{:b}", cast(int) -1 ) 
     852                == "11111111111111111111111111111111" ); 
     853        assert( Formatter( "{:b}", cast(long) -1 ) 
     854                == "1111111111111111111111111111111111111111111111111111111111111111" ); 
     855 
     856        assert( Formatter( "{:o}", cast(byte) -1 ) == "377" ); 
     857        assert( Formatter( "{:o}", cast(short) -1 ) == "177777" ); 
     858        assert( Formatter( "{:o}", cast(int) -1 ) == "37777777777" ); 
     859        assert( Formatter( "{:o}", cast(long) -1 ) == "1777777777777777777777" ); 
     860 
     861        assert( Formatter( "{:d}", cast(byte) -1 ) == "-1" ); 
     862        assert( Formatter( "{:d}", cast(short) -1 ) == "-1" ); 
     863        assert( Formatter( "{:d}", cast(int) -1 ) == "-1" ); 
     864        assert( Formatter( "{:d}", cast(long) -1 ) == "-1" ); 
     865 
     866        assert( Formatter( "{:x}", cast(byte) -1 ) == "ff" ); 
     867        assert( Formatter( "{:x}", cast(short) -1 ) == "ffff" ); 
     868        assert( Formatter( "{:x}", cast(int) -1 ) == "ffffffff" ); 
     869        assert( Formatter( "{:x}", cast(long) -1 ) == "ffffffffffffffff" ); 
     870 
    846871        // argument index 
    847872        assert( Formatter( "a{0}b{1}c{2}", "x", "y", "z" ) == "axbycz" ); 
    848873        assert( Formatter( "a{2}b{1}c{0}", "x", "y", "z" ) == "azbycx" );