]> Cypherpunks repositories - gostls13.git/commitdiff
Count utf8 runes, not bytes when determining string width. Note
authorStephen Ma <stephenm@golang.org>
Sun, 28 Feb 2010 10:15:56 +0000 (21:15 +1100)
committerRob Pike <r@golang.org>
Sun, 28 Feb 2010 10:15:56 +0000 (21:15 +1100)
that pad() still counts bytes, but it's currently only used for
1 byte runes.

Fixes #612.

R=r
CC=golang-dev
https://golang.org/cl/217064

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

index b54c25899fc7448df1cd561b254edbe77086afc1..3752b35264d6790e234c69cc546f2114da6dae6d 100644 (file)
@@ -86,6 +86,7 @@ var fmttests = []fmtTest{
 
        // width
        fmtTest{"%5s", "abc", "  abc"},
+       fmtTest{"%2s", "\u263a", " \u263a"},
        fmtTest{"%-5s", "abc", "abc  "},
        fmtTest{"%05s", "abc", "00abc"},
 
index 38b234414fbfa8462de924412565e06d3b375666..88ef3504e71f2ef194b9eb438e603a68653b4e53 100644 (file)
@@ -7,6 +7,7 @@ package fmt
 import (
        "bytes"
        "strconv"
+       "utf8"
 )
 
 const (
@@ -127,7 +128,7 @@ func (f *fmt) padString(s string) {
        var padding []byte
        var left, right int
        if f.widPresent && f.wid != 0 {
-               padding, left, right = f.computePadding(len(s))
+               padding, left, right = f.computePadding(utf8.RuneCountInString(s))
        }
        if left > 0 {
                f.writePadding(left, padding)
index ecb8adbc37a80cd70fbb9a73b93e79571b2fce14..37405424bb1ac61adbfde5ec6bed7cc87e822149 100644 (file)
@@ -43,7 +43,9 @@
        For numeric values, the width and precision flags control
        formatting; width sets the width of the field, precision the
        number of places after the decimal, if appropriate.  The
-       format %6.2f prints 123.45.
+       format %6.2f prints 123.45. The width of a field is the number
+       of Unicode code points in the string. This differs from C's printf where
+       the field width is the number of bytes.
 
        Other flags:
                +       always print a sign for numeric values