Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: width is measured in bytes not Unicode codepoints when using %c and %q for runes #8275

Closed
gopherbot opened this issue Jun 23, 2014 · 5 comments
Milestone

Comments

@gopherbot
Copy link

by MihaiBorobocea:

What does 'go version' print?
go1.3

What steps reproduce the problem?
package main

import "fmt"

func main() {
    fmt.Printf("(%5q)\n", "§") // (  "§")
    fmt.Printf("(%5q)\n", '§')   // ( '§')

    fmt.Printf("(%3s)\n", "§") // (  §)
    fmt.Printf("(%3c)\n", '§')   // ( §)
}

// Or run this same program at http://play.golang.org/p/UHE8wQK50u

What happened?
When printing a rune with %q or %c and specifying a width (for padding with spaces) the
width is counted in bytes. The program prints the rune U+00A7 which uses 2 bytes in
UTF-8. The lines with rune args produce a string shorter than the corresponding lines
with string args.

What should have happened instead?
The width should have been measured in runes, like it happens when printing a string.
Package fmt's doc says “Width and precision are measured in units of Unicode code
points”.

Am I misunderstanding how this is supposed to work? This seems too basic to have gone
unnoticed.
I've tried to find this in the code, and I'll attach an RFC diff in case my progress
saves you some time.
@ianlancetaylor
Copy link
Contributor

Comment 1:

Labels changed: added repo-main, release-go1.4.

@gopherbot
Copy link
Author

Comment 2 by MihaiBorobocea:

I've submitted my progress at https://golang.org/cl/104320043

@robpike
Copy link
Contributor

robpike commented Jul 29, 2014

Comment 3:

Status changed to Accepted.

@gopherbot
Copy link
Author

Comment 4:

CL https://golang.org/cl/104320043 mentions this issue.

@robpike
Copy link
Contributor

robpike commented Jul 29, 2014

Comment 5:

This issue was closed by revision bfdeb57.

Status changed to Fixed.

@rsc rsc added this to the Go1.4 milestone Apr 14, 2015
@rsc rsc removed the release-go1.4 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jun 25, 2018
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 golang#8275.

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/104320043
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jul 9, 2018
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 golang#8275.

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/104320043
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants