From c81d09d92bee8f21a79d8bbef3efc6f9f0e36a7e Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Fri, 15 May 2009 15:18:09 -0700 Subject: [PATCH] implement %#o %#x %#X formats R=rsc OCL=28936 CL=28936 --- src/lib/fmt/fmt_test.go | 7 ++++++- src/lib/fmt/format.go | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib/fmt/fmt_test.go b/src/lib/fmt/fmt_test.go index e4158624b0e..e8abc2f0d71 100644 --- a/src/lib/fmt/fmt_test.go +++ b/src/lib/fmt/fmt_test.go @@ -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" }, diff --git a/src/lib/fmt/format.go b/src/lib/fmt/format.go index a66440f2615..3cd492980db 100644 --- a/src/lib/fmt/format.go +++ b/src/lib/fmt/format.go @@ -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--;