Thread overview
[Bug 113] New: std.format.doFormat and small hex numbers
Apr 21, 2006
d-bugmail
Apr 22, 2006
d-bugmail
Apr 22, 2006
d-bugmail
April 21, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=113

           Summary: std.format.doFormat and small hex numbers
           Product: D
           Version: 0.154
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: fvbommel@wxs.nl


There's a bug in std.format.doFormat regarding small hexadecimals, specifically 0xA to 0xF.

This small test program demonstrates the problem:

-----

D:\Temp>cat test.d
import std.stdio;

int main()
{
    for (int i = 10; i < 16; i++) writefln("%d - %x", i, i);
    return 0;
}

D:\Temp>dmd -run test.d
10 - :
11 - ;
12 - <
13 - =
14 - >
15 - ?

-----

As you can see, this program doesn't produce the expected output (which would
have the lines end in a-f instead of the various punctuation marks).
Similar behavior can be observed by using std.string.format and probably
anything else that relies on std.format.doFormat to perform such formatting.

From a quick look at the code the bug seems to originate around line 875 of src/phobos/std/format.d where for single-character numbers (vnumber < base) the single-character output '0'+vnumber is produced. This "optimization" seems to result in this bug when vnumber > 9 and base > 10.


-- 

April 22, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=113


rioshin@mbnet.fi changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rioshin@mbnet.fi




------- Comment #1 from rioshin@mbnet.fi  2006-04-22 04:09 -------
Ok, this definitely is a bug in the library. Currently the code looks like:

if (vnumber < base)
{
    vchar = '0' + vnumber;
    goto L2;
}

which results in a bug in case vnumber >= 10 and base > 10. To fix it, we could use:

if (vnumber < base && vnumber < 10)
{
    vchar = '0' + vnumber;
    goto L2;
}
else if (vnumber < base)
{
    vchar = 'A' + (vnumber - 10);
    goto L2;
}


-- 

April 22, 2006
http://d.puremagic.com/bugzilla/show_bug.cgi?id=113





------- Comment #2 from fvbommel@wxs.nl  2006-04-22 04:21 -------
(In reply to comment #1)
>     vchar = 'A' + (vnumber - 10);

I think you mean something more like:

     vchar = (uc ? 'A' : 'a') + (vnumber - 10);

but yeah, something like that. Or rewire the condition to just go to the normal code for (vnumber >= 10).


--