aboutsummaryrefslogtreecommitdiff
path: root/src/strconv
AgeCommit message (Collapse)Author
2021-10-06all: use bytes.Cut, strings.CutRuss Cox
Many uses of Index/IndexByte/IndexRune/Split/SplitN can be written more clearly using the new Cut functions. Do that. Also rewrite to other functions if that's clearer. For #46336. Change-Id: I68d024716ace41a57a8bf74455c62279bde0f448 Reviewed-on: https://go-review.googlesource.com/c/go/+/351711 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-08-21strconv: reject surrogate halves in UnquoteJoe Tsai
Unquote implements unescaping a "single-quoted, doubled-quoted, or backquoted Go string literal". Therefore, it should reject anything that the Go specification explicitly forbids. The section on "Rune literals" explicitly rejects rune values "above 0x10FFFF and surrogate halves". We properly checked for the previous condition, but were failing to check for the latter. In general, "r > utf8.MaxRune" is probably the wrong check, while !utf8.ValidRune(r) is the more correct check. We make changes to both UnquoteChar and appendEscapedRune to use the correct check. The change to appendEscapedRune is technically a noop since callers of that function already guarantee that the provided rune is valid. Fixes #47853 Change-Id: Ib8977e56b91943ec8ada821b8d217b5e9a66f950 Reviewed-on: https://go-review.googlesource.com/c/go/+/343877 Trust: Joe Tsai <joetsai@digital-static.net> Run-TryBot: Joe Tsai <joetsai@digital-static.net> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2021-08-17all: fix typosYasuhiro Matsumoto
Change-Id: I83180c472db8795803c1b9be3a33f35959e4dcc2 Reviewed-on: https://go-review.googlesource.com/c/go/+/336889 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Austin Clements <austin@google.com>
2021-06-09strconv: document parsing of leading +/-Damien Neil
Explicitly document the handling of a sign prefix, and the interaction between the sign and base prefixes. Fixes #46641. Change-Id: I3cd6773e3f074fe671a944a05a79d2408137fcd4 Reviewed-on: https://go-review.googlesource.com/c/go/+/325875 Trust: Damien Neil <dneil@google.com> Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2021-06-07strconv: ParseFloat: always return ErrSyntax for bad syntaxIan Lance Taylor
Previously we would sometimes return ErrRange if the parseable part of the floating point number was out of range. Fixes #46628 Change-Id: I15bbbb1e2a56fa27c19fe25ab5554d988cbfd9d2 Reviewed-on: https://go-review.googlesource.com/c/go/+/325750 Trust: Ian Lance Taylor <iant@golang.org> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2021-05-04strconv: fix a typoEndlessCheng
Change-Id: I17911dc95fb2126b41dc95f8839c4b3fa8f35d12 GitHub-Last-Rev: 88e120329301bd73db39e1081737a83f49a5672b GitHub-Pull-Request: golang/go#45933 Reviewed-on: https://go-review.googlesource.com/c/go/+/316689 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2021-05-04strconv: add QuotedPrefixJoe Tsai
QuotedPrefix is similar to Unquote, but returns the quoted string verbatim and ignores any data after the quoted string. Fixes #45033 Change-Id: I9f69fe9e3e45cbe9e63581cf1b457facb625045d Reviewed-on: https://go-review.googlesource.com/c/go/+/314775 Trust: Joe Tsai <joetsai@digital-static.net> Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
2021-04-27strconv: remove unused extfloat (Grisu3) ftoa codeRémy Oudompheng
Change-Id: Ie8995362f9e8072b173d08820330d2d1e512aa71 Reviewed-on: https://go-review.googlesource.com/c/go/+/314029 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
2021-04-15strconv: Implement Ryū algorithm for ftoa shortest modeRémy Oudompheng
This patch implements the algorithm from Ulf Adams, "Ryū: Fast Float-to-String Conversion" (doi:10.1145/3192366.3192369) for formatting floating-point numbers with a fixed number of decimal digits. It is not a direct translation of the reference C implementation but still follows the original paper. In particular, it uses full 128-bit powers of 10, which allows for more precision in the other modes (fixed ftoa, atof). name old time/op new time/op delta AppendFloat/Decimal-4 49.6ns ± 3% 59.3ns ± 0% +19.59% (p=0.008 n=5+5) AppendFloat/Float-4 122ns ± 1% 91ns ± 1% -25.92% (p=0.008 n=5+5) AppendFloat/Exp-4 89.3ns ± 1% 100.0ns ± 1% +11.98% (p=0.008 n=5+5) AppendFloat/NegExp-4 88.3ns ± 2% 97.1ns ± 1% +9.87% (p=0.008 n=5+5) AppendFloat/LongExp-4 143ns ± 2% 103ns ± 0% -28.17% (p=0.016 n=5+4) AppendFloat/Big-4 144ns ± 1% 110ns ± 1% -23.26% (p=0.008 n=5+5) AppendFloat/BinaryExp-4 46.2ns ± 2% 46.0ns ± 1% ~ (p=0.603 n=5+5) AppendFloat/32Integer-4 49.1ns ± 1% 58.7ns ± 1% +19.57% (p=0.008 n=5+5) AppendFloat/32ExactFraction-4 95.6ns ± 1% 88.6ns ± 1% -7.30% (p=0.008 n=5+5) AppendFloat/32Point-4 122ns ± 1% 87ns ± 1% -28.63% (p=0.008 n=5+5) AppendFloat/32Exp-4 88.6ns ± 2% 95.0ns ± 1% +7.29% (p=0.008 n=5+5) AppendFloat/32NegExp-4 87.2ns ± 1% 91.3ns ± 1% +4.63% (p=0.008 n=5+5) AppendFloat/32Shortest-4 107ns ± 1% 82ns ± 0% -24.08% (p=0.008 n=5+5) AppendFloat/Slowpath64-4 1.00µs ± 1% 0.10µs ± 0% -89.92% (p=0.016 n=5+4) AppendFloat/SlowpathDenormal64-4 34.1µs ± 3% 0.1µs ± 1% -99.72% (p=0.008 n=5+5) Fixes #15672 Change-Id: Ib90dfa245f62490a6666671896013cf3f9a1fb22 Reviewed-on: https://go-review.googlesource.com/c/go/+/170080 Trust: Emmanuel Odeke <emmanuel@orijtech.com> Trust: Nigel Tao <nigeltao@golang.org> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Nigel Tao <nigeltao@golang.org>
2021-04-15strconv: implement Ryū-like algorithm for fixed precision ftoaRémy Oudompheng
This patch implements a simplified version of Ulf Adams, "Ryū: Fast Float-to-String Conversion" (doi:10.1145/3192366.3192369) for formatting floating-point numbers with a fixed number of decimal digits. It uses the same principles but does not need to handle the complex task of finding a shortest representation. This allows to handle a few more cases than Grisu3, notably formatting with up to 18 significant digits. name old time/op new time/op delta AppendFloat/32Fixed8Hard-4 72.0ns ± 2% 56.0ns ± 2% -22.28% (p=0.000 n=10+10) AppendFloat/32Fixed9Hard-4 74.8ns ± 0% 64.2ns ± 2% -14.16% (p=0.000 n=8+10) AppendFloat/64Fixed1-4 60.4ns ± 1% 54.2ns ± 1% -10.31% (p=0.000 n=10+9) AppendFloat/64Fixed2-4 66.3ns ± 1% 53.3ns ± 1% -19.54% (p=0.000 n=10+9) AppendFloat/64Fixed3-4 61.0ns ± 1% 55.0ns ± 2% -9.80% (p=0.000 n=9+10) AppendFloat/64Fixed4-4 66.9ns ± 0% 52.0ns ± 2% -22.20% (p=0.000 n=8+10) AppendFloat/64Fixed12-4 95.5ns ± 1% 76.2ns ± 3% -20.19% (p=0.000 n=10+9) AppendFloat/64Fixed16-4 1.62µs ± 0% 0.07µs ± 2% -95.69% (p=0.000 n=10+10) AppendFloat/64Fixed12Hard-4 1.27µs ± 1% 0.07µs ± 1% -94.83% (p=0.000 n=9+9) AppendFloat/64Fixed17Hard-4 3.68µs ± 1% 0.08µs ± 2% -97.86% (p=0.000 n=10+9) AppendFloat/64Fixed18Hard-4 3.67µs ± 0% 3.72µs ± 1% +1.44% (p=0.000 n=9+10) Updates #15672 Change-Id: I160963e141dd48287ad8cf57bcc3c686277788e8 Reviewed-on: https://go-review.googlesource.com/c/go/+/170079 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Trust: Emmanuel Odeke <emmanuel@orijtech.com> Trust: Nigel Tao <nigeltao@golang.org> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org>
2021-02-23all: faster midpoint computation in binary searchyangwenmai
On my machine (3.1 GHz Quad-Core Intel Core i7, macOS 10.15.7 10.15.7), go 1.15.6 benchstat: name old time/op new time/op delta SearchInts-8 20.3ns ± 1% 16.6ns ± 6% -18.37% (p=0.000 n=9+10) Change-Id: I346e5955fd6df6ce10254b22267dbc8d5a2b16c0 Reviewed-on: https://go-review.googlesource.com/c/go/+/279439 Reviewed-by: Ben Shi <powerman1st@163.com> Reviewed-by: Robert Griesemer <gri@golang.org> Trust: Robert Griesemer <gri@golang.org>
2021-02-20all: go fmt std cmd (but revert vendor)Russ Cox
Make all our package sources use Go 1.17 gofmt format (adding //go:build lines). Part of //go:build change (#41184). See https://golang.org/design/draft-gobuild Change-Id: Ia0534360e4957e58cd9a18429c39d0e32a6addb4 Reviewed-on: https://go-review.googlesource.com/c/go/+/294430 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-12-14Merge branch 'master' into dev.regabiAlexander Rakoczy
Change-Id: I098acdbc5e2676aeb8700d935e796a9c29d04b88
2020-12-09all: update to use os.ReadFile, os.WriteFile, os.CreateTemp, os.MkdirTempRuss Cox
As part of #42026, these helpers from io/ioutil were moved to os. (ioutil.TempFile and TempDir became os.CreateTemp and MkdirTemp.) Update the Go tree to use the preferred names. As usual, code compiled with the Go 1.4 bootstrap toolchain and code vendored from other sources is excluded. ReadDir changes are in a separate CL, because they are not a simple search and replace. For #42026. Change-Id: If318df0216d57e95ea0c4093b89f65e5b0ababb3 Reviewed-on: https://go-review.googlesource.com/c/go/+/266365 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-11-24[dev.regabi] strconv: add to bootstrap packagesMatthew Dempsky
go/constant relies on strconv for parsing Go literals, while older versions of strconv either lack recent Go language features (e.g., Go 1.13's new numeric literals) or have errors (e.g., mishandling of carriage returns in raw string literals prior to Go 1.8). This requires two changes: 1. Splitting out the internal/bytealg dependency into a separate file, which can be easily substituted with a simple loop for bootstrap builds. 2. Updating eisel_lemire.go to not utilize Go 1.13 functionality (underscores in numeric literals and signed shift counts). Change-Id: Ib48a858a03b155eebdcd08d577aec2254337e70e Reviewed-on: https://go-review.googlesource.com/c/go/+/272749 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> Trust: Matthew Dempsky <mdempsky@google.com>
2020-11-03strconv: revert ParseFloat/ParseComplex error on incorrect bitSizeBen Hoyt
This is a partial revert of https://go-review.googlesource.com/c/go/+/248219 because we found that a non-trivial amount of code erroneously calls ParseFloat(s, 10) or even ParseFloat(s, 0) and expects it to work -- before that change was merged, ParseFloat accepted a bitSize of anything other than 32 or 64 to mean 64 (and ParseComplex was similar). So revert that behavior to avoid breaking people's code, and add tests for this. I may add a vet check to flag ParseFloat(s, not_32_or_64) in a later change. See #42297 for more details. Change-Id: I4bc0156bd74f67a39d5561b6e5fde3f2d20bd622 Reviewed-on: https://go-review.googlesource.com/c/go/+/267319 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-10-30strconv: fix incorrect bit size in ParseComplex; add testsBen Hoyt
In ParseComplex, the "size" passed to parseFloatPrefix should be 64 for complex128, not 128. It still works because of how parseFloatPrefix is forgiving about the size if it's not 32, but worth fixing anyway. Make ParseComplex and ParseFloat return a bit size error for anything other than 128 or 64 (for ParseComplex), or 64 or 32 (for ParseFloat). Add "InvalidBitSize" tests for these cases. Add tests for ParseComplex with bitSize==64: this is done in a similar way to how the ParseFloat 32-bit tests work, re-using the tests for the larger bit size. Add tests for FormatComplex -- there were none before. Fixes #40706 Change-Id: I16ddd546e5237207cc3b8c2181dd708eca42b04f Reviewed-on: https://go-review.googlesource.com/c/go/+/248219 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Trust: Minux Ma <minux@golang.org> Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2020-10-29strconv: make Eisel-Lemire handle long mantissasRémy Oudompheng
In many cases, it is not necessary to parse long decimal mantissas entirely to produce the correctly rounded floating-point number. It is enough to parse the short, rounded lower and upper bounds and in most cases they round to the same floating point number because uint64 can hold 19 digits. Previously this case was handled by the extFloat code path (Grisu3 algorithm). name old time/op new time/op delta Atof64Big-4 1.07µs ± 2% 0.11µs ± 2% -89.61% (p=0.000 n=10+9) Atof64RandomLongFloats-4 8.03µs ± 2% 0.14µs ± 7% -98.24% (p=0.000 n=10+10) Atof32RandomLong-4 760ns ± 1% 156ns ± 0% -79.46% (p=0.000 n=10+8) Benchmarks versus extFloat: name old time/op new time/op delta Atof64Big-4 121ns ± 3% 111ns ± 2% -7.93% (p=0.000 n=10+9) Atof64RandomLongFloats-4 144ns ± 1% 142ns ± 7% ~ (p=0.167 n=10+10) Atof32RandomLong-4 129ns ± 1% 156ns ± 0% +21.12% (p=0.000 n=10+8) Change-Id: Id734b8c11e74b49a444fda67ee72870ae9422e60 Reviewed-on: https://go-review.googlesource.com/c/go/+/264677 Trust: Emmanuel Odeke <emmanuel@orijtech.com> Trust: Russ Cox <rsc@golang.org> Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Nigel Tao <nigeltao@golang.org>
2020-10-29strconv: remove extfloat.go atof code pathNigel Tao
Prior to this commit, strconv.ParseFloat (known in C as atof) takes the first of four algorithms to succeed: atof64exact, eiselLemire64, extFloat, fallback. The Eisel-Lemire implementation is a recent addition but, now that it exists, the extFloat implementation (based on the algorithm used by https://github.com/google/double-conversion) is largely redundant. This Go program: func parseOneMillionFloats(bitSize int, normallyDistributed bool) { rng := rand.New(rand.NewSource(1)) for i := 0; i < 1_000_000; { x := 0.0 if normallyDistributed { x = rng.NormFloat64() } else if bitSize == 32 { x = float64(math.Float32frombits(rng.Uint32())) } else { x = math.Float64frombits( uint64(rng.Uint32())<<32 | uint64(rng.Uint32())) } if math.IsInf(x, 0) { continue } s := strconv.FormatFloat(x, 'g', -1, bitSize) strconv.ParseFloat(s, bitSize) i++ } } triggers the four algorithms by these percentages: bitSize=32, normallyDistributed=false 07.4274% atof32exact 91.2982% eiselLemire32 00.8673% extFloat 00.0269% fallback bitSize=32, normallyDistributed=true 27.6356% atof32exact 72.3641% eiselLemire32 00.0003% extFloat 00.0000% fallback bitSize=64, normallyDistributed=false 01.2076% atof64exact 98.6216% eiselLemire64 00.1081% extFloat 00.0130% fallback bitSize=64, normallyDistributed=true 24.8826% atof64exact 75.1174% eiselLemire64 00.0000% extFloat 00.0000% fallback This commit removes the extfloat.go atof code (but keeps the extfloat.go ftoa code for now), reducing the number of atof algorithms from 4 to 3. The benchmarks (below) show some regressions but these are arguably largely artificial situations. Atof*RandomBits generates uniformly distributed uint32/uint64 values and reinterprets the bits as float32/float64 values. The change in headline numbers (arithmetic means) are primarily due to relatively large changes for relatively rare cases. Atof64Big parses a hard-coded "123456789123456789123456789". name old time/op new time/op delta Atof64Decimal-4 47.1ns ± 1% 47.4ns ± 2% ~ (p=0.516 n=5+5) Atof64Float-4 56.4ns ± 1% 55.9ns ± 2% ~ (p=0.206 n=5+5) Atof64FloatExp-4 68.8ns ± 0% 68.7ns ± 1% ~ (p=0.516 n=5+5) Atof64Big-4 157ns ± 2% 1528ns ± 2% +875.99% (p=0.008 n=5+5) Atof64RandomBits-4 156ns ± 1% 186ns ± 1% +19.49% (p=0.008 n=5+5) Atof64RandomFloats-4 144ns ± 0% 143ns ± 1% ~ (p=0.365 n=5+5) Atof32Decimal-4 47.6ns ± 1% 47.5ns ± 2% ~ (p=0.714 n=5+5) Atof32Float-4 54.3ns ± 2% 54.1ns ± 1% ~ (p=0.532 n=5+5) Atof32FloatExp-4 75.2ns ± 1% 75.7ns ± 3% ~ (p=0.794 n=5+5) Atof32Random-4 108ns ± 1% 120ns ± 1% +10.54% (p=0.008 n=5+5) Fixes #36657 Change-Id: Id3c4e1700f969f885b580be54c8892b4fe042a79 Reviewed-on: https://go-review.googlesource.com/c/go/+/264518 Reviewed-by: Robert Griesemer <gri@golang.org> Trust: Robert Griesemer <gri@golang.org> Trust: Nigel Tao <nigeltao@golang.org>
2020-10-23strconv: add eiselLemire32Nigel Tao
This does for ParseFloat(etc, 32) what commit a2eb53c571 did for ParseFloat(etc, 64). name old time/op new time/op delta Atof32Decimal-4 48.3ns ± 4% 48.8ns ± 2% ~ (p=0.548 n=5+5) Atof32Float-4 56.2ns ± 5% 54.7ns ± 3% ~ (p=0.246 n=5+5) Atof32FloatExp-4 104ns ± 0% 76ns ± 2% -27.19% (p=0.008 n=5+5) Atof32Random-4 142ns ± 2% 109ns ± 1% -23.07% (p=0.008 n=5+5) Change-Id: I6ee5a2f2d791d4fe3028f1d40aca96400120fda0 Reviewed-on: https://go-review.googlesource.com/c/go/+/264517 Trust: Nigel Tao <nigeltao@golang.org> Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2020-10-22strconv: increase the Eisel-Lemire exp10 rangeNigel Tao
This grows the exp10 range for which the Eisel-Lemire algorithm applies from [-307, +288] to [-348, +347], roughly equivalent to the existing powersOfTen table in extfloat.go (which uses a different algorithm). name old time/op new time/op delta Atof64Decimal-4 48.4ns ± 1% 48.7ns ± 3% ~ (p=0.698 n=5+5) Atof64Float-4 57.9ns ± 1% 58.1ns ± 2% ~ (p=0.873 n=5+5) Atof64FloatExp-4 71.8ns ± 2% 72.2ns ± 2% ~ (p=0.730 n=5+5) Atof64Big-4 165ns ± 1% 164ns ± 1% ~ (p=0.635 n=5+5) Atof64RandomBits-4 165ns ± 1% 165ns ± 6% ~ (p=0.143 n=5+5) Atof64RandomFloats-4 147ns ± 2% 147ns ± 1% ~ (p=0.857 n=5+5) Change-Id: Idf7dc5297db6db2bd9e0bd4cb0e55e021916fa43 Reviewed-on: https://go-review.googlesource.com/c/go/+/264139 Reviewed-by: Robert Griesemer <gri@golang.org> Trust: Robert Griesemer <gri@golang.org> Trust: Nigel Tao <nigeltao@golang.org>
2020-10-22strconv: fix Eisel-Lemire for negative zeroNigel Tao
This is somewhat academic (and no tests failed before this commit), since func atof64 only calls func eiselLemire when func atof64exact fails, and func atof64exact doesn't fail when parsing positive or negative zeroes. But it's still worth fixing. Change-Id: Ibe6ef4c8fd96827673b711d5456003fbc447e39c Reviewed-on: https://go-review.googlesource.com/c/go/+/264140 Trust: Nigel Tao <nigeltao@golang.org> Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2020-10-17strconv: use the Eisel-Lemire ParseFloat algorithmNigel Tao
Also fix BenchmarkAtof64Random* to initialize the test data when none of the TestAtof* tests are run. Passing "go test -test.count=5 -test.run=xxx -test.bench=Atof64" on to benchstat: name old time/op new time/op delta Atof64Decimal-4 47.9ns ± 0% 48.3ns ± 1% ~ (p=0.238 n=4+5) Atof64Float-4 58.3ns ± 3% 57.7ns ± 0% ~ (p=0.151 n=5+5) Atof64FloatExp-4 107ns ± 0% 71ns ± 1% -33.89% (p=0.016 n=4+5) Atof64Big-4 163ns ± 0% 166ns ± 2% ~ (p=0.159 n=4+5) Atof64RandomBits-4 299ns ± 1% 166ns ± 1% -44.41% (p=0.008 n=5+5) Atof64RandomFloats-4 188ns ± 1% 144ns ± 0% -23.03% (p=0.008 n=5+5) The canada.json file from github.com/miloyip/nativejson-benchmark is full of geospatial coordinates (i.e. numbers). With this program: src, _ := ioutil.ReadFile("canada.json") for i := 0; i < 5; i++ { now := time.Now() for j := 0; j < 10; j++ { dst := interface{}(nil) if err := json.Unmarshal(src, &dst); err != nil { log.Fatal(err) } } fmt.Println(time.Since(now)) } Median of the 5 printed numbers, lower is better. Before: 760.819549ms After: 702.651646ms Ratio: 1.08x The new detailedPowersOfTen table weighs in at 596 * 16 = 9536 bytes, but some of that weight gain can be clawed back, in a follow-up commit, that folds in the existing powersOfTen table in extfloat.go. RELNOTE=yes Change-Id: I3953110deaa1f5f6941e88e8417c4665b649ed80 Reviewed-on: https://go-review.googlesource.com/c/go/+/260858 Run-TryBot: Nigel Tao <nigeltao@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> Trust: Nigel Tao <nigeltao@golang.org>
2020-08-20unicode: upgrade to Unicode 13.0.0Marcel van Lohuizen
Fixes #40755 Change-Id: I14b3977317994095db8ae1bd873c174641209356 Reviewed-on: https://go-review.googlesource.com/c/go/+/248765 Run-TryBot: Marcel van Lohuizen <mpvl@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-05-08strconv: fix ParseComplex for strings with separatorsRobert Griesemer
The recently added function parseFloatPrefix tested the entire string for correct placement of separators rather than just the consumed part. The 4-char fix is in readFloat (atof.go:303). Added more tests. Also added some white space for nicer grouping of the test cases. While at it, removed the need for calling testing.Run. Fixes #38962. Change-Id: Ifce84f362bb4ede559103f8d535556d3de9325f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/233017 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-05-08strconv: add ParseComplex and FormatComplexpj
Adds two functions to deal with complex numbers: * FormatComplex * ParseComplex ParseComplex accepts complex numbers in this format: N+Ni Fixes #36771 Change-Id: Id184dc9e277e5fa01a714ad656a88255ead05085 GitHub-Last-Rev: 036a075d36363774a95f6000b7c4098896474744 GitHub-Pull-Request: golang/go#36815 Reviewed-on: https://go-review.googlesource.com/c/go/+/216617 Reviewed-by: Robert Griesemer <gri@golang.org>
2020-04-30strconv: fix for parseFloatPrefixRobert Griesemer
parseFloatPrefix accepts a string if it has a valid floating-point number as prefix. Make sure that "infi", "infin", ... etc. are accepted as valid numbers "inf" with suffix "i", "in", etc. This is important for parsing complex numbers such as "0+infi". This change does not affect the correctness of ParseFloat because ParseFloat rejects strings that contain a suffix after a valid floating-point number. Updates #36771. Change-Id: Ie1693a8ca2f8edf07b57688e0b35751b7100d39d Reviewed-on: https://go-review.googlesource.com/c/go/+/231237 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-04-30strconv: implement parseFloatPrefix returning no. of bytes consumedRobert Griesemer
parseFloatPrefix will make it easier to implement ParseComplex. Verified that there's no relevant performance impact: Benchmarks run on a "quiet" MacBook Pro, 3.3GHz Dual-Core Intel Core i7, with 16GB 2133MHz LPDDR3 RAM running macOS 10.15.4. name old time/op new time/op delta Atof64Decimal-4 38.2ns ± 4% 38.4ns ± 3% ~ (p=0.802 n=5+5) Atof64Float-4 41.1ns ± 3% 43.0ns ± 1% +4.77% (p=0.008 n=5+5) Atof64FloatExp-4 71.9ns ± 3% 70.1ns ± 1% ~ (p=0.063 n=5+5) Atof64Big-4 124ns ± 5% 119ns ± 0% ~ (p=0.143 n=5+4) Atof64RandomBits-4 57.2ns ± 1% 55.7ns ± 2% -2.66% (p=0.016 n=4+5) Atof64RandomFloats-4 56.8ns ± 1% 56.9ns ± 4% ~ (p=0.556 n=4+5) Atof32Decimal-4 35.4ns ± 5% 35.9ns ± 0% ~ (p=0.127 n=5+5) Atof32Float-4 39.6ns ± 7% 40.3ns ± 1% ~ (p=0.135 n=5+5) Atof32FloatExp-4 73.7ns ± 7% 71.9ns ± 0% ~ (p=0.175 n=5+4) Atof32Random-4 103ns ± 6% 98ns ± 2% -5.03% (p=0.008 n=5+5) Updates #36771. Change-Id: I8ff66b582ae8b468d89c9ffc35c569c735cf0341 Reviewed-on: https://go-review.googlesource.com/c/go/+/230737 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-04-27strconv: remove redundant conversions to intsmasher164
IntSize is an untyped constant that does not need explicit conversion. Annotating IntSize as an int and running github.com/mdempsky/unconvert reveals these two cases. Fixes #38682. Change-Id: I014646b7457ddcde32474810153229dcf0c269c6 Reviewed-on: https://go-review.googlesource.com/c/go/+/230306 Run-TryBot: Akhil Indurti <aindurti@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2020-04-11strconv: add comment re extFloat errorscaleNigel Tao
Change-Id: I6f006ba72e1711ba2a24cd71552855ad88284eec Reviewed-on: https://go-review.googlesource.com/c/go/+/227797 Reviewed-by: Rémy Oudompheng <remyoudompheng@gmail.com> Reviewed-by: Nigel Tao <nigeltao@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>
2020-01-26strconv: stop describing Unicode graphic characters as non-ASCIIIan Lance Taylor
Fixes #36778 Change-Id: I3c4ce100fc219bda0ff1d7a086c2309ed695691d Reviewed-on: https://go-review.googlesource.com/c/go/+/216478 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2019-11-11strconv: reformat and tidy comments in exampleRob Pike
Apply the suggestions made in the too-late review of golang.org/cl/137215 to move the comments to a separate line and use proper punctuation. Change-Id: If2b4e5ce8af8c78fa51280d5c87c852a76dae459 Reviewed-on: https://go-review.googlesource.com/c/go/+/206125 Reviewed-by: Robert Griesemer <gri@golang.org>
2019-11-05unicode: upgrade to Unicode 12Marcel van Lohuizen
This does not include an upgrade of golang.org/x/net. This is optional and best done as a separate CL. Change-Id: Ifecc3fb6e3b7fe026b4ddefbe637186a3445b0bc Reviewed-on: https://go-review.googlesource.com/c/go/+/204658 Run-TryBot: Marcel van Lohuizen <mpvl@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2019-09-30strconv: add Unwrap to custom error typesPantelis Sampaziotis
Updates #30322 This change adds the Unwrap method to NumError. NumError is the only custom error type of the strconv that has a nested exported error. Change-Id: I8774886348880365a83f72a1d106276def27dffe GitHub-Last-Rev: 712f3df8842f48f988cebfc527476781a7cf7140 GitHub-Pull-Request: golang/go#34213 Reviewed-on: https://go-review.googlesource.com/c/go/+/194563 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2019-08-28strconv: Speed improvement to number parsingSam Arnold
Run underscore validation only if we have seen underscores. Some performance results on my laptop: name old time/op new time/op delta Atof64Decimal-12 30.5ns ± 0% 23.8ns ± 0% -22.02% (p=0.016 n=5+4) Atof64Float-12 39.0ns ± 0% 28.7ns ± 0% -26.39% (p=0.002 n=6+6) Atof64FloatExp-12 64.4ns ± 1% 54.4ns ± 1% -15.65% (p=0.002 n=6+6) Atof64Big-12 115ns ± 1% 87ns ± 1% -24.45% (p=0.002 n=6+6) Atof64RandomBits-12 187ns ±14% 156ns ±19% -16.46% (p=0.032 n=6+6) Atof64RandomFloats-12 126ns ± 0% 105ns ± 1% -16.65% (p=0.000 n=6+5) Atof32Decimal-12 32.0ns ± 1% 24.0ns ± 1% -24.97% (p=0.002 n=6+6) Atof32Float-12 37.1ns ± 1% 27.0ns ± 1% -27.42% (p=0.002 n=6+6) Atof32FloatExp-12 68.4ns ± 1% 54.2ns ± 1% -20.77% (p=0.002 n=6+6) Atof32Random-12 92.0ns ± 1% 77.4ns ± 0% -15.81% (p=0.000 n=6+5) ParseInt/Pos/7bit-12 19.4ns ± 1% 13.8ns ±10% -28.94% (p=0.002 n=6+6) ParseInt/Pos/26bit-12 29.1ns ± 1% 19.8ns ± 2% -31.92% (p=0.002 n=6+6) ParseInt/Pos/31bit-12 33.1ns ± 0% 22.3ns ± 3% -32.62% (p=0.004 n=5+6) ParseInt/Pos/56bit-12 47.8ns ± 1% 30.7ns ± 1% -35.78% (p=0.004 n=6+5) ParseInt/Pos/63bit-12 51.9ns ± 1% 33.4ns ± 2% -35.49% (p=0.002 n=6+6) ParseInt/Neg/7bit-12 18.5ns ± 4% 13.4ns ± 3% -27.88% (p=0.002 n=6+6) ParseInt/Neg/26bit-12 28.4ns ± 3% 19.7ns ± 3% -30.38% (p=0.002 n=6+6) ParseInt/Neg/31bit-12 31.9ns ± 1% 21.8ns ± 2% -31.56% (p=0.002 n=6+6) ParseInt/Neg/56bit-12 46.2ns ± 0% 30.6ns ± 1% -33.73% (p=0.004 n=5+6) ParseInt/Neg/63bit-12 50.2ns ± 1% 33.2ns ± 1% -33.96% (p=0.002 n=6+6) Fixes #33330 Change-Id: I119da66457c2fbaf6e88bb90cf56417a16df8f0e Reviewed-on: https://go-review.googlesource.com/c/go/+/187957 Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2019-08-23strconv: simplify the text for bases in ParseIntRob Pike
Followon from a review comment in https://golang.org/cl/191078 Change-Id: If115b2ae0df5e5cb9babd60802947ddb687d56c2 Reviewed-on: https://go-review.googlesource.com/c/go/+/191219 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-08-22strconv: update documentationEmmanuel T Odeke
Fixes #33750. Updates #31197. Change-Id: I26f63cef57e5f0eec85b84554c82f6d47b4f41a1 Reviewed-on: https://go-review.googlesource.com/c/go/+/191078 Reviewed-by: Robert Griesemer <gri@golang.org>
2019-05-31strconv: document handling of NaN and ±InfAndrew Gerrand
In addition to the example that was added in 203b80ab, mention these special cases in the doc comment. This change also adjusts the example to include "+Inf", as it was not otherwise mentioned that the plus symbol may be present. Fix #30990 Change-Id: I97d66f4aff6a17a6ccc0ee2e7f32e39ae91ae454 Reviewed-on: https://go-review.googlesource.com/c/go/+/179738 Reviewed-by: Alex Miasoedov <msoedov@gmail.com> Reviewed-by: Benny Siegert <bsiegert@gmail.com> Run-TryBot: Benny Siegert <bsiegert@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-05-23strconv: fix rounding in FormatFloat fallback pathCaleb Spare
Float formatting uses a multiprecision fallback path where Grisu3 algorithm fails. This has a bug during the rounding phase: the difference between the decimal value and the upper bound is examined byte-by-byte and doesn't properly handle the case where the first divergence has a difference of 1. For instance (using an example from #29491), for the number 498484681984085570, roundShortest examines the three decimal values: lower: 498484681984085536 d: 498484681984085568 upper: 498484681984085600 After examining the 16th digit, we know that rounding d up will fall within the bounds unless all remaining digits of d are 9 and all remaining digits of upper are 0: d: ...855xx upper: ...856xx However, the loop forgets that d and upper have already diverged and then on the next iteration sees that the 17th digit of d is actually lower than the 17th digit of upper and decides that we still can't round up: d: ...8556x upper: ...8560x Thus the original value is incorrectly rounded down to 498484681984085560 instead of the closer (and equally short) 498484681984085570. Thanks to Brian Kessler for diagnosing this bug. Fix it by remembering when we've seen divergence in previous digits. This CL also fixes another bug in the same loop: for some inputs, the decimal value d or the lower bound may have fewer digits than the upper bound, yet the iteration through the digits starts at i=0 for each of them. For instance, given the float64 value 1e23, we have d: 99999999999999991611392 upper: 100000000000000000000000 but the loop starts by comparing '9' to '1' rather than '0' to '1'. I haven't found any cases where this second bug causes incorrect output because when the digit comparison fails on the first loop iteration the upper bound always has more nonzero digits (i.e., the expression 'i+1 < upper.nd' is always true). Fixes #29491 Change-Id: I58856a7a2e47935ec2f233d9f717ef15c78bb2d0 Reviewed-on: https://go-review.googlesource.com/c/go/+/157697 Run-TryBot: Caleb Spare <cespare@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rémy Oudompheng <remyoudompheng@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2019-04-29strconv: Document ParseFloat's special casesAlex Myasoedov
Updates #30990 Change-Id: I968fb13251ab3796328089046a3f0fc5c7eb9df9 Reviewed-on: https://go-review.googlesource.com/c/go/+/174204 Reviewed-by: Benny Siegert <bsiegert@gmail.com> Run-TryBot: Benny Siegert <bsiegert@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-04-25all: update to Unicode 11Marcel van Lohuizen
This does *not* update the vendored tables. A commit updating these tables should follow soon, Mostly generated running UNICODE_VERSION=11.0.0 in x/text. Manually updated next.txt file. Updates golang/go#27945. Change-Id: I939a01e235aeca898ee9afc99a531e7ad8444e12 Reviewed-on: https://go-review.googlesource.com/c/go/+/154420 Run-TryBot: Marcel van Lohuizen <mpvl@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2019-04-18strconv: pre-allocate in appendQuotedWithRob Pike
The byte-at-a-time allocation done quoting strings in appendQuotedWith grows the output incrementally, which is poor behavior for very large strings. An easy fix is to make sure the buffer has enough room at least for an unquoted string. Add a benchmark with a megabyte of non-ASCII data. Before: 39 allocations. After: 7 allocations. We could do better by doing a lot more work but this seems like a big result for little effort. Fixes #31472. Change-Id: I852139e0a2bd13722c4dd329ded8ae1759abad5b Reviewed-on: https://go-review.googlesource.com/c/go/+/172677 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-03-04strconv: simplify (*extFloat).Multiply using math/bits.Mul64Rémy Oudompheng
This method was using a handwritten long multiplication of uint64s. Since implementation of #24813 we can remove it and replace it by Mul64 from math/bits. This brings a small speedup for 64-bit platforms. Benchmarks on Haswell Celeron 2955U. benchmark old ns/op new ns/op delta BenchmarkAppendFloat/Decimal-2 127 127 +0.00% BenchmarkAppendFloat/Float-2 340 317 -6.76% BenchmarkAppendFloat/Exp-2 258 233 -9.69% BenchmarkAppendFloat/NegExp-2 256 231 -9.77% BenchmarkAppendFloat/Big-2 402 375 -6.72% BenchmarkAppendFloat/BinaryExp-2 113 114 +0.88% BenchmarkAppendFloat/32Integer-2 125 125 +0.00% BenchmarkAppendFloat/32ExactFraction-2 274 249 -9.12% BenchmarkAppendFloat/32Point-2 339 317 -6.49% BenchmarkAppendFloat/32Exp-2 255 229 -10.20% BenchmarkAppendFloat/32NegExp-2 254 229 -9.84% BenchmarkAppendFloat/64Fixed1-2 165 154 -6.67% BenchmarkAppendFloat/64Fixed2-2 184 176 -4.35% BenchmarkAppendFloat/64Fixed3-2 168 158 -5.95% BenchmarkAppendFloat/64Fixed4-2 187 177 -5.35% BenchmarkAppendFloat/Slowpath64-2 84977 84883 -0.11% Change-Id: If05784e856289b3b7bf136567882e7ee10234756 Reviewed-on: https://go-review.googlesource.com/c/go/+/157717 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2019-02-27strconv: remove use of DeepEqual for testing errorsMarcel van Lohuizen
Comparing errors using DeepEqual breaks if frame information is added as proposed in Issue #29934. Updates #29934. Change-Id: I0372883288f974998138f95f6c7c79a60f922a3e Reviewed-on: https://go-review.googlesource.com/c/162177 Run-TryBot: Marcel van Lohuizen <mpvl@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> Reviewed-by: Damien Neil <dneil@google.com>
2019-02-12strconv: add 0b, 0o integer prefixes in ParseInt, ParseUintRuss Cox
This CL modifies ParseInt and ParseUint to recognize 0b and 0o as binary and octal base prefixes when base == 0. See golang.org/design/19308-number-literals for background. For #19308. For #12711. Change-Id: I8efe067f415aa517bdefbff7e230d3fa1694d530 Reviewed-on: https://go-review.googlesource.com/c/160244 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2019-02-12strconv: accept underscores in ParseInt, ParseUint, ParseFloatRuss Cox
This CL modifies ParseInt, ParseUint, and ParseFloat to accept digit-separating underscores in their arguments. For ParseInt and ParseUint, the underscores are only allowed when base == 0. See golang.org/design/19308-number-literals for background. For #28493. Change-Id: I057ca2539d89314643f591ba8144c3ea7126651c Reviewed-on: https://go-review.googlesource.com/c/160243 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2019-02-12strconv: format hex floatsRuss Cox
This CL updates FormatFloat to format standard hexadecimal floating-point constants, using the 'x' and 'X' verbs. See golang.org/design/19308-number-literals for background. For #29008. Change-Id: I540b8f71d492cfdb7c58af533d357a564591f28b Reviewed-on: https://go-review.googlesource.com/c/160242 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2019-02-12strconv: parse hex floatsRuss Cox
This CL updates ParseFloat to recognize standard hexadecimal floating-point constants. See golang.org/design/19308-number-literals for background. For #29008. Change-Id: I45f3b0c36b5d92c0e8a4b35c05443a83d7a6d4b3 Reviewed-on: https://go-review.googlesource.com/c/160241 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2018-12-31strconv: make docs for Itoa and Atoi slightly higher levelJosh Bleecher Snyder
Fixes #29461 Change-Id: I5db8bc80e5bd0778dced8471581c67e66853aada Reviewed-on: https://go-review.googlesource.com/c/155924 Reviewed-by: Rob Pike <r@golang.org>