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: padding flag is not accepted with minus flag #51419

Closed
adamdecaf opened this issue Mar 1, 2022 · 8 comments
Closed

fmt: padding flag is not accepted with minus flag #51419

adamdecaf opened this issue Mar 1, 2022 · 8 comments
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@adamdecaf
Copy link
Contributor

What version of Go are you using (go version)?

go version go1.17.6 darwin/amd64

Does this issue reproduce with the latest release?

Yes, verified in the go.dev sandbox.

What operating system and processor architecture are you using (go env)?

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/adam/Library/Caches/go-build"
GOENV="/Users/adam/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/adam/code/pkg/mod"
GONOPROXY="github.com/moov-io/*,github.com/moovfinancial/*"
GONOSUMDB="github.com/moov-io/*,github.com/moovfinancial/*"
GOOS="darwin"
GOPATH="/Users/adam/code/"
GOPRIVATE="github.com/moov-io/*,github.com/moovfinancial/*"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.17.6/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.17.6/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/adam/code/src/github.com/moovfinancial/disco-settlement/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5c/9jlzxrmd1_7_fw3d05nb62dc0000gn/T/go-build2908583461=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

It seems that the minus and padding character flags don't work when both are specified. I'm seeing the following format no pad with "0" as specified in the flag.

Example: https://go.dev/play/p/IDEqulgJAwD

What did you expect to see?

buf.WriteString("123456789")

// Expect '1234567890000'
fmt.Println(fmt.Sprintf("'%-013.13s'", buf.String()))

What did you see instead?

'123456789    '
@adamdecaf
Copy link
Contributor Author

adamdecaf commented Mar 1, 2022

Context, we have a 13 character fixed width field whose last two characters need to be "00". We can easily work around this, but it was unclear if this behaviour should be expected or not.

@robpike
Copy link
Contributor

robpike commented Mar 2, 2022

Padding a string with zeros doesn't seem right, but the documentation doesn't say it shouldn't work.

Just for reference, as the fmt package was written to broadly follow C's stdio, I tried the equivalent C program, and got these warnings from clang:

x.c:6:11: warning: flag '0' results in undefined behavior with 's' conversion specifier [-Wformat]
printf("%013.13s\n", s);
~^~~~~~~
x.c:8:12: warning: flag '0' results in undefined behavior with 's' conversion specifier [-Wformat]
printf("%-013.13s\n", s);
~~^~~~~~~
x.c:8:12: warning: flag '0' is ignored when flag '-' is present [-Wformat]
printf("%-013.13s\n", s);
~~^~~~~~~
x.c:9:11: warning: flag '0' results in undefined behavior with 's' conversion specifier [-Wformat]
printf("%0-13.13s\n", s);
~^~~~~~~~
x.c:9:11: warning: flag '0' is ignored when flag '-' is present [-Wformat]
printf("%0-13.13s\n", s);

Meanwhile its output is:

123456789
0000123456789
1234567890000
1234567890000

which implies the warnings are largely irrelevant. Yay warnings.

So although you're on thin ice here, I'd be willing to accept this as a bug, as long as fixing it doesn't break too many things, which seems plausible.

@adamdecaf
Copy link
Contributor Author

I didn't see this existing test which is explicit about rejecting using zeros as right-padding.

https://github.com/golang/go/blob/go1.17.7/src/fmt/fmt_test.go#L990-L992

It looks like this was changed as part of a performance improvement, but I'm unsure why it was changed back then.

abcad1e

@robpike
Copy link
Contributor

robpike commented Mar 2, 2022

It is very odd to right-pad a string with zeros. It's easy to argue it's wrong to expect the library to do it for you.

So I'm back on the 'not a bug' side of the fence, perhaps with a line in the documentation about it.

@adamdecaf
Copy link
Contributor Author

Agreed. There's been a test rejecting this behavior since 2016, so I'm going to file a change updating the docs. Changing this would break people's code.

@gopherbot
Copy link

Change https://go.dev/cl/389434 mentions this issue: fmt: clarify right-padded strings use spaces

@cagedmantis cagedmantis added this to the Backlog milestone Mar 5, 2022
@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 5, 2022
@cagedmantis
Copy link
Contributor

/cc @martisch

@dmitshur dmitshur modified the milestones: Backlog, Go1.19 Mar 7, 2022
@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. Documentation and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Mar 7, 2022
@gopherbot
Copy link

Change https://go.dev/cl/390674 mentions this issue: fmt: use tabs for indentation

gopherbot pushed a commit that referenced this issue Mar 8, 2022
Replace 24 spaces added in CL 389434 with 3 tabs,
so the new line is indented like other lines around it.

Updates #51419.

Change-Id: Ic3e50023a01f233c52dda53c36de2c461222d95c
Reviewed-on: https://go-review.googlesource.com/c/go/+/390674
Trust: Dmitri Shuralyov <dmitshur@golang.org>
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Adam Shannon <adamkshannon@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@golang golang locked and limited conversation to collaborators Mar 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants