aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2015-06-27 11:07:30 +1000
committerRob Pike <r@golang.org>2015-06-29 07:17:23 +0000
commita76c1a5c7fb704e5658bbbe9f27b4301b86bfd80 (patch)
tree75dafa9908fb9caf752c1b27cb24ccda6502c985 /src
parentc97e73d8497f61b61299e5d2d371ef672d0c596d (diff)
downloadgo-a76c1a5c7fb704e5658bbbe9f27b4301b86bfd80.tar.xz
fmt: restore padding for %x on byte slices and strings
Also improve the documentation. A prior fix in this release changed the properties for empty strings and slices, incorrectly. Previous behavior is now restored and better documented. Add lots of tests. The behavior is that when using a string-like format (%s %q %x %X) a byte slice is equivalent to a string, and printed as a unit. The padding applies to the entire object. (The space and sharp flags apply elementwise.) Fixes #11422. Fixes #10430. Change-Id: I758f0521caf71630437e43990ec6d6c9a92655e3 Reviewed-on: https://go-review.googlesource.com/11600 Reviewed-by: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/fmt/doc.go5
-rw-r--r--src/fmt/fmt_test.go26
-rw-r--r--src/fmt/format.go2
3 files changed, 28 insertions, 5 deletions
diff --git a/src/fmt/doc.go b/src/fmt/doc.go
index 2efe6ee5b4..ef91368ef0 100644
--- a/src/fmt/doc.go
+++ b/src/fmt/doc.go
@@ -40,7 +40,7 @@
%F synonym for %f
%g %e for large exponents, %f otherwise
%G %E for large exponents, %F otherwise
- String and slice of bytes:
+ String and slice of bytes (treated equivalently with these verbs):
%s the uninterpreted bytes of the string or slice
%q a double-quoted string safely escaped with Go syntax
%x base 16, lower-case, two characters per byte
@@ -164,6 +164,9 @@
of strings, and %6.2f will control formatting for each element
of a floating-point array.
+ However, when printing a byte slice with a string-like verb
+ (%s %q %x %X), it is treated identically to a string, as a single item.
+
To avoid recursion in cases such as
type X string
func (x X) String() string { return Sprintf("<%s>", x) }
diff --git a/src/fmt/fmt_test.go b/src/fmt/fmt_test.go
index 28b7e0551a..90a4031d5b 100644
--- a/src/fmt/fmt_test.go
+++ b/src/fmt/fmt_test.go
@@ -452,10 +452,30 @@ var fmtTests = []struct {
{"%q", []string{"a", "b"}, `["a" "b"]`},
{"% 02x", []byte{1}, "01"},
{"% 02x", []byte{1, 2, 3}, "01 02 03"},
- // Special care for empty slices.
+ // Padding with byte slices.
{"%x", []byte{}, ""},
- {"%02x", []byte{}, ""},
- {"% 02x", []byte{}, ""},
+ {"%02x", []byte{}, "00"},
+ {"% 02x", []byte{}, "00"},
+ {"%08x", []byte{0xab}, "000000ab"},
+ {"% 08x", []byte{0xab}, "000000ab"},
+ {"%08x", []byte{0xab, 0xcd}, "0000abcd"},
+ {"% 08x", []byte{0xab, 0xcd}, "000ab cd"},
+ {"%8x", []byte{0xab}, " ab"},
+ {"% 8x", []byte{0xab}, " ab"},
+ {"%8x", []byte{0xab, 0xcd}, " abcd"},
+ {"% 8x", []byte{0xab, 0xcd}, " ab cd"},
+ // Same for strings
+ {"%x", "", ""},
+ {"%02x", "", "00"},
+ {"% 02x", "", "00"},
+ {"%08x", "\xab", "000000ab"},
+ {"% 08x", "\xab", "000000ab"},
+ {"%08x", "\xab\xcd", "0000abcd"},
+ {"% 08x", "\xab\xcd", "000ab cd"},
+ {"%8x", "\xab", " ab"},
+ {"% 8x", "\xab", " ab"},
+ {"%8x", "\xab\xcd", " abcd"},
+ {"% 8x", "\xab\xcd", " ab cd"},
// renamings
{"%v", renamedBool(true), "true"},
diff --git a/src/fmt/format.go b/src/fmt/format.go
index ac9f6d881a..517b18f7d4 100644
--- a/src/fmt/format.go
+++ b/src/fmt/format.go
@@ -346,7 +346,7 @@ func (f *fmt) fmt_sbx(s string, b []byte, digits string) {
}
buf = append(buf, digits[c>>4], digits[c&0xF])
}
- f.buf.Write(buf)
+ f.pad(buf)
}
// fmt_sx formats a string as a hexadecimal encoding of its bytes.