]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: measure width in runes not bytes with %c and %q for ints
authorMihai Borobocea <MihaiBorobocea@gmail.com>
Tue, 29 Jul 2014 23:46:53 +0000 (16:46 -0700)
committerRob Pike <r@golang.org>
Tue, 29 Jul 2014 23:46:53 +0000 (16:46 -0700)
This is meant to share my progress on Issue 8275, if it's useful to you. I'm not familiar with the formatter's internals, so this change is likely naive.

Change these calls to measure width in runes not bytes:
fmt.Printf("(%5q)\n", '§')
fmt.Printf("(%3c)\n", '§')

 Fixes #8275.

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/104320043

src/pkg/fmt/fmt_test.go
src/pkg/fmt/format.go

index ef8b2ad86e9b28f97760067cd9555f9fce4675da..89227cce806eba888fdc751d3d646dad8a7ba704 100644 (file)
@@ -183,6 +183,8 @@ var fmtTests = []struct {
        {"%.3q", "日本語日本語", `"日本語"`},
        {"%.3q", []byte("日本語日本語"), `"日本語"`},
        {"%10.1q", "日本語日本語", `       "日"`},
+       {"%3c", '⌘', "  ⌘"},
+       {"%5q", '\u2026', `  '…'`},
        {"%10v", nil, "     <nil>"},
        {"%-10v", nil, "<nil>     "},
 
index f50163c4a2dba2f1dc075cee703837dd334c54f3..8aeffd7b2b7d7b21774c41b4325035cded96a85d 100644 (file)
@@ -114,7 +114,7 @@ func (f *fmt) pad(b []byte) {
                f.buf.Write(b)
                return
        }
-       padding, left, right := f.computePadding(len(b))
+       padding, left, right := f.computePadding(utf8.RuneCount(b))
        if left > 0 {
                f.writePadding(left, padding)
        }