]> Cypherpunks repositories - gostls13.git/commit
cmd/gofmt, go/format, go/printer: move number normalization to printer
authorDmitri Shuralyov <dmitshur@golang.org>
Fri, 28 Feb 2020 01:20:39 +0000 (20:20 -0500)
committerDmitri Shuralyov <dmitshur@golang.org>
Fri, 1 May 2020 21:54:06 +0000 (21:54 +0000)
commit5c8715f70adf13411668b8de76e5fef78c8e3f32
tree04597999fb564dc054f9e77864d60f9109fa5e01
parentcb00d9343156859401fadac1ceadab3398c3a1c8
cmd/gofmt, go/format, go/printer: move number normalization to printer

Normalization of number prefixes and exponents was added in CL 160184
directly in cmd/gofmt. The same behavior change needs to be applied in
the go/format package. This is done by moving the normalization code
into go/printer, behind a new StdFormat mode, which is then re-used
by both cmd/gofmt and go/format.

Note that formatting of Go source code changes over time, so the exact
byte output produced by go/printer may change between versions of Go
when using StdFormat mode. What is guaranteed is that the new formatting
is equivalent Go code.

Clients looking to format Go code with standard formatting consistent
with cmd/gofmt and go/format would need to start using this flag, but
a better alternative is to use the go/format package instead.

Benchstat numbers on go test go/printer -bench=BenchmarkPrint:

name     old time/op    new time/op    delta
Print-8    4.56ms ± 1%    4.57ms ± 0%   ~     (p=0.700 n=3+3)

name     old alloc/op   new alloc/op   delta
Print-8     467kB ± 0%     467kB ± 0%   ~     (p=1.000 n=3+3)

name     old allocs/op  new allocs/op  delta
Print-8     17.2k ± 0%     17.2k ± 0%   ~     (all equal)

That benchmark data doesn't contain any numbers that need to be
normalized. More work needs to be performed when formatting Go code
with numbers, but it is unavoidable to produce standard formatting.

Fixes #37476.
For #37453.

Change-Id: If50bde4035c3ee6e6ff0ece5691f6d3566ffe8d5
Reviewed-on: https://go-review.googlesource.com/c/go/+/231461
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/gofmt/gofmt.go
src/go/format/format.go
src/go/format/format_test.go
src/go/printer/nodes.go
src/go/printer/performance_test.go
src/go/printer/printer.go
src/go/printer/printer_test.go
src/go/printer/testdata/go2numbers.golden [new file with mode: 0644]
src/go/printer/testdata/go2numbers.input [new file with mode: 0644]
src/go/printer/testdata/go2numbers.stdfmt [new file with mode: 0644]