golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.21k stars 17.7k forks source link

x/text/message: catalog.Var between two substitutions #70438

Open AlphaLxy opened 3 days ago

AlphaLxy commented 3 days ago

Go version

go version go1.21.4 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN='/Users/xxx/go/bin'
GOCACHE='/Users/xxx/Library/Caches/go-build'
GOENV='/Users/xxx/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/xxx/go/pkg/mod'
GOOS='darwin'
GOPATH='/Users/xxx/go'
GOROOT='/opt/homebrew/opt/go/libexec'
GOSUMDB='sum.golang.google.cn'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/opt/go/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.4'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/b1/0fd1b6hs7lz0fm_mh346lybm0000gn/T/go-build2121787907=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

golang.org/x/text=v0.20.0

I use catalog.Var between two %d substitutions.

lang := language.English
v := catalog.Var("var", catalog.String("value"))
message.Set(lang, "key1", v, catalog.String("%d %d\n"))              // two %d
message.Set(lang, "key2", v, catalog.String("%d ${var} %d\n"))       // var between two %d
message.Set(lang, "key3", v, catalog.String("%d %d ${var}\n"))       // var after two %d
message.Set(lang, "key4", v, catalog.String("%[1]d ${var} %[2]d\n")) // var between two %[?]d

printer := message.NewPrinter(lang)
printer.Printf("key1", 1, 2) // 1 2
printer.Printf("key2", 1, 2) // 1 %!(EXTRA int=2)value%!(EXTRA int=2) 2
printer.Printf("key3", 1, 2) // 1 2 value
printer.Printf("key4", 1, 2) // 1 value 2

What did you see happen?

1 2
1 %!(EXTRA int=2)value%!(EXTRA int=2) 2
1 2 value
1 value 2

What did you expect to see?

1 2
1 value 2
1 2 value
1 value 2
gabyhelp commented 3 days ago

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

AlphaLxy commented 3 days ago

Seems that

func (p *printer) doPrintf(fmt string) {
    for p.fmt.Parser.SetFormat(fmt); p.fmt.Scan(); {
        switch p.fmt.Status {
        case format.StatusText:
            p.WriteString(p.fmt.Text())
        case format.StatusSubstitution:
            p.printArg(p.Arg(p.fmt.ArgNum), p.fmt.Verb)

do not set p.fmt.Reordered.

dmitshur commented 1 day ago

CC @mpvl.