1
0
mirror of https://github.com/golang/go synced 2024-11-25 04:07:55 -07:00

fmt: allow "% X" as well as "% x"

R=rsc, cw, PeterGo
CC=golang-dev
https://golang.org/cl/3319042
This commit is contained in:
Rob Pike 2010-11-29 07:30:36 -08:00
parent abb0c0967c
commit d90d0ede3f
3 changed files with 6 additions and 2 deletions

View File

@ -58,7 +58,7 @@
0X for hex (%#X); suppress 0x for %p (%#p); 0X for hex (%#X); suppress 0x for %p (%#p);
print a raw (backquoted) string if possible for %q (%#q) print a raw (backquoted) string if possible for %q (%#q)
' ' (space) leave a space for elided sign in numbers (% d); ' ' (space) leave a space for elided sign in numbers (% d);
put spaces between bytes printing strings or slices in hex (% x) put spaces between bytes printing strings or slices in hex (% x, % X)
0 pad with leading zeros rather than spaces 0 pad with leading zeros rather than spaces
For each Printf-like function, there is also a Print function For each Printf-like function, there is also a Print function

View File

@ -121,7 +121,8 @@ var fmttests = []fmtTest{
// basic bytes // basic bytes
{"%s", []byte("abc"), "abc"}, {"%s", []byte("abc"), "abc"},
{"%x", []byte("abc"), "616263"}, {"%x", []byte("abc"), "616263"},
{"% x", []byte("abc"), "61 62 63"}, {"% x", []byte("abc\xff"), "61 62 63 ff"},
{"% X", []byte("abc\xff"), "61 62 63 FF"},
{"%x", []byte("xyz"), "78797a"}, {"%x", []byte("xyz"), "78797a"},
{"%X", []byte("xyz"), "78797A"}, {"%X", []byte("xyz"), "78797A"},
{"%q", []byte("abc"), `"abc"`}, {"%q", []byte("abc"), `"abc"`},

View File

@ -255,6 +255,9 @@ func (f *fmt) fmt_sx(s string) {
func (f *fmt) fmt_sX(s string) { func (f *fmt) fmt_sX(s string) {
t := "" t := ""
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
if i > 0 && f.space {
t += " "
}
v := s[i] v := s[i]
t += string(udigits[v>>4]) t += string(udigits[v>>4])
t += string(udigits[v&0xF]) t += string(udigits[v&0xF])