aboutsummaryrefslogtreecommitdiff
path: root/src/fmt
AgeCommit message (Collapse)Author
2024-05-23all: change from sort functions to slices functions where feasibleIan Lance Taylor
Doing this because the slices functions are slightly faster and slightly easier to use. It also removes one dependency layer. This CL does not change packages that are used during bootstrap, as the bootstrap compiler does not have the required slices functions. It does not change the go/scanner package because the ErrorList Len, Swap, and Less methods are part of the Go 1 API. Change-Id: If52899be791c829198e11d2408727720b91ebe8a Reviewed-on: https://go-review.googlesource.com/c/go/+/587655 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com> Reviewed-by: Damien Neil <dneil@google.com>
2024-05-13fmt, internal/fmtsort: refactor SortedMap to use slice of structs for map ↵aimuz
sorting This change refactors the SortedMap type in the fmtsort package from using two parallel slices for keys and values to a single slice of structs. This improves code clarity and reduces the complexity of handling map entries. Affected files and their respective functions have been updated to work with the new structure, including adjustments in fmt/print.go and text/template/exec.go to iterate over the new map representation. goos: darwin goarch: arm64 pkg: fmt cpu: Apple M2 Max │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ SprintfPadding-12 21.29n ± 5% 20.89n ± 8% ~ (p=0.393 n=10) SprintfEmpty-12 2.986n ± 4% 2.997n ± 10% ~ (p=0.697 n=10) SprintfString-12 8.327n ± 9% 8.493n ± 12% ~ (p=0.579 n=10) SprintfTruncateString-12 15.93n ± 10% 15.56n ± 10% ~ (p=0.853 n=10) SprintfTruncateBytes-12 14.56n ± 12% 14.13n ± 11% ~ (p=0.796 n=10) SprintfSlowParsingPath-12 9.026n ± 15% 9.511n ± 14% ~ (p=0.646 n=10) SprintfQuoteString-12 40.88n ± 3% 40.73n ± 1% ~ (p=0.782 n=10) SprintfInt-12 6.279n ± 7% 6.130n ± 6% ~ (p=0.218 n=10) SprintfIntInt-12 11.08n ± 10% 11.37n ± 10% ~ (p=0.424 n=10) SprintfPrefixedInt-12 31.24n ± 3% 31.21n ± 2% ~ (p=0.912 n=10) SprintfFloat-12 13.96n ± 7% 13.99n ± 15% ~ (p=0.986 n=10) SprintfComplex-12 49.16n ± 7% 50.57n ± 6% ~ (p=0.436 n=10) SprintfBoolean-12 7.578n ± 15% 7.267n ± 11% ~ (p=0.529 n=10) SprintfHexString-12 36.14n ± 2% 35.74n ± 1% ~ (p=0.118 n=10) SprintfHexBytes-12 48.74n ± 1% 48.34n ± 4% ~ (p=0.128 n=10) SprintfBytes-12 60.16n ± 3% 61.36n ± 5% ~ (p=0.218 n=10) SprintfStringer-12 39.02n ± 10% 39.31n ± 9% ~ (p=0.739 n=10) SprintfStructure-12 161.2n ± 1% 133.9n ± 4% -16.90% (p=0.000 n=10) ManyArgs-12 31.87n ± 17% 33.00n ± 12% ~ (p=0.165 n=10) FprintInt-12 32.32n ± 0% 33.13n ± 1% +2.49% (p=0.000 n=10) FprintfBytes-12 47.31n ± 0% 47.99n ± 1% +1.44% (p=0.000 n=10) FprintIntNoAlloc-12 32.05n ± 1% 33.12n ± 0% +3.34% (p=0.000 n=10) ScanInts-12 130.5µ ± 1% 131.3µ ± 0% +0.57% (p=0.000 n=10) ScanRecursiveInt-12 40.83m ± 1% 40.65m ± 2% ~ (p=0.353 n=10) ScanRecursiveIntReaderWrapper-12 40.77m ± 2% 40.83m ± 2% ~ (p=0.971 n=10) geomean 100.6n 100.3n -0.32% │ old.txt │ new.txt │ │ B/op │ B/op vs base │ SprintfPadding-12 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfEmpty-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfString-12 5.000 ± 0% 5.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateString-12 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateBytes-12 16.00 ± 0% 16.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfSlowParsingPath-12 5.000 ± 0% 5.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfQuoteString-12 32.00 ± 0% 32.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfIntInt-12 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfPrefixedInt-12 64.00 ± 0% 64.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfFloat-12 8.000 ± 0% 8.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfComplex-12 24.00 ± 0% 24.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfBoolean-12 4.000 ± 0% 4.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexString-12 80.00 ± 0% 80.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexBytes-12 104.0 ± 0% 104.0 ± 0% ~ (p=1.000 n=10) ¹ SprintfBytes-12 88.00 ± 0% 88.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfStringer-12 32.00 ± 0% 32.00 ± 0% ~ (p=1.000 n=10) ¹ SprintfStructure-12 216.0 ± 0% 168.0 ± 0% -22.22% (p=0.000 n=10) ManyArgs-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintfBytes-12 24.00 ± 0% 24.00 ± 0% ~ (p=1.000 n=10) ¹ FprintIntNoAlloc-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ScanInts-12 14.87Ki ± 0% 14.87Ki ± 0% ~ (p=1.000 n=10) ¹ ScanRecursiveInt-12 16.37Ki ± 0% 16.34Ki ± 9% ~ (p=0.950 n=10) ScanRecursiveIntReaderWrapper-12 16.43Ki ± 8% 16.35Ki ± 0% ~ (p=0.052 n=10) geomean ² -1.03% ² ¹ all samples are equal ² summaries must be >0 to compute geomean │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ SprintfPadding-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfEmpty-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfTruncateBytes-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfSlowParsingPath-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfQuoteString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfIntInt-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfPrefixedInt-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfFloat-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfComplex-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfBoolean-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexString-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfHexBytes-12 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfBytes-12 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfStringer-12 4.000 ± 0% 4.000 ± 0% ~ (p=1.000 n=10) ¹ SprintfStructure-12 8.000 ± 0% 6.000 ± 0% -25.00% (p=0.000 n=10) ManyArgs-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintInt-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ FprintfBytes-12 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ FprintIntNoAlloc-12 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ScanInts-12 1.590k ± 0% 1.590k ± 0% ~ (p=1.000 n=10) ¹ ScanRecursiveInt-12 1.592k ± 0% 1.592k ± 0% ~ (p=0.303 n=10) ScanRecursiveIntReaderWrapper-12 1.594k ± 0% 1.594k ± 0% ~ (p=0.582 n=10) geomean ² -1.14% ² ¹ all samples are equal ² summaries must be >0 to compute geomean Change-Id: I2e850d827d2fd7d6618db60f7071977af5639032 GitHub-Last-Rev: 5a4afcf045331c6864902e848ededc1562d5fa53 GitHub-Pull-Request: golang/go#67256 Reviewed-on: https://go-review.googlesource.com/c/go/+/584155 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: qiu laidongfeng2 <2645477756@qq.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Auto-Submit: Ian Lance Taylor <iant@google.com>
2024-04-02fmt: add available godoc linkcui fliter
Change-Id: Ia7eaa654b44625983d09284d906a7b67ef589696 Reviewed-on: https://go-review.googlesource.com/c/go/+/535082 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-03-04fmt: allow padding and minus flags at the same timeMitar
Existing implementation did not allow setting both padding and minus flags at the same time because standard formatting does not allow that. But custom Formatter interface implementations might have use of it. This change moves the check from the place flags are parsed to where they are used in standard formatting. Fixes #61784 Change-Id: If5909d45dc929ddf911453e1056a4661abe76e52 GitHub-Last-Rev: d99ec55d3bbd9b2a8f14c8ade2fb25d6e0c174c3 GitHub-Pull-Request: golang/go#61836 Reviewed-on: https://go-review.googlesource.com/c/go/+/516975 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Martin Möhrmann <moehrmann@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Martin Möhrmann <martin@golang.org> Auto-Submit: Ian Lance Taylor <iant@golang.org>
2024-02-13fmt: clear width and precision when recovering formatting object from the poolRob Pike
Probably a day 1 oversight, and almost always inconsequential, but there is evidence of occasional trouble. There is no reason not to clear them. I tried and failed to write a test to catch this, but the change should be harmless and is all but certain to fix the problem. Fixes #61913 Change-Id: I0f7bbb4ab2780d8999d3ff7a35255dc07fb5c7e1 Reviewed-on: https://go-review.googlesource.com/c/go/+/556215 Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Than McIntosh <thanm@google.com>
2024-02-13fmt: document how %#g handles infinities and NaNsRob Pike
The %#g format prints a "Go-syntax representation", but there is no such thing for IEEE754 infinities and NaNs, so just document what happens, which is that it prints +Inf, -Inf, or NaN. We could show something like math.Inf(1) and math.Nan(), but that doesn't sit right, and anyway for NaNs you can't even recover the original value. Simpler and more honest to give up. Fixes #51486 Change-Id: I8d4e8186f5d7acc3e0e7b51d0b322142908ea0a2 Reviewed-on: https://go-review.googlesource.com/c/go/+/557235 Run-TryBot: Rob Pike <r@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2024-01-31fmt: update docs for %03sRuss Cox
%03s zero-pads a string with spaces; always has and now always will. Fixes #56486. Change-Id: Ia336581ae7db1c3456699e69e14a3071f50c9f2a Reviewed-on: https://go-review.googlesource.com/c/go/+/559197 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-01-31fmt: revert "don't pad strings with zeros"Russ Cox
This reverts CL 555776 (commit 704401ffa06). Scores of tests break inside Google, and there was a test for the old behavior, so clearly we thought it was correct at one point. An example of code that broke inside Google is: func (pn ProjectNumber) PaddedHexString() string { return fmt.Sprintf("%016s", strconv.FormatInt(int64(pn), 16)) } Here is another example: // IPv4toISO create ISO address base on a given IPv4 address. func IPv4toISO(v4 string) (string, error) { if net.ParseIP(v4).To4() == nil { return "", fmt.Errorf("invalid IPv4 address") } s := strings.Split(v4, ".") var ss string for _, n := range s { ss = ss + fmt.Sprintf("%03s", n) } if len(ss) != 12 { return "", fmt.Errorf("invalid IPv4 address") } return fmt.Sprint("49.0001." + ss[0:4] + "." + ss[4:8] + "." + ss[8:12] + ".00"), nil } This is doing the weird but apparently standard conversion from IPv4 to ISO ISIS Area 1 (see for example [1]). Here is an example from github.com/netbirdio/netbird: func generateNewToken() (string, string, error) { secret, err := b.Random(PATSecretLength) if err != nil { return "", "", err } checksum := crc32.ChecksumIEEE([]byte(secret)) encodedChecksum := base62.Encode(checksum) paddedChecksum := fmt.Sprintf("%06s", encodedChecksum) plainToken := PATPrefix + secret + paddedChecksum hashedToken := sha256.Sum256([]byte(plainToken)) encodedHashedToken := b64.StdEncoding.EncodeToString(hashedToken[:]) return encodedHashedToken, plainToken, nil } base62.Encode returns a string no leading zeros; the %06s adds leading zeros. Are there other ways to write these examples? Yes. Has all this code worked until now? Also yes. The change to this behavior observed that right padding doesn't add zeros, only left padding, but that makes sense: in numbers without decimal points, zeros on the left preserve the value while zeros on the right change it. Since we agree that this case is probably not important either way, preserve the long-time behavior of %0s. Will document it in a followup CL: this is a clean revert. Reopen #56486. [1] https://community.cisco.com/t5/routing/isis-net-address-configuration/m-p/1338984/highlight/true#M127827 Change-Id: Ie7dd35227f46933ccc9bfa1eac5fa8608f6d1918 Reviewed-on: https://go-review.googlesource.com/c/go/+/559196 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Rob Pike <r@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2024-01-23fmt: don't pad strings with zerosRob Pike
It's what the documentation says, and oddly it already behaves correctly for right padding, not left. (We never pad with zeros on the right.) Just don't do it. Fixes #56486 Change-Id: I2465edea93c69084e33bee0d945d5a1b85e6cd14 Reviewed-on: https://go-review.googlesource.com/c/go/+/555776 Reviewed-by: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Rob Pike <r@golang.org> Reviewed-by: Cherry Mui <cherryyz@google.com>
2023-09-25fmt: clarify that we don't call String for %#vIan Lance Taylor
Change-Id: I4edf8bd6f9ab813acf1d05c603f6f562fa00cb48 Reviewed-on: https://go-review.googlesource.com/c/go/+/529975 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2023-09-08fmt: adjust comment after CL 524940Daniel Martí
https://go.dev/cl/524940 swapped a call to Slice with Bytes, but the comment below still referenced Slice. Change-Id: Iedc772e1c49c4108bcd06f4cea0e637f011d053c Reviewed-on: https://go-review.googlesource.com/c/go/+/526356 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: t hepudds <thepudds1460@gmail.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-09-07fmt: avoid reflect.Value.Slice to help escape analysisthepudds
This is part of a series of CLs that aim to reduce how often interface arguments escape for the print functions in fmt. Prior to this change, one reason arguments escape is because printValue calls reflect.Value.Slice, which causes its value argument to escape (though at this CL, that is shrouded in the fmt escape analysis logs by other printValue escape reasons). This CL avoids that usage by calling f.Bytes instead, which is possible because we know f is a slice of bytes or an addressable array of bytes. Arguments still escape for other reasons. Change-Id: Ic3f064117a364007e1dd3197cef9d641abbf784a Reviewed-on: https://go-review.googlesource.com/c/go/+/524940 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: t hepudds <thepudds1460@gmail.com>
2023-09-07fmt: avoid reflect.Value.Pointer to help escape analysisthepudds
This is part of a series of CLs that aim to reduce how often interface arguments escape for the print functions in fmt. Prior to this change, one reason arguments escape is because fmtPointer calls reflect.Value.Pointer: ./print.go:551:39: parameter value leaks to <heap> for (*pp).fmtPointer with derefs=0: ./print.go:551:39: flow: <heap> ← value: ./print.go:551:39: from reflect.Value.Pointer(value) (call parameter) at ./print.go:555:20 printValue also has its value argument escape for this reason, among others. This CL changes those uses to reflect.Value.UnsafePointer instead, which does not cause an escape. Arguments still escape for other reasons. Change-Id: I81c4f737f11fe835c5ccb122caee40a39b553451 Reviewed-on: https://go-review.googlesource.com/c/go/+/524939 Reviewed-by: Ian Lance Taylor <iant@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: t hepudds <thepudds1460@gmail.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2023-08-17fmt: amend comment for getFieldJes Cok
Change-Id: I52c9ed0c1a178f3ae3eb4f135d8f11018075fe3b GitHub-Last-Rev: 407aa89c88fc7bb3e4ad9ef55c2a0c5c2f92642c GitHub-Pull-Request: golang/go#62061 Reviewed-on: https://go-review.googlesource.com/c/go/+/519935 Auto-Submit: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
2023-08-17fmt: fix receiver names are differentch3nnn
"buffer" call the receiver "b" in other method, don't call it "bp" in another. Keep the same receiver names, as prescribed in Go Code Review Comments (https://go.dev/s/style#receiver-names). Change-Id: I9fafc799a9e4102419ed743b941bca74e908f5c0 GitHub-Last-Rev: c8b851d372f3966e3c5eec7c331ad05aacb1ebda GitHub-Pull-Request: golang/go#62066 Reviewed-on: https://go-review.googlesource.com/c/go/+/520016 Auto-Submit: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
2023-05-24fmt: correct documentation for FormatterPaul Jolly
Before this CL, the documentation for Formatter suggested that implementers of Format(f State, verb rune) could use Fprint(f) or Sprint(f) to generate output. The Sprint(f) suggestion however is invalid. Fix that by simply suggesting Sprint() alongside Fprint(f). Fixes #60358 Change-Id: I024e996f6360b812968ef2cd5073cb4c223459e3 Reviewed-on: https://go-review.googlesource.com/c/go/+/497379 Reviewed-by: Bryan Mills <bcmills@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> Run-TryBot: Paul Jolly <paul@myitcv.org.uk> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2023-05-23fmt,math/big,net/url: fixes to old BenchmarksEgon Elbre
b.ResetTimer used to also stop the timer, however it does not anymore. These benchmarks hadn't been fixed and as a result ended up measuring some additional things. Also, make some for loops more conventional. Change-Id: I76ca68456d85eec51722a80587e5b2c9f5d836a1 Reviewed-on: https://go-review.googlesource.com/c/go/+/496996 Run-TryBot: Damien Neil <dneil@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Auto-Submit: Damien Neil <dneil@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Damien Neil <dneil@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
2023-05-08all: make safe for new vet analyzerRuss Cox
The unused analyzer handles dot imports now, so a few tests have picked up vet errors. This CL errors like: context/x_test.go:524:47: result of context.WithValue call not used Change-Id: I711a62fd7b50381f8ea45ac526bf0c946a171047 Reviewed-on: https://go-review.googlesource.com/c/go/+/493598 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com>
2023-02-16src: rename unexported errors by adding prefix errOleksandr Redko
By convention, use `err` as prefix for variables of type `error`. Change-Id: I9401d5d47e994a27be245b2c8b1edd55cdd52db1 Reviewed-on: https://go-review.googlesource.com/c/go/+/467536 Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com>
2023-01-30all: fix problematic commentscui fliter
Change-Id: If092ae7c72b66f172ae32fa6c7294a7ac250362e Reviewed-on: https://go-review.googlesource.com/c/go/+/463995 Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Than McIntosh <thanm@google.com> Run-TryBot: Than McIntosh <thanm@google.com>
2022-11-23fmt: add uintptr test casekijimaD
uintptr case was not covered, so add test. Change-Id: I894e06cb7db250d5dc1f14293c0d5834bfb00b9a GitHub-Last-Rev: 92f8301cb03b503500def0ae076fbf4de8ec448c GitHub-Pull-Request: golang/go#56912 Reviewed-on: https://go-review.googlesource.com/c/go/+/452955 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Martin Möhrmann <moehrmann@google.com> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Rob Pike <r@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-09-29errors, fmt: add support for wrapping multiple errorsDamien Neil
An error which implements an "Unwrap() []error" method wraps all the non-nil errors in the returned []error. We replace the concept of the "error chain" inspected by errors.Is and errors.As with the "error tree". Is and As perform a pre-order, depth-first traversal of an error's tree. As returns the first matching result, if any. The new errors.Join function returns an error wrapping a list of errors. The fmt.Errorf function now supports multiple instances of the %w verb. For #53435. Change-Id: Ib7402e70b68e28af8f201d2b66bd8e87ccfb5283 Reviewed-on: https://go-review.googlesource.com/c/go/+/432898 Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Damien Neil <dneil@google.com> Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
2022-09-21fmt: rely on utf8.AppendRuneJoe Tsai
This is both simpler and more performant. The need for fmt.fmtC to manually check for utf8.MaxRune is subtle to avoid overflow when converting uint64 to rune, so a test case was added to exercise this edge case. Change-Id: I0f2e6cce91dcd4cc6b88190c29807ca1c58e999d Reviewed-on: https://go-review.googlesource.com/c/go/+/412335 Auto-Submit: Joseph Tsai <joetsai@digital-static.net> Auto-Submit: Robert Griesemer <gri@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> Reviewed-by: Cherry Mui <cherryyz@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> Run-TryBot: Robert Griesemer <gri@google.com>
2022-09-03fmt: recycle printers with large buffersCarlo Alberto Ferraris
Previously when a printer had a large buffer we dropped both the buffer and the printer. There is no need to drop the printer in this case, as a printer with a nil buffer is valid. So we just drop the buffer and recycle the printer anyway. This saves one allocation in case the buffer is over the limit. Also tighten some of the tests for other unrelated cases. Change-Id: Iba1b6a71ca4691464b8c68ab0b6ab0d4d5d6168c Reviewed-on: https://go-review.googlesource.com/c/go/+/427395 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Rob Pike <r@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-08-06fmt: add a function to recover the original format string given a StateRob Pike
Sometimes when implementing a Formatter it's helpful to use the fmt package without invoking the formatter. This new function, FormatString, makes that easier in some cases by recreating the original formatting directive (such as "%3.2f") that caused Formatter.Format to be called. The original Formatter interface is probably not what we would design today, but we're stuck with it. FormatString, although it takes a State as an argument, compensates by making Formatter a little more flexible. The State does not include the verb so (unlike in the issue), we must provide it explicitly in the call to FormatString. Doing it there minimizes allocations by returning the complete format string. Fixes #51668 Updates #51195 Change-Id: Ie31c8256515864b2f460df45fbd231286b8b7a28 Reviewed-on: https://go-review.googlesource.com/c/go/+/400875 Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Russ Cox <rsc@golang.org>
2022-05-17fmt: add Append, Appendln, AppendfRob Pike
These are straightforward variants of the existing Sprintf etc., but append the resulting bytes to a provided buffer rather than returning a string. Internally, there is potentially some allocation because the package uses a pool of buffers to build its output. We make no attempt to override that, so the result is first printed into the pool and then copied to the output. Since it is a managed pool, asymptotically there should be no extra allocation. Fixes #47579 RELNOTE=yes Change-Id: Icef797f9b6f0c84d03e7035d95c06cdb819e2649 Reviewed-on: https://go-review.googlesource.com/c/go/+/406177 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-04-11all: gofmt main repoRuss Cox
[This CL is part of a sequence implementing the proposal #51082. The design doc is at https://go.dev/s/godocfmt-design.] Run the updated gofmt, which reformats doc comments, on the main repository. Vendored files are excluded. For #51082. Change-Id: I7332f099b60f716295fb34719c98c04eb1a85407 Reviewed-on: https://go-review.googlesource.com/c/go/+/384268 Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2022-04-05all: replace `` and '' with “ (U+201C) and ” (U+201D) in doc commentsRuss Cox
go/doc in all its forms applies this replacement when rendering the comments. We are considering formatting doc comments, including doing this replacement as part of the formatting. Apply it to our source files ahead of time. For #51082. Change-Id: Ifcc1f5861abb57c5d14e7d8c2102dfb31b7a3a19 Reviewed-on: https://go-review.googlesource.com/c/go/+/384262 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2022-04-01all: fix various doc comment formatting nitsRuss Cox
A run of lines that are indented with any number of spaces or tabs format as a <pre> block. This commit fixes various doc comments that format badly according to that (standard) rule. For example, consider: // - List item. // Second line. // - Another item. Because the - lines are unindented, this is actually two paragraphs separated by a one-line <pre> block. This CL rewrites it to: // - List item. // Second line. // - Another item. Today, that will format as a single <pre> block. In a future release, we hope to format it as a bulleted list. Various other minor fixes as well, all in preparation for reformatting. For #51082. Change-Id: I95cf06040d4186830e571cd50148be3bf8daf189 Reviewed-on: https://go-review.googlesource.com/c/go/+/384257 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-03-16fmt, strconv: document use of Unicode replacement character in %qRuss Cox
Fixes #51526. Change-Id: I365a763454bd201f804df29f800416b1731b8ebc Reviewed-on: https://go-review.googlesource.com/c/go/+/390436 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Rob Pike <r@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2022-03-15all: untab /* */ doc commentsRuss Cox
A long time ago, gofmt insisted on inserting tabs in /* */ comments at the top level of the file, like this: /* Package doc comment. */ package p Gofmt still insists on the tab for comments not at top level, but it has relaxed the rules about top-level comments. A few very old doc comments are indented, left over from the old rule. We are considering formatting doc comments, and so to make everything consistent, standardize on unindented doc comments by removing tabs in the few doc comments that are still indented this way. Also update some cmd/gofmt testdata to match. Change-Id: I293742e39b52f8a48ec41f72ca4acdafa7ce43bc Reviewed-on: https://go-review.googlesource.com/c/go/+/384261 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2022-03-08fmt: use tabs for indentationDmitri Shuralyov
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>
2022-03-05fmt: clarify right-padded strings use spacesAdam Shannon
Fixes #51419 Change-Id: I0a32f41a6e6e01481ad58c7dddb57ec7085d77af Reviewed-on: https://go-review.googlesource.com/c/go/+/389434 Reviewed-by: Rob Pike <r@golang.org> Trust: Ian Lance Taylor <iant@golang.org>
2021-12-13all: gofmt -w -r 'interface{} -> any' srcRuss Cox
And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
2021-10-26all: use reflect.{Pointer,PointerTo}Cuong Manh Le
Updates #47651 Updates #48665 Change-Id: I69a87b45a5cad7a07fbd855040cd9935cf874554 Reviewed-on: https://go-review.googlesource.com/c/go/+/358454 Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2021-06-07fmt: split package documentation into more sectionsBranden J Brown
The package-level documentation on fmt previously had only two formal sections, for printing and scanning. Because of this, the section on printing was very long, including some pseudo-sections describing particular features. This feature makes those pseudo-sections into proper sections, both to improve readability and so that those sections have hyperlinks on documentation sites. Fixes #46522 Change-Id: I38b7bc3447610faca446051da235edcbbd063f61 Reviewed-on: https://go-review.googlesource.com/c/go/+/324349 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Trust: Tobias Klauser <tobias.klauser@gmail.com>
2021-03-13fmt: use “truncateString” not “truncate” in method doczfCode
Change-Id: If1acb6a8533a782f80c7d1f0ad5155e98e1134dd GitHub-Last-Rev: 03384a3d99dd89d802635f7ef48ce4456ec338b0 GitHub-Pull-Request: golang/go#44375 Reviewed-on: https://go-review.googlesource.com/c/go/+/293629 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Rob Pike <r@golang.org> Trust: Rob Pike <r@golang.org>
2020-10-13fmt: explain how Formatter interface affects verbs and flagsBryan Boreham
Formatter is mentioned further down, but it's helpful to add it amongst the verbs and flags. Background: I spent a while puzzling how "%+v" prints a stack trace for github.com/pkg/errors when this isn't documented under 'flags'. Change-Id: Ic70145902a36780147dedca568b3cf482974fc38 GitHub-Last-Rev: 6571b499f211a2266812af66dd3b88dff602cabf GitHub-Pull-Request: golang/go#39860 Reviewed-on: https://go-review.googlesource.com/c/go/+/240000 Reviewed-by: Rob Pike <r@golang.org> Trust: Rob Pike <r@golang.org> Trust: Ian Lance Taylor <iant@golang.org>
2020-08-17fmt: avoid badverb formatting for %q when used with integersMartin Möhrmann
Instead of returning a bad verb error format for runes above utf8.Maxrune return a quoted utf8.RuneError rune (\ufffd). This makes the behaviour consistent with the "c" verb and aligns behaviour to not return bad verb error format when a verb is applied to the correct argument type. Fixes #14569 Change-Id: I679485f6bb90ebe408423ab68af16cce38816cd0 Reviewed-on: https://go-review.googlesource.com/c/go/+/248759 Run-TryBot: Martin Möhrmann <moehrmann@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2020-02-26fmt: do not remove trailing zeros for %g and %G with #(sharp) flagyah01
Fixes #36562 Change-Id: Id98ae9f7362cfb825b306c36649d505692d6d60e GitHub-Last-Rev: 405d51b12eb04da8cc3559c92f1546e69a8c1a19 GitHub-Pull-Request: golang/go#36588 Reviewed-on: https://go-review.googlesource.com/c/go/+/215001 Reviewed-by: Rob Pike <r@golang.org>
2020-02-26all: avoid string(i) where i has type intIan Lance Taylor
Instead use string(r) where r has type rune. This is in preparation for a vet warning for string(i). Updates #32479 Change-Id: Ic205269bba1bd41723950219ecfb67ce17a7aa79 Reviewed-on: https://go-review.googlesource.com/c/go/+/220844 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Akhil Indurti <aindurti@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Toshihiro Shiino <shiino.toshihiro@gmail.com>
2019-10-25fmt: fix handling of %% verb in ScanfRob Pike
There were a couple of bugs, including not requiring a percent and returning the wrong error for a bad format containing %%. Both are addressed by fixing the first. Fixes #34180. Change-Id: If96c0c0258bcb95eec49871437d719cb9d399d9b Reviewed-on: https://go-review.googlesource.com/c/go/+/202879 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2019-09-27cmd: update x/tools version to enforce only one %wHasit Bhatt
As mentioned in https://golang.org/issue/34062#issuecomment-529692313 src/cmd refers to older version of golang.org/x/tools. Hence, not checking if multiple errors are used in the same fmt.Errorf. Updating golang.org/x/tools version to latest in src/cmd. Fixes #34062 Change-Id: I358dec2c3d3af2b19add766b8488b919109b81d6 Reviewed-on: https://go-review.googlesource.com/c/go/+/196843 Run-TryBot: Michael Matloob <matloob@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
2019-08-23doc/go1.13: document fmt's number syntax updatesEmmanuel T Odeke
Fixes #32815 Change-Id: Ia8ac9943a920a056ba7dbc69c1c70fa188f7aca8 Reviewed-on: https://go-review.googlesource.com/c/go/+/191578 Reviewed-by: Robert Griesemer <gri@golang.org>
2019-06-27fmt: fix typo in errors.goBaokun Lee
Fixes #32802 Change-Id: I756ca49285130b45777bd29de440db296d9632e9 Reviewed-on: https://go-review.googlesource.com/c/go/+/184057 Run-TryBot: Baokun Lee <nototon@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
2019-05-23fmt: always clear wrapErrsMartin Möhrmann
Like panicking and erroring - wrapErrs should always be reset to the default false. wrapErrs should only be true when set by Errorf. Change-Id: I4d51cc2f0905109e232b0983dc5331bd34f138bc Reviewed-on: https://go-review.googlesource.com/c/go/+/178517 Run-TryBot: Martin Möhrmann <moehrmann@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
2019-05-15fmt: support %wDamien Neil
When fmt.Errorf is provided with a %w verb with an error operand, return an error implementing an Unwrap method returning that operand. It is invalid to use %w with other formatting functions, to use %w multiple times in a format string, or to use %w with a non-error operand. When the Errorf format string contains an invalid use of %w, the returned error does not implement Unwrap. Change-Id: I534e20d3b163ab22c2b137b1c9095906dc243221 Reviewed-on: https://go-review.googlesource.com/c/go/+/176998 Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
2019-05-15errors, fmt: revert rejected changes for Go 1.13Damien Neil
Reverts the following changes: https://go.googlesource.com/go/+/1f90d081391d4f5911960fd28d81d7ea5e554a8f https://go.googlesource.com/go/+/8bf18b56a47a98b9dd2fa03beb358312237a8c76 https://go.googlesource.com/go/+/5402854c3557f87fa2741a52ffc15dfb1ef333cc https://go.googlesource.com/go/+/37f84817247d3b8e687a701ccb0d6bc7ffe3cb78 https://go.googlesource.com/go/+/6be6f114e0d483a233101a67c9644cd72bd3ae7a Partially reverts the followinng change, removing the errors.Opaque function and the errors.Wrapper type definition: https://go.googlesource.com/go/+/62f5e8156ef56fa61e6af56f4ccc633bde1a9120 Updates documentation referencing the Wrapper type. Change-Id: Ia622883e39cafb06809853e3fd90b21441124534 Reviewed-on: https://go-review.googlesource.com/c/go/+/176997 Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
2019-05-09fmt: rename buffer.WriteByte to writeByteRuss Cox
Renaming the method makes clear, both to readers and to vet, that this method is not the implementation of io.ByteWriter. Working toward making the tree vet-safe instead of having so many exceptions in cmd/vet/all/whitelist. For #31916. Change-Id: I79da062ca6469b62a6b9e284c6cf2413c7425249 Reviewed-on: https://go-review.googlesource.com/c/go/+/176109 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Austin Clements <austin@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-03-25fmt: fix spelling mistake in exampleRob Pike
Mea culpa. Beat Takeshi, sumimasen. Fixes #31023. Change-Id: Ie2f27a5867724a8a1b8c3082c3389c8fd6d1dee7 Reviewed-on: https://go-review.googlesource.com/c/go/+/168861 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>