1
0
mirror of https://github.com/golang/go synced 2024-11-23 05:40:04 -07:00

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
This commit is contained in:
Mihai Borobocea 2014-07-29 16:46:53 -07:00 committed by Rob Pike
parent d2204e6c0e
commit bfdeb57cc3
2 changed files with 3 additions and 1 deletions

View File

@ -183,6 +183,8 @@ var fmtTests = []struct {
{"%.3q", "日本語日本語", `"日本語"`},
{"%.3q", []byte("日本語日本語"), `"日本語"`},
{"%10.1q", "日本語日本語", ` "日"`},
{"%3c", '⌘', " ⌘"},
{"%5q", '\u2026', ` '…'`},
{"%10v", nil, " <nil>"},
{"%-10v", nil, "<nil> "},

View 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)
}