Skip to content

Commit a76c1a5

Browse files
committed
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>
1 parent c97e73d commit a76c1a5

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/fmt/doc.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
%F synonym for %f
4141
%g %e for large exponents, %f otherwise
4242
%G %E for large exponents, %F otherwise
43-
String and slice of bytes:
43+
String and slice of bytes (treated equivalently with these verbs):
4444
%s the uninterpreted bytes of the string or slice
4545
%q a double-quoted string safely escaped with Go syntax
4646
%x base 16, lower-case, two characters per byte
@@ -164,6 +164,9 @@
164164
of strings, and %6.2f will control formatting for each element
165165
of a floating-point array.
166166
167+
However, when printing a byte slice with a string-like verb
168+
(%s %q %x %X), it is treated identically to a string, as a single item.
169+
167170
To avoid recursion in cases such as
168171
type X string
169172
func (x X) String() string { return Sprintf("<%s>", x) }

src/fmt/fmt_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,30 @@ var fmtTests = []struct {
452452
{"%q", []string{"a", "b"}, `["a" "b"]`},
453453
{"% 02x", []byte{1}, "01"},
454454
{"% 02x", []byte{1, 2, 3}, "01 02 03"},
455-
// Special care for empty slices.
455+
// Padding with byte slices.
456456
{"%x", []byte{}, ""},
457-
{"%02x", []byte{}, ""},
458-
{"% 02x", []byte{}, ""},
457+
{"%02x", []byte{}, "00"},
458+
{"% 02x", []byte{}, "00"},
459+
{"%08x", []byte{0xab}, "000000ab"},
460+
{"% 08x", []byte{0xab}, "000000ab"},
461+
{"%08x", []byte{0xab, 0xcd}, "0000abcd"},
462+
{"% 08x", []byte{0xab, 0xcd}, "000ab cd"},
463+
{"%8x", []byte{0xab}, " ab"},
464+
{"% 8x", []byte{0xab}, " ab"},
465+
{"%8x", []byte{0xab, 0xcd}, " abcd"},
466+
{"% 8x", []byte{0xab, 0xcd}, " ab cd"},
467+
// Same for strings
468+
{"%x", "", ""},
469+
{"%02x", "", "00"},
470+
{"% 02x", "", "00"},
471+
{"%08x", "\xab", "000000ab"},
472+
{"% 08x", "\xab", "000000ab"},
473+
{"%08x", "\xab\xcd", "0000abcd"},
474+
{"% 08x", "\xab\xcd", "000ab cd"},
475+
{"%8x", "\xab", " ab"},
476+
{"% 8x", "\xab", " ab"},
477+
{"%8x", "\xab\xcd", " abcd"},
478+
{"% 8x", "\xab\xcd", " ab cd"},
459479

460480
// renamings
461481
{"%v", renamedBool(true), "true"},

src/fmt/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ func (f *fmt) fmt_sbx(s string, b []byte, digits string) {
346346
}
347347
buf = append(buf, digits[c>>4], digits[c&0xF])
348348
}
349-
f.buf.Write(buf)
349+
f.pad(buf)
350350
}
351351

352352
// fmt_sx formats a string as a hexadecimal encoding of its bytes.

0 commit comments

Comments
 (0)