1
0
mirror of https://github.com/golang/go synced 2024-11-20 11:24:47 -07:00

implement %#o %#x %#X formats

R=rsc
OCL=28936
CL=28936
This commit is contained in:
Rob Pike 2009-05-15 15:18:09 -07:00
parent 66f5e89082
commit c81d09d92b
2 changed files with 21 additions and 1 deletions

View File

@ -77,7 +77,6 @@ var fmttests = []fmtTest{
fmtTest{ "%+d", 12345, "+12345" }, fmtTest{ "%+d", 12345, "+12345" },
fmtTest{ "%+d", -12345, "-12345" }, fmtTest{ "%+d", -12345, "-12345" },
fmtTest{ "% d", 12345, " 12345" }, fmtTest{ "% d", 12345, " 12345" },
fmtTest{ "% d", -12345, "-12345" },
// arrays // arrays
fmtTest{ "%v", array, "[1 2 3 4 5]" }, fmtTest{ "%v", array, "[1 2 3 4 5]" },
@ -92,12 +91,15 @@ var fmttests = []fmtTest{
fmtTest{ "%d", uint32(b32), "4294967295" }, fmtTest{ "%d", uint32(b32), "4294967295" },
fmtTest{ "%d", uint64(b64), "18446744073709551615" }, fmtTest{ "%d", uint64(b64), "18446744073709551615" },
fmtTest{ "%o", 01234, "1234" }, fmtTest{ "%o", 01234, "1234" },
fmtTest{ "%#o", 01234, "01234" },
fmtTest{ "%o", uint32(b32), "37777777777" }, fmtTest{ "%o", uint32(b32), "37777777777" },
fmtTest{ "%o", uint64(b64), "1777777777777777777777" }, fmtTest{ "%o", uint64(b64), "1777777777777777777777" },
fmtTest{ "%x", 0x1234abcd, "1234abcd" }, fmtTest{ "%x", 0x1234abcd, "1234abcd" },
fmtTest{ "%#x", 0x1234abcd, "0x1234abcd" },
fmtTest{ "%x", b32-0x1234567, "fedcba98" }, fmtTest{ "%x", b32-0x1234567, "fedcba98" },
fmtTest{ "%X", 0x1234abcd, "1234ABCD" }, fmtTest{ "%X", 0x1234abcd, "1234ABCD" },
fmtTest{ "%X", b32-0x1234567, "FEDCBA98" }, fmtTest{ "%X", b32-0x1234567, "FEDCBA98" },
fmtTest{ "%#X", 0, "0X0" },
fmtTest{ "%x", b64, "ffffffffffffffff" }, fmtTest{ "%x", b64, "ffffffffffffffff" },
fmtTest{ "%b", 7, "111" }, fmtTest{ "%b", 7, "111" },
fmtTest{ "%b", b64, "1111111111111111111111111111111111111111111111111111111111111111" }, fmtTest{ "%b", b64, "1111111111111111111111111111111111111111111111111111111111111111" },
@ -125,6 +127,9 @@ var fmttests = []fmtTest{
fmtTest{ "%20d", 1234, " 1234" }, fmtTest{ "%20d", 1234, " 1234" },
fmtTest{ "%-20.8d", 1234, "00001234 " }, fmtTest{ "%-20.8d", 1234, "00001234 " },
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{ "%.20b", 7, "00000000000000000111" },
fmtTest{ "%20.5s", "qwertyuiop", " qwert" }, fmtTest{ "%20.5s", "qwertyuiop", " qwert" },
fmtTest{ "%.5s", "qwertyuiop", "qwert" }, fmtTest{ "%.5s", "qwertyuiop", "qwert" },

View File

@ -202,6 +202,21 @@ func (f *Fmt) integer(a int64, base uint, is_signed bool, digits *string) string
i--; 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 { if negative {
buf[i] = '-'; buf[i] = '-';
i--; i--;