From bfdeb57cc31d5db0501e4d364b74088f5c0e60b1 Mon Sep 17 00:00:00 2001 From: Mihai Borobocea Date: Tue, 29 Jul 2014 16:46:53 -0700 Subject: [PATCH] fmt: measure width in runes not bytes with %c and %q for ints MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 2 ++ src/pkg/fmt/format.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) 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) } -- 2.48.1