From: Mihai Borobocea Date: Tue, 29 Jul 2014 23:46:53 +0000 (-0700) Subject: fmt: measure width in runes not bytes with %c and %q for ints X-Git-Tag: go1.4beta1~992 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=bfdeb57cc31d5db0501e4d364b74088f5c0e60b1;p=gostls13.git fmt: measure width in runes not bytes with %c and %q for ints 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 --- diff --git a/src/pkg/fmt/fmt_test.go b/src/pkg/fmt/fmt_test.go index ef8b2ad86e..89227cce80 100644 --- a/src/pkg/fmt/fmt_test.go +++ b/src/pkg/fmt/fmt_test.go @@ -183,6 +183,8 @@ var fmtTests = []struct { {"%.3q", "日本語日本語", `"日本語"`}, {"%.3q", []byte("日本語日本語"), `"日本語"`}, {"%10.1q", "日本語日本語", ` "日"`}, + {"%3c", '⌘', " ⌘"}, + {"%5q", '\u2026', ` '…'`}, {"%10v", nil, " "}, {"%-10v", nil, " "}, diff --git a/src/pkg/fmt/format.go b/src/pkg/fmt/format.go index f50163c4a2..8aeffd7b2b 100644 --- a/src/pkg/fmt/format.go +++ b/src/pkg/fmt/format.go @@ -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) }