Bug 113 – std.format.doFormat and small hex numbers
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2006-04-21T17:07:00Z
Last change time
2014-02-15T13:29:05Z
Assigned to
bugzilla
Creator
fvbommel
Comments
Comment #0 by fvbommel — 2006-04-21T17:07:26Z
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.
Comment #1 by rioshin — 2006-04-22T04:09:02Z
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;
}
Comment #2 by fvbommel — 2006-04-22T04:21:10Z
(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).