Commit c81d09d9 authored by Rob Pike's avatar Rob Pike

implement %#o %#x %#X formats

R=rsc
OCL=28936
CL=28936
parent 66f5e890
......@@ -77,7 +77,6 @@ var fmttests = []fmtTest{
fmtTest{ "%+d", 12345, "+12345" },
fmtTest{ "%+d", -12345, "-12345" },
fmtTest{ "% d", 12345, " 12345" },
fmtTest{ "% d", -12345, "-12345" },
// arrays
fmtTest{ "%v", array, "[1 2 3 4 5]" },
......@@ -92,12 +91,15 @@ var fmttests = []fmtTest{
fmtTest{ "%d", uint32(b32), "4294967295" },
fmtTest{ "%d", uint64(b64), "18446744073709551615" },
fmtTest{ "%o", 01234, "1234" },
fmtTest{ "%#o", 01234, "01234" },
fmtTest{ "%o", uint32(b32), "37777777777" },
fmtTest{ "%o", uint64(b64), "1777777777777777777777" },
fmtTest{ "%x", 0x1234abcd, "1234abcd" },
fmtTest{ "%#x", 0x1234abcd, "0x1234abcd" },
fmtTest{ "%x", b32-0x1234567, "fedcba98" },
fmtTest{ "%X", 0x1234abcd, "1234ABCD" },
fmtTest{ "%X", b32-0x1234567, "FEDCBA98" },
fmtTest{ "%#X", 0, "0X0" },
fmtTest{ "%x", b64, "ffffffffffffffff" },
fmtTest{ "%b", 7, "111" },
fmtTest{ "%b", b64, "1111111111111111111111111111111111111111111111111111111111111111" },
......@@ -125,6 +127,9 @@ var fmttests = []fmtTest{
fmtTest{ "%20d", 1234, " 1234" },
fmtTest{ "%-20.8d", 1234, "00001234 " },
fmtTest{ "%-20.8d", -1234, "-00001234 " },
fmtTest{ "%-#20.8x", 0x1234abc, "0x01234abc " },
fmtTest{ "%-#20.8X", 0x1234abc, "0X01234ABC " },
fmtTest{ "%-#20.8o", 01234, "00001234 " },
fmtTest{ "%.20b", 7, "00000000000000000111" },
fmtTest{ "%20.5s", "qwertyuiop", " qwert" },
fmtTest{ "%.5s", "qwertyuiop", "qwert" },
......
......@@ -202,6 +202,21 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string
i--;
}
if f.sharp {
switch base {
case 8:
if buf[i+1] != '0' {
buf[i] = '0';
i--;
}
case 16:
buf[i] = 'x' + digits[10]-'a';
i--;
buf[i] = '0';
i--;
}
}
if negative {
buf[i] = '-';
i--;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment