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: inconsistent padding of integers with and without f.sharp #14616

Closed
martisch opened this issue Mar 3, 2016 · 5 comments
Closed

fmt: inconsistent padding of integers with and without f.sharp #14616

martisch opened this issue Mar 3, 2016 · 5 comments
Milestone

Comments

@martisch
Copy link
Contributor

@martisch martisch commented Mar 3, 2016

go1.4 go1.6 (see playground)

http://play.golang.org/p/C8nFpvFtCa

package main

import (
    "fmt"
)

func main() {
    fmt.Printf("|%#6x|\n",1)
    fmt.Printf("|%#06x|\n",1)
    fmt.Printf("|%06x|\n",1)
    fmt.Printf("|%6x|\n",1)
    fmt.Printf("|%#.6x|\n",1)
}

Output

|   0x1|
|0x000001|
|000001|
|     1|
|0x000001|

I would have expected the length of the output of %#6x and %#06x to be the same, namely 6 characters/runes as the width flag suggests.

A few places in the standard library use e.g. %#08x instead of %#.8x.
There are 37 places outside of fmt_test.go for grep "%#0.*x" of which 19 seem to be error message formatting.

The fmt_test.go tests that fail were added to check overflow of the internal buffer.
Interestingly besides these only the decode test that uses %#08x in https://github.com/golang/arch/blob/master/arm/armasm/gnu.go#L70 seems to fail if i change the behavior of the 0 padding to be the same as the space padding. ( only a small change in the integer function of fmt when f.sharp is set )

Is it to late to make them behave the same with padding as to much code may already rely on this? Then maybe the documentation should note this deviation from normal padding behavior instead.
( Maybe this is something for go vet. when %#0Lx is used it was maybe intended to be %#.Lx .)

@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Mar 3, 2016
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 3, 2016

@martisch
Copy link
Contributor Author

@martisch martisch commented Mar 10, 2016

comparison to c:
printf("%#06x", 0xffff)
prints 0xffff not 0x00ffff as go currently does.

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 10, 2016

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

@gopherbot
Copy link

@gopherbot gopherbot commented Apr 17, 2016

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

@martisch
Copy link
Contributor Author

@martisch martisch commented Apr 19, 2016

Will not change since it would break existing code.

@martisch martisch closed this Apr 19, 2016
@martisch martisch added the Go2 label Mar 28, 2017
@golang golang locked and limited conversation to collaborators Mar 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.