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
Labels
FrozenDueToAge v2 A language change or incompatible library change
Milestone

Comments

@martisch
Copy link
Contributor

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

CC @robpike

@martisch
Copy link
Contributor Author

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

@gopherbot
Copy link

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

@gopherbot
Copy link

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

@martisch
Copy link
Contributor Author

Will not change since it would break existing code.

@martisch martisch added the v2 A language change or incompatible library change 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.
Labels
FrozenDueToAge v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

3 participants