summaryrefslogtreecommitdiffstats
path: root/src/strconv
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:25:22 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-16 19:25:22 +0000
commitf6ad4dcef54c5ce997a4bad5a6d86de229015700 (patch)
tree7cfa4e31ace5c2bd95c72b154d15af494b2bcbef /src/strconv
parentInitial commit. (diff)
downloadgolang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.tar.xz
golang-1.22-f6ad4dcef54c5ce997a4bad5a6d86de229015700.zip
Adding upstream version 1.22.1.upstream/1.22.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/strconv')
-rw-r--r--src/strconv/atob.go35
-rw-r--r--src/strconv/atob_test.go91
-rw-r--r--src/strconv/atoc.go105
-rw-r--r--src/strconv/atoc_test.go229
-rw-r--r--src/strconv/atof.go709
-rw-r--r--src/strconv/atof_test.go754
-rw-r--r--src/strconv/atoi.go332
-rw-r--r--src/strconv/atoi_test.go677
-rw-r--r--src/strconv/bytealg.go14
-rw-r--r--src/strconv/bytealg_bootstrap.go17
-rw-r--r--src/strconv/ctoa.go27
-rw-r--r--src/strconv/ctoa_test.go53
-rw-r--r--src/strconv/decimal.go415
-rw-r--r--src/strconv/decimal_test.go127
-rw-r--r--src/strconv/doc.go56
-rw-r--r--src/strconv/eisel_lemire.go884
-rw-r--r--src/strconv/example_test.go440
-rw-r--r--src/strconv/export_test.go10
-rw-r--r--src/strconv/fp_test.go142
-rw-r--r--src/strconv/ftoa.go570
-rw-r--r--src/strconv/ftoa_test.go324
-rw-r--r--src/strconv/ftoaryu.go569
-rw-r--r--src/strconv/ftoaryu_test.go31
-rw-r--r--src/strconv/internal_test.go31
-rw-r--r--src/strconv/isprint.go752
-rw-r--r--src/strconv/itoa.go205
-rw-r--r--src/strconv/itoa_test.go243
-rw-r--r--src/strconv/makeisprint.go203
-rw-r--r--src/strconv/quote.go599
-rw-r--r--src/strconv/quote_test.go384
-rw-r--r--src/strconv/strconv_test.go160
-rw-r--r--src/strconv/testdata/testfp.txt181
32 files changed, 9369 insertions, 0 deletions
diff --git a/src/strconv/atob.go b/src/strconv/atob.go
new file mode 100644
index 0000000..0a49500
--- /dev/null
+++ b/src/strconv/atob.go
@@ -0,0 +1,35 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv
+
+// ParseBool returns the boolean value represented by the string.
+// It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
+// Any other value returns an error.
+func ParseBool(str string) (bool, error) {
+ switch str {
+ case "1", "t", "T", "true", "TRUE", "True":
+ return true, nil
+ case "0", "f", "F", "false", "FALSE", "False":
+ return false, nil
+ }
+ return false, syntaxError("ParseBool", str)
+}
+
+// FormatBool returns "true" or "false" according to the value of b.
+func FormatBool(b bool) string {
+ if b {
+ return "true"
+ }
+ return "false"
+}
+
+// AppendBool appends "true" or "false", according to the value of b,
+// to dst and returns the extended buffer.
+func AppendBool(dst []byte, b bool) []byte {
+ if b {
+ return append(dst, "true"...)
+ }
+ return append(dst, "false"...)
+}
diff --git a/src/strconv/atob_test.go b/src/strconv/atob_test.go
new file mode 100644
index 0000000..40d43a9
--- /dev/null
+++ b/src/strconv/atob_test.go
@@ -0,0 +1,91 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ "bytes"
+ . "strconv"
+ "testing"
+)
+
+type atobTest struct {
+ in string
+ out bool
+ err error
+}
+
+var atobtests = []atobTest{
+ {"", false, ErrSyntax},
+ {"asdf", false, ErrSyntax},
+ {"0", false, nil},
+ {"f", false, nil},
+ {"F", false, nil},
+ {"FALSE", false, nil},
+ {"false", false, nil},
+ {"False", false, nil},
+ {"1", true, nil},
+ {"t", true, nil},
+ {"T", true, nil},
+ {"TRUE", true, nil},
+ {"true", true, nil},
+ {"True", true, nil},
+}
+
+func TestParseBool(t *testing.T) {
+ for _, test := range atobtests {
+ b, e := ParseBool(test.in)
+ if test.err != nil {
+ // expect an error
+ if e == nil {
+ t.Errorf("ParseBool(%s) = nil; want %s", test.in, test.err)
+ } else {
+ // NumError assertion must succeed; it's the only thing we return.
+ if e.(*NumError).Err != test.err {
+ t.Errorf("ParseBool(%s) = %s; want %s", test.in, e, test.err)
+ }
+ }
+ } else {
+ if e != nil {
+ t.Errorf("ParseBool(%s) = %s; want nil", test.in, e)
+ }
+ if b != test.out {
+ t.Errorf("ParseBool(%s) = %t; want %t", test.in, b, test.out)
+ }
+ }
+ }
+}
+
+var boolString = map[bool]string{
+ true: "true",
+ false: "false",
+}
+
+func TestFormatBool(t *testing.T) {
+ for b, s := range boolString {
+ if f := FormatBool(b); f != s {
+ t.Errorf("FormatBool(%v) = %q; want %q", b, f, s)
+ }
+ }
+}
+
+type appendBoolTest struct {
+ b bool
+ in []byte
+ out []byte
+}
+
+var appendBoolTests = []appendBoolTest{
+ {true, []byte("foo "), []byte("foo true")},
+ {false, []byte("foo "), []byte("foo false")},
+}
+
+func TestAppendBool(t *testing.T) {
+ for _, test := range appendBoolTests {
+ b := AppendBool(test.in, test.b)
+ if !bytes.Equal(b, test.out) {
+ t.Errorf("AppendBool(%q, %v) = %q; want %q", test.in, test.b, b, test.out)
+ }
+ }
+}
diff --git a/src/strconv/atoc.go b/src/strconv/atoc.go
new file mode 100644
index 0000000..f6fdd14
--- /dev/null
+++ b/src/strconv/atoc.go
@@ -0,0 +1,105 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv
+
+const fnParseComplex = "ParseComplex"
+
+// convErr splits an error returned by parseFloatPrefix
+// into a syntax or range error for ParseComplex.
+func convErr(err error, s string) (syntax, range_ error) {
+ if x, ok := err.(*NumError); ok {
+ x.Func = fnParseComplex
+ x.Num = cloneString(s)
+ if x.Err == ErrRange {
+ return nil, x
+ }
+ }
+ return err, nil
+}
+
+// ParseComplex converts the string s to a complex number
+// with the precision specified by bitSize: 64 for complex64, or 128 for complex128.
+// When bitSize=64, the result still has type complex128, but it will be
+// convertible to complex64 without changing its value.
+//
+// The number represented by s must be of the form N, Ni, or N±Ni, where N stands
+// for a floating-point number as recognized by ParseFloat, and i is the imaginary
+// component. If the second N is unsigned, a + sign is required between the two components
+// as indicated by the ±. If the second N is NaN, only a + sign is accepted.
+// The form may be parenthesized and cannot contain any spaces.
+// The resulting complex number consists of the two components converted by ParseFloat.
+//
+// The errors that ParseComplex returns have concrete type *NumError
+// and include err.Num = s.
+//
+// If s is not syntactically well-formed, ParseComplex returns err.Err = ErrSyntax.
+//
+// If s is syntactically well-formed but either component is more than 1/2 ULP
+// away from the largest floating point number of the given component's size,
+// ParseComplex returns err.Err = ErrRange and c = ±Inf for the respective component.
+func ParseComplex(s string, bitSize int) (complex128, error) {
+ size := 64
+ if bitSize == 64 {
+ size = 32 // complex64 uses float32 parts
+ }
+
+ orig := s
+
+ // Remove parentheses, if any.
+ if len(s) >= 2 && s[0] == '(' && s[len(s)-1] == ')' {
+ s = s[1 : len(s)-1]
+ }
+
+ var pending error // pending range error, or nil
+
+ // Read real part (possibly imaginary part if followed by 'i').
+ re, n, err := parseFloatPrefix(s, size)
+ if err != nil {
+ err, pending = convErr(err, orig)
+ if err != nil {
+ return 0, err
+ }
+ }
+ s = s[n:]
+
+ // If we have nothing left, we're done.
+ if len(s) == 0 {
+ return complex(re, 0), pending
+ }
+
+ // Otherwise, look at the next character.
+ switch s[0] {
+ case '+':
+ // Consume the '+' to avoid an error if we have "+NaNi", but
+ // do this only if we don't have a "++" (don't hide that error).
+ if len(s) > 1 && s[1] != '+' {
+ s = s[1:]
+ }
+ case '-':
+ // ok
+ case 'i':
+ // If 'i' is the last character, we only have an imaginary part.
+ if len(s) == 1 {
+ return complex(0, re), pending
+ }
+ fallthrough
+ default:
+ return 0, syntaxError(fnParseComplex, orig)
+ }
+
+ // Read imaginary part.
+ im, n, err := parseFloatPrefix(s, size)
+ if err != nil {
+ err, pending = convErr(err, orig)
+ if err != nil {
+ return 0, err
+ }
+ }
+ s = s[n:]
+ if s != "i" {
+ return 0, syntaxError(fnParseComplex, orig)
+ }
+ return complex(re, im), pending
+}
diff --git a/src/strconv/atoc_test.go b/src/strconv/atoc_test.go
new file mode 100644
index 0000000..4c1aad0
--- /dev/null
+++ b/src/strconv/atoc_test.go
@@ -0,0 +1,229 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ "math"
+ "math/cmplx"
+ "reflect"
+ . "strconv"
+ "testing"
+)
+
+var (
+ infp0 = complex(math.Inf(+1), 0)
+ infm0 = complex(math.Inf(-1), 0)
+ inf0p = complex(0, math.Inf(+1))
+ inf0m = complex(0, math.Inf(-1))
+
+ infpp = complex(math.Inf(+1), math.Inf(+1))
+ infpm = complex(math.Inf(+1), math.Inf(-1))
+ infmp = complex(math.Inf(-1), math.Inf(+1))
+ infmm = complex(math.Inf(-1), math.Inf(-1))
+)
+
+type atocTest struct {
+ in string
+ out complex128
+ err error
+}
+
+func TestParseComplex(t *testing.T) {
+ tests := []atocTest{
+ // Clearly invalid
+ {"", 0, ErrSyntax},
+ {" ", 0, ErrSyntax},
+ {"(", 0, ErrSyntax},
+ {")", 0, ErrSyntax},
+ {"i", 0, ErrSyntax},
+ {"+i", 0, ErrSyntax},
+ {"-i", 0, ErrSyntax},
+ {"1I", 0, ErrSyntax},
+ {"10 + 5i", 0, ErrSyntax},
+ {"3+", 0, ErrSyntax},
+ {"3+5", 0, ErrSyntax},
+ {"3+5+5i", 0, ErrSyntax},
+
+ // Parentheses
+ {"()", 0, ErrSyntax},
+ {"(i)", 0, ErrSyntax},
+ {"(0)", 0, nil},
+ {"(1i)", 1i, nil},
+ {"(3.0+5.5i)", 3.0 + 5.5i, nil},
+ {"(1)+1i", 0, ErrSyntax},
+ {"(3.0+5.5i", 0, ErrSyntax},
+ {"3.0+5.5i)", 0, ErrSyntax},
+
+ // NaNs
+ {"NaN", complex(math.NaN(), 0), nil},
+ {"NANi", complex(0, math.NaN()), nil},
+ {"nan+nAni", complex(math.NaN(), math.NaN()), nil},
+ {"+NaN", 0, ErrSyntax},
+ {"-NaN", 0, ErrSyntax},
+ {"NaN-NaNi", 0, ErrSyntax},
+
+ // Infs
+ {"Inf", infp0, nil},
+ {"+inf", infp0, nil},
+ {"-inf", infm0, nil},
+ {"Infinity", infp0, nil},
+ {"+INFINITY", infp0, nil},
+ {"-infinity", infm0, nil},
+ {"+infi", inf0p, nil},
+ {"0-infinityi", inf0m, nil},
+ {"Inf+Infi", infpp, nil},
+ {"+Inf-Infi", infpm, nil},
+ {"-Infinity+Infi", infmp, nil},
+ {"inf-inf", 0, ErrSyntax},
+
+ // Zeros
+ {"0", 0, nil},
+ {"0i", 0, nil},
+ {"-0.0i", 0, nil},
+ {"0+0.0i", 0, nil},
+ {"0e+0i", 0, nil},
+ {"0e-0+0i", 0, nil},
+ {"-0.0-0.0i", 0, nil},
+ {"0e+012345", 0, nil},
+ {"0x0p+012345i", 0, nil},
+ {"0x0.00p-012345i", 0, nil},
+ {"+0e-0+0e-0i", 0, nil},
+ {"0e+0+0e+0i", 0, nil},
+ {"-0e+0-0e+0i", 0, nil},
+
+ // Regular non-zeroes
+ {"0.1", 0.1, nil},
+ {"0.1i", 0 + 0.1i, nil},
+ {"0.123", 0.123, nil},
+ {"0.123i", 0 + 0.123i, nil},
+ {"0.123+0.123i", 0.123 + 0.123i, nil},
+ {"99", 99, nil},
+ {"+99", 99, nil},
+ {"-99", -99, nil},
+ {"+1i", 1i, nil},
+ {"-1i", -1i, nil},
+ {"+3+1i", 3 + 1i, nil},
+ {"30+3i", 30 + 3i, nil},
+ {"+3e+3-3e+3i", 3e+3 - 3e+3i, nil},
+ {"+3e+3+3e+3i", 3e+3 + 3e+3i, nil},
+ {"+3e+3+3e+3i+", 0, ErrSyntax},
+
+ // Separators
+ {"0.1", 0.1, nil},
+ {"0.1i", 0 + 0.1i, nil},
+ {"0.1_2_3", 0.123, nil},
+ {"+0x_3p3i", 0x3p3i, nil},
+ {"0_0+0x_0p0i", 0, nil},
+ {"0x_10.3p-8+0x3p3i", 0x10.3p-8 + 0x3p3i, nil},
+ {"+0x_1_0.3p-8+0x_3_0p3i", 0x10.3p-8 + 0x30p3i, nil},
+ {"0x1_0.3p+8-0x_3p3i", 0x10.3p+8 - 0x3p3i, nil},
+
+ // Hexadecimals
+ {"0x10.3p-8+0x3p3i", 0x10.3p-8 + 0x3p3i, nil},
+ {"+0x10.3p-8+0x3p3i", 0x10.3p-8 + 0x3p3i, nil},
+ {"0x10.3p+8-0x3p3i", 0x10.3p+8 - 0x3p3i, nil},
+ {"0x1p0", 1, nil},
+ {"0x1p1", 2, nil},
+ {"0x1p-1", 0.5, nil},
+ {"0x1ep-1", 15, nil},
+ {"-0x1ep-1", -15, nil},
+ {"-0x2p3", -16, nil},
+ {"0x1e2", 0, ErrSyntax},
+ {"1p2", 0, ErrSyntax},
+ {"0x1e2i", 0, ErrSyntax},
+
+ // ErrRange
+ // next float64 - too large
+ {"+0x1p1024", infp0, ErrRange},
+ {"-0x1p1024", infm0, ErrRange},
+ {"+0x1p1024i", inf0p, ErrRange},
+ {"-0x1p1024i", inf0m, ErrRange},
+ {"+0x1p1024+0x1p1024i", infpp, ErrRange},
+ {"+0x1p1024-0x1p1024i", infpm, ErrRange},
+ {"-0x1p1024+0x1p1024i", infmp, ErrRange},
+ {"-0x1p1024-0x1p1024i", infmm, ErrRange},
+ // the border is ...158079
+ // borderline - okay
+ {"+0x1.fffffffffffff7fffp1023+0x1.fffffffffffff7fffp1023i", 1.7976931348623157e+308 + 1.7976931348623157e+308i, nil},
+ {"+0x1.fffffffffffff7fffp1023-0x1.fffffffffffff7fffp1023i", 1.7976931348623157e+308 - 1.7976931348623157e+308i, nil},
+ {"-0x1.fffffffffffff7fffp1023+0x1.fffffffffffff7fffp1023i", -1.7976931348623157e+308 + 1.7976931348623157e+308i, nil},
+ {"-0x1.fffffffffffff7fffp1023-0x1.fffffffffffff7fffp1023i", -1.7976931348623157e+308 - 1.7976931348623157e+308i, nil},
+ // borderline - too large
+ {"+0x1.fffffffffffff8p1023", infp0, ErrRange},
+ {"-0x1fffffffffffff.8p+971", infm0, ErrRange},
+ {"+0x1.fffffffffffff8p1023i", inf0p, ErrRange},
+ {"-0x1fffffffffffff.8p+971i", inf0m, ErrRange},
+ {"+0x1.fffffffffffff8p1023+0x1.fffffffffffff8p1023i", infpp, ErrRange},
+ {"+0x1.fffffffffffff8p1023-0x1.fffffffffffff8p1023i", infpm, ErrRange},
+ {"-0x1fffffffffffff.8p+971+0x1fffffffffffff.8p+971i", infmp, ErrRange},
+ {"-0x1fffffffffffff8p+967-0x1fffffffffffff8p+967i", infmm, ErrRange},
+ // a little too large
+ {"1e308+1e308i", 1e+308 + 1e+308i, nil},
+ {"2e308+2e308i", infpp, ErrRange},
+ {"1e309+1e309i", infpp, ErrRange},
+ {"0x1p1025+0x1p1025i", infpp, ErrRange},
+ {"2e308", infp0, ErrRange},
+ {"1e309", infp0, ErrRange},
+ {"0x1p1025", infp0, ErrRange},
+ {"2e308i", inf0p, ErrRange},
+ {"1e309i", inf0p, ErrRange},
+ {"0x1p1025i", inf0p, ErrRange},
+ // way too large
+ {"+1e310+1e310i", infpp, ErrRange},
+ {"+1e310-1e310i", infpm, ErrRange},
+ {"-1e310+1e310i", infmp, ErrRange},
+ {"-1e310-1e310i", infmm, ErrRange},
+ // under/overflow exponent
+ {"1e-4294967296", 0, nil},
+ {"1e-4294967296i", 0, nil},
+ {"1e-4294967296+1i", 1i, nil},
+ {"1+1e-4294967296i", 1, nil},
+ {"1e-4294967296+1e-4294967296i", 0, nil},
+ {"1e+4294967296", infp0, ErrRange},
+ {"1e+4294967296i", inf0p, ErrRange},
+ {"1e+4294967296+1e+4294967296i", infpp, ErrRange},
+ {"1e+4294967296-1e+4294967296i", infpm, ErrRange},
+ }
+ for i := range tests {
+ test := &tests[i]
+ if test.err != nil {
+ test.err = &NumError{Func: "ParseComplex", Num: test.in, Err: test.err}
+ }
+ got, err := ParseComplex(test.in, 128)
+ if !reflect.DeepEqual(err, test.err) {
+ t.Fatalf("ParseComplex(%q, 128) = %v, %v; want %v, %v", test.in, got, err, test.out, test.err)
+ }
+ if !(cmplx.IsNaN(test.out) && cmplx.IsNaN(got)) && got != test.out {
+ t.Fatalf("ParseComplex(%q, 128) = %v, %v; want %v, %v", test.in, got, err, test.out, test.err)
+ }
+
+ if complex128(complex64(test.out)) == test.out {
+ got, err := ParseComplex(test.in, 64)
+ if !reflect.DeepEqual(err, test.err) {
+ t.Fatalf("ParseComplex(%q, 64) = %v, %v; want %v, %v", test.in, got, err, test.out, test.err)
+ }
+ got64 := complex64(got)
+ if complex128(got64) != test.out {
+ t.Fatalf("ParseComplex(%q, 64) = %v, %v; want %v, %v", test.in, got, err, test.out, test.err)
+ }
+ }
+ }
+}
+
+// Issue 42297: allow ParseComplex(s, not_32_or_64) for legacy reasons
+func TestParseComplexIncorrectBitSize(t *testing.T) {
+ const s = "1.5e308+1.0e307i"
+ const want = 1.5e308 + 1.0e307i
+
+ for _, bitSize := range []int{0, 10, 100, 256} {
+ c, err := ParseComplex(s, bitSize)
+ if err != nil {
+ t.Fatalf("ParseComplex(%q, %d) gave error %s", s, bitSize, err)
+ }
+ if c != want {
+ t.Fatalf("ParseComplex(%q, %d) = %g (expected %g)", s, bitSize, c, want)
+ }
+ }
+}
diff --git a/src/strconv/atof.go b/src/strconv/atof.go
new file mode 100644
index 0000000..8fc9042
--- /dev/null
+++ b/src/strconv/atof.go
@@ -0,0 +1,709 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv
+
+// decimal to binary floating point conversion.
+// Algorithm:
+// 1) Store input in multiprecision decimal.
+// 2) Multiply/divide decimal by powers of two until in range [0.5, 1)
+// 3) Multiply by 2^precision and round to get mantissa.
+
+import "math"
+
+var optimize = true // set to false to force slow-path conversions for testing
+
+// commonPrefixLenIgnoreCase returns the length of the common
+// prefix of s and prefix, with the character case of s ignored.
+// The prefix argument must be all lower-case.
+func commonPrefixLenIgnoreCase(s, prefix string) int {
+ n := len(prefix)
+ if n > len(s) {
+ n = len(s)
+ }
+ for i := 0; i < n; i++ {
+ c := s[i]
+ if 'A' <= c && c <= 'Z' {
+ c += 'a' - 'A'
+ }
+ if c != prefix[i] {
+ return i
+ }
+ }
+ return n
+}
+
+// special returns the floating-point value for the special,
+// possibly signed floating-point representations inf, infinity,
+// and NaN. The result is ok if a prefix of s contains one
+// of these representations and n is the length of that prefix.
+// The character case is ignored.
+func special(s string) (f float64, n int, ok bool) {
+ if len(s) == 0 {
+ return 0, 0, false
+ }
+ sign := 1
+ nsign := 0
+ switch s[0] {
+ case '+', '-':
+ if s[0] == '-' {
+ sign = -1
+ }
+ nsign = 1
+ s = s[1:]
+ fallthrough
+ case 'i', 'I':
+ n := commonPrefixLenIgnoreCase(s, "infinity")
+ // Anything longer than "inf" is ok, but if we
+ // don't have "infinity", only consume "inf".
+ if 3 < n && n < 8 {
+ n = 3
+ }
+ if n == 3 || n == 8 {
+ return math.Inf(sign), nsign + n, true
+ }
+ case 'n', 'N':
+ if commonPrefixLenIgnoreCase(s, "nan") == 3 {
+ return math.NaN(), 3, true
+ }
+ }
+ return 0, 0, false
+}
+
+func (b *decimal) set(s string) (ok bool) {
+ i := 0
+ b.neg = false
+ b.trunc = false
+
+ // optional sign
+ if i >= len(s) {
+ return
+ }
+ switch {
+ case s[i] == '+':
+ i++
+ case s[i] == '-':
+ b.neg = true
+ i++
+ }
+
+ // digits
+ sawdot := false
+ sawdigits := false
+ for ; i < len(s); i++ {
+ switch {
+ case s[i] == '_':
+ // readFloat already checked underscores
+ continue
+ case s[i] == '.':
+ if sawdot {
+ return
+ }
+ sawdot = true
+ b.dp = b.nd
+ continue
+
+ case '0' <= s[i] && s[i] <= '9':
+ sawdigits = true
+ if s[i] == '0' && b.nd == 0 { // ignore leading zeros
+ b.dp--
+ continue
+ }
+ if b.nd < len(b.d) {
+ b.d[b.nd] = s[i]
+ b.nd++
+ } else if s[i] != '0' {
+ b.trunc = true
+ }
+ continue
+ }
+ break
+ }
+ if !sawdigits {
+ return
+ }
+ if !sawdot {
+ b.dp = b.nd
+ }
+
+ // optional exponent moves decimal point.
+ // if we read a very large, very long number,
+ // just be sure to move the decimal point by
+ // a lot (say, 100000). it doesn't matter if it's
+ // not the exact number.
+ if i < len(s) && lower(s[i]) == 'e' {
+ i++
+ if i >= len(s) {
+ return
+ }
+ esign := 1
+ if s[i] == '+' {
+ i++
+ } else if s[i] == '-' {
+ i++
+ esign = -1
+ }
+ if i >= len(s) || s[i] < '0' || s[i] > '9' {
+ return
+ }
+ e := 0
+ for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i++ {
+ if s[i] == '_' {
+ // readFloat already checked underscores
+ continue
+ }
+ if e < 10000 {
+ e = e*10 + int(s[i]) - '0'
+ }
+ }
+ b.dp += e * esign
+ }
+
+ if i != len(s) {
+ return
+ }
+
+ ok = true
+ return
+}
+
+// readFloat reads a decimal or hexadecimal mantissa and exponent from a float
+// string representation in s; the number may be followed by other characters.
+// readFloat reports the number of bytes consumed (i), and whether the number
+// is valid (ok).
+func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex bool, i int, ok bool) {
+ underscores := false
+
+ // optional sign
+ if i >= len(s) {
+ return
+ }
+ switch {
+ case s[i] == '+':
+ i++
+ case s[i] == '-':
+ neg = true
+ i++
+ }
+
+ // digits
+ base := uint64(10)
+ maxMantDigits := 19 // 10^19 fits in uint64
+ expChar := byte('e')
+ if i+2 < len(s) && s[i] == '0' && lower(s[i+1]) == 'x' {
+ base = 16
+ maxMantDigits = 16 // 16^16 fits in uint64
+ i += 2
+ expChar = 'p'
+ hex = true
+ }
+ sawdot := false
+ sawdigits := false
+ nd := 0
+ ndMant := 0
+ dp := 0
+loop:
+ for ; i < len(s); i++ {
+ switch c := s[i]; true {
+ case c == '_':
+ underscores = true
+ continue
+
+ case c == '.':
+ if sawdot {
+ break loop
+ }
+ sawdot = true
+ dp = nd
+ continue
+
+ case '0' <= c && c <= '9':
+ sawdigits = true
+ if c == '0' && nd == 0 { // ignore leading zeros
+ dp--
+ continue
+ }
+ nd++
+ if ndMant < maxMantDigits {
+ mantissa *= base
+ mantissa += uint64(c - '0')
+ ndMant++
+ } else if c != '0' {
+ trunc = true
+ }
+ continue
+
+ case base == 16 && 'a' <= lower(c) && lower(c) <= 'f':
+ sawdigits = true
+ nd++
+ if ndMant < maxMantDigits {
+ mantissa *= 16
+ mantissa += uint64(lower(c) - 'a' + 10)
+ ndMant++
+ } else {
+ trunc = true
+ }
+ continue
+ }
+ break
+ }
+ if !sawdigits {
+ return
+ }
+ if !sawdot {
+ dp = nd
+ }
+
+ if base == 16 {
+ dp *= 4
+ ndMant *= 4
+ }
+
+ // optional exponent moves decimal point.
+ // if we read a very large, very long number,
+ // just be sure to move the decimal point by
+ // a lot (say, 100000). it doesn't matter if it's
+ // not the exact number.
+ if i < len(s) && lower(s[i]) == expChar {
+ i++
+ if i >= len(s) {
+ return
+ }
+ esign := 1
+ if s[i] == '+' {
+ i++
+ } else if s[i] == '-' {
+ i++
+ esign = -1
+ }
+ if i >= len(s) || s[i] < '0' || s[i] > '9' {
+ return
+ }
+ e := 0
+ for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i++ {
+ if s[i] == '_' {
+ underscores = true
+ continue
+ }
+ if e < 10000 {
+ e = e*10 + int(s[i]) - '0'
+ }
+ }
+ dp += e * esign
+ } else if base == 16 {
+ // Must have exponent.
+ return
+ }
+
+ if mantissa != 0 {
+ exp = dp - ndMant
+ }
+
+ if underscores && !underscoreOK(s[:i]) {
+ return
+ }
+
+ ok = true
+ return
+}
+
+// decimal power of ten to binary power of two.
+var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26}
+
+func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) {
+ var exp int
+ var mant uint64
+
+ // Zero is always a special case.
+ if d.nd == 0 {
+ mant = 0
+ exp = flt.bias
+ goto out
+ }
+
+ // Obvious overflow/underflow.
+ // These bounds are for 64-bit floats.
+ // Will have to change if we want to support 80-bit floats in the future.
+ if d.dp > 310 {
+ goto overflow
+ }
+ if d.dp < -330 {
+ // zero
+ mant = 0
+ exp = flt.bias
+ goto out
+ }
+
+ // Scale by powers of two until in range [0.5, 1.0)
+ exp = 0
+ for d.dp > 0 {
+ var n int
+ if d.dp >= len(powtab) {
+ n = 27
+ } else {
+ n = powtab[d.dp]
+ }
+ d.Shift(-n)
+ exp += n
+ }
+ for d.dp < 0 || d.dp == 0 && d.d[0] < '5' {
+ var n int
+ if -d.dp >= len(powtab) {
+ n = 27
+ } else {
+ n = powtab[-d.dp]
+ }
+ d.Shift(n)
+ exp -= n
+ }
+
+ // Our range is [0.5,1) but floating point range is [1,2).
+ exp--
+
+ // Minimum representable exponent is flt.bias+1.
+ // If the exponent is smaller, move it up and
+ // adjust d accordingly.
+ if exp < flt.bias+1 {
+ n := flt.bias + 1 - exp
+ d.Shift(-n)
+ exp += n
+ }
+
+ if exp-flt.bias >= 1<<flt.expbits-1 {
+ goto overflow
+ }
+
+ // Extract 1+flt.mantbits bits.
+ d.Shift(int(1 + flt.mantbits))
+ mant = d.RoundedInteger()
+
+ // Rounding might have added a bit; shift down.
+ if mant == 2<<flt.mantbits {
+ mant >>= 1
+ exp++
+ if exp-flt.bias >= 1<<flt.expbits-1 {
+ goto overflow
+ }
+ }
+
+ // Denormalized?
+ if mant&(1<<flt.mantbits) == 0 {
+ exp = flt.bias
+ }
+ goto out
+
+overflow:
+ // ±Inf
+ mant = 0
+ exp = 1<<flt.expbits - 1 + flt.bias
+ overflow = true
+
+out:
+ // Assemble bits.
+ bits := mant & (uint64(1)<<flt.mantbits - 1)
+ bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
+ if d.neg {
+ bits |= 1 << flt.mantbits << flt.expbits
+ }
+ return bits, overflow
+}
+
+// Exact powers of 10.
+var float64pow10 = []float64{
+ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
+ 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
+ 1e20, 1e21, 1e22,
+}
+var float32pow10 = []float32{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10}
+
+// If possible to convert decimal representation to 64-bit float f exactly,
+// entirely in floating-point math, do so, avoiding the expense of decimalToFloatBits.
+// Three common cases:
+//
+// value is exact integer
+// value is exact integer * exact power of ten
+// value is exact integer / exact power of ten
+//
+// These all produce potentially inexact but correctly rounded answers.
+func atof64exact(mantissa uint64, exp int, neg bool) (f float64, ok bool) {
+ if mantissa>>float64info.mantbits != 0 {
+ return
+ }
+ f = float64(mantissa)
+ if neg {
+ f = -f
+ }
+ switch {
+ case exp == 0:
+ // an integer.
+ return f, true
+ // Exact integers are <= 10^15.
+ // Exact powers of ten are <= 10^22.
+ case exp > 0 && exp <= 15+22: // int * 10^k
+ // If exponent is big but number of digits is not,
+ // can move a few zeros into the integer part.
+ if exp > 22 {
+ f *= float64pow10[exp-22]
+ exp = 22
+ }
+ if f > 1e15 || f < -1e15 {
+ // the exponent was really too large.
+ return
+ }
+ return f * float64pow10[exp], true
+ case exp < 0 && exp >= -22: // int / 10^k
+ return f / float64pow10[-exp], true
+ }
+ return
+}
+
+// If possible to compute mantissa*10^exp to 32-bit float f exactly,
+// entirely in floating-point math, do so, avoiding the machinery above.
+func atof32exact(mantissa uint64, exp int, neg bool) (f float32, ok bool) {
+ if mantissa>>float32info.mantbits != 0 {
+ return
+ }
+ f = float32(mantissa)
+ if neg {
+ f = -f
+ }
+ switch {
+ case exp == 0:
+ return f, true
+ // Exact integers are <= 10^7.
+ // Exact powers of ten are <= 10^10.
+ case exp > 0 && exp <= 7+10: // int * 10^k
+ // If exponent is big but number of digits is not,
+ // can move a few zeros into the integer part.
+ if exp > 10 {
+ f *= float32pow10[exp-10]
+ exp = 10
+ }
+ if f > 1e7 || f < -1e7 {
+ // the exponent was really too large.
+ return
+ }
+ return f * float32pow10[exp], true
+ case exp < 0 && exp >= -10: // int / 10^k
+ return f / float32pow10[-exp], true
+ }
+ return
+}
+
+// atofHex converts the hex floating-point string s
+// to a rounded float32 or float64 value (depending on flt==&float32info or flt==&float64info)
+// and returns it as a float64.
+// The string s has already been parsed into a mantissa, exponent, and sign (neg==true for negative).
+// If trunc is true, trailing non-zero bits have been omitted from the mantissa.
+func atofHex(s string, flt *floatInfo, mantissa uint64, exp int, neg, trunc bool) (float64, error) {
+ maxExp := 1<<flt.expbits + flt.bias - 2
+ minExp := flt.bias + 1
+ exp += int(flt.mantbits) // mantissa now implicitly divided by 2^mantbits.
+
+ // Shift mantissa and exponent to bring representation into float range.
+ // Eventually we want a mantissa with a leading 1-bit followed by mantbits other bits.
+ // For rounding, we need two more, where the bottom bit represents
+ // whether that bit or any later bit was non-zero.
+ // (If the mantissa has already lost non-zero bits, trunc is true,
+ // and we OR in a 1 below after shifting left appropriately.)
+ for mantissa != 0 && mantissa>>(flt.mantbits+2) == 0 {
+ mantissa <<= 1
+ exp--
+ }
+ if trunc {
+ mantissa |= 1
+ }
+ for mantissa>>(1+flt.mantbits+2) != 0 {
+ mantissa = mantissa>>1 | mantissa&1
+ exp++
+ }
+
+ // If exponent is too negative,
+ // denormalize in hopes of making it representable.
+ // (The -2 is for the rounding bits.)
+ for mantissa > 1 && exp < minExp-2 {
+ mantissa = mantissa>>1 | mantissa&1
+ exp++
+ }
+
+ // Round using two bottom bits.
+ round := mantissa & 3
+ mantissa >>= 2
+ round |= mantissa & 1 // round to even (round up if mantissa is odd)
+ exp += 2
+ if round == 3 {
+ mantissa++
+ if mantissa == 1<<(1+flt.mantbits) {
+ mantissa >>= 1
+ exp++
+ }
+ }
+
+ if mantissa>>flt.mantbits == 0 { // Denormal or zero.
+ exp = flt.bias
+ }
+ var err error
+ if exp > maxExp { // infinity and range error
+ mantissa = 1 << flt.mantbits
+ exp = maxExp + 1
+ err = rangeError(fnParseFloat, s)
+ }
+
+ bits := mantissa & (1<<flt.mantbits - 1)
+ bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
+ if neg {
+ bits |= 1 << flt.mantbits << flt.expbits
+ }
+ if flt == &float32info {
+ return float64(math.Float32frombits(uint32(bits))), err
+ }
+ return math.Float64frombits(bits), err
+}
+
+const fnParseFloat = "ParseFloat"
+
+func atof32(s string) (f float32, n int, err error) {
+ if val, n, ok := special(s); ok {
+ return float32(val), n, nil
+ }
+
+ mantissa, exp, neg, trunc, hex, n, ok := readFloat(s)
+ if !ok {
+ return 0, n, syntaxError(fnParseFloat, s)
+ }
+
+ if hex {
+ f, err := atofHex(s[:n], &float32info, mantissa, exp, neg, trunc)
+ return float32(f), n, err
+ }
+
+ if optimize {
+ // Try pure floating-point arithmetic conversion, and if that fails,
+ // the Eisel-Lemire algorithm.
+ if !trunc {
+ if f, ok := atof32exact(mantissa, exp, neg); ok {
+ return f, n, nil
+ }
+ }
+ f, ok := eiselLemire32(mantissa, exp, neg)
+ if ok {
+ if !trunc {
+ return f, n, nil
+ }
+ // Even if the mantissa was truncated, we may
+ // have found the correct result. Confirm by
+ // converting the upper mantissa bound.
+ fUp, ok := eiselLemire32(mantissa+1, exp, neg)
+ if ok && f == fUp {
+ return f, n, nil
+ }
+ }
+ }
+
+ // Slow fallback.
+ var d decimal
+ if !d.set(s[:n]) {
+ return 0, n, syntaxError(fnParseFloat, s)
+ }
+ b, ovf := d.floatBits(&float32info)
+ f = math.Float32frombits(uint32(b))
+ if ovf {
+ err = rangeError(fnParseFloat, s)
+ }
+ return f, n, err
+}
+
+func atof64(s string) (f float64, n int, err error) {
+ if val, n, ok := special(s); ok {
+ return val, n, nil
+ }
+
+ mantissa, exp, neg, trunc, hex, n, ok := readFloat(s)
+ if !ok {
+ return 0, n, syntaxError(fnParseFloat, s)
+ }
+
+ if hex {
+ f, err := atofHex(s[:n], &float64info, mantissa, exp, neg, trunc)
+ return f, n, err
+ }
+
+ if optimize {
+ // Try pure floating-point arithmetic conversion, and if that fails,
+ // the Eisel-Lemire algorithm.
+ if !trunc {
+ if f, ok := atof64exact(mantissa, exp, neg); ok {
+ return f, n, nil
+ }
+ }
+ f, ok := eiselLemire64(mantissa, exp, neg)
+ if ok {
+ if !trunc {
+ return f, n, nil
+ }
+ // Even if the mantissa was truncated, we may
+ // have found the correct result. Confirm by
+ // converting the upper mantissa bound.
+ fUp, ok := eiselLemire64(mantissa+1, exp, neg)
+ if ok && f == fUp {
+ return f, n, nil
+ }
+ }
+ }
+
+ // Slow fallback.
+ var d decimal
+ if !d.set(s[:n]) {
+ return 0, n, syntaxError(fnParseFloat, s)
+ }
+ b, ovf := d.floatBits(&float64info)
+ f = math.Float64frombits(b)
+ if ovf {
+ err = rangeError(fnParseFloat, s)
+ }
+ return f, n, err
+}
+
+// ParseFloat converts the string s to a floating-point number
+// with the precision specified by bitSize: 32 for float32, or 64 for float64.
+// When bitSize=32, the result still has type float64, but it will be
+// convertible to float32 without changing its value.
+//
+// ParseFloat accepts decimal and hexadecimal floating-point numbers
+// as defined by the Go syntax for [floating-point literals].
+// If s is well-formed and near a valid floating-point number,
+// ParseFloat returns the nearest floating-point number rounded
+// using IEEE754 unbiased rounding.
+// (Parsing a hexadecimal floating-point value only rounds when
+// there are more bits in the hexadecimal representation than
+// will fit in the mantissa.)
+//
+// The errors that ParseFloat returns have concrete type *NumError
+// and include err.Num = s.
+//
+// If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
+//
+// If s is syntactically well-formed but is more than 1/2 ULP
+// away from the largest floating point number of the given size,
+// ParseFloat returns f = ±Inf, err.Err = ErrRange.
+//
+// ParseFloat recognizes the string "NaN", and the (possibly signed) strings "Inf" and "Infinity"
+// as their respective special floating point values. It ignores case when matching.
+//
+// [floating-point literals]: https://go.dev/ref/spec#Floating-point_literals
+func ParseFloat(s string, bitSize int) (float64, error) {
+ f, n, err := parseFloatPrefix(s, bitSize)
+ if n != len(s) && (err == nil || err.(*NumError).Err != ErrSyntax) {
+ return 0, syntaxError(fnParseFloat, s)
+ }
+ return f, err
+}
+
+func parseFloatPrefix(s string, bitSize int) (float64, int, error) {
+ if bitSize == 32 {
+ f, n, err := atof32(s)
+ return float64(f), n, err
+ }
+ return atof64(s)
+}
diff --git a/src/strconv/atof_test.go b/src/strconv/atof_test.go
new file mode 100644
index 0000000..7b287b4
--- /dev/null
+++ b/src/strconv/atof_test.go
@@ -0,0 +1,754 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ "math"
+ "math/rand"
+ "reflect"
+ . "strconv"
+ "strings"
+ "sync"
+ "testing"
+)
+
+type atofTest struct {
+ in string
+ out string
+ err error
+}
+
+var atoftests = []atofTest{
+ {"", "0", ErrSyntax},
+ {"1", "1", nil},
+ {"+1", "1", nil},
+ {"1x", "0", ErrSyntax},
+ {"1.1.", "0", ErrSyntax},
+ {"1e23", "1e+23", nil},
+ {"1E23", "1e+23", nil},
+ {"100000000000000000000000", "1e+23", nil},
+ {"1e-100", "1e-100", nil},
+ {"123456700", "1.234567e+08", nil},
+ {"99999999999999974834176", "9.999999999999997e+22", nil},
+ {"100000000000000000000001", "1.0000000000000001e+23", nil},
+ {"100000000000000008388608", "1.0000000000000001e+23", nil},
+ {"100000000000000016777215", "1.0000000000000001e+23", nil},
+ {"100000000000000016777216", "1.0000000000000003e+23", nil},
+ {"-1", "-1", nil},
+ {"-0.1", "-0.1", nil},
+ {"-0", "-0", nil},
+ {"1e-20", "1e-20", nil},
+ {"625e-3", "0.625", nil},
+
+ // Hexadecimal floating-point.
+ {"0x1p0", "1", nil},
+ {"0x1p1", "2", nil},
+ {"0x1p-1", "0.5", nil},
+ {"0x1ep-1", "15", nil},
+ {"-0x1ep-1", "-15", nil},
+ {"-0x1_ep-1", "-15", nil},
+ {"0x1p-200", "6.223015277861142e-61", nil},
+ {"0x1p200", "1.6069380442589903e+60", nil},
+ {"0x1fFe2.p0", "131042", nil},
+ {"0x1fFe2.P0", "131042", nil},
+ {"-0x2p3", "-16", nil},
+ {"0x0.fp4", "15", nil},
+ {"0x0.fp0", "0.9375", nil},
+ {"0x1e2", "0", ErrSyntax},
+ {"1p2", "0", ErrSyntax},
+
+ // zeros
+ {"0", "0", nil},
+ {"0e0", "0", nil},
+ {"-0e0", "-0", nil},
+ {"+0e0", "0", nil},
+ {"0e-0", "0", nil},
+ {"-0e-0", "-0", nil},
+ {"+0e-0", "0", nil},
+ {"0e+0", "0", nil},
+ {"-0e+0", "-0", nil},
+ {"+0e+0", "0", nil},
+ {"0e+01234567890123456789", "0", nil},
+ {"0.00e-01234567890123456789", "0", nil},
+ {"-0e+01234567890123456789", "-0", nil},
+ {"-0.00e-01234567890123456789", "-0", nil},
+ {"0x0p+01234567890123456789", "0", nil},
+ {"0x0.00p-01234567890123456789", "0", nil},
+ {"-0x0p+01234567890123456789", "-0", nil},
+ {"-0x0.00p-01234567890123456789", "-0", nil},
+
+ {"0e291", "0", nil}, // issue 15364
+ {"0e292", "0", nil}, // issue 15364
+ {"0e347", "0", nil}, // issue 15364
+ {"0e348", "0", nil}, // issue 15364
+ {"-0e291", "-0", nil},
+ {"-0e292", "-0", nil},
+ {"-0e347", "-0", nil},
+ {"-0e348", "-0", nil},
+ {"0x0p126", "0", nil},
+ {"0x0p127", "0", nil},
+ {"0x0p128", "0", nil},
+ {"0x0p129", "0", nil},
+ {"0x0p130", "0", nil},
+ {"0x0p1022", "0", nil},
+ {"0x0p1023", "0", nil},
+ {"0x0p1024", "0", nil},
+ {"0x0p1025", "0", nil},
+ {"0x0p1026", "0", nil},
+ {"-0x0p126", "-0", nil},
+ {"-0x0p127", "-0", nil},
+ {"-0x0p128", "-0", nil},
+ {"-0x0p129", "-0", nil},
+ {"-0x0p130", "-0", nil},
+ {"-0x0p1022", "-0", nil},
+ {"-0x0p1023", "-0", nil},
+ {"-0x0p1024", "-0", nil},
+ {"-0x0p1025", "-0", nil},
+ {"-0x0p1026", "-0", nil},
+
+ // NaNs
+ {"nan", "NaN", nil},
+ {"NaN", "NaN", nil},
+ {"NAN", "NaN", nil},
+
+ // Infs
+ {"inf", "+Inf", nil},
+ {"-Inf", "-Inf", nil},
+ {"+INF", "+Inf", nil},
+ {"-Infinity", "-Inf", nil},
+ {"+INFINITY", "+Inf", nil},
+ {"Infinity", "+Inf", nil},
+
+ // largest float64
+ {"1.7976931348623157e308", "1.7976931348623157e+308", nil},
+ {"-1.7976931348623157e308", "-1.7976931348623157e+308", nil},
+ {"0x1.fffffffffffffp1023", "1.7976931348623157e+308", nil},
+ {"-0x1.fffffffffffffp1023", "-1.7976931348623157e+308", nil},
+ {"0x1fffffffffffffp+971", "1.7976931348623157e+308", nil},
+ {"-0x1fffffffffffffp+971", "-1.7976931348623157e+308", nil},
+ {"0x.1fffffffffffffp1027", "1.7976931348623157e+308", nil},
+ {"-0x.1fffffffffffffp1027", "-1.7976931348623157e+308", nil},
+
+ // next float64 - too large
+ {"1.7976931348623159e308", "+Inf", ErrRange},
+ {"-1.7976931348623159e308", "-Inf", ErrRange},
+ {"0x1p1024", "+Inf", ErrRange},
+ {"-0x1p1024", "-Inf", ErrRange},
+ {"0x2p1023", "+Inf", ErrRange},
+ {"-0x2p1023", "-Inf", ErrRange},
+ {"0x.1p1028", "+Inf", ErrRange},
+ {"-0x.1p1028", "-Inf", ErrRange},
+ {"0x.2p1027", "+Inf", ErrRange},
+ {"-0x.2p1027", "-Inf", ErrRange},
+
+ // the border is ...158079
+ // borderline - okay
+ {"1.7976931348623158e308", "1.7976931348623157e+308", nil},
+ {"-1.7976931348623158e308", "-1.7976931348623157e+308", nil},
+ {"0x1.fffffffffffff7fffp1023", "1.7976931348623157e+308", nil},
+ {"-0x1.fffffffffffff7fffp1023", "-1.7976931348623157e+308", nil},
+ // borderline - too large
+ {"1.797693134862315808e308", "+Inf", ErrRange},
+ {"-1.797693134862315808e308", "-Inf", ErrRange},
+ {"0x1.fffffffffffff8p1023", "+Inf", ErrRange},
+ {"-0x1.fffffffffffff8p1023", "-Inf", ErrRange},
+ {"0x1fffffffffffff.8p+971", "+Inf", ErrRange},
+ {"-0x1fffffffffffff8p+967", "-Inf", ErrRange},
+ {"0x.1fffffffffffff8p1027", "+Inf", ErrRange},
+ {"-0x.1fffffffffffff9p1027", "-Inf", ErrRange},
+
+ // a little too large
+ {"1e308", "1e+308", nil},
+ {"2e308", "+Inf", ErrRange},
+ {"1e309", "+Inf", ErrRange},
+ {"0x1p1025", "+Inf", ErrRange},
+
+ // way too large
+ {"1e310", "+Inf", ErrRange},
+ {"-1e310", "-Inf", ErrRange},
+ {"1e400", "+Inf", ErrRange},
+ {"-1e400", "-Inf", ErrRange},
+ {"1e400000", "+Inf", ErrRange},
+ {"-1e400000", "-Inf", ErrRange},
+ {"0x1p1030", "+Inf", ErrRange},
+ {"0x1p2000", "+Inf", ErrRange},
+ {"0x1p2000000000", "+Inf", ErrRange},
+ {"-0x1p1030", "-Inf", ErrRange},
+ {"-0x1p2000", "-Inf", ErrRange},
+ {"-0x1p2000000000", "-Inf", ErrRange},
+
+ // denormalized
+ {"1e-305", "1e-305", nil},
+ {"1e-306", "1e-306", nil},
+ {"1e-307", "1e-307", nil},
+ {"1e-308", "1e-308", nil},
+ {"1e-309", "1e-309", nil},
+ {"1e-310", "1e-310", nil},
+ {"1e-322", "1e-322", nil},
+ // smallest denormal
+ {"5e-324", "5e-324", nil},
+ {"4e-324", "5e-324", nil},
+ {"3e-324", "5e-324", nil},
+ // too small
+ {"2e-324", "0", nil},
+ // way too small
+ {"1e-350", "0", nil},
+ {"1e-400000", "0", nil},
+
+ // Near denormals and denormals.
+ {"0x2.00000000000000p-1010", "1.8227805048890994e-304", nil}, // 0x00e0000000000000
+ {"0x1.fffffffffffff0p-1010", "1.8227805048890992e-304", nil}, // 0x00dfffffffffffff
+ {"0x1.fffffffffffff7p-1010", "1.8227805048890992e-304", nil}, // rounded down
+ {"0x1.fffffffffffff8p-1010", "1.8227805048890994e-304", nil}, // rounded up
+ {"0x1.fffffffffffff9p-1010", "1.8227805048890994e-304", nil}, // rounded up
+
+ {"0x2.00000000000000p-1022", "4.450147717014403e-308", nil}, // 0x0020000000000000
+ {"0x1.fffffffffffff0p-1022", "4.4501477170144023e-308", nil}, // 0x001fffffffffffff
+ {"0x1.fffffffffffff7p-1022", "4.4501477170144023e-308", nil}, // rounded down
+ {"0x1.fffffffffffff8p-1022", "4.450147717014403e-308", nil}, // rounded up
+ {"0x1.fffffffffffff9p-1022", "4.450147717014403e-308", nil}, // rounded up
+
+ {"0x1.00000000000000p-1022", "2.2250738585072014e-308", nil}, // 0x0010000000000000
+ {"0x0.fffffffffffff0p-1022", "2.225073858507201e-308", nil}, // 0x000fffffffffffff
+ {"0x0.ffffffffffffe0p-1022", "2.2250738585072004e-308", nil}, // 0x000ffffffffffffe
+ {"0x0.ffffffffffffe7p-1022", "2.2250738585072004e-308", nil}, // rounded down
+ {"0x1.ffffffffffffe8p-1023", "2.225073858507201e-308", nil}, // rounded up
+ {"0x1.ffffffffffffe9p-1023", "2.225073858507201e-308", nil}, // rounded up
+
+ {"0x0.00000003fffff0p-1022", "2.072261e-317", nil}, // 0x00000000003fffff
+ {"0x0.00000003456780p-1022", "1.694649e-317", nil}, // 0x0000000000345678
+ {"0x0.00000003456787p-1022", "1.694649e-317", nil}, // rounded down
+ {"0x0.00000003456788p-1022", "1.694649e-317", nil}, // rounded down (half to even)
+ {"0x0.00000003456790p-1022", "1.6946496e-317", nil}, // 0x0000000000345679
+ {"0x0.00000003456789p-1022", "1.6946496e-317", nil}, // rounded up
+
+ {"0x0.0000000345678800000000000000000000000001p-1022", "1.6946496e-317", nil}, // rounded up
+
+ {"0x0.000000000000f0p-1022", "7.4e-323", nil}, // 0x000000000000000f
+ {"0x0.00000000000060p-1022", "3e-323", nil}, // 0x0000000000000006
+ {"0x0.00000000000058p-1022", "3e-323", nil}, // rounded up
+ {"0x0.00000000000057p-1022", "2.5e-323", nil}, // rounded down
+ {"0x0.00000000000050p-1022", "2.5e-323", nil}, // 0x0000000000000005
+
+ {"0x0.00000000000010p-1022", "5e-324", nil}, // 0x0000000000000001
+ {"0x0.000000000000081p-1022", "5e-324", nil}, // rounded up
+ {"0x0.00000000000008p-1022", "0", nil}, // rounded down
+ {"0x0.00000000000007fp-1022", "0", nil}, // rounded down
+
+ // try to overflow exponent
+ {"1e-4294967296", "0", nil},
+ {"1e+4294967296", "+Inf", ErrRange},
+ {"1e-18446744073709551616", "0", nil},
+ {"1e+18446744073709551616", "+Inf", ErrRange},
+ {"0x1p-4294967296", "0", nil},
+ {"0x1p+4294967296", "+Inf", ErrRange},
+ {"0x1p-18446744073709551616", "0", nil},
+ {"0x1p+18446744073709551616", "+Inf", ErrRange},
+
+ // Parse errors
+ {"1e", "0", ErrSyntax},
+ {"1e-", "0", ErrSyntax},
+ {".e-1", "0", ErrSyntax},
+ {"1\x00.2", "0", ErrSyntax},
+ {"0x", "0", ErrSyntax},
+ {"0x.", "0", ErrSyntax},
+ {"0x1", "0", ErrSyntax},
+ {"0x.1", "0", ErrSyntax},
+ {"0x1p", "0", ErrSyntax},
+ {"0x.1p", "0", ErrSyntax},
+ {"0x1p+", "0", ErrSyntax},
+ {"0x.1p+", "0", ErrSyntax},
+ {"0x1p-", "0", ErrSyntax},
+ {"0x.1p-", "0", ErrSyntax},
+ {"0x1p+2", "4", nil},
+ {"0x.1p+2", "0.25", nil},
+ {"0x1p-2", "0.25", nil},
+ {"0x.1p-2", "0.015625", nil},
+
+ // https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
+ {"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
+ // https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
+ {"2.2250738585072011e-308", "2.225073858507201e-308", nil},
+
+ // A very large number (initially wrongly parsed by the fast algorithm).
+ {"4.630813248087435e+307", "4.630813248087435e+307", nil},
+
+ // A different kind of very large number.
+ {"22.222222222222222", "22.22222222222222", nil},
+ {"2." + strings.Repeat("2", 4000) + "e+1", "22.22222222222222", nil},
+ {"0x1.1111111111111p222", "7.18931911124017e+66", nil},
+ {"0x2.2222222222222p221", "7.18931911124017e+66", nil},
+ {"0x2." + strings.Repeat("2", 4000) + "p221", "7.18931911124017e+66", nil},
+
+ // Exactly halfway between 1 and math.Nextafter(1, 2).
+ // Round to even (down).
+ {"1.00000000000000011102230246251565404236316680908203125", "1", nil},
+ {"0x1.00000000000008p0", "1", nil},
+ // Slightly lower; still round down.
+ {"1.00000000000000011102230246251565404236316680908203124", "1", nil},
+ {"0x1.00000000000007Fp0", "1", nil},
+ // Slightly higher; round up.
+ {"1.00000000000000011102230246251565404236316680908203126", "1.0000000000000002", nil},
+ {"0x1.000000000000081p0", "1.0000000000000002", nil},
+ {"0x1.00000000000009p0", "1.0000000000000002", nil},
+ // Slightly higher, but you have to read all the way to the end.
+ {"1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1", "1.0000000000000002", nil},
+ {"0x1.00000000000008" + strings.Repeat("0", 10000) + "1p0", "1.0000000000000002", nil},
+
+ // Halfway between x := math.Nextafter(1, 2) and math.Nextafter(x, 2)
+ // Round to even (up).
+ {"1.00000000000000033306690738754696212708950042724609375", "1.0000000000000004", nil},
+ {"0x1.00000000000018p0", "1.0000000000000004", nil},
+
+ // Halfway between 1090544144181609278303144771584 and 1090544144181609419040633126912
+ // (15497564393479157p+46, should round to even 15497564393479156p+46, issue 36657)
+ {"1090544144181609348671888949248", "1.0905441441816093e+30", nil},
+ // slightly above, rounds up
+ {"1090544144181609348835077142190", "1.0905441441816094e+30", nil},
+
+ // Underscores.
+ {"1_23.50_0_0e+1_2", "1.235e+14", nil},
+ {"-_123.5e+12", "0", ErrSyntax},
+ {"+_123.5e+12", "0", ErrSyntax},
+ {"_123.5e+12", "0", ErrSyntax},
+ {"1__23.5e+12", "0", ErrSyntax},
+ {"123_.5e+12", "0", ErrSyntax},
+ {"123._5e+12", "0", ErrSyntax},
+ {"123.5_e+12", "0", ErrSyntax},
+ {"123.5__0e+12", "0", ErrSyntax},
+ {"123.5e_+12", "0", ErrSyntax},
+ {"123.5e+_12", "0", ErrSyntax},
+ {"123.5e_-12", "0", ErrSyntax},
+ {"123.5e-_12", "0", ErrSyntax},
+ {"123.5e+1__2", "0", ErrSyntax},
+ {"123.5e+12_", "0", ErrSyntax},
+
+ {"0x_1_2.3_4_5p+1_2", "74565", nil},
+ {"-_0x12.345p+12", "0", ErrSyntax},
+ {"+_0x12.345p+12", "0", ErrSyntax},
+ {"_0x12.345p+12", "0", ErrSyntax},
+ {"0x__12.345p+12", "0", ErrSyntax},
+ {"0x1__2.345p+12", "0", ErrSyntax},
+ {"0x12_.345p+12", "0", ErrSyntax},
+ {"0x12._345p+12", "0", ErrSyntax},
+ {"0x12.3__45p+12", "0", ErrSyntax},
+ {"0x12.345_p+12", "0", ErrSyntax},
+ {"0x12.345p_+12", "0", ErrSyntax},
+ {"0x12.345p+_12", "0", ErrSyntax},
+ {"0x12.345p_-12", "0", ErrSyntax},
+ {"0x12.345p-_12", "0", ErrSyntax},
+ {"0x12.345p+1__2", "0", ErrSyntax},
+ {"0x12.345p+12_", "0", ErrSyntax},
+
+ {"1e100x", "0", ErrSyntax},
+ {"1e1000x", "0", ErrSyntax},
+}
+
+var atof32tests = []atofTest{
+ // Hex
+ {"0x1p-100", "7.888609e-31", nil},
+ {"0x1p100", "1.2676506e+30", nil},
+
+ // Exactly halfway between 1 and the next float32.
+ // Round to even (down).
+ {"1.000000059604644775390625", "1", nil},
+ {"0x1.000001p0", "1", nil},
+ // Slightly lower.
+ {"1.000000059604644775390624", "1", nil},
+ {"0x1.0000008p0", "1", nil},
+ {"0x1.000000fp0", "1", nil},
+ // Slightly higher.
+ {"1.000000059604644775390626", "1.0000001", nil},
+ {"0x1.000002p0", "1.0000001", nil},
+ {"0x1.0000018p0", "1.0000001", nil},
+ {"0x1.0000011p0", "1.0000001", nil},
+ // Slightly higher, but you have to read all the way to the end.
+ {"1.000000059604644775390625" + strings.Repeat("0", 10000) + "1", "1.0000001", nil},
+ {"0x1.000001" + strings.Repeat("0", 10000) + "1p0", "1.0000001", nil},
+
+ // largest float32: (1<<128) * (1 - 2^-24)
+ {"340282346638528859811704183484516925440", "3.4028235e+38", nil},
+ {"-340282346638528859811704183484516925440", "-3.4028235e+38", nil},
+ {"0x.ffffffp128", "3.4028235e+38", nil},
+ {"-340282346638528859811704183484516925440", "-3.4028235e+38", nil},
+ {"-0x.ffffffp128", "-3.4028235e+38", nil},
+ // next float32 - too large
+ {"3.4028236e38", "+Inf", ErrRange},
+ {"-3.4028236e38", "-Inf", ErrRange},
+ {"0x1.0p128", "+Inf", ErrRange},
+ {"-0x1.0p128", "-Inf", ErrRange},
+ // the border is 3.40282356779...e+38
+ // borderline - okay
+ {"3.402823567e38", "3.4028235e+38", nil},
+ {"-3.402823567e38", "-3.4028235e+38", nil},
+ {"0x.ffffff7fp128", "3.4028235e+38", nil},
+ {"-0x.ffffff7fp128", "-3.4028235e+38", nil},
+ // borderline - too large
+ {"3.4028235678e38", "+Inf", ErrRange},
+ {"-3.4028235678e38", "-Inf", ErrRange},
+ {"0x.ffffff8p128", "+Inf", ErrRange},
+ {"-0x.ffffff8p128", "-Inf", ErrRange},
+
+ // Denormals: less than 2^-126
+ {"1e-38", "1e-38", nil},
+ {"1e-39", "1e-39", nil},
+ {"1e-40", "1e-40", nil},
+ {"1e-41", "1e-41", nil},
+ {"1e-42", "1e-42", nil},
+ {"1e-43", "1e-43", nil},
+ {"1e-44", "1e-44", nil},
+ {"6e-45", "6e-45", nil}, // 4p-149 = 5.6e-45
+ {"5e-45", "6e-45", nil},
+
+ // Smallest denormal
+ {"1e-45", "1e-45", nil}, // 1p-149 = 1.4e-45
+ {"2e-45", "1e-45", nil},
+ {"3e-45", "3e-45", nil},
+
+ // Near denormals and denormals.
+ {"0x0.89aBcDp-125", "1.2643093e-38", nil}, // 0x0089abcd
+ {"0x0.8000000p-125", "1.1754944e-38", nil}, // 0x00800000
+ {"0x0.1234560p-125", "1.671814e-39", nil}, // 0x00123456
+ {"0x0.1234567p-125", "1.671814e-39", nil}, // rounded down
+ {"0x0.1234568p-125", "1.671814e-39", nil}, // rounded down
+ {"0x0.1234569p-125", "1.671815e-39", nil}, // rounded up
+ {"0x0.1234570p-125", "1.671815e-39", nil}, // 0x00123457
+ {"0x0.0000010p-125", "1e-45", nil}, // 0x00000001
+ {"0x0.00000081p-125", "1e-45", nil}, // rounded up
+ {"0x0.0000008p-125", "0", nil}, // rounded down
+ {"0x0.0000007p-125", "0", nil}, // rounded down
+
+ // 2^92 = 8388608p+69 = 4951760157141521099596496896 (4.9517602e27)
+ // is an exact power of two that needs 8 decimal digits to be correctly
+ // parsed back.
+ // The float32 before is 16777215p+68 = 4.95175986e+27
+ // The halfway is 4.951760009. A bad algorithm that thinks the previous
+ // float32 is 8388607p+69 will shorten incorrectly to 4.95176e+27.
+ {"4951760157141521099596496896", "4.9517602e+27", nil},
+}
+
+type atofSimpleTest struct {
+ x float64
+ s string
+}
+
+var (
+ atofOnce sync.Once
+ atofRandomTests []atofSimpleTest
+ benchmarksRandomBits [1024]string
+ benchmarksRandomNormal [1024]string
+)
+
+func initAtof() {
+ atofOnce.Do(initAtofOnce)
+}
+
+func initAtofOnce() {
+ // The atof routines return NumErrors wrapping
+ // the error and the string. Convert the table above.
+ for i := range atoftests {
+ test := &atoftests[i]
+ if test.err != nil {
+ test.err = &NumError{"ParseFloat", test.in, test.err}
+ }
+ }
+ for i := range atof32tests {
+ test := &atof32tests[i]
+ if test.err != nil {
+ test.err = &NumError{"ParseFloat", test.in, test.err}
+ }
+ }
+
+ // Generate random inputs for tests and benchmarks
+ if testing.Short() {
+ atofRandomTests = make([]atofSimpleTest, 100)
+ } else {
+ atofRandomTests = make([]atofSimpleTest, 10000)
+ }
+ for i := range atofRandomTests {
+ n := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
+ x := math.Float64frombits(n)
+ s := FormatFloat(x, 'g', -1, 64)
+ atofRandomTests[i] = atofSimpleTest{x, s}
+ }
+
+ for i := range benchmarksRandomBits {
+ bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
+ x := math.Float64frombits(bits)
+ benchmarksRandomBits[i] = FormatFloat(x, 'g', -1, 64)
+ }
+
+ for i := range benchmarksRandomNormal {
+ x := rand.NormFloat64()
+ benchmarksRandomNormal[i] = FormatFloat(x, 'g', -1, 64)
+ }
+}
+
+func TestParseFloatPrefix(t *testing.T) {
+ for i := range atoftests {
+ test := &atoftests[i]
+ if test.err != nil {
+ continue
+ }
+ // Adding characters that do not extend a number should not invalidate it.
+ // Test a few. The "i" and "init" cases test that we accept "infi", "infinit"
+ // correctly as "inf" with suffix.
+ for _, suffix := range []string{" ", "q", "+", "-", "<", "=", ">", "(", ")", "i", "init"} {
+ in := test.in + suffix
+ _, n, err := ParseFloatPrefix(in, 64)
+ if err != nil {
+ t.Errorf("ParseFloatPrefix(%q, 64): err = %v; want no error", in, err)
+ }
+ if n != len(test.in) {
+ t.Errorf("ParseFloatPrefix(%q, 64): n = %d; want %d", in, n, len(test.in))
+ }
+ }
+ }
+}
+
+func testAtof(t *testing.T, opt bool) {
+ initAtof()
+ oldopt := SetOptimize(opt)
+ for i := 0; i < len(atoftests); i++ {
+ test := &atoftests[i]
+ out, err := ParseFloat(test.in, 64)
+ outs := FormatFloat(out, 'g', -1, 64)
+ if outs != test.out || !reflect.DeepEqual(err, test.err) {
+ t.Errorf("ParseFloat(%v, 64) = %v, %v want %v, %v",
+ test.in, out, err, test.out, test.err)
+ }
+
+ if float64(float32(out)) == out {
+ out, err := ParseFloat(test.in, 32)
+ out32 := float32(out)
+ if float64(out32) != out {
+ t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32))
+ continue
+ }
+ outs := FormatFloat(float64(out32), 'g', -1, 32)
+ if outs != test.out || !reflect.DeepEqual(err, test.err) {
+ t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v",
+ test.in, out32, err, test.out, test.err, out)
+ }
+ }
+ }
+ for _, test := range atof32tests {
+ out, err := ParseFloat(test.in, 32)
+ out32 := float32(out)
+ if float64(out32) != out {
+ t.Errorf("ParseFloat(%v, 32) = %v, not a float32 (closest is %v)", test.in, out, float64(out32))
+ continue
+ }
+ outs := FormatFloat(float64(out32), 'g', -1, 32)
+ if outs != test.out || !reflect.DeepEqual(err, test.err) {
+ t.Errorf("ParseFloat(%v, 32) = %v, %v want %v, %v # %v",
+ test.in, out32, err, test.out, test.err, out)
+ }
+ }
+ SetOptimize(oldopt)
+}
+
+func TestAtof(t *testing.T) { testAtof(t, true) }
+
+func TestAtofSlow(t *testing.T) { testAtof(t, false) }
+
+func TestAtofRandom(t *testing.T) {
+ initAtof()
+ for _, test := range atofRandomTests {
+ x, _ := ParseFloat(test.s, 64)
+ switch {
+ default:
+ t.Errorf("number %s badly parsed as %b (expected %b)", test.s, x, test.x)
+ case x == test.x:
+ case math.IsNaN(test.x) && math.IsNaN(x):
+ }
+ }
+ t.Logf("tested %d random numbers", len(atofRandomTests))
+}
+
+var roundTripCases = []struct {
+ f float64
+ s string
+}{
+ // Issue 2917.
+ // This test will break the optimized conversion if the
+ // FPU is using 80-bit registers instead of 64-bit registers,
+ // usually because the operating system initialized the
+ // thread with 80-bit precision and the Go runtime didn't
+ // fix the FP control word.
+ {8865794286000691 << 39, "4.87402195346389e+27"},
+ {8865794286000692 << 39, "4.8740219534638903e+27"},
+}
+
+func TestRoundTrip(t *testing.T) {
+ for _, tt := range roundTripCases {
+ old := SetOptimize(false)
+ s := FormatFloat(tt.f, 'g', -1, 64)
+ if s != tt.s {
+ t.Errorf("no-opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s)
+ }
+ f, err := ParseFloat(tt.s, 64)
+ if f != tt.f || err != nil {
+ t.Errorf("no-opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f)
+ }
+ SetOptimize(true)
+ s = FormatFloat(tt.f, 'g', -1, 64)
+ if s != tt.s {
+ t.Errorf("opt FormatFloat(%b) = %s, want %s", tt.f, s, tt.s)
+ }
+ f, err = ParseFloat(tt.s, 64)
+ if f != tt.f || err != nil {
+ t.Errorf("opt ParseFloat(%s) = %b, %v want %b, nil", tt.s, f, err, tt.f)
+ }
+ SetOptimize(old)
+ }
+}
+
+// TestRoundTrip32 tries a fraction of all finite positive float32 values.
+func TestRoundTrip32(t *testing.T) {
+ step := uint32(997)
+ if testing.Short() {
+ step = 99991
+ }
+ count := 0
+ for i := uint32(0); i < 0xff<<23; i += step {
+ f := math.Float32frombits(i)
+ if i&1 == 1 {
+ f = -f // negative
+ }
+ s := FormatFloat(float64(f), 'g', -1, 32)
+
+ parsed, err := ParseFloat(s, 32)
+ parsed32 := float32(parsed)
+ switch {
+ case err != nil:
+ t.Errorf("ParseFloat(%q, 32) gave error %s", s, err)
+ case float64(parsed32) != parsed:
+ t.Errorf("ParseFloat(%q, 32) = %v, not a float32 (nearest is %v)", s, parsed, parsed32)
+ case parsed32 != f:
+ t.Errorf("ParseFloat(%q, 32) = %b (expected %b)", s, parsed32, f)
+ }
+ count++
+ }
+ t.Logf("tested %d float32's", count)
+}
+
+// Issue 42297: a lot of code in the wild accidentally calls ParseFloat(s, 10)
+// or ParseFloat(s, 0), so allow bitSize values other than 32 and 64.
+func TestParseFloatIncorrectBitSize(t *testing.T) {
+ const s = "1.5e308"
+ const want = 1.5e308
+
+ for _, bitSize := range []int{0, 10, 100, 128} {
+ f, err := ParseFloat(s, bitSize)
+ if err != nil {
+ t.Fatalf("ParseFloat(%q, %d) gave error %s", s, bitSize, err)
+ }
+ if f != want {
+ t.Fatalf("ParseFloat(%q, %d) = %g (expected %g)", s, bitSize, f, want)
+ }
+ }
+}
+
+func BenchmarkAtof64Decimal(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ParseFloat("33909", 64)
+ }
+}
+
+func BenchmarkAtof64Float(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ParseFloat("339.7784", 64)
+ }
+}
+
+func BenchmarkAtof64FloatExp(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ParseFloat("-5.09e75", 64)
+ }
+}
+
+func BenchmarkAtof64Big(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ParseFloat("123456789123456789123456789", 64)
+ }
+}
+
+func BenchmarkAtof64RandomBits(b *testing.B) {
+ initAtof()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ ParseFloat(benchmarksRandomBits[i%1024], 64)
+ }
+}
+
+func BenchmarkAtof64RandomFloats(b *testing.B) {
+ initAtof()
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ ParseFloat(benchmarksRandomNormal[i%1024], 64)
+ }
+}
+
+func BenchmarkAtof64RandomLongFloats(b *testing.B) {
+ initAtof()
+ samples := make([]string, len(atofRandomTests))
+ for i, t := range atofRandomTests {
+ samples[i] = FormatFloat(t.x, 'g', 20, 64)
+ }
+ b.ResetTimer()
+ idx := 0
+ for i := 0; i < b.N; i++ {
+ ParseFloat(samples[idx], 64)
+ idx++
+ if idx == len(samples) {
+ idx = 0
+ }
+ }
+}
+
+func BenchmarkAtof32Decimal(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ParseFloat("33909", 32)
+ }
+}
+
+func BenchmarkAtof32Float(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ParseFloat("339.778", 32)
+ }
+}
+
+func BenchmarkAtof32FloatExp(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ ParseFloat("12.3456e32", 32)
+ }
+}
+
+func BenchmarkAtof32Random(b *testing.B) {
+ n := uint32(997)
+ var float32strings [4096]string
+ for i := range float32strings {
+ n = (99991*n + 42) % (0xff << 23)
+ float32strings[i] = FormatFloat(float64(math.Float32frombits(n)), 'g', -1, 32)
+ }
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ ParseFloat(float32strings[i%4096], 32)
+ }
+}
+
+func BenchmarkAtof32RandomLong(b *testing.B) {
+ n := uint32(997)
+ var float32strings [4096]string
+ for i := range float32strings {
+ n = (99991*n + 42) % (0xff << 23)
+ float32strings[i] = FormatFloat(float64(math.Float32frombits(n)), 'g', 20, 32)
+ }
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ ParseFloat(float32strings[i%4096], 32)
+ }
+}
diff --git a/src/strconv/atoi.go b/src/strconv/atoi.go
new file mode 100644
index 0000000..520d826
--- /dev/null
+++ b/src/strconv/atoi.go
@@ -0,0 +1,332 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv
+
+import "errors"
+
+// lower(c) is a lower-case letter if and only if
+// c is either that lower-case letter or the equivalent upper-case letter.
+// Instead of writing c == 'x' || c == 'X' one can write lower(c) == 'x'.
+// Note that lower of non-letters can produce other non-letters.
+func lower(c byte) byte {
+ return c | ('x' - 'X')
+}
+
+// ErrRange indicates that a value is out of range for the target type.
+var ErrRange = errors.New("value out of range")
+
+// ErrSyntax indicates that a value does not have the right syntax for the target type.
+var ErrSyntax = errors.New("invalid syntax")
+
+// A NumError records a failed conversion.
+type NumError struct {
+ Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat, ParseComplex)
+ Num string // the input
+ Err error // the reason the conversion failed (e.g. ErrRange, ErrSyntax, etc.)
+}
+
+func (e *NumError) Error() string {
+ return "strconv." + e.Func + ": " + "parsing " + Quote(e.Num) + ": " + e.Err.Error()
+}
+
+func (e *NumError) Unwrap() error { return e.Err }
+
+// cloneString returns a string copy of x.
+//
+// All ParseXXX functions allow the input string to escape to the error value.
+// This hurts strconv.ParseXXX(string(b)) calls where b is []byte since
+// the conversion from []byte must allocate a string on the heap.
+// If we assume errors are infrequent, then we can avoid escaping the input
+// back to the output by copying it first. This allows the compiler to call
+// strconv.ParseXXX without a heap allocation for most []byte to string
+// conversions, since it can now prove that the string cannot escape Parse.
+//
+// TODO: Use strings.Clone instead? However, we cannot depend on "strings"
+// since it incurs a transitive dependency on "unicode".
+// Either move strings.Clone to an internal/bytealg or make the
+// "strings" to "unicode" dependency lighter (see https://go.dev/issue/54098).
+func cloneString(x string) string { return string([]byte(x)) }
+
+func syntaxError(fn, str string) *NumError {
+ return &NumError{fn, cloneString(str), ErrSyntax}
+}
+
+func rangeError(fn, str string) *NumError {
+ return &NumError{fn, cloneString(str), ErrRange}
+}
+
+func baseError(fn, str string, base int) *NumError {
+ return &NumError{fn, cloneString(str), errors.New("invalid base " + Itoa(base))}
+}
+
+func bitSizeError(fn, str string, bitSize int) *NumError {
+ return &NumError{fn, cloneString(str), errors.New("invalid bit size " + Itoa(bitSize))}
+}
+
+const intSize = 32 << (^uint(0) >> 63)
+
+// IntSize is the size in bits of an int or uint value.
+const IntSize = intSize
+
+const maxUint64 = 1<<64 - 1
+
+// ParseUint is like ParseInt but for unsigned numbers.
+//
+// A sign prefix is not permitted.
+func ParseUint(s string, base int, bitSize int) (uint64, error) {
+ const fnParseUint = "ParseUint"
+
+ if s == "" {
+ return 0, syntaxError(fnParseUint, s)
+ }
+
+ base0 := base == 0
+
+ s0 := s
+ switch {
+ case 2 <= base && base <= 36:
+ // valid base; nothing to do
+
+ case base == 0:
+ // Look for octal, hex prefix.
+ base = 10
+ if s[0] == '0' {
+ switch {
+ case len(s) >= 3 && lower(s[1]) == 'b':
+ base = 2
+ s = s[2:]
+ case len(s) >= 3 && lower(s[1]) == 'o':
+ base = 8
+ s = s[2:]
+ case len(s) >= 3 && lower(s[1]) == 'x':
+ base = 16
+ s = s[2:]
+ default:
+ base = 8
+ s = s[1:]
+ }
+ }
+
+ default:
+ return 0, baseError(fnParseUint, s0, base)
+ }
+
+ if bitSize == 0 {
+ bitSize = IntSize
+ } else if bitSize < 0 || bitSize > 64 {
+ return 0, bitSizeError(fnParseUint, s0, bitSize)
+ }
+
+ // Cutoff is the smallest number such that cutoff*base > maxUint64.
+ // Use compile-time constants for common cases.
+ var cutoff uint64
+ switch base {
+ case 10:
+ cutoff = maxUint64/10 + 1
+ case 16:
+ cutoff = maxUint64/16 + 1
+ default:
+ cutoff = maxUint64/uint64(base) + 1
+ }
+
+ maxVal := uint64(1)<<uint(bitSize) - 1
+
+ underscores := false
+ var n uint64
+ for _, c := range []byte(s) {
+ var d byte
+ switch {
+ case c == '_' && base0:
+ underscores = true
+ continue
+ case '0' <= c && c <= '9':
+ d = c - '0'
+ case 'a' <= lower(c) && lower(c) <= 'z':
+ d = lower(c) - 'a' + 10
+ default:
+ return 0, syntaxError(fnParseUint, s0)
+ }
+
+ if d >= byte(base) {
+ return 0, syntaxError(fnParseUint, s0)
+ }
+
+ if n >= cutoff {
+ // n*base overflows
+ return maxVal, rangeError(fnParseUint, s0)
+ }
+ n *= uint64(base)
+
+ n1 := n + uint64(d)
+ if n1 < n || n1 > maxVal {
+ // n+d overflows
+ return maxVal, rangeError(fnParseUint, s0)
+ }
+ n = n1
+ }
+
+ if underscores && !underscoreOK(s0) {
+ return 0, syntaxError(fnParseUint, s0)
+ }
+
+ return n, nil
+}
+
+// ParseInt interprets a string s in the given base (0, 2 to 36) and
+// bit size (0 to 64) and returns the corresponding value i.
+//
+// The string may begin with a leading sign: "+" or "-".
+//
+// If the base argument is 0, the true base is implied by the string's
+// prefix following the sign (if present): 2 for "0b", 8 for "0" or "0o",
+// 16 for "0x", and 10 otherwise. Also, for argument base 0 only,
+// underscore characters are permitted as defined by the Go syntax for
+// [integer literals].
+//
+// The bitSize argument specifies the integer type
+// that the result must fit into. Bit sizes 0, 8, 16, 32, and 64
+// correspond to int, int8, int16, int32, and int64.
+// If bitSize is below 0 or above 64, an error is returned.
+//
+// The errors that ParseInt returns have concrete type *NumError
+// and include err.Num = s. If s is empty or contains invalid
+// digits, err.Err = ErrSyntax and the returned value is 0;
+// if the value corresponding to s cannot be represented by a
+// signed integer of the given size, err.Err = ErrRange and the
+// returned value is the maximum magnitude integer of the
+// appropriate bitSize and sign.
+//
+// [integer literals]: https://go.dev/ref/spec#Integer_literals
+func ParseInt(s string, base int, bitSize int) (i int64, err error) {
+ const fnParseInt = "ParseInt"
+
+ if s == "" {
+ return 0, syntaxError(fnParseInt, s)
+ }
+
+ // Pick off leading sign.
+ s0 := s
+ neg := false
+ if s[0] == '+' {
+ s = s[1:]
+ } else if s[0] == '-' {
+ neg = true
+ s = s[1:]
+ }
+
+ // Convert unsigned and check range.
+ var un uint64
+ un, err = ParseUint(s, base, bitSize)
+ if err != nil && err.(*NumError).Err != ErrRange {
+ err.(*NumError).Func = fnParseInt
+ err.(*NumError).Num = cloneString(s0)
+ return 0, err
+ }
+
+ if bitSize == 0 {
+ bitSize = IntSize
+ }
+
+ cutoff := uint64(1 << uint(bitSize-1))
+ if !neg && un >= cutoff {
+ return int64(cutoff - 1), rangeError(fnParseInt, s0)
+ }
+ if neg && un > cutoff {
+ return -int64(cutoff), rangeError(fnParseInt, s0)
+ }
+ n := int64(un)
+ if neg {
+ n = -n
+ }
+ return n, nil
+}
+
+// Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
+func Atoi(s string) (int, error) {
+ const fnAtoi = "Atoi"
+
+ sLen := len(s)
+ if intSize == 32 && (0 < sLen && sLen < 10) ||
+ intSize == 64 && (0 < sLen && sLen < 19) {
+ // Fast path for small integers that fit int type.
+ s0 := s
+ if s[0] == '-' || s[0] == '+' {
+ s = s[1:]
+ if len(s) < 1 {
+ return 0, syntaxError(fnAtoi, s0)
+ }
+ }
+
+ n := 0
+ for _, ch := range []byte(s) {
+ ch -= '0'
+ if ch > 9 {
+ return 0, syntaxError(fnAtoi, s0)
+ }
+ n = n*10 + int(ch)
+ }
+ if s0[0] == '-' {
+ n = -n
+ }
+ return n, nil
+ }
+
+ // Slow path for invalid, big, or underscored integers.
+ i64, err := ParseInt(s, 10, 0)
+ if nerr, ok := err.(*NumError); ok {
+ nerr.Func = fnAtoi
+ }
+ return int(i64), err
+}
+
+// underscoreOK reports whether the underscores in s are allowed.
+// Checking them in this one function lets all the parsers skip over them simply.
+// Underscore must appear only between digits or between a base prefix and a digit.
+func underscoreOK(s string) bool {
+ // saw tracks the last character (class) we saw:
+ // ^ for beginning of number,
+ // 0 for a digit or base prefix,
+ // _ for an underscore,
+ // ! for none of the above.
+ saw := '^'
+ i := 0
+
+ // Optional sign.
+ if len(s) >= 1 && (s[0] == '-' || s[0] == '+') {
+ s = s[1:]
+ }
+
+ // Optional base prefix.
+ hex := false
+ if len(s) >= 2 && s[0] == '0' && (lower(s[1]) == 'b' || lower(s[1]) == 'o' || lower(s[1]) == 'x') {
+ i = 2
+ saw = '0' // base prefix counts as a digit for "underscore as digit separator"
+ hex = lower(s[1]) == 'x'
+ }
+
+ // Number proper.
+ for ; i < len(s); i++ {
+ // Digits are always okay.
+ if '0' <= s[i] && s[i] <= '9' || hex && 'a' <= lower(s[i]) && lower(s[i]) <= 'f' {
+ saw = '0'
+ continue
+ }
+ // Underscore must follow digit.
+ if s[i] == '_' {
+ if saw != '0' {
+ return false
+ }
+ saw = '_'
+ continue
+ }
+ // Underscore must also be followed by digit.
+ if saw == '_' {
+ return false
+ }
+ // Saw non-digit, non-underscore.
+ saw = '!'
+ }
+ return saw != '_'
+}
diff --git a/src/strconv/atoi_test.go b/src/strconv/atoi_test.go
new file mode 100644
index 0000000..d7f8f25
--- /dev/null
+++ b/src/strconv/atoi_test.go
@@ -0,0 +1,677 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ "errors"
+ "fmt"
+ "reflect"
+ . "strconv"
+ "testing"
+)
+
+type parseUint64Test struct {
+ in string
+ out uint64
+ err error
+}
+
+var parseUint64Tests = []parseUint64Test{
+ {"", 0, ErrSyntax},
+ {"0", 0, nil},
+ {"1", 1, nil},
+ {"12345", 12345, nil},
+ {"012345", 12345, nil},
+ {"12345x", 0, ErrSyntax},
+ {"98765432100", 98765432100, nil},
+ {"18446744073709551615", 1<<64 - 1, nil},
+ {"18446744073709551616", 1<<64 - 1, ErrRange},
+ {"18446744073709551620", 1<<64 - 1, ErrRange},
+ {"1_2_3_4_5", 0, ErrSyntax}, // base=10 so no underscores allowed
+ {"_12345", 0, ErrSyntax},
+ {"1__2345", 0, ErrSyntax},
+ {"12345_", 0, ErrSyntax},
+ {"-0", 0, ErrSyntax},
+ {"-1", 0, ErrSyntax},
+ {"+1", 0, ErrSyntax},
+}
+
+type parseUint64BaseTest struct {
+ in string
+ base int
+ out uint64
+ err error
+}
+
+var parseUint64BaseTests = []parseUint64BaseTest{
+ {"", 0, 0, ErrSyntax},
+ {"0", 0, 0, nil},
+ {"0x", 0, 0, ErrSyntax},
+ {"0X", 0, 0, ErrSyntax},
+ {"1", 0, 1, nil},
+ {"12345", 0, 12345, nil},
+ {"012345", 0, 012345, nil},
+ {"0x12345", 0, 0x12345, nil},
+ {"0X12345", 0, 0x12345, nil},
+ {"12345x", 0, 0, ErrSyntax},
+ {"0xabcdefg123", 0, 0, ErrSyntax},
+ {"123456789abc", 0, 0, ErrSyntax},
+ {"98765432100", 0, 98765432100, nil},
+ {"18446744073709551615", 0, 1<<64 - 1, nil},
+ {"18446744073709551616", 0, 1<<64 - 1, ErrRange},
+ {"18446744073709551620", 0, 1<<64 - 1, ErrRange},
+ {"0xFFFFFFFFFFFFFFFF", 0, 1<<64 - 1, nil},
+ {"0x10000000000000000", 0, 1<<64 - 1, ErrRange},
+ {"01777777777777777777777", 0, 1<<64 - 1, nil},
+ {"01777777777777777777778", 0, 0, ErrSyntax},
+ {"02000000000000000000000", 0, 1<<64 - 1, ErrRange},
+ {"0200000000000000000000", 0, 1 << 61, nil},
+ {"0b", 0, 0, ErrSyntax},
+ {"0B", 0, 0, ErrSyntax},
+ {"0b101", 0, 5, nil},
+ {"0B101", 0, 5, nil},
+ {"0o", 0, 0, ErrSyntax},
+ {"0O", 0, 0, ErrSyntax},
+ {"0o377", 0, 255, nil},
+ {"0O377", 0, 255, nil},
+
+ // underscores allowed with base == 0 only
+ {"1_2_3_4_5", 0, 12345, nil}, // base 0 => 10
+ {"_12345", 0, 0, ErrSyntax},
+ {"1__2345", 0, 0, ErrSyntax},
+ {"12345_", 0, 0, ErrSyntax},
+
+ {"1_2_3_4_5", 10, 0, ErrSyntax}, // base 10
+ {"_12345", 10, 0, ErrSyntax},
+ {"1__2345", 10, 0, ErrSyntax},
+ {"12345_", 10, 0, ErrSyntax},
+
+ {"0x_1_2_3_4_5", 0, 0x12345, nil}, // base 0 => 16
+ {"_0x12345", 0, 0, ErrSyntax},
+ {"0x__12345", 0, 0, ErrSyntax},
+ {"0x1__2345", 0, 0, ErrSyntax},
+ {"0x1234__5", 0, 0, ErrSyntax},
+ {"0x12345_", 0, 0, ErrSyntax},
+
+ {"1_2_3_4_5", 16, 0, ErrSyntax}, // base 16
+ {"_12345", 16, 0, ErrSyntax},
+ {"1__2345", 16, 0, ErrSyntax},
+ {"1234__5", 16, 0, ErrSyntax},
+ {"12345_", 16, 0, ErrSyntax},
+
+ {"0_1_2_3_4_5", 0, 012345, nil}, // base 0 => 8 (0377)
+ {"_012345", 0, 0, ErrSyntax},
+ {"0__12345", 0, 0, ErrSyntax},
+ {"01234__5", 0, 0, ErrSyntax},
+ {"012345_", 0, 0, ErrSyntax},
+
+ {"0o_1_2_3_4_5", 0, 012345, nil}, // base 0 => 8 (0o377)
+ {"_0o12345", 0, 0, ErrSyntax},
+ {"0o__12345", 0, 0, ErrSyntax},
+ {"0o1234__5", 0, 0, ErrSyntax},
+ {"0o12345_", 0, 0, ErrSyntax},
+
+ {"0_1_2_3_4_5", 8, 0, ErrSyntax}, // base 8
+ {"_012345", 8, 0, ErrSyntax},
+ {"0__12345", 8, 0, ErrSyntax},
+ {"01234__5", 8, 0, ErrSyntax},
+ {"012345_", 8, 0, ErrSyntax},
+
+ {"0b_1_0_1", 0, 5, nil}, // base 0 => 2 (0b101)
+ {"_0b101", 0, 0, ErrSyntax},
+ {"0b__101", 0, 0, ErrSyntax},
+ {"0b1__01", 0, 0, ErrSyntax},
+ {"0b10__1", 0, 0, ErrSyntax},
+ {"0b101_", 0, 0, ErrSyntax},
+
+ {"1_0_1", 2, 0, ErrSyntax}, // base 2
+ {"_101", 2, 0, ErrSyntax},
+ {"1_01", 2, 0, ErrSyntax},
+ {"10_1", 2, 0, ErrSyntax},
+ {"101_", 2, 0, ErrSyntax},
+}
+
+type parseInt64Test struct {
+ in string
+ out int64
+ err error
+}
+
+var parseInt64Tests = []parseInt64Test{
+ {"", 0, ErrSyntax},
+ {"0", 0, nil},
+ {"-0", 0, nil},
+ {"+0", 0, nil},
+ {"1", 1, nil},
+ {"-1", -1, nil},
+ {"+1", 1, nil},
+ {"12345", 12345, nil},
+ {"-12345", -12345, nil},
+ {"012345", 12345, nil},
+ {"-012345", -12345, nil},
+ {"98765432100", 98765432100, nil},
+ {"-98765432100", -98765432100, nil},
+ {"9223372036854775807", 1<<63 - 1, nil},
+ {"-9223372036854775807", -(1<<63 - 1), nil},
+ {"9223372036854775808", 1<<63 - 1, ErrRange},
+ {"-9223372036854775808", -1 << 63, nil},
+ {"9223372036854775809", 1<<63 - 1, ErrRange},
+ {"-9223372036854775809", -1 << 63, ErrRange},
+ {"-1_2_3_4_5", 0, ErrSyntax}, // base=10 so no underscores allowed
+ {"-_12345", 0, ErrSyntax},
+ {"_12345", 0, ErrSyntax},
+ {"1__2345", 0, ErrSyntax},
+ {"12345_", 0, ErrSyntax},
+ {"123%45", 0, ErrSyntax},
+}
+
+type parseInt64BaseTest struct {
+ in string
+ base int
+ out int64
+ err error
+}
+
+var parseInt64BaseTests = []parseInt64BaseTest{
+ {"", 0, 0, ErrSyntax},
+ {"0", 0, 0, nil},
+ {"-0", 0, 0, nil},
+ {"1", 0, 1, nil},
+ {"-1", 0, -1, nil},
+ {"12345", 0, 12345, nil},
+ {"-12345", 0, -12345, nil},
+ {"012345", 0, 012345, nil},
+ {"-012345", 0, -012345, nil},
+ {"0x12345", 0, 0x12345, nil},
+ {"-0X12345", 0, -0x12345, nil},
+ {"12345x", 0, 0, ErrSyntax},
+ {"-12345x", 0, 0, ErrSyntax},
+ {"98765432100", 0, 98765432100, nil},
+ {"-98765432100", 0, -98765432100, nil},
+ {"9223372036854775807", 0, 1<<63 - 1, nil},
+ {"-9223372036854775807", 0, -(1<<63 - 1), nil},
+ {"9223372036854775808", 0, 1<<63 - 1, ErrRange},
+ {"-9223372036854775808", 0, -1 << 63, nil},
+ {"9223372036854775809", 0, 1<<63 - 1, ErrRange},
+ {"-9223372036854775809", 0, -1 << 63, ErrRange},
+
+ // other bases
+ {"g", 17, 16, nil},
+ {"10", 25, 25, nil},
+ {"holycow", 35, (((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, nil},
+ {"holycow", 36, (((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, nil},
+
+ // base 2
+ {"0", 2, 0, nil},
+ {"-1", 2, -1, nil},
+ {"1010", 2, 10, nil},
+ {"1000000000000000", 2, 1 << 15, nil},
+ {"111111111111111111111111111111111111111111111111111111111111111", 2, 1<<63 - 1, nil},
+ {"1000000000000000000000000000000000000000000000000000000000000000", 2, 1<<63 - 1, ErrRange},
+ {"-1000000000000000000000000000000000000000000000000000000000000000", 2, -1 << 63, nil},
+ {"-1000000000000000000000000000000000000000000000000000000000000001", 2, -1 << 63, ErrRange},
+
+ // base 8
+ {"-10", 8, -8, nil},
+ {"57635436545", 8, 057635436545, nil},
+ {"100000000", 8, 1 << 24, nil},
+
+ // base 16
+ {"10", 16, 16, nil},
+ {"-123456789abcdef", 16, -0x123456789abcdef, nil},
+ {"7fffffffffffffff", 16, 1<<63 - 1, nil},
+
+ // underscores
+ {"-0x_1_2_3_4_5", 0, -0x12345, nil},
+ {"0x_1_2_3_4_5", 0, 0x12345, nil},
+ {"-_0x12345", 0, 0, ErrSyntax},
+ {"_-0x12345", 0, 0, ErrSyntax},
+ {"_0x12345", 0, 0, ErrSyntax},
+ {"0x__12345", 0, 0, ErrSyntax},
+ {"0x1__2345", 0, 0, ErrSyntax},
+ {"0x1234__5", 0, 0, ErrSyntax},
+ {"0x12345_", 0, 0, ErrSyntax},
+
+ {"-0_1_2_3_4_5", 0, -012345, nil}, // octal
+ {"0_1_2_3_4_5", 0, 012345, nil}, // octal
+ {"-_012345", 0, 0, ErrSyntax},
+ {"_-012345", 0, 0, ErrSyntax},
+ {"_012345", 0, 0, ErrSyntax},
+ {"0__12345", 0, 0, ErrSyntax},
+ {"01234__5", 0, 0, ErrSyntax},
+ {"012345_", 0, 0, ErrSyntax},
+
+ {"+0xf", 0, 0xf, nil},
+ {"-0xf", 0, -0xf, nil},
+ {"0x+f", 0, 0, ErrSyntax},
+ {"0x-f", 0, 0, ErrSyntax},
+}
+
+type parseUint32Test struct {
+ in string
+ out uint32
+ err error
+}
+
+var parseUint32Tests = []parseUint32Test{
+ {"", 0, ErrSyntax},
+ {"0", 0, nil},
+ {"1", 1, nil},
+ {"12345", 12345, nil},
+ {"012345", 12345, nil},
+ {"12345x", 0, ErrSyntax},
+ {"987654321", 987654321, nil},
+ {"4294967295", 1<<32 - 1, nil},
+ {"4294967296", 1<<32 - 1, ErrRange},
+ {"1_2_3_4_5", 0, ErrSyntax}, // base=10 so no underscores allowed
+ {"_12345", 0, ErrSyntax},
+ {"_12345", 0, ErrSyntax},
+ {"1__2345", 0, ErrSyntax},
+ {"12345_", 0, ErrSyntax},
+}
+
+type parseInt32Test struct {
+ in string
+ out int32
+ err error
+}
+
+var parseInt32Tests = []parseInt32Test{
+ {"", 0, ErrSyntax},
+ {"0", 0, nil},
+ {"-0", 0, nil},
+ {"1", 1, nil},
+ {"-1", -1, nil},
+ {"12345", 12345, nil},
+ {"-12345", -12345, nil},
+ {"012345", 12345, nil},
+ {"-012345", -12345, nil},
+ {"12345x", 0, ErrSyntax},
+ {"-12345x", 0, ErrSyntax},
+ {"987654321", 987654321, nil},
+ {"-987654321", -987654321, nil},
+ {"2147483647", 1<<31 - 1, nil},
+ {"-2147483647", -(1<<31 - 1), nil},
+ {"2147483648", 1<<31 - 1, ErrRange},
+ {"-2147483648", -1 << 31, nil},
+ {"2147483649", 1<<31 - 1, ErrRange},
+ {"-2147483649", -1 << 31, ErrRange},
+ {"-1_2_3_4_5", 0, ErrSyntax}, // base=10 so no underscores allowed
+ {"-_12345", 0, ErrSyntax},
+ {"_12345", 0, ErrSyntax},
+ {"1__2345", 0, ErrSyntax},
+ {"12345_", 0, ErrSyntax},
+ {"123%45", 0, ErrSyntax},
+}
+
+type numErrorTest struct {
+ num, want string
+}
+
+var numErrorTests = []numErrorTest{
+ {"0", `strconv.ParseFloat: parsing "0": failed`},
+ {"`", "strconv.ParseFloat: parsing \"`\": failed"},
+ {"1\x00.2", `strconv.ParseFloat: parsing "1\x00.2": failed`},
+}
+
+func init() {
+ // The parse routines return NumErrors wrapping
+ // the error and the string. Convert the tables above.
+ for i := range parseUint64Tests {
+ test := &parseUint64Tests[i]
+ if test.err != nil {
+ test.err = &NumError{"ParseUint", test.in, test.err}
+ }
+ }
+ for i := range parseUint64BaseTests {
+ test := &parseUint64BaseTests[i]
+ if test.err != nil {
+ test.err = &NumError{"ParseUint", test.in, test.err}
+ }
+ }
+ for i := range parseInt64Tests {
+ test := &parseInt64Tests[i]
+ if test.err != nil {
+ test.err = &NumError{"ParseInt", test.in, test.err}
+ }
+ }
+ for i := range parseInt64BaseTests {
+ test := &parseInt64BaseTests[i]
+ if test.err != nil {
+ test.err = &NumError{"ParseInt", test.in, test.err}
+ }
+ }
+ for i := range parseUint32Tests {
+ test := &parseUint32Tests[i]
+ if test.err != nil {
+ test.err = &NumError{"ParseUint", test.in, test.err}
+ }
+ }
+ for i := range parseInt32Tests {
+ test := &parseInt32Tests[i]
+ if test.err != nil {
+ test.err = &NumError{"ParseInt", test.in, test.err}
+ }
+ }
+}
+
+func TestParseUint32(t *testing.T) {
+ for i := range parseUint32Tests {
+ test := &parseUint32Tests[i]
+ out, err := ParseUint(test.in, 10, 32)
+ if uint64(test.out) != out || !reflect.DeepEqual(test.err, err) {
+ t.Errorf("ParseUint(%q, 10, 32) = %v, %v want %v, %v",
+ test.in, out, err, test.out, test.err)
+ }
+ }
+}
+
+func TestParseUint64(t *testing.T) {
+ for i := range parseUint64Tests {
+ test := &parseUint64Tests[i]
+ out, err := ParseUint(test.in, 10, 64)
+ if test.out != out || !reflect.DeepEqual(test.err, err) {
+ t.Errorf("ParseUint(%q, 10, 64) = %v, %v want %v, %v",
+ test.in, out, err, test.out, test.err)
+ }
+ }
+}
+
+func TestParseUint64Base(t *testing.T) {
+ for i := range parseUint64BaseTests {
+ test := &parseUint64BaseTests[i]
+ out, err := ParseUint(test.in, test.base, 64)
+ if test.out != out || !reflect.DeepEqual(test.err, err) {
+ t.Errorf("ParseUint(%q, %v, 64) = %v, %v want %v, %v",
+ test.in, test.base, out, err, test.out, test.err)
+ }
+ }
+}
+
+func TestParseInt32(t *testing.T) {
+ for i := range parseInt32Tests {
+ test := &parseInt32Tests[i]
+ out, err := ParseInt(test.in, 10, 32)
+ if int64(test.out) != out || !reflect.DeepEqual(test.err, err) {
+ t.Errorf("ParseInt(%q, 10 ,32) = %v, %v want %v, %v",
+ test.in, out, err, test.out, test.err)
+ }
+ }
+}
+
+func TestParseInt64(t *testing.T) {
+ for i := range parseInt64Tests {
+ test := &parseInt64Tests[i]
+ out, err := ParseInt(test.in, 10, 64)
+ if test.out != out || !reflect.DeepEqual(test.err, err) {
+ t.Errorf("ParseInt(%q, 10, 64) = %v, %v want %v, %v",
+ test.in, out, err, test.out, test.err)
+ }
+ }
+}
+
+func TestParseInt64Base(t *testing.T) {
+ for i := range parseInt64BaseTests {
+ test := &parseInt64BaseTests[i]
+ out, err := ParseInt(test.in, test.base, 64)
+ if test.out != out || !reflect.DeepEqual(test.err, err) {
+ t.Errorf("ParseInt(%q, %v, 64) = %v, %v want %v, %v",
+ test.in, test.base, out, err, test.out, test.err)
+ }
+ }
+}
+
+func TestParseUint(t *testing.T) {
+ switch IntSize {
+ case 32:
+ for i := range parseUint32Tests {
+ test := &parseUint32Tests[i]
+ out, err := ParseUint(test.in, 10, 0)
+ if uint64(test.out) != out || !reflect.DeepEqual(test.err, err) {
+ t.Errorf("ParseUint(%q, 10, 0) = %v, %v want %v, %v",
+ test.in, out, err, test.out, test.err)
+ }
+ }
+ case 64:
+ for i := range parseUint64Tests {
+ test := &parseUint64Tests[i]
+ out, err := ParseUint(test.in, 10, 0)
+ if test.out != out || !reflect.DeepEqual(test.err, err) {
+ t.Errorf("ParseUint(%q, 10, 0) = %v, %v want %v, %v",
+ test.in, out, err, test.out, test.err)
+ }
+ }
+ }
+}
+
+func TestParseInt(t *testing.T) {
+ switch IntSize {
+ case 32:
+ for i := range parseInt32Tests {
+ test := &parseInt32Tests[i]
+ out, err := ParseInt(test.in, 10, 0)
+ if int64(test.out) != out || !reflect.DeepEqual(test.err, err) {
+ t.Errorf("ParseInt(%q, 10, 0) = %v, %v want %v, %v",
+ test.in, out, err, test.out, test.err)
+ }
+ }
+ case 64:
+ for i := range parseInt64Tests {
+ test := &parseInt64Tests[i]
+ out, err := ParseInt(test.in, 10, 0)
+ if test.out != out || !reflect.DeepEqual(test.err, err) {
+ t.Errorf("ParseInt(%q, 10, 0) = %v, %v want %v, %v",
+ test.in, out, err, test.out, test.err)
+ }
+ }
+ }
+}
+
+func TestAtoi(t *testing.T) {
+ switch IntSize {
+ case 32:
+ for i := range parseInt32Tests {
+ test := &parseInt32Tests[i]
+ out, err := Atoi(test.in)
+ var testErr error
+ if test.err != nil {
+ testErr = &NumError{"Atoi", test.in, test.err.(*NumError).Err}
+ }
+ if int(test.out) != out || !reflect.DeepEqual(testErr, err) {
+ t.Errorf("Atoi(%q) = %v, %v want %v, %v",
+ test.in, out, err, test.out, testErr)
+ }
+ }
+ case 64:
+ for i := range parseInt64Tests {
+ test := &parseInt64Tests[i]
+ out, err := Atoi(test.in)
+ var testErr error
+ if test.err != nil {
+ testErr = &NumError{"Atoi", test.in, test.err.(*NumError).Err}
+ }
+ if test.out != int64(out) || !reflect.DeepEqual(testErr, err) {
+ t.Errorf("Atoi(%q) = %v, %v want %v, %v",
+ test.in, out, err, test.out, testErr)
+ }
+ }
+ }
+}
+
+func bitSizeErrStub(name string, bitSize int) error {
+ return BitSizeError(name, "0", bitSize)
+}
+
+func baseErrStub(name string, base int) error {
+ return BaseError(name, "0", base)
+}
+
+func noErrStub(name string, arg int) error {
+ return nil
+}
+
+type parseErrorTest struct {
+ arg int
+ errStub func(name string, arg int) error
+}
+
+var parseBitSizeTests = []parseErrorTest{
+ {-1, bitSizeErrStub},
+ {0, noErrStub},
+ {64, noErrStub},
+ {65, bitSizeErrStub},
+}
+
+var parseBaseTests = []parseErrorTest{
+ {-1, baseErrStub},
+ {0, noErrStub},
+ {1, baseErrStub},
+ {2, noErrStub},
+ {36, noErrStub},
+ {37, baseErrStub},
+}
+
+func equalError(a, b error) bool {
+ if a == nil {
+ return b == nil
+ }
+ if b == nil {
+ return a == nil
+ }
+ return a.Error() == b.Error()
+}
+
+func TestParseIntBitSize(t *testing.T) {
+ for i := range parseBitSizeTests {
+ test := &parseBitSizeTests[i]
+ testErr := test.errStub("ParseInt", test.arg)
+ _, err := ParseInt("0", 0, test.arg)
+ if !equalError(testErr, err) {
+ t.Errorf("ParseInt(\"0\", 0, %v) = 0, %v want 0, %v",
+ test.arg, err, testErr)
+ }
+ }
+}
+
+func TestParseUintBitSize(t *testing.T) {
+ for i := range parseBitSizeTests {
+ test := &parseBitSizeTests[i]
+ testErr := test.errStub("ParseUint", test.arg)
+ _, err := ParseUint("0", 0, test.arg)
+ if !equalError(testErr, err) {
+ t.Errorf("ParseUint(\"0\", 0, %v) = 0, %v want 0, %v",
+ test.arg, err, testErr)
+ }
+ }
+}
+
+func TestParseIntBase(t *testing.T) {
+ for i := range parseBaseTests {
+ test := &parseBaseTests[i]
+ testErr := test.errStub("ParseInt", test.arg)
+ _, err := ParseInt("0", test.arg, 0)
+ if !equalError(testErr, err) {
+ t.Errorf("ParseInt(\"0\", %v, 0) = 0, %v want 0, %v",
+ test.arg, err, testErr)
+ }
+ }
+}
+
+func TestParseUintBase(t *testing.T) {
+ for i := range parseBaseTests {
+ test := &parseBaseTests[i]
+ testErr := test.errStub("ParseUint", test.arg)
+ _, err := ParseUint("0", test.arg, 0)
+ if !equalError(testErr, err) {
+ t.Errorf("ParseUint(\"0\", %v, 0) = 0, %v want 0, %v",
+ test.arg, err, testErr)
+ }
+ }
+}
+
+func TestNumError(t *testing.T) {
+ for _, test := range numErrorTests {
+ err := &NumError{
+ Func: "ParseFloat",
+ Num: test.num,
+ Err: errors.New("failed"),
+ }
+ if got := err.Error(); got != test.want {
+ t.Errorf(`(&NumError{"ParseFloat", %q, "failed"}).Error() = %v, want %v`, test.num, got, test.want)
+ }
+ }
+}
+
+func TestNumErrorUnwrap(t *testing.T) {
+ err := &NumError{Err: ErrSyntax}
+ if !errors.Is(err, ErrSyntax) {
+ t.Error("errors.Is failed, wanted success")
+ }
+}
+
+func BenchmarkParseInt(b *testing.B) {
+ b.Run("Pos", func(b *testing.B) {
+ benchmarkParseInt(b, 1)
+ })
+ b.Run("Neg", func(b *testing.B) {
+ benchmarkParseInt(b, -1)
+ })
+}
+
+type benchCase struct {
+ name string
+ num int64
+}
+
+func benchmarkParseInt(b *testing.B, neg int) {
+ cases := []benchCase{
+ {"7bit", 1<<7 - 1},
+ {"26bit", 1<<26 - 1},
+ {"31bit", 1<<31 - 1},
+ {"56bit", 1<<56 - 1},
+ {"63bit", 1<<63 - 1},
+ }
+ for _, cs := range cases {
+ b.Run(cs.name, func(b *testing.B) {
+ s := fmt.Sprintf("%d", cs.num*int64(neg))
+ for i := 0; i < b.N; i++ {
+ out, _ := ParseInt(s, 10, 64)
+ BenchSink += int(out)
+ }
+ })
+ }
+}
+
+func BenchmarkAtoi(b *testing.B) {
+ b.Run("Pos", func(b *testing.B) {
+ benchmarkAtoi(b, 1)
+ })
+ b.Run("Neg", func(b *testing.B) {
+ benchmarkAtoi(b, -1)
+ })
+}
+
+func benchmarkAtoi(b *testing.B, neg int) {
+ cases := []benchCase{
+ {"7bit", 1<<7 - 1},
+ {"26bit", 1<<26 - 1},
+ {"31bit", 1<<31 - 1},
+ }
+ if IntSize == 64 {
+ cases = append(cases, []benchCase{
+ {"56bit", 1<<56 - 1},
+ {"63bit", 1<<63 - 1},
+ }...)
+ }
+ for _, cs := range cases {
+ b.Run(cs.name, func(b *testing.B) {
+ s := fmt.Sprintf("%d", cs.num*int64(neg))
+ for i := 0; i < b.N; i++ {
+ out, _ := Atoi(s)
+ BenchSink += out
+ }
+ })
+ }
+}
diff --git a/src/strconv/bytealg.go b/src/strconv/bytealg.go
new file mode 100644
index 0000000..6fe1624
--- /dev/null
+++ b/src/strconv/bytealg.go
@@ -0,0 +1,14 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build !compiler_bootstrap
+
+package strconv
+
+import "internal/bytealg"
+
+// index returns the index of the first instance of c in s, or -1 if missing.
+func index(s string, c byte) int {
+ return bytealg.IndexByteString(s, c)
+}
diff --git a/src/strconv/bytealg_bootstrap.go b/src/strconv/bytealg_bootstrap.go
new file mode 100644
index 0000000..12d96e5
--- /dev/null
+++ b/src/strconv/bytealg_bootstrap.go
@@ -0,0 +1,17 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build compiler_bootstrap
+
+package strconv
+
+// index returns the index of the first instance of c in s, or -1 if missing.
+func index(s string, c byte) int {
+ for i := 0; i < len(s); i++ {
+ if s[i] == c {
+ return i
+ }
+ }
+ return -1
+}
diff --git a/src/strconv/ctoa.go b/src/strconv/ctoa.go
new file mode 100644
index 0000000..c16a2e5
--- /dev/null
+++ b/src/strconv/ctoa.go
@@ -0,0 +1,27 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv
+
+// FormatComplex converts the complex number c to a string of the
+// form (a+bi) where a and b are the real and imaginary parts,
+// formatted according to the format fmt and precision prec.
+//
+// The format fmt and precision prec have the same meaning as in FormatFloat.
+// It rounds the result assuming that the original was obtained from a complex
+// value of bitSize bits, which must be 64 for complex64 and 128 for complex128.
+func FormatComplex(c complex128, fmt byte, prec, bitSize int) string {
+ if bitSize != 64 && bitSize != 128 {
+ panic("invalid bitSize")
+ }
+ bitSize >>= 1 // complex64 uses float32 internally
+
+ // Check if imaginary part has a sign. If not, add one.
+ im := FormatFloat(imag(c), fmt, prec, bitSize)
+ if im[0] != '+' && im[0] != '-' {
+ im = "+" + im
+ }
+
+ return "(" + FormatFloat(real(c), fmt, prec, bitSize) + im + "i)"
+}
diff --git a/src/strconv/ctoa_test.go b/src/strconv/ctoa_test.go
new file mode 100644
index 0000000..8b77898
--- /dev/null
+++ b/src/strconv/ctoa_test.go
@@ -0,0 +1,53 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ . "strconv"
+ "testing"
+)
+
+func TestFormatComplex(t *testing.T) {
+ tests := []struct {
+ c complex128
+ fmt byte
+ prec int
+ bitSize int
+ out string
+ }{
+ // a variety of signs
+ {1 + 2i, 'g', -1, 128, "(1+2i)"},
+ {3 - 4i, 'g', -1, 128, "(3-4i)"},
+ {-5 + 6i, 'g', -1, 128, "(-5+6i)"},
+ {-7 - 8i, 'g', -1, 128, "(-7-8i)"},
+
+ // test that fmt and prec are working
+ {3.14159 + 0.00123i, 'e', 3, 128, "(3.142e+00+1.230e-03i)"},
+ {3.14159 + 0.00123i, 'f', 3, 128, "(3.142+0.001i)"},
+ {3.14159 + 0.00123i, 'g', 3, 128, "(3.14+0.00123i)"},
+
+ // ensure bitSize rounding is working
+ {1.2345678901234567 + 9.876543210987654i, 'f', -1, 128, "(1.2345678901234567+9.876543210987654i)"},
+ {1.2345678901234567 + 9.876543210987654i, 'f', -1, 64, "(1.2345679+9.876543i)"},
+
+ // other cases are handled by FormatFloat tests
+ }
+ for _, test := range tests {
+ out := FormatComplex(test.c, test.fmt, test.prec, test.bitSize)
+ if out != test.out {
+ t.Fatalf("FormatComplex(%v, %q, %d, %d) = %q; want %q",
+ test.c, test.fmt, test.prec, test.bitSize, out, test.out)
+ }
+ }
+}
+
+func TestFormatComplexInvalidBitSize(t *testing.T) {
+ defer func() {
+ if r := recover(); r == nil {
+ t.Fatalf("expected panic due to invalid bitSize")
+ }
+ }()
+ _ = FormatComplex(1+2i, 'g', -1, 100)
+}
diff --git a/src/strconv/decimal.go b/src/strconv/decimal.go
new file mode 100644
index 0000000..b580018
--- /dev/null
+++ b/src/strconv/decimal.go
@@ -0,0 +1,415 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Multiprecision decimal numbers.
+// For floating-point formatting only; not general purpose.
+// Only operations are assign and (binary) left/right shift.
+// Can do binary floating point in multiprecision decimal precisely
+// because 2 divides 10; cannot do decimal floating point
+// in multiprecision binary precisely.
+
+package strconv
+
+type decimal struct {
+ d [800]byte // digits, big-endian representation
+ nd int // number of digits used
+ dp int // decimal point
+ neg bool // negative flag
+ trunc bool // discarded nonzero digits beyond d[:nd]
+}
+
+func (a *decimal) String() string {
+ n := 10 + a.nd
+ if a.dp > 0 {
+ n += a.dp
+ }
+ if a.dp < 0 {
+ n += -a.dp
+ }
+
+ buf := make([]byte, n)
+ w := 0
+ switch {
+ case a.nd == 0:
+ return "0"
+
+ case a.dp <= 0:
+ // zeros fill space between decimal point and digits
+ buf[w] = '0'
+ w++
+ buf[w] = '.'
+ w++
+ w += digitZero(buf[w : w+-a.dp])
+ w += copy(buf[w:], a.d[0:a.nd])
+
+ case a.dp < a.nd:
+ // decimal point in middle of digits
+ w += copy(buf[w:], a.d[0:a.dp])
+ buf[w] = '.'
+ w++
+ w += copy(buf[w:], a.d[a.dp:a.nd])
+
+ default:
+ // zeros fill space between digits and decimal point
+ w += copy(buf[w:], a.d[0:a.nd])
+ w += digitZero(buf[w : w+a.dp-a.nd])
+ }
+ return string(buf[0:w])
+}
+
+func digitZero(dst []byte) int {
+ for i := range dst {
+ dst[i] = '0'
+ }
+ return len(dst)
+}
+
+// trim trailing zeros from number.
+// (They are meaningless; the decimal point is tracked
+// independent of the number of digits.)
+func trim(a *decimal) {
+ for a.nd > 0 && a.d[a.nd-1] == '0' {
+ a.nd--
+ }
+ if a.nd == 0 {
+ a.dp = 0
+ }
+}
+
+// Assign v to a.
+func (a *decimal) Assign(v uint64) {
+ var buf [24]byte
+
+ // Write reversed decimal in buf.
+ n := 0
+ for v > 0 {
+ v1 := v / 10
+ v -= 10 * v1
+ buf[n] = byte(v + '0')
+ n++
+ v = v1
+ }
+
+ // Reverse again to produce forward decimal in a.d.
+ a.nd = 0
+ for n--; n >= 0; n-- {
+ a.d[a.nd] = buf[n]
+ a.nd++
+ }
+ a.dp = a.nd
+ trim(a)
+}
+
+// Maximum shift that we can do in one pass without overflow.
+// A uint has 32 or 64 bits, and we have to be able to accommodate 9<<k.
+const uintSize = 32 << (^uint(0) >> 63)
+const maxShift = uintSize - 4
+
+// Binary shift right (/ 2) by k bits. k <= maxShift to avoid overflow.
+func rightShift(a *decimal, k uint) {
+ r := 0 // read pointer
+ w := 0 // write pointer
+
+ // Pick up enough leading digits to cover first shift.
+ var n uint
+ for ; n>>k == 0; r++ {
+ if r >= a.nd {
+ if n == 0 {
+ // a == 0; shouldn't get here, but handle anyway.
+ a.nd = 0
+ return
+ }
+ for n>>k == 0 {
+ n = n * 10
+ r++
+ }
+ break
+ }
+ c := uint(a.d[r])
+ n = n*10 + c - '0'
+ }
+ a.dp -= r - 1
+
+ var mask uint = (1 << k) - 1
+
+ // Pick up a digit, put down a digit.
+ for ; r < a.nd; r++ {
+ c := uint(a.d[r])
+ dig := n >> k
+ n &= mask
+ a.d[w] = byte(dig + '0')
+ w++
+ n = n*10 + c - '0'
+ }
+
+ // Put down extra digits.
+ for n > 0 {
+ dig := n >> k
+ n &= mask
+ if w < len(a.d) {
+ a.d[w] = byte(dig + '0')
+ w++
+ } else if dig > 0 {
+ a.trunc = true
+ }
+ n = n * 10
+ }
+
+ a.nd = w
+ trim(a)
+}
+
+// Cheat sheet for left shift: table indexed by shift count giving
+// number of new digits that will be introduced by that shift.
+//
+// For example, leftcheats[4] = {2, "625"}. That means that
+// if we are shifting by 4 (multiplying by 16), it will add 2 digits
+// when the string prefix is "625" through "999", and one fewer digit
+// if the string prefix is "000" through "624".
+//
+// Credit for this trick goes to Ken.
+
+type leftCheat struct {
+ delta int // number of new digits
+ cutoff string // minus one digit if original < a.
+}
+
+var leftcheats = []leftCheat{
+ // Leading digits of 1/2^i = 5^i.
+ // 5^23 is not an exact 64-bit floating point number,
+ // so have to use bc for the math.
+ // Go up to 60 to be large enough for 32bit and 64bit platforms.
+ /*
+ seq 60 | sed 's/^/5^/' | bc |
+ awk 'BEGIN{ print "\t{ 0, \"\" }," }
+ {
+ log2 = log(2)/log(10)
+ printf("\t{ %d, \"%s\" },\t// * %d\n",
+ int(log2*NR+1), $0, 2**NR)
+ }'
+ */
+ {0, ""},
+ {1, "5"}, // * 2
+ {1, "25"}, // * 4
+ {1, "125"}, // * 8
+ {2, "625"}, // * 16
+ {2, "3125"}, // * 32
+ {2, "15625"}, // * 64
+ {3, "78125"}, // * 128
+ {3, "390625"}, // * 256
+ {3, "1953125"}, // * 512
+ {4, "9765625"}, // * 1024
+ {4, "48828125"}, // * 2048
+ {4, "244140625"}, // * 4096
+ {4, "1220703125"}, // * 8192
+ {5, "6103515625"}, // * 16384
+ {5, "30517578125"}, // * 32768
+ {5, "152587890625"}, // * 65536
+ {6, "762939453125"}, // * 131072
+ {6, "3814697265625"}, // * 262144
+ {6, "19073486328125"}, // * 524288
+ {7, "95367431640625"}, // * 1048576
+ {7, "476837158203125"}, // * 2097152
+ {7, "2384185791015625"}, // * 4194304
+ {7, "11920928955078125"}, // * 8388608
+ {8, "59604644775390625"}, // * 16777216
+ {8, "298023223876953125"}, // * 33554432
+ {8, "1490116119384765625"}, // * 67108864
+ {9, "7450580596923828125"}, // * 134217728
+ {9, "37252902984619140625"}, // * 268435456
+ {9, "186264514923095703125"}, // * 536870912
+ {10, "931322574615478515625"}, // * 1073741824
+ {10, "4656612873077392578125"}, // * 2147483648
+ {10, "23283064365386962890625"}, // * 4294967296
+ {10, "116415321826934814453125"}, // * 8589934592
+ {11, "582076609134674072265625"}, // * 17179869184
+ {11, "2910383045673370361328125"}, // * 34359738368
+ {11, "14551915228366851806640625"}, // * 68719476736
+ {12, "72759576141834259033203125"}, // * 137438953472
+ {12, "363797880709171295166015625"}, // * 274877906944
+ {12, "1818989403545856475830078125"}, // * 549755813888
+ {13, "9094947017729282379150390625"}, // * 1099511627776
+ {13, "45474735088646411895751953125"}, // * 2199023255552
+ {13, "227373675443232059478759765625"}, // * 4398046511104
+ {13, "1136868377216160297393798828125"}, // * 8796093022208
+ {14, "5684341886080801486968994140625"}, // * 17592186044416
+ {14, "28421709430404007434844970703125"}, // * 35184372088832
+ {14, "142108547152020037174224853515625"}, // * 70368744177664
+ {15, "710542735760100185871124267578125"}, // * 140737488355328
+ {15, "3552713678800500929355621337890625"}, // * 281474976710656
+ {15, "17763568394002504646778106689453125"}, // * 562949953421312
+ {16, "88817841970012523233890533447265625"}, // * 1125899906842624
+ {16, "444089209850062616169452667236328125"}, // * 2251799813685248
+ {16, "2220446049250313080847263336181640625"}, // * 4503599627370496
+ {16, "11102230246251565404236316680908203125"}, // * 9007199254740992
+ {17, "55511151231257827021181583404541015625"}, // * 18014398509481984
+ {17, "277555756156289135105907917022705078125"}, // * 36028797018963968
+ {17, "1387778780781445675529539585113525390625"}, // * 72057594037927936
+ {18, "6938893903907228377647697925567626953125"}, // * 144115188075855872
+ {18, "34694469519536141888238489627838134765625"}, // * 288230376151711744
+ {18, "173472347597680709441192448139190673828125"}, // * 576460752303423488
+ {19, "867361737988403547205962240695953369140625"}, // * 1152921504606846976
+}
+
+// Is the leading prefix of b lexicographically less than s?
+func prefixIsLessThan(b []byte, s string) bool {
+ for i := 0; i < len(s); i++ {
+ if i >= len(b) {
+ return true
+ }
+ if b[i] != s[i] {
+ return b[i] < s[i]
+ }
+ }
+ return false
+}
+
+// Binary shift left (* 2) by k bits. k <= maxShift to avoid overflow.
+func leftShift(a *decimal, k uint) {
+ delta := leftcheats[k].delta
+ if prefixIsLessThan(a.d[0:a.nd], leftcheats[k].cutoff) {
+ delta--
+ }
+
+ r := a.nd // read index
+ w := a.nd + delta // write index
+
+ // Pick up a digit, put down a digit.
+ var n uint
+ for r--; r >= 0; r-- {
+ n += (uint(a.d[r]) - '0') << k
+ quo := n / 10
+ rem := n - 10*quo
+ w--
+ if w < len(a.d) {
+ a.d[w] = byte(rem + '0')
+ } else if rem != 0 {
+ a.trunc = true
+ }
+ n = quo
+ }
+
+ // Put down extra digits.
+ for n > 0 {
+ quo := n / 10
+ rem := n - 10*quo
+ w--
+ if w < len(a.d) {
+ a.d[w] = byte(rem + '0')
+ } else if rem != 0 {
+ a.trunc = true
+ }
+ n = quo
+ }
+
+ a.nd += delta
+ if a.nd >= len(a.d) {
+ a.nd = len(a.d)
+ }
+ a.dp += delta
+ trim(a)
+}
+
+// Binary shift left (k > 0) or right (k < 0).
+func (a *decimal) Shift(k int) {
+ switch {
+ case a.nd == 0:
+ // nothing to do: a == 0
+ case k > 0:
+ for k > maxShift {
+ leftShift(a, maxShift)
+ k -= maxShift
+ }
+ leftShift(a, uint(k))
+ case k < 0:
+ for k < -maxShift {
+ rightShift(a, maxShift)
+ k += maxShift
+ }
+ rightShift(a, uint(-k))
+ }
+}
+
+// If we chop a at nd digits, should we round up?
+func shouldRoundUp(a *decimal, nd int) bool {
+ if nd < 0 || nd >= a.nd {
+ return false
+ }
+ if a.d[nd] == '5' && nd+1 == a.nd { // exactly halfway - round to even
+ // if we truncated, a little higher than what's recorded - always round up
+ if a.trunc {
+ return true
+ }
+ return nd > 0 && (a.d[nd-1]-'0')%2 != 0
+ }
+ // not halfway - digit tells all
+ return a.d[nd] >= '5'
+}
+
+// Round a to nd digits (or fewer).
+// If nd is zero, it means we're rounding
+// just to the left of the digits, as in
+// 0.09 -> 0.1.
+func (a *decimal) Round(nd int) {
+ if nd < 0 || nd >= a.nd {
+ return
+ }
+ if shouldRoundUp(a, nd) {
+ a.RoundUp(nd)
+ } else {
+ a.RoundDown(nd)
+ }
+}
+
+// Round a down to nd digits (or fewer).
+func (a *decimal) RoundDown(nd int) {
+ if nd < 0 || nd >= a.nd {
+ return
+ }
+ a.nd = nd
+ trim(a)
+}
+
+// Round a up to nd digits (or fewer).
+func (a *decimal) RoundUp(nd int) {
+ if nd < 0 || nd >= a.nd {
+ return
+ }
+
+ // round up
+ for i := nd - 1; i >= 0; i-- {
+ c := a.d[i]
+ if c < '9' { // can stop after this digit
+ a.d[i]++
+ a.nd = i + 1
+ return
+ }
+ }
+
+ // Number is all 9s.
+ // Change to single 1 with adjusted decimal point.
+ a.d[0] = '1'
+ a.nd = 1
+ a.dp++
+}
+
+// Extract integer part, rounded appropriately.
+// No guarantees about overflow.
+func (a *decimal) RoundedInteger() uint64 {
+ if a.dp > 20 {
+ return 0xFFFFFFFFFFFFFFFF
+ }
+ var i int
+ n := uint64(0)
+ for i = 0; i < a.dp && i < a.nd; i++ {
+ n = n*10 + uint64(a.d[i]-'0')
+ }
+ for ; i < a.dp; i++ {
+ n *= 10
+ }
+ if shouldRoundUp(a, a.dp) {
+ n++
+ }
+ return n
+}
diff --git a/src/strconv/decimal_test.go b/src/strconv/decimal_test.go
new file mode 100644
index 0000000..13a127f
--- /dev/null
+++ b/src/strconv/decimal_test.go
@@ -0,0 +1,127 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ . "strconv"
+ "testing"
+)
+
+type shiftTest struct {
+ i uint64
+ shift int
+ out string
+}
+
+var shifttests = []shiftTest{
+ {0, -100, "0"},
+ {0, 100, "0"},
+ {1, 100, "1267650600228229401496703205376"},
+ {1, -100,
+ "0.00000000000000000000000000000078886090522101180541" +
+ "17285652827862296732064351090230047702789306640625",
+ },
+ {12345678, 8, "3160493568"},
+ {12345678, -8, "48225.3046875"},
+ {195312, 9, "99999744"},
+ {1953125, 9, "1000000000"},
+}
+
+func TestDecimalShift(t *testing.T) {
+ for i := 0; i < len(shifttests); i++ {
+ test := &shifttests[i]
+ d := NewDecimal(test.i)
+ d.Shift(test.shift)
+ s := d.String()
+ if s != test.out {
+ t.Errorf("Decimal %v << %v = %v, want %v",
+ test.i, test.shift, s, test.out)
+ }
+ }
+}
+
+type roundTest struct {
+ i uint64
+ nd int
+ down, round, up string
+ int uint64
+}
+
+var roundtests = []roundTest{
+ {0, 4, "0", "0", "0", 0},
+ {12344999, 4, "12340000", "12340000", "12350000", 12340000},
+ {12345000, 4, "12340000", "12340000", "12350000", 12340000},
+ {12345001, 4, "12340000", "12350000", "12350000", 12350000},
+ {23454999, 4, "23450000", "23450000", "23460000", 23450000},
+ {23455000, 4, "23450000", "23460000", "23460000", 23460000},
+ {23455001, 4, "23450000", "23460000", "23460000", 23460000},
+
+ {99994999, 4, "99990000", "99990000", "100000000", 99990000},
+ {99995000, 4, "99990000", "100000000", "100000000", 100000000},
+ {99999999, 4, "99990000", "100000000", "100000000", 100000000},
+
+ {12994999, 4, "12990000", "12990000", "13000000", 12990000},
+ {12995000, 4, "12990000", "13000000", "13000000", 13000000},
+ {12999999, 4, "12990000", "13000000", "13000000", 13000000},
+}
+
+func TestDecimalRound(t *testing.T) {
+ for i := 0; i < len(roundtests); i++ {
+ test := &roundtests[i]
+ d := NewDecimal(test.i)
+ d.RoundDown(test.nd)
+ s := d.String()
+ if s != test.down {
+ t.Errorf("Decimal %v RoundDown %d = %v, want %v",
+ test.i, test.nd, s, test.down)
+ }
+ d = NewDecimal(test.i)
+ d.Round(test.nd)
+ s = d.String()
+ if s != test.round {
+ t.Errorf("Decimal %v Round %d = %v, want %v",
+ test.i, test.nd, s, test.down)
+ }
+ d = NewDecimal(test.i)
+ d.RoundUp(test.nd)
+ s = d.String()
+ if s != test.up {
+ t.Errorf("Decimal %v RoundUp %d = %v, want %v",
+ test.i, test.nd, s, test.up)
+ }
+ }
+}
+
+type roundIntTest struct {
+ i uint64
+ shift int
+ int uint64
+}
+
+var roundinttests = []roundIntTest{
+ {0, 100, 0},
+ {512, -8, 2},
+ {513, -8, 2},
+ {640, -8, 2},
+ {641, -8, 3},
+ {384, -8, 2},
+ {385, -8, 2},
+ {383, -8, 1},
+ {1, 100, 1<<64 - 1},
+ {1000, 0, 1000},
+}
+
+func TestDecimalRoundedInteger(t *testing.T) {
+ for i := 0; i < len(roundinttests); i++ {
+ test := roundinttests[i]
+ d := NewDecimal(test.i)
+ d.Shift(test.shift)
+ int := d.RoundedInteger()
+ if int != test.int {
+ t.Errorf("Decimal %v >> %v RoundedInteger = %v, want %v",
+ test.i, test.shift, int, test.int)
+ }
+ }
+}
diff --git a/src/strconv/doc.go b/src/strconv/doc.go
new file mode 100644
index 0000000..fa20f90
--- /dev/null
+++ b/src/strconv/doc.go
@@ -0,0 +1,56 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package strconv implements conversions to and from string representations
+// of basic data types.
+//
+// # Numeric Conversions
+//
+// The most common numeric conversions are Atoi (string to int) and Itoa (int to string).
+//
+// i, err := strconv.Atoi("-42")
+// s := strconv.Itoa(-42)
+//
+// These assume decimal and the Go int type.
+//
+// [ParseBool], [ParseFloat], [ParseInt], and [ParseUint] convert strings to values:
+//
+// b, err := strconv.ParseBool("true")
+// f, err := strconv.ParseFloat("3.1415", 64)
+// i, err := strconv.ParseInt("-42", 10, 64)
+// u, err := strconv.ParseUint("42", 10, 64)
+//
+// The parse functions return the widest type (float64, int64, and uint64),
+// but if the size argument specifies a narrower width the result can be
+// converted to that narrower type without data loss:
+//
+// s := "2147483647" // biggest int32
+// i64, err := strconv.ParseInt(s, 10, 32)
+// ...
+// i := int32(i64)
+//
+// [FormatBool], [FormatFloat], [FormatInt], and [FormatUint] convert values to strings:
+//
+// s := strconv.FormatBool(true)
+// s := strconv.FormatFloat(3.1415, 'E', -1, 64)
+// s := strconv.FormatInt(-42, 16)
+// s := strconv.FormatUint(42, 16)
+//
+// [AppendBool], [AppendFloat], [AppendInt], and [AppendUint] are similar but
+// append the formatted value to a destination slice.
+//
+// # String Conversions
+//
+// [Quote] and [QuoteToASCII] convert strings to quoted Go string literals.
+// The latter guarantees that the result is an ASCII string, by escaping
+// any non-ASCII Unicode with \u:
+//
+// q := strconv.Quote("Hello, 世界")
+// q := strconv.QuoteToASCII("Hello, 世界")
+//
+// [QuoteRune] and [QuoteRuneToASCII] are similar but accept runes and
+// return quoted Go rune literals.
+//
+// [Unquote] and [UnquoteChar] unquote Go string and rune literals.
+package strconv
diff --git a/src/strconv/eisel_lemire.go b/src/strconv/eisel_lemire.go
new file mode 100644
index 0000000..03842e5
--- /dev/null
+++ b/src/strconv/eisel_lemire.go
@@ -0,0 +1,884 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv
+
+// This file implements the Eisel-Lemire ParseFloat algorithm, published in
+// 2020 and discussed extensively at
+// https://nigeltao.github.io/blog/2020/eisel-lemire.html
+//
+// The original C++ implementation is at
+// https://github.com/lemire/fast_double_parser/blob/644bef4306059d3be01a04e77d3cc84b379c596f/include/fast_double_parser.h#L840
+//
+// This Go re-implementation closely follows the C re-implementation at
+// https://github.com/google/wuffs/blob/ba3818cb6b473a2ed0b38ecfc07dbbd3a97e8ae7/internal/cgen/base/floatconv-submodule-code.c#L990
+//
+// Additional testing (on over several million test strings) is done by
+// https://github.com/nigeltao/parse-number-fxx-test-data/blob/5280dcfccf6d0b02a65ae282dad0b6d9de50e039/script/test-go-strconv.go
+
+import (
+ "math"
+ "math/bits"
+)
+
+func eiselLemire64(man uint64, exp10 int, neg bool) (f float64, ok bool) {
+ // The terse comments in this function body refer to sections of the
+ // https://nigeltao.github.io/blog/2020/eisel-lemire.html blog post.
+
+ // Exp10 Range.
+ if man == 0 {
+ if neg {
+ f = math.Float64frombits(0x8000000000000000) // Negative zero.
+ }
+ return f, true
+ }
+ if exp10 < detailedPowersOfTenMinExp10 || detailedPowersOfTenMaxExp10 < exp10 {
+ return 0, false
+ }
+
+ // Normalization.
+ clz := bits.LeadingZeros64(man)
+ man <<= uint(clz)
+ const float64ExponentBias = 1023
+ retExp2 := uint64(217706*exp10>>16+64+float64ExponentBias) - uint64(clz)
+
+ // Multiplication.
+ xHi, xLo := bits.Mul64(man, detailedPowersOfTen[exp10-detailedPowersOfTenMinExp10][1])
+
+ // Wider Approximation.
+ if xHi&0x1FF == 0x1FF && xLo+man < man {
+ yHi, yLo := bits.Mul64(man, detailedPowersOfTen[exp10-detailedPowersOfTenMinExp10][0])
+ mergedHi, mergedLo := xHi, xLo+yHi
+ if mergedLo < xLo {
+ mergedHi++
+ }
+ if mergedHi&0x1FF == 0x1FF && mergedLo+1 == 0 && yLo+man < man {
+ return 0, false
+ }
+ xHi, xLo = mergedHi, mergedLo
+ }
+
+ // Shifting to 54 Bits.
+ msb := xHi >> 63
+ retMantissa := xHi >> (msb + 9)
+ retExp2 -= 1 ^ msb
+
+ // Half-way Ambiguity.
+ if xLo == 0 && xHi&0x1FF == 0 && retMantissa&3 == 1 {
+ return 0, false
+ }
+
+ // From 54 to 53 Bits.
+ retMantissa += retMantissa & 1
+ retMantissa >>= 1
+ if retMantissa>>53 > 0 {
+ retMantissa >>= 1
+ retExp2 += 1
+ }
+ // retExp2 is a uint64. Zero or underflow means that we're in subnormal
+ // float64 space. 0x7FF or above means that we're in Inf/NaN float64 space.
+ //
+ // The if block is equivalent to (but has fewer branches than):
+ // if retExp2 <= 0 || retExp2 >= 0x7FF { etc }
+ if retExp2-1 >= 0x7FF-1 {
+ return 0, false
+ }
+ retBits := retExp2<<52 | retMantissa&0x000FFFFFFFFFFFFF
+ if neg {
+ retBits |= 0x8000000000000000
+ }
+ return math.Float64frombits(retBits), true
+}
+
+func eiselLemire32(man uint64, exp10 int, neg bool) (f float32, ok bool) {
+ // The terse comments in this function body refer to sections of the
+ // https://nigeltao.github.io/blog/2020/eisel-lemire.html blog post.
+ //
+ // That blog post discusses the float64 flavor (11 exponent bits with a
+ // -1023 bias, 52 mantissa bits) of the algorithm, but the same approach
+ // applies to the float32 flavor (8 exponent bits with a -127 bias, 23
+ // mantissa bits). The computation here happens with 64-bit values (e.g.
+ // man, xHi, retMantissa) before finally converting to a 32-bit float.
+
+ // Exp10 Range.
+ if man == 0 {
+ if neg {
+ f = math.Float32frombits(0x80000000) // Negative zero.
+ }
+ return f, true
+ }
+ if exp10 < detailedPowersOfTenMinExp10 || detailedPowersOfTenMaxExp10 < exp10 {
+ return 0, false
+ }
+
+ // Normalization.
+ clz := bits.LeadingZeros64(man)
+ man <<= uint(clz)
+ const float32ExponentBias = 127
+ retExp2 := uint64(217706*exp10>>16+64+float32ExponentBias) - uint64(clz)
+
+ // Multiplication.
+ xHi, xLo := bits.Mul64(man, detailedPowersOfTen[exp10-detailedPowersOfTenMinExp10][1])
+
+ // Wider Approximation.
+ if xHi&0x3FFFFFFFFF == 0x3FFFFFFFFF && xLo+man < man {
+ yHi, yLo := bits.Mul64(man, detailedPowersOfTen[exp10-detailedPowersOfTenMinExp10][0])
+ mergedHi, mergedLo := xHi, xLo+yHi
+ if mergedLo < xLo {
+ mergedHi++
+ }
+ if mergedHi&0x3FFFFFFFFF == 0x3FFFFFFFFF && mergedLo+1 == 0 && yLo+man < man {
+ return 0, false
+ }
+ xHi, xLo = mergedHi, mergedLo
+ }
+
+ // Shifting to 54 Bits (and for float32, it's shifting to 25 bits).
+ msb := xHi >> 63
+ retMantissa := xHi >> (msb + 38)
+ retExp2 -= 1 ^ msb
+
+ // Half-way Ambiguity.
+ if xLo == 0 && xHi&0x3FFFFFFFFF == 0 && retMantissa&3 == 1 {
+ return 0, false
+ }
+
+ // From 54 to 53 Bits (and for float32, it's from 25 to 24 bits).
+ retMantissa += retMantissa & 1
+ retMantissa >>= 1
+ if retMantissa>>24 > 0 {
+ retMantissa >>= 1
+ retExp2 += 1
+ }
+ // retExp2 is a uint64. Zero or underflow means that we're in subnormal
+ // float32 space. 0xFF or above means that we're in Inf/NaN float32 space.
+ //
+ // The if block is equivalent to (but has fewer branches than):
+ // if retExp2 <= 0 || retExp2 >= 0xFF { etc }
+ if retExp2-1 >= 0xFF-1 {
+ return 0, false
+ }
+ retBits := retExp2<<23 | retMantissa&0x007FFFFF
+ if neg {
+ retBits |= 0x80000000
+ }
+ return math.Float32frombits(uint32(retBits)), true
+}
+
+// detailedPowersOfTen{Min,Max}Exp10 is the power of 10 represented by the
+// first and last rows of detailedPowersOfTen. Both bounds are inclusive.
+const (
+ detailedPowersOfTenMinExp10 = -348
+ detailedPowersOfTenMaxExp10 = +347
+)
+
+// detailedPowersOfTen contains 128-bit mantissa approximations (rounded down)
+// to the powers of 10. For example:
+//
+// - 1e43 ≈ (0xE596B7B0_C643C719 * (2 ** 79))
+// - 1e43 = (0xE596B7B0_C643C719_6D9CCD05_D0000000 * (2 ** 15))
+//
+// The mantissas are explicitly listed. The exponents are implied by a linear
+// expression with slope 217706.0/65536.0 ≈ log(10)/log(2).
+//
+// The table was generated by
+// https://github.com/google/wuffs/blob/ba3818cb6b473a2ed0b38ecfc07dbbd3a97e8ae7/script/print-mpb-powers-of-10.go
+var detailedPowersOfTen = [...][2]uint64{
+ {0x1732C869CD60E453, 0xFA8FD5A0081C0288}, // 1e-348
+ {0x0E7FBD42205C8EB4, 0x9C99E58405118195}, // 1e-347
+ {0x521FAC92A873B261, 0xC3C05EE50655E1FA}, // 1e-346
+ {0xE6A797B752909EF9, 0xF4B0769E47EB5A78}, // 1e-345
+ {0x9028BED2939A635C, 0x98EE4A22ECF3188B}, // 1e-344
+ {0x7432EE873880FC33, 0xBF29DCABA82FDEAE}, // 1e-343
+ {0x113FAA2906A13B3F, 0xEEF453D6923BD65A}, // 1e-342
+ {0x4AC7CA59A424C507, 0x9558B4661B6565F8}, // 1e-341
+ {0x5D79BCF00D2DF649, 0xBAAEE17FA23EBF76}, // 1e-340
+ {0xF4D82C2C107973DC, 0xE95A99DF8ACE6F53}, // 1e-339
+ {0x79071B9B8A4BE869, 0x91D8A02BB6C10594}, // 1e-338
+ {0x9748E2826CDEE284, 0xB64EC836A47146F9}, // 1e-337
+ {0xFD1B1B2308169B25, 0xE3E27A444D8D98B7}, // 1e-336
+ {0xFE30F0F5E50E20F7, 0x8E6D8C6AB0787F72}, // 1e-335
+ {0xBDBD2D335E51A935, 0xB208EF855C969F4F}, // 1e-334
+ {0xAD2C788035E61382, 0xDE8B2B66B3BC4723}, // 1e-333
+ {0x4C3BCB5021AFCC31, 0x8B16FB203055AC76}, // 1e-332
+ {0xDF4ABE242A1BBF3D, 0xADDCB9E83C6B1793}, // 1e-331
+ {0xD71D6DAD34A2AF0D, 0xD953E8624B85DD78}, // 1e-330
+ {0x8672648C40E5AD68, 0x87D4713D6F33AA6B}, // 1e-329
+ {0x680EFDAF511F18C2, 0xA9C98D8CCB009506}, // 1e-328
+ {0x0212BD1B2566DEF2, 0xD43BF0EFFDC0BA48}, // 1e-327
+ {0x014BB630F7604B57, 0x84A57695FE98746D}, // 1e-326
+ {0x419EA3BD35385E2D, 0xA5CED43B7E3E9188}, // 1e-325
+ {0x52064CAC828675B9, 0xCF42894A5DCE35EA}, // 1e-324
+ {0x7343EFEBD1940993, 0x818995CE7AA0E1B2}, // 1e-323
+ {0x1014EBE6C5F90BF8, 0xA1EBFB4219491A1F}, // 1e-322
+ {0xD41A26E077774EF6, 0xCA66FA129F9B60A6}, // 1e-321
+ {0x8920B098955522B4, 0xFD00B897478238D0}, // 1e-320
+ {0x55B46E5F5D5535B0, 0x9E20735E8CB16382}, // 1e-319
+ {0xEB2189F734AA831D, 0xC5A890362FDDBC62}, // 1e-318
+ {0xA5E9EC7501D523E4, 0xF712B443BBD52B7B}, // 1e-317
+ {0x47B233C92125366E, 0x9A6BB0AA55653B2D}, // 1e-316
+ {0x999EC0BB696E840A, 0xC1069CD4EABE89F8}, // 1e-315
+ {0xC00670EA43CA250D, 0xF148440A256E2C76}, // 1e-314
+ {0x380406926A5E5728, 0x96CD2A865764DBCA}, // 1e-313
+ {0xC605083704F5ECF2, 0xBC807527ED3E12BC}, // 1e-312
+ {0xF7864A44C633682E, 0xEBA09271E88D976B}, // 1e-311
+ {0x7AB3EE6AFBE0211D, 0x93445B8731587EA3}, // 1e-310
+ {0x5960EA05BAD82964, 0xB8157268FDAE9E4C}, // 1e-309
+ {0x6FB92487298E33BD, 0xE61ACF033D1A45DF}, // 1e-308
+ {0xA5D3B6D479F8E056, 0x8FD0C16206306BAB}, // 1e-307
+ {0x8F48A4899877186C, 0xB3C4F1BA87BC8696}, // 1e-306
+ {0x331ACDABFE94DE87, 0xE0B62E2929ABA83C}, // 1e-305
+ {0x9FF0C08B7F1D0B14, 0x8C71DCD9BA0B4925}, // 1e-304
+ {0x07ECF0AE5EE44DD9, 0xAF8E5410288E1B6F}, // 1e-303
+ {0xC9E82CD9F69D6150, 0xDB71E91432B1A24A}, // 1e-302
+ {0xBE311C083A225CD2, 0x892731AC9FAF056E}, // 1e-301
+ {0x6DBD630A48AAF406, 0xAB70FE17C79AC6CA}, // 1e-300
+ {0x092CBBCCDAD5B108, 0xD64D3D9DB981787D}, // 1e-299
+ {0x25BBF56008C58EA5, 0x85F0468293F0EB4E}, // 1e-298
+ {0xAF2AF2B80AF6F24E, 0xA76C582338ED2621}, // 1e-297
+ {0x1AF5AF660DB4AEE1, 0xD1476E2C07286FAA}, // 1e-296
+ {0x50D98D9FC890ED4D, 0x82CCA4DB847945CA}, // 1e-295
+ {0xE50FF107BAB528A0, 0xA37FCE126597973C}, // 1e-294
+ {0x1E53ED49A96272C8, 0xCC5FC196FEFD7D0C}, // 1e-293
+ {0x25E8E89C13BB0F7A, 0xFF77B1FCBEBCDC4F}, // 1e-292
+ {0x77B191618C54E9AC, 0x9FAACF3DF73609B1}, // 1e-291
+ {0xD59DF5B9EF6A2417, 0xC795830D75038C1D}, // 1e-290
+ {0x4B0573286B44AD1D, 0xF97AE3D0D2446F25}, // 1e-289
+ {0x4EE367F9430AEC32, 0x9BECCE62836AC577}, // 1e-288
+ {0x229C41F793CDA73F, 0xC2E801FB244576D5}, // 1e-287
+ {0x6B43527578C1110F, 0xF3A20279ED56D48A}, // 1e-286
+ {0x830A13896B78AAA9, 0x9845418C345644D6}, // 1e-285
+ {0x23CC986BC656D553, 0xBE5691EF416BD60C}, // 1e-284
+ {0x2CBFBE86B7EC8AA8, 0xEDEC366B11C6CB8F}, // 1e-283
+ {0x7BF7D71432F3D6A9, 0x94B3A202EB1C3F39}, // 1e-282
+ {0xDAF5CCD93FB0CC53, 0xB9E08A83A5E34F07}, // 1e-281
+ {0xD1B3400F8F9CFF68, 0xE858AD248F5C22C9}, // 1e-280
+ {0x23100809B9C21FA1, 0x91376C36D99995BE}, // 1e-279
+ {0xABD40A0C2832A78A, 0xB58547448FFFFB2D}, // 1e-278
+ {0x16C90C8F323F516C, 0xE2E69915B3FFF9F9}, // 1e-277
+ {0xAE3DA7D97F6792E3, 0x8DD01FAD907FFC3B}, // 1e-276
+ {0x99CD11CFDF41779C, 0xB1442798F49FFB4A}, // 1e-275
+ {0x40405643D711D583, 0xDD95317F31C7FA1D}, // 1e-274
+ {0x482835EA666B2572, 0x8A7D3EEF7F1CFC52}, // 1e-273
+ {0xDA3243650005EECF, 0xAD1C8EAB5EE43B66}, // 1e-272
+ {0x90BED43E40076A82, 0xD863B256369D4A40}, // 1e-271
+ {0x5A7744A6E804A291, 0x873E4F75E2224E68}, // 1e-270
+ {0x711515D0A205CB36, 0xA90DE3535AAAE202}, // 1e-269
+ {0x0D5A5B44CA873E03, 0xD3515C2831559A83}, // 1e-268
+ {0xE858790AFE9486C2, 0x8412D9991ED58091}, // 1e-267
+ {0x626E974DBE39A872, 0xA5178FFF668AE0B6}, // 1e-266
+ {0xFB0A3D212DC8128F, 0xCE5D73FF402D98E3}, // 1e-265
+ {0x7CE66634BC9D0B99, 0x80FA687F881C7F8E}, // 1e-264
+ {0x1C1FFFC1EBC44E80, 0xA139029F6A239F72}, // 1e-263
+ {0xA327FFB266B56220, 0xC987434744AC874E}, // 1e-262
+ {0x4BF1FF9F0062BAA8, 0xFBE9141915D7A922}, // 1e-261
+ {0x6F773FC3603DB4A9, 0x9D71AC8FADA6C9B5}, // 1e-260
+ {0xCB550FB4384D21D3, 0xC4CE17B399107C22}, // 1e-259
+ {0x7E2A53A146606A48, 0xF6019DA07F549B2B}, // 1e-258
+ {0x2EDA7444CBFC426D, 0x99C102844F94E0FB}, // 1e-257
+ {0xFA911155FEFB5308, 0xC0314325637A1939}, // 1e-256
+ {0x793555AB7EBA27CA, 0xF03D93EEBC589F88}, // 1e-255
+ {0x4BC1558B2F3458DE, 0x96267C7535B763B5}, // 1e-254
+ {0x9EB1AAEDFB016F16, 0xBBB01B9283253CA2}, // 1e-253
+ {0x465E15A979C1CADC, 0xEA9C227723EE8BCB}, // 1e-252
+ {0x0BFACD89EC191EC9, 0x92A1958A7675175F}, // 1e-251
+ {0xCEF980EC671F667B, 0xB749FAED14125D36}, // 1e-250
+ {0x82B7E12780E7401A, 0xE51C79A85916F484}, // 1e-249
+ {0xD1B2ECB8B0908810, 0x8F31CC0937AE58D2}, // 1e-248
+ {0x861FA7E6DCB4AA15, 0xB2FE3F0B8599EF07}, // 1e-247
+ {0x67A791E093E1D49A, 0xDFBDCECE67006AC9}, // 1e-246
+ {0xE0C8BB2C5C6D24E0, 0x8BD6A141006042BD}, // 1e-245
+ {0x58FAE9F773886E18, 0xAECC49914078536D}, // 1e-244
+ {0xAF39A475506A899E, 0xDA7F5BF590966848}, // 1e-243
+ {0x6D8406C952429603, 0x888F99797A5E012D}, // 1e-242
+ {0xC8E5087BA6D33B83, 0xAAB37FD7D8F58178}, // 1e-241
+ {0xFB1E4A9A90880A64, 0xD5605FCDCF32E1D6}, // 1e-240
+ {0x5CF2EEA09A55067F, 0x855C3BE0A17FCD26}, // 1e-239
+ {0xF42FAA48C0EA481E, 0xA6B34AD8C9DFC06F}, // 1e-238
+ {0xF13B94DAF124DA26, 0xD0601D8EFC57B08B}, // 1e-237
+ {0x76C53D08D6B70858, 0x823C12795DB6CE57}, // 1e-236
+ {0x54768C4B0C64CA6E, 0xA2CB1717B52481ED}, // 1e-235
+ {0xA9942F5DCF7DFD09, 0xCB7DDCDDA26DA268}, // 1e-234
+ {0xD3F93B35435D7C4C, 0xFE5D54150B090B02}, // 1e-233
+ {0xC47BC5014A1A6DAF, 0x9EFA548D26E5A6E1}, // 1e-232
+ {0x359AB6419CA1091B, 0xC6B8E9B0709F109A}, // 1e-231
+ {0xC30163D203C94B62, 0xF867241C8CC6D4C0}, // 1e-230
+ {0x79E0DE63425DCF1D, 0x9B407691D7FC44F8}, // 1e-229
+ {0x985915FC12F542E4, 0xC21094364DFB5636}, // 1e-228
+ {0x3E6F5B7B17B2939D, 0xF294B943E17A2BC4}, // 1e-227
+ {0xA705992CEECF9C42, 0x979CF3CA6CEC5B5A}, // 1e-226
+ {0x50C6FF782A838353, 0xBD8430BD08277231}, // 1e-225
+ {0xA4F8BF5635246428, 0xECE53CEC4A314EBD}, // 1e-224
+ {0x871B7795E136BE99, 0x940F4613AE5ED136}, // 1e-223
+ {0x28E2557B59846E3F, 0xB913179899F68584}, // 1e-222
+ {0x331AEADA2FE589CF, 0xE757DD7EC07426E5}, // 1e-221
+ {0x3FF0D2C85DEF7621, 0x9096EA6F3848984F}, // 1e-220
+ {0x0FED077A756B53A9, 0xB4BCA50B065ABE63}, // 1e-219
+ {0xD3E8495912C62894, 0xE1EBCE4DC7F16DFB}, // 1e-218
+ {0x64712DD7ABBBD95C, 0x8D3360F09CF6E4BD}, // 1e-217
+ {0xBD8D794D96AACFB3, 0xB080392CC4349DEC}, // 1e-216
+ {0xECF0D7A0FC5583A0, 0xDCA04777F541C567}, // 1e-215
+ {0xF41686C49DB57244, 0x89E42CAAF9491B60}, // 1e-214
+ {0x311C2875C522CED5, 0xAC5D37D5B79B6239}, // 1e-213
+ {0x7D633293366B828B, 0xD77485CB25823AC7}, // 1e-212
+ {0xAE5DFF9C02033197, 0x86A8D39EF77164BC}, // 1e-211
+ {0xD9F57F830283FDFC, 0xA8530886B54DBDEB}, // 1e-210
+ {0xD072DF63C324FD7B, 0xD267CAA862A12D66}, // 1e-209
+ {0x4247CB9E59F71E6D, 0x8380DEA93DA4BC60}, // 1e-208
+ {0x52D9BE85F074E608, 0xA46116538D0DEB78}, // 1e-207
+ {0x67902E276C921F8B, 0xCD795BE870516656}, // 1e-206
+ {0x00BA1CD8A3DB53B6, 0x806BD9714632DFF6}, // 1e-205
+ {0x80E8A40ECCD228A4, 0xA086CFCD97BF97F3}, // 1e-204
+ {0x6122CD128006B2CD, 0xC8A883C0FDAF7DF0}, // 1e-203
+ {0x796B805720085F81, 0xFAD2A4B13D1B5D6C}, // 1e-202
+ {0xCBE3303674053BB0, 0x9CC3A6EEC6311A63}, // 1e-201
+ {0xBEDBFC4411068A9C, 0xC3F490AA77BD60FC}, // 1e-200
+ {0xEE92FB5515482D44, 0xF4F1B4D515ACB93B}, // 1e-199
+ {0x751BDD152D4D1C4A, 0x991711052D8BF3C5}, // 1e-198
+ {0xD262D45A78A0635D, 0xBF5CD54678EEF0B6}, // 1e-197
+ {0x86FB897116C87C34, 0xEF340A98172AACE4}, // 1e-196
+ {0xD45D35E6AE3D4DA0, 0x9580869F0E7AAC0E}, // 1e-195
+ {0x8974836059CCA109, 0xBAE0A846D2195712}, // 1e-194
+ {0x2BD1A438703FC94B, 0xE998D258869FACD7}, // 1e-193
+ {0x7B6306A34627DDCF, 0x91FF83775423CC06}, // 1e-192
+ {0x1A3BC84C17B1D542, 0xB67F6455292CBF08}, // 1e-191
+ {0x20CABA5F1D9E4A93, 0xE41F3D6A7377EECA}, // 1e-190
+ {0x547EB47B7282EE9C, 0x8E938662882AF53E}, // 1e-189
+ {0xE99E619A4F23AA43, 0xB23867FB2A35B28D}, // 1e-188
+ {0x6405FA00E2EC94D4, 0xDEC681F9F4C31F31}, // 1e-187
+ {0xDE83BC408DD3DD04, 0x8B3C113C38F9F37E}, // 1e-186
+ {0x9624AB50B148D445, 0xAE0B158B4738705E}, // 1e-185
+ {0x3BADD624DD9B0957, 0xD98DDAEE19068C76}, // 1e-184
+ {0xE54CA5D70A80E5D6, 0x87F8A8D4CFA417C9}, // 1e-183
+ {0x5E9FCF4CCD211F4C, 0xA9F6D30A038D1DBC}, // 1e-182
+ {0x7647C3200069671F, 0xD47487CC8470652B}, // 1e-181
+ {0x29ECD9F40041E073, 0x84C8D4DFD2C63F3B}, // 1e-180
+ {0xF468107100525890, 0xA5FB0A17C777CF09}, // 1e-179
+ {0x7182148D4066EEB4, 0xCF79CC9DB955C2CC}, // 1e-178
+ {0xC6F14CD848405530, 0x81AC1FE293D599BF}, // 1e-177
+ {0xB8ADA00E5A506A7C, 0xA21727DB38CB002F}, // 1e-176
+ {0xA6D90811F0E4851C, 0xCA9CF1D206FDC03B}, // 1e-175
+ {0x908F4A166D1DA663, 0xFD442E4688BD304A}, // 1e-174
+ {0x9A598E4E043287FE, 0x9E4A9CEC15763E2E}, // 1e-173
+ {0x40EFF1E1853F29FD, 0xC5DD44271AD3CDBA}, // 1e-172
+ {0xD12BEE59E68EF47C, 0xF7549530E188C128}, // 1e-171
+ {0x82BB74F8301958CE, 0x9A94DD3E8CF578B9}, // 1e-170
+ {0xE36A52363C1FAF01, 0xC13A148E3032D6E7}, // 1e-169
+ {0xDC44E6C3CB279AC1, 0xF18899B1BC3F8CA1}, // 1e-168
+ {0x29AB103A5EF8C0B9, 0x96F5600F15A7B7E5}, // 1e-167
+ {0x7415D448F6B6F0E7, 0xBCB2B812DB11A5DE}, // 1e-166
+ {0x111B495B3464AD21, 0xEBDF661791D60F56}, // 1e-165
+ {0xCAB10DD900BEEC34, 0x936B9FCEBB25C995}, // 1e-164
+ {0x3D5D514F40EEA742, 0xB84687C269EF3BFB}, // 1e-163
+ {0x0CB4A5A3112A5112, 0xE65829B3046B0AFA}, // 1e-162
+ {0x47F0E785EABA72AB, 0x8FF71A0FE2C2E6DC}, // 1e-161
+ {0x59ED216765690F56, 0xB3F4E093DB73A093}, // 1e-160
+ {0x306869C13EC3532C, 0xE0F218B8D25088B8}, // 1e-159
+ {0x1E414218C73A13FB, 0x8C974F7383725573}, // 1e-158
+ {0xE5D1929EF90898FA, 0xAFBD2350644EEACF}, // 1e-157
+ {0xDF45F746B74ABF39, 0xDBAC6C247D62A583}, // 1e-156
+ {0x6B8BBA8C328EB783, 0x894BC396CE5DA772}, // 1e-155
+ {0x066EA92F3F326564, 0xAB9EB47C81F5114F}, // 1e-154
+ {0xC80A537B0EFEFEBD, 0xD686619BA27255A2}, // 1e-153
+ {0xBD06742CE95F5F36, 0x8613FD0145877585}, // 1e-152
+ {0x2C48113823B73704, 0xA798FC4196E952E7}, // 1e-151
+ {0xF75A15862CA504C5, 0xD17F3B51FCA3A7A0}, // 1e-150
+ {0x9A984D73DBE722FB, 0x82EF85133DE648C4}, // 1e-149
+ {0xC13E60D0D2E0EBBA, 0xA3AB66580D5FDAF5}, // 1e-148
+ {0x318DF905079926A8, 0xCC963FEE10B7D1B3}, // 1e-147
+ {0xFDF17746497F7052, 0xFFBBCFE994E5C61F}, // 1e-146
+ {0xFEB6EA8BEDEFA633, 0x9FD561F1FD0F9BD3}, // 1e-145
+ {0xFE64A52EE96B8FC0, 0xC7CABA6E7C5382C8}, // 1e-144
+ {0x3DFDCE7AA3C673B0, 0xF9BD690A1B68637B}, // 1e-143
+ {0x06BEA10CA65C084E, 0x9C1661A651213E2D}, // 1e-142
+ {0x486E494FCFF30A62, 0xC31BFA0FE5698DB8}, // 1e-141
+ {0x5A89DBA3C3EFCCFA, 0xF3E2F893DEC3F126}, // 1e-140
+ {0xF89629465A75E01C, 0x986DDB5C6B3A76B7}, // 1e-139
+ {0xF6BBB397F1135823, 0xBE89523386091465}, // 1e-138
+ {0x746AA07DED582E2C, 0xEE2BA6C0678B597F}, // 1e-137
+ {0xA8C2A44EB4571CDC, 0x94DB483840B717EF}, // 1e-136
+ {0x92F34D62616CE413, 0xBA121A4650E4DDEB}, // 1e-135
+ {0x77B020BAF9C81D17, 0xE896A0D7E51E1566}, // 1e-134
+ {0x0ACE1474DC1D122E, 0x915E2486EF32CD60}, // 1e-133
+ {0x0D819992132456BA, 0xB5B5ADA8AAFF80B8}, // 1e-132
+ {0x10E1FFF697ED6C69, 0xE3231912D5BF60E6}, // 1e-131
+ {0xCA8D3FFA1EF463C1, 0x8DF5EFABC5979C8F}, // 1e-130
+ {0xBD308FF8A6B17CB2, 0xB1736B96B6FD83B3}, // 1e-129
+ {0xAC7CB3F6D05DDBDE, 0xDDD0467C64BCE4A0}, // 1e-128
+ {0x6BCDF07A423AA96B, 0x8AA22C0DBEF60EE4}, // 1e-127
+ {0x86C16C98D2C953C6, 0xAD4AB7112EB3929D}, // 1e-126
+ {0xE871C7BF077BA8B7, 0xD89D64D57A607744}, // 1e-125
+ {0x11471CD764AD4972, 0x87625F056C7C4A8B}, // 1e-124
+ {0xD598E40D3DD89BCF, 0xA93AF6C6C79B5D2D}, // 1e-123
+ {0x4AFF1D108D4EC2C3, 0xD389B47879823479}, // 1e-122
+ {0xCEDF722A585139BA, 0x843610CB4BF160CB}, // 1e-121
+ {0xC2974EB4EE658828, 0xA54394FE1EEDB8FE}, // 1e-120
+ {0x733D226229FEEA32, 0xCE947A3DA6A9273E}, // 1e-119
+ {0x0806357D5A3F525F, 0x811CCC668829B887}, // 1e-118
+ {0xCA07C2DCB0CF26F7, 0xA163FF802A3426A8}, // 1e-117
+ {0xFC89B393DD02F0B5, 0xC9BCFF6034C13052}, // 1e-116
+ {0xBBAC2078D443ACE2, 0xFC2C3F3841F17C67}, // 1e-115
+ {0xD54B944B84AA4C0D, 0x9D9BA7832936EDC0}, // 1e-114
+ {0x0A9E795E65D4DF11, 0xC5029163F384A931}, // 1e-113
+ {0x4D4617B5FF4A16D5, 0xF64335BCF065D37D}, // 1e-112
+ {0x504BCED1BF8E4E45, 0x99EA0196163FA42E}, // 1e-111
+ {0xE45EC2862F71E1D6, 0xC06481FB9BCF8D39}, // 1e-110
+ {0x5D767327BB4E5A4C, 0xF07DA27A82C37088}, // 1e-109
+ {0x3A6A07F8D510F86F, 0x964E858C91BA2655}, // 1e-108
+ {0x890489F70A55368B, 0xBBE226EFB628AFEA}, // 1e-107
+ {0x2B45AC74CCEA842E, 0xEADAB0ABA3B2DBE5}, // 1e-106
+ {0x3B0B8BC90012929D, 0x92C8AE6B464FC96F}, // 1e-105
+ {0x09CE6EBB40173744, 0xB77ADA0617E3BBCB}, // 1e-104
+ {0xCC420A6A101D0515, 0xE55990879DDCAABD}, // 1e-103
+ {0x9FA946824A12232D, 0x8F57FA54C2A9EAB6}, // 1e-102
+ {0x47939822DC96ABF9, 0xB32DF8E9F3546564}, // 1e-101
+ {0x59787E2B93BC56F7, 0xDFF9772470297EBD}, // 1e-100
+ {0x57EB4EDB3C55B65A, 0x8BFBEA76C619EF36}, // 1e-99
+ {0xEDE622920B6B23F1, 0xAEFAE51477A06B03}, // 1e-98
+ {0xE95FAB368E45ECED, 0xDAB99E59958885C4}, // 1e-97
+ {0x11DBCB0218EBB414, 0x88B402F7FD75539B}, // 1e-96
+ {0xD652BDC29F26A119, 0xAAE103B5FCD2A881}, // 1e-95
+ {0x4BE76D3346F0495F, 0xD59944A37C0752A2}, // 1e-94
+ {0x6F70A4400C562DDB, 0x857FCAE62D8493A5}, // 1e-93
+ {0xCB4CCD500F6BB952, 0xA6DFBD9FB8E5B88E}, // 1e-92
+ {0x7E2000A41346A7A7, 0xD097AD07A71F26B2}, // 1e-91
+ {0x8ED400668C0C28C8, 0x825ECC24C873782F}, // 1e-90
+ {0x728900802F0F32FA, 0xA2F67F2DFA90563B}, // 1e-89
+ {0x4F2B40A03AD2FFB9, 0xCBB41EF979346BCA}, // 1e-88
+ {0xE2F610C84987BFA8, 0xFEA126B7D78186BC}, // 1e-87
+ {0x0DD9CA7D2DF4D7C9, 0x9F24B832E6B0F436}, // 1e-86
+ {0x91503D1C79720DBB, 0xC6EDE63FA05D3143}, // 1e-85
+ {0x75A44C6397CE912A, 0xF8A95FCF88747D94}, // 1e-84
+ {0xC986AFBE3EE11ABA, 0x9B69DBE1B548CE7C}, // 1e-83
+ {0xFBE85BADCE996168, 0xC24452DA229B021B}, // 1e-82
+ {0xFAE27299423FB9C3, 0xF2D56790AB41C2A2}, // 1e-81
+ {0xDCCD879FC967D41A, 0x97C560BA6B0919A5}, // 1e-80
+ {0x5400E987BBC1C920, 0xBDB6B8E905CB600F}, // 1e-79
+ {0x290123E9AAB23B68, 0xED246723473E3813}, // 1e-78
+ {0xF9A0B6720AAF6521, 0x9436C0760C86E30B}, // 1e-77
+ {0xF808E40E8D5B3E69, 0xB94470938FA89BCE}, // 1e-76
+ {0xB60B1D1230B20E04, 0xE7958CB87392C2C2}, // 1e-75
+ {0xB1C6F22B5E6F48C2, 0x90BD77F3483BB9B9}, // 1e-74
+ {0x1E38AEB6360B1AF3, 0xB4ECD5F01A4AA828}, // 1e-73
+ {0x25C6DA63C38DE1B0, 0xE2280B6C20DD5232}, // 1e-72
+ {0x579C487E5A38AD0E, 0x8D590723948A535F}, // 1e-71
+ {0x2D835A9DF0C6D851, 0xB0AF48EC79ACE837}, // 1e-70
+ {0xF8E431456CF88E65, 0xDCDB1B2798182244}, // 1e-69
+ {0x1B8E9ECB641B58FF, 0x8A08F0F8BF0F156B}, // 1e-68
+ {0xE272467E3D222F3F, 0xAC8B2D36EED2DAC5}, // 1e-67
+ {0x5B0ED81DCC6ABB0F, 0xD7ADF884AA879177}, // 1e-66
+ {0x98E947129FC2B4E9, 0x86CCBB52EA94BAEA}, // 1e-65
+ {0x3F2398D747B36224, 0xA87FEA27A539E9A5}, // 1e-64
+ {0x8EEC7F0D19A03AAD, 0xD29FE4B18E88640E}, // 1e-63
+ {0x1953CF68300424AC, 0x83A3EEEEF9153E89}, // 1e-62
+ {0x5FA8C3423C052DD7, 0xA48CEAAAB75A8E2B}, // 1e-61
+ {0x3792F412CB06794D, 0xCDB02555653131B6}, // 1e-60
+ {0xE2BBD88BBEE40BD0, 0x808E17555F3EBF11}, // 1e-59
+ {0x5B6ACEAEAE9D0EC4, 0xA0B19D2AB70E6ED6}, // 1e-58
+ {0xF245825A5A445275, 0xC8DE047564D20A8B}, // 1e-57
+ {0xEED6E2F0F0D56712, 0xFB158592BE068D2E}, // 1e-56
+ {0x55464DD69685606B, 0x9CED737BB6C4183D}, // 1e-55
+ {0xAA97E14C3C26B886, 0xC428D05AA4751E4C}, // 1e-54
+ {0xD53DD99F4B3066A8, 0xF53304714D9265DF}, // 1e-53
+ {0xE546A8038EFE4029, 0x993FE2C6D07B7FAB}, // 1e-52
+ {0xDE98520472BDD033, 0xBF8FDB78849A5F96}, // 1e-51
+ {0x963E66858F6D4440, 0xEF73D256A5C0F77C}, // 1e-50
+ {0xDDE7001379A44AA8, 0x95A8637627989AAD}, // 1e-49
+ {0x5560C018580D5D52, 0xBB127C53B17EC159}, // 1e-48
+ {0xAAB8F01E6E10B4A6, 0xE9D71B689DDE71AF}, // 1e-47
+ {0xCAB3961304CA70E8, 0x9226712162AB070D}, // 1e-46
+ {0x3D607B97C5FD0D22, 0xB6B00D69BB55C8D1}, // 1e-45
+ {0x8CB89A7DB77C506A, 0xE45C10C42A2B3B05}, // 1e-44
+ {0x77F3608E92ADB242, 0x8EB98A7A9A5B04E3}, // 1e-43
+ {0x55F038B237591ED3, 0xB267ED1940F1C61C}, // 1e-42
+ {0x6B6C46DEC52F6688, 0xDF01E85F912E37A3}, // 1e-41
+ {0x2323AC4B3B3DA015, 0x8B61313BBABCE2C6}, // 1e-40
+ {0xABEC975E0A0D081A, 0xAE397D8AA96C1B77}, // 1e-39
+ {0x96E7BD358C904A21, 0xD9C7DCED53C72255}, // 1e-38
+ {0x7E50D64177DA2E54, 0x881CEA14545C7575}, // 1e-37
+ {0xDDE50BD1D5D0B9E9, 0xAA242499697392D2}, // 1e-36
+ {0x955E4EC64B44E864, 0xD4AD2DBFC3D07787}, // 1e-35
+ {0xBD5AF13BEF0B113E, 0x84EC3C97DA624AB4}, // 1e-34
+ {0xECB1AD8AEACDD58E, 0xA6274BBDD0FADD61}, // 1e-33
+ {0x67DE18EDA5814AF2, 0xCFB11EAD453994BA}, // 1e-32
+ {0x80EACF948770CED7, 0x81CEB32C4B43FCF4}, // 1e-31
+ {0xA1258379A94D028D, 0xA2425FF75E14FC31}, // 1e-30
+ {0x096EE45813A04330, 0xCAD2F7F5359A3B3E}, // 1e-29
+ {0x8BCA9D6E188853FC, 0xFD87B5F28300CA0D}, // 1e-28
+ {0x775EA264CF55347D, 0x9E74D1B791E07E48}, // 1e-27
+ {0x95364AFE032A819D, 0xC612062576589DDA}, // 1e-26
+ {0x3A83DDBD83F52204, 0xF79687AED3EEC551}, // 1e-25
+ {0xC4926A9672793542, 0x9ABE14CD44753B52}, // 1e-24
+ {0x75B7053C0F178293, 0xC16D9A0095928A27}, // 1e-23
+ {0x5324C68B12DD6338, 0xF1C90080BAF72CB1}, // 1e-22
+ {0xD3F6FC16EBCA5E03, 0x971DA05074DA7BEE}, // 1e-21
+ {0x88F4BB1CA6BCF584, 0xBCE5086492111AEA}, // 1e-20
+ {0x2B31E9E3D06C32E5, 0xEC1E4A7DB69561A5}, // 1e-19
+ {0x3AFF322E62439FCF, 0x9392EE8E921D5D07}, // 1e-18
+ {0x09BEFEB9FAD487C2, 0xB877AA3236A4B449}, // 1e-17
+ {0x4C2EBE687989A9B3, 0xE69594BEC44DE15B}, // 1e-16
+ {0x0F9D37014BF60A10, 0x901D7CF73AB0ACD9}, // 1e-15
+ {0x538484C19EF38C94, 0xB424DC35095CD80F}, // 1e-14
+ {0x2865A5F206B06FB9, 0xE12E13424BB40E13}, // 1e-13
+ {0xF93F87B7442E45D3, 0x8CBCCC096F5088CB}, // 1e-12
+ {0xF78F69A51539D748, 0xAFEBFF0BCB24AAFE}, // 1e-11
+ {0xB573440E5A884D1B, 0xDBE6FECEBDEDD5BE}, // 1e-10
+ {0x31680A88F8953030, 0x89705F4136B4A597}, // 1e-9
+ {0xFDC20D2B36BA7C3D, 0xABCC77118461CEFC}, // 1e-8
+ {0x3D32907604691B4C, 0xD6BF94D5E57A42BC}, // 1e-7
+ {0xA63F9A49C2C1B10F, 0x8637BD05AF6C69B5}, // 1e-6
+ {0x0FCF80DC33721D53, 0xA7C5AC471B478423}, // 1e-5
+ {0xD3C36113404EA4A8, 0xD1B71758E219652B}, // 1e-4
+ {0x645A1CAC083126E9, 0x83126E978D4FDF3B}, // 1e-3
+ {0x3D70A3D70A3D70A3, 0xA3D70A3D70A3D70A}, // 1e-2
+ {0xCCCCCCCCCCCCCCCC, 0xCCCCCCCCCCCCCCCC}, // 1e-1
+ {0x0000000000000000, 0x8000000000000000}, // 1e0
+ {0x0000000000000000, 0xA000000000000000}, // 1e1
+ {0x0000000000000000, 0xC800000000000000}, // 1e2
+ {0x0000000000000000, 0xFA00000000000000}, // 1e3
+ {0x0000000000000000, 0x9C40000000000000}, // 1e4
+ {0x0000000000000000, 0xC350000000000000}, // 1e5
+ {0x0000000000000000, 0xF424000000000000}, // 1e6
+ {0x0000000000000000, 0x9896800000000000}, // 1e7
+ {0x0000000000000000, 0xBEBC200000000000}, // 1e8
+ {0x0000000000000000, 0xEE6B280000000000}, // 1e9
+ {0x0000000000000000, 0x9502F90000000000}, // 1e10
+ {0x0000000000000000, 0xBA43B74000000000}, // 1e11
+ {0x0000000000000000, 0xE8D4A51000000000}, // 1e12
+ {0x0000000000000000, 0x9184E72A00000000}, // 1e13
+ {0x0000000000000000, 0xB5E620F480000000}, // 1e14
+ {0x0000000000000000, 0xE35FA931A0000000}, // 1e15
+ {0x0000000000000000, 0x8E1BC9BF04000000}, // 1e16
+ {0x0000000000000000, 0xB1A2BC2EC5000000}, // 1e17
+ {0x0000000000000000, 0xDE0B6B3A76400000}, // 1e18
+ {0x0000000000000000, 0x8AC7230489E80000}, // 1e19
+ {0x0000000000000000, 0xAD78EBC5AC620000}, // 1e20
+ {0x0000000000000000, 0xD8D726B7177A8000}, // 1e21
+ {0x0000000000000000, 0x878678326EAC9000}, // 1e22
+ {0x0000000000000000, 0xA968163F0A57B400}, // 1e23
+ {0x0000000000000000, 0xD3C21BCECCEDA100}, // 1e24
+ {0x0000000000000000, 0x84595161401484A0}, // 1e25
+ {0x0000000000000000, 0xA56FA5B99019A5C8}, // 1e26
+ {0x0000000000000000, 0xCECB8F27F4200F3A}, // 1e27
+ {0x4000000000000000, 0x813F3978F8940984}, // 1e28
+ {0x5000000000000000, 0xA18F07D736B90BE5}, // 1e29
+ {0xA400000000000000, 0xC9F2C9CD04674EDE}, // 1e30
+ {0x4D00000000000000, 0xFC6F7C4045812296}, // 1e31
+ {0xF020000000000000, 0x9DC5ADA82B70B59D}, // 1e32
+ {0x6C28000000000000, 0xC5371912364CE305}, // 1e33
+ {0xC732000000000000, 0xF684DF56C3E01BC6}, // 1e34
+ {0x3C7F400000000000, 0x9A130B963A6C115C}, // 1e35
+ {0x4B9F100000000000, 0xC097CE7BC90715B3}, // 1e36
+ {0x1E86D40000000000, 0xF0BDC21ABB48DB20}, // 1e37
+ {0x1314448000000000, 0x96769950B50D88F4}, // 1e38
+ {0x17D955A000000000, 0xBC143FA4E250EB31}, // 1e39
+ {0x5DCFAB0800000000, 0xEB194F8E1AE525FD}, // 1e40
+ {0x5AA1CAE500000000, 0x92EFD1B8D0CF37BE}, // 1e41
+ {0xF14A3D9E40000000, 0xB7ABC627050305AD}, // 1e42
+ {0x6D9CCD05D0000000, 0xE596B7B0C643C719}, // 1e43
+ {0xE4820023A2000000, 0x8F7E32CE7BEA5C6F}, // 1e44
+ {0xDDA2802C8A800000, 0xB35DBF821AE4F38B}, // 1e45
+ {0xD50B2037AD200000, 0xE0352F62A19E306E}, // 1e46
+ {0x4526F422CC340000, 0x8C213D9DA502DE45}, // 1e47
+ {0x9670B12B7F410000, 0xAF298D050E4395D6}, // 1e48
+ {0x3C0CDD765F114000, 0xDAF3F04651D47B4C}, // 1e49
+ {0xA5880A69FB6AC800, 0x88D8762BF324CD0F}, // 1e50
+ {0x8EEA0D047A457A00, 0xAB0E93B6EFEE0053}, // 1e51
+ {0x72A4904598D6D880, 0xD5D238A4ABE98068}, // 1e52
+ {0x47A6DA2B7F864750, 0x85A36366EB71F041}, // 1e53
+ {0x999090B65F67D924, 0xA70C3C40A64E6C51}, // 1e54
+ {0xFFF4B4E3F741CF6D, 0xD0CF4B50CFE20765}, // 1e55
+ {0xBFF8F10E7A8921A4, 0x82818F1281ED449F}, // 1e56
+ {0xAFF72D52192B6A0D, 0xA321F2D7226895C7}, // 1e57
+ {0x9BF4F8A69F764490, 0xCBEA6F8CEB02BB39}, // 1e58
+ {0x02F236D04753D5B4, 0xFEE50B7025C36A08}, // 1e59
+ {0x01D762422C946590, 0x9F4F2726179A2245}, // 1e60
+ {0x424D3AD2B7B97EF5, 0xC722F0EF9D80AAD6}, // 1e61
+ {0xD2E0898765A7DEB2, 0xF8EBAD2B84E0D58B}, // 1e62
+ {0x63CC55F49F88EB2F, 0x9B934C3B330C8577}, // 1e63
+ {0x3CBF6B71C76B25FB, 0xC2781F49FFCFA6D5}, // 1e64
+ {0x8BEF464E3945EF7A, 0xF316271C7FC3908A}, // 1e65
+ {0x97758BF0E3CBB5AC, 0x97EDD871CFDA3A56}, // 1e66
+ {0x3D52EEED1CBEA317, 0xBDE94E8E43D0C8EC}, // 1e67
+ {0x4CA7AAA863EE4BDD, 0xED63A231D4C4FB27}, // 1e68
+ {0x8FE8CAA93E74EF6A, 0x945E455F24FB1CF8}, // 1e69
+ {0xB3E2FD538E122B44, 0xB975D6B6EE39E436}, // 1e70
+ {0x60DBBCA87196B616, 0xE7D34C64A9C85D44}, // 1e71
+ {0xBC8955E946FE31CD, 0x90E40FBEEA1D3A4A}, // 1e72
+ {0x6BABAB6398BDBE41, 0xB51D13AEA4A488DD}, // 1e73
+ {0xC696963C7EED2DD1, 0xE264589A4DCDAB14}, // 1e74
+ {0xFC1E1DE5CF543CA2, 0x8D7EB76070A08AEC}, // 1e75
+ {0x3B25A55F43294BCB, 0xB0DE65388CC8ADA8}, // 1e76
+ {0x49EF0EB713F39EBE, 0xDD15FE86AFFAD912}, // 1e77
+ {0x6E3569326C784337, 0x8A2DBF142DFCC7AB}, // 1e78
+ {0x49C2C37F07965404, 0xACB92ED9397BF996}, // 1e79
+ {0xDC33745EC97BE906, 0xD7E77A8F87DAF7FB}, // 1e80
+ {0x69A028BB3DED71A3, 0x86F0AC99B4E8DAFD}, // 1e81
+ {0xC40832EA0D68CE0C, 0xA8ACD7C0222311BC}, // 1e82
+ {0xF50A3FA490C30190, 0xD2D80DB02AABD62B}, // 1e83
+ {0x792667C6DA79E0FA, 0x83C7088E1AAB65DB}, // 1e84
+ {0x577001B891185938, 0xA4B8CAB1A1563F52}, // 1e85
+ {0xED4C0226B55E6F86, 0xCDE6FD5E09ABCF26}, // 1e86
+ {0x544F8158315B05B4, 0x80B05E5AC60B6178}, // 1e87
+ {0x696361AE3DB1C721, 0xA0DC75F1778E39D6}, // 1e88
+ {0x03BC3A19CD1E38E9, 0xC913936DD571C84C}, // 1e89
+ {0x04AB48A04065C723, 0xFB5878494ACE3A5F}, // 1e90
+ {0x62EB0D64283F9C76, 0x9D174B2DCEC0E47B}, // 1e91
+ {0x3BA5D0BD324F8394, 0xC45D1DF942711D9A}, // 1e92
+ {0xCA8F44EC7EE36479, 0xF5746577930D6500}, // 1e93
+ {0x7E998B13CF4E1ECB, 0x9968BF6ABBE85F20}, // 1e94
+ {0x9E3FEDD8C321A67E, 0xBFC2EF456AE276E8}, // 1e95
+ {0xC5CFE94EF3EA101E, 0xEFB3AB16C59B14A2}, // 1e96
+ {0xBBA1F1D158724A12, 0x95D04AEE3B80ECE5}, // 1e97
+ {0x2A8A6E45AE8EDC97, 0xBB445DA9CA61281F}, // 1e98
+ {0xF52D09D71A3293BD, 0xEA1575143CF97226}, // 1e99
+ {0x593C2626705F9C56, 0x924D692CA61BE758}, // 1e100
+ {0x6F8B2FB00C77836C, 0xB6E0C377CFA2E12E}, // 1e101
+ {0x0B6DFB9C0F956447, 0xE498F455C38B997A}, // 1e102
+ {0x4724BD4189BD5EAC, 0x8EDF98B59A373FEC}, // 1e103
+ {0x58EDEC91EC2CB657, 0xB2977EE300C50FE7}, // 1e104
+ {0x2F2967B66737E3ED, 0xDF3D5E9BC0F653E1}, // 1e105
+ {0xBD79E0D20082EE74, 0x8B865B215899F46C}, // 1e106
+ {0xECD8590680A3AA11, 0xAE67F1E9AEC07187}, // 1e107
+ {0xE80E6F4820CC9495, 0xDA01EE641A708DE9}, // 1e108
+ {0x3109058D147FDCDD, 0x884134FE908658B2}, // 1e109
+ {0xBD4B46F0599FD415, 0xAA51823E34A7EEDE}, // 1e110
+ {0x6C9E18AC7007C91A, 0xD4E5E2CDC1D1EA96}, // 1e111
+ {0x03E2CF6BC604DDB0, 0x850FADC09923329E}, // 1e112
+ {0x84DB8346B786151C, 0xA6539930BF6BFF45}, // 1e113
+ {0xE612641865679A63, 0xCFE87F7CEF46FF16}, // 1e114
+ {0x4FCB7E8F3F60C07E, 0x81F14FAE158C5F6E}, // 1e115
+ {0xE3BE5E330F38F09D, 0xA26DA3999AEF7749}, // 1e116
+ {0x5CADF5BFD3072CC5, 0xCB090C8001AB551C}, // 1e117
+ {0x73D9732FC7C8F7F6, 0xFDCB4FA002162A63}, // 1e118
+ {0x2867E7FDDCDD9AFA, 0x9E9F11C4014DDA7E}, // 1e119
+ {0xB281E1FD541501B8, 0xC646D63501A1511D}, // 1e120
+ {0x1F225A7CA91A4226, 0xF7D88BC24209A565}, // 1e121
+ {0x3375788DE9B06958, 0x9AE757596946075F}, // 1e122
+ {0x0052D6B1641C83AE, 0xC1A12D2FC3978937}, // 1e123
+ {0xC0678C5DBD23A49A, 0xF209787BB47D6B84}, // 1e124
+ {0xF840B7BA963646E0, 0x9745EB4D50CE6332}, // 1e125
+ {0xB650E5A93BC3D898, 0xBD176620A501FBFF}, // 1e126
+ {0xA3E51F138AB4CEBE, 0xEC5D3FA8CE427AFF}, // 1e127
+ {0xC66F336C36B10137, 0x93BA47C980E98CDF}, // 1e128
+ {0xB80B0047445D4184, 0xB8A8D9BBE123F017}, // 1e129
+ {0xA60DC059157491E5, 0xE6D3102AD96CEC1D}, // 1e130
+ {0x87C89837AD68DB2F, 0x9043EA1AC7E41392}, // 1e131
+ {0x29BABE4598C311FB, 0xB454E4A179DD1877}, // 1e132
+ {0xF4296DD6FEF3D67A, 0xE16A1DC9D8545E94}, // 1e133
+ {0x1899E4A65F58660C, 0x8CE2529E2734BB1D}, // 1e134
+ {0x5EC05DCFF72E7F8F, 0xB01AE745B101E9E4}, // 1e135
+ {0x76707543F4FA1F73, 0xDC21A1171D42645D}, // 1e136
+ {0x6A06494A791C53A8, 0x899504AE72497EBA}, // 1e137
+ {0x0487DB9D17636892, 0xABFA45DA0EDBDE69}, // 1e138
+ {0x45A9D2845D3C42B6, 0xD6F8D7509292D603}, // 1e139
+ {0x0B8A2392BA45A9B2, 0x865B86925B9BC5C2}, // 1e140
+ {0x8E6CAC7768D7141E, 0xA7F26836F282B732}, // 1e141
+ {0x3207D795430CD926, 0xD1EF0244AF2364FF}, // 1e142
+ {0x7F44E6BD49E807B8, 0x8335616AED761F1F}, // 1e143
+ {0x5F16206C9C6209A6, 0xA402B9C5A8D3A6E7}, // 1e144
+ {0x36DBA887C37A8C0F, 0xCD036837130890A1}, // 1e145
+ {0xC2494954DA2C9789, 0x802221226BE55A64}, // 1e146
+ {0xF2DB9BAA10B7BD6C, 0xA02AA96B06DEB0FD}, // 1e147
+ {0x6F92829494E5ACC7, 0xC83553C5C8965D3D}, // 1e148
+ {0xCB772339BA1F17F9, 0xFA42A8B73ABBF48C}, // 1e149
+ {0xFF2A760414536EFB, 0x9C69A97284B578D7}, // 1e150
+ {0xFEF5138519684ABA, 0xC38413CF25E2D70D}, // 1e151
+ {0x7EB258665FC25D69, 0xF46518C2EF5B8CD1}, // 1e152
+ {0xEF2F773FFBD97A61, 0x98BF2F79D5993802}, // 1e153
+ {0xAAFB550FFACFD8FA, 0xBEEEFB584AFF8603}, // 1e154
+ {0x95BA2A53F983CF38, 0xEEAABA2E5DBF6784}, // 1e155
+ {0xDD945A747BF26183, 0x952AB45CFA97A0B2}, // 1e156
+ {0x94F971119AEEF9E4, 0xBA756174393D88DF}, // 1e157
+ {0x7A37CD5601AAB85D, 0xE912B9D1478CEB17}, // 1e158
+ {0xAC62E055C10AB33A, 0x91ABB422CCB812EE}, // 1e159
+ {0x577B986B314D6009, 0xB616A12B7FE617AA}, // 1e160
+ {0xED5A7E85FDA0B80B, 0xE39C49765FDF9D94}, // 1e161
+ {0x14588F13BE847307, 0x8E41ADE9FBEBC27D}, // 1e162
+ {0x596EB2D8AE258FC8, 0xB1D219647AE6B31C}, // 1e163
+ {0x6FCA5F8ED9AEF3BB, 0xDE469FBD99A05FE3}, // 1e164
+ {0x25DE7BB9480D5854, 0x8AEC23D680043BEE}, // 1e165
+ {0xAF561AA79A10AE6A, 0xADA72CCC20054AE9}, // 1e166
+ {0x1B2BA1518094DA04, 0xD910F7FF28069DA4}, // 1e167
+ {0x90FB44D2F05D0842, 0x87AA9AFF79042286}, // 1e168
+ {0x353A1607AC744A53, 0xA99541BF57452B28}, // 1e169
+ {0x42889B8997915CE8, 0xD3FA922F2D1675F2}, // 1e170
+ {0x69956135FEBADA11, 0x847C9B5D7C2E09B7}, // 1e171
+ {0x43FAB9837E699095, 0xA59BC234DB398C25}, // 1e172
+ {0x94F967E45E03F4BB, 0xCF02B2C21207EF2E}, // 1e173
+ {0x1D1BE0EEBAC278F5, 0x8161AFB94B44F57D}, // 1e174
+ {0x6462D92A69731732, 0xA1BA1BA79E1632DC}, // 1e175
+ {0x7D7B8F7503CFDCFE, 0xCA28A291859BBF93}, // 1e176
+ {0x5CDA735244C3D43E, 0xFCB2CB35E702AF78}, // 1e177
+ {0x3A0888136AFA64A7, 0x9DEFBF01B061ADAB}, // 1e178
+ {0x088AAA1845B8FDD0, 0xC56BAEC21C7A1916}, // 1e179
+ {0x8AAD549E57273D45, 0xF6C69A72A3989F5B}, // 1e180
+ {0x36AC54E2F678864B, 0x9A3C2087A63F6399}, // 1e181
+ {0x84576A1BB416A7DD, 0xC0CB28A98FCF3C7F}, // 1e182
+ {0x656D44A2A11C51D5, 0xF0FDF2D3F3C30B9F}, // 1e183
+ {0x9F644AE5A4B1B325, 0x969EB7C47859E743}, // 1e184
+ {0x873D5D9F0DDE1FEE, 0xBC4665B596706114}, // 1e185
+ {0xA90CB506D155A7EA, 0xEB57FF22FC0C7959}, // 1e186
+ {0x09A7F12442D588F2, 0x9316FF75DD87CBD8}, // 1e187
+ {0x0C11ED6D538AEB2F, 0xB7DCBF5354E9BECE}, // 1e188
+ {0x8F1668C8A86DA5FA, 0xE5D3EF282A242E81}, // 1e189
+ {0xF96E017D694487BC, 0x8FA475791A569D10}, // 1e190
+ {0x37C981DCC395A9AC, 0xB38D92D760EC4455}, // 1e191
+ {0x85BBE253F47B1417, 0xE070F78D3927556A}, // 1e192
+ {0x93956D7478CCEC8E, 0x8C469AB843B89562}, // 1e193
+ {0x387AC8D1970027B2, 0xAF58416654A6BABB}, // 1e194
+ {0x06997B05FCC0319E, 0xDB2E51BFE9D0696A}, // 1e195
+ {0x441FECE3BDF81F03, 0x88FCF317F22241E2}, // 1e196
+ {0xD527E81CAD7626C3, 0xAB3C2FDDEEAAD25A}, // 1e197
+ {0x8A71E223D8D3B074, 0xD60B3BD56A5586F1}, // 1e198
+ {0xF6872D5667844E49, 0x85C7056562757456}, // 1e199
+ {0xB428F8AC016561DB, 0xA738C6BEBB12D16C}, // 1e200
+ {0xE13336D701BEBA52, 0xD106F86E69D785C7}, // 1e201
+ {0xECC0024661173473, 0x82A45B450226B39C}, // 1e202
+ {0x27F002D7F95D0190, 0xA34D721642B06084}, // 1e203
+ {0x31EC038DF7B441F4, 0xCC20CE9BD35C78A5}, // 1e204
+ {0x7E67047175A15271, 0xFF290242C83396CE}, // 1e205
+ {0x0F0062C6E984D386, 0x9F79A169BD203E41}, // 1e206
+ {0x52C07B78A3E60868, 0xC75809C42C684DD1}, // 1e207
+ {0xA7709A56CCDF8A82, 0xF92E0C3537826145}, // 1e208
+ {0x88A66076400BB691, 0x9BBCC7A142B17CCB}, // 1e209
+ {0x6ACFF893D00EA435, 0xC2ABF989935DDBFE}, // 1e210
+ {0x0583F6B8C4124D43, 0xF356F7EBF83552FE}, // 1e211
+ {0xC3727A337A8B704A, 0x98165AF37B2153DE}, // 1e212
+ {0x744F18C0592E4C5C, 0xBE1BF1B059E9A8D6}, // 1e213
+ {0x1162DEF06F79DF73, 0xEDA2EE1C7064130C}, // 1e214
+ {0x8ADDCB5645AC2BA8, 0x9485D4D1C63E8BE7}, // 1e215
+ {0x6D953E2BD7173692, 0xB9A74A0637CE2EE1}, // 1e216
+ {0xC8FA8DB6CCDD0437, 0xE8111C87C5C1BA99}, // 1e217
+ {0x1D9C9892400A22A2, 0x910AB1D4DB9914A0}, // 1e218
+ {0x2503BEB6D00CAB4B, 0xB54D5E4A127F59C8}, // 1e219
+ {0x2E44AE64840FD61D, 0xE2A0B5DC971F303A}, // 1e220
+ {0x5CEAECFED289E5D2, 0x8DA471A9DE737E24}, // 1e221
+ {0x7425A83E872C5F47, 0xB10D8E1456105DAD}, // 1e222
+ {0xD12F124E28F77719, 0xDD50F1996B947518}, // 1e223
+ {0x82BD6B70D99AAA6F, 0x8A5296FFE33CC92F}, // 1e224
+ {0x636CC64D1001550B, 0xACE73CBFDC0BFB7B}, // 1e225
+ {0x3C47F7E05401AA4E, 0xD8210BEFD30EFA5A}, // 1e226
+ {0x65ACFAEC34810A71, 0x8714A775E3E95C78}, // 1e227
+ {0x7F1839A741A14D0D, 0xA8D9D1535CE3B396}, // 1e228
+ {0x1EDE48111209A050, 0xD31045A8341CA07C}, // 1e229
+ {0x934AED0AAB460432, 0x83EA2B892091E44D}, // 1e230
+ {0xF81DA84D5617853F, 0xA4E4B66B68B65D60}, // 1e231
+ {0x36251260AB9D668E, 0xCE1DE40642E3F4B9}, // 1e232
+ {0xC1D72B7C6B426019, 0x80D2AE83E9CE78F3}, // 1e233
+ {0xB24CF65B8612F81F, 0xA1075A24E4421730}, // 1e234
+ {0xDEE033F26797B627, 0xC94930AE1D529CFC}, // 1e235
+ {0x169840EF017DA3B1, 0xFB9B7CD9A4A7443C}, // 1e236
+ {0x8E1F289560EE864E, 0x9D412E0806E88AA5}, // 1e237
+ {0xF1A6F2BAB92A27E2, 0xC491798A08A2AD4E}, // 1e238
+ {0xAE10AF696774B1DB, 0xF5B5D7EC8ACB58A2}, // 1e239
+ {0xACCA6DA1E0A8EF29, 0x9991A6F3D6BF1765}, // 1e240
+ {0x17FD090A58D32AF3, 0xBFF610B0CC6EDD3F}, // 1e241
+ {0xDDFC4B4CEF07F5B0, 0xEFF394DCFF8A948E}, // 1e242
+ {0x4ABDAF101564F98E, 0x95F83D0A1FB69CD9}, // 1e243
+ {0x9D6D1AD41ABE37F1, 0xBB764C4CA7A4440F}, // 1e244
+ {0x84C86189216DC5ED, 0xEA53DF5FD18D5513}, // 1e245
+ {0x32FD3CF5B4E49BB4, 0x92746B9BE2F8552C}, // 1e246
+ {0x3FBC8C33221DC2A1, 0xB7118682DBB66A77}, // 1e247
+ {0x0FABAF3FEAA5334A, 0xE4D5E82392A40515}, // 1e248
+ {0x29CB4D87F2A7400E, 0x8F05B1163BA6832D}, // 1e249
+ {0x743E20E9EF511012, 0xB2C71D5BCA9023F8}, // 1e250
+ {0x914DA9246B255416, 0xDF78E4B2BD342CF6}, // 1e251
+ {0x1AD089B6C2F7548E, 0x8BAB8EEFB6409C1A}, // 1e252
+ {0xA184AC2473B529B1, 0xAE9672ABA3D0C320}, // 1e253
+ {0xC9E5D72D90A2741E, 0xDA3C0F568CC4F3E8}, // 1e254
+ {0x7E2FA67C7A658892, 0x8865899617FB1871}, // 1e255
+ {0xDDBB901B98FEEAB7, 0xAA7EEBFB9DF9DE8D}, // 1e256
+ {0x552A74227F3EA565, 0xD51EA6FA85785631}, // 1e257
+ {0xD53A88958F87275F, 0x8533285C936B35DE}, // 1e258
+ {0x8A892ABAF368F137, 0xA67FF273B8460356}, // 1e259
+ {0x2D2B7569B0432D85, 0xD01FEF10A657842C}, // 1e260
+ {0x9C3B29620E29FC73, 0x8213F56A67F6B29B}, // 1e261
+ {0x8349F3BA91B47B8F, 0xA298F2C501F45F42}, // 1e262
+ {0x241C70A936219A73, 0xCB3F2F7642717713}, // 1e263
+ {0xED238CD383AA0110, 0xFE0EFB53D30DD4D7}, // 1e264
+ {0xF4363804324A40AA, 0x9EC95D1463E8A506}, // 1e265
+ {0xB143C6053EDCD0D5, 0xC67BB4597CE2CE48}, // 1e266
+ {0xDD94B7868E94050A, 0xF81AA16FDC1B81DA}, // 1e267
+ {0xCA7CF2B4191C8326, 0x9B10A4E5E9913128}, // 1e268
+ {0xFD1C2F611F63A3F0, 0xC1D4CE1F63F57D72}, // 1e269
+ {0xBC633B39673C8CEC, 0xF24A01A73CF2DCCF}, // 1e270
+ {0xD5BE0503E085D813, 0x976E41088617CA01}, // 1e271
+ {0x4B2D8644D8A74E18, 0xBD49D14AA79DBC82}, // 1e272
+ {0xDDF8E7D60ED1219E, 0xEC9C459D51852BA2}, // 1e273
+ {0xCABB90E5C942B503, 0x93E1AB8252F33B45}, // 1e274
+ {0x3D6A751F3B936243, 0xB8DA1662E7B00A17}, // 1e275
+ {0x0CC512670A783AD4, 0xE7109BFBA19C0C9D}, // 1e276
+ {0x27FB2B80668B24C5, 0x906A617D450187E2}, // 1e277
+ {0xB1F9F660802DEDF6, 0xB484F9DC9641E9DA}, // 1e278
+ {0x5E7873F8A0396973, 0xE1A63853BBD26451}, // 1e279
+ {0xDB0B487B6423E1E8, 0x8D07E33455637EB2}, // 1e280
+ {0x91CE1A9A3D2CDA62, 0xB049DC016ABC5E5F}, // 1e281
+ {0x7641A140CC7810FB, 0xDC5C5301C56B75F7}, // 1e282
+ {0xA9E904C87FCB0A9D, 0x89B9B3E11B6329BA}, // 1e283
+ {0x546345FA9FBDCD44, 0xAC2820D9623BF429}, // 1e284
+ {0xA97C177947AD4095, 0xD732290FBACAF133}, // 1e285
+ {0x49ED8EABCCCC485D, 0x867F59A9D4BED6C0}, // 1e286
+ {0x5C68F256BFFF5A74, 0xA81F301449EE8C70}, // 1e287
+ {0x73832EEC6FFF3111, 0xD226FC195C6A2F8C}, // 1e288
+ {0xC831FD53C5FF7EAB, 0x83585D8FD9C25DB7}, // 1e289
+ {0xBA3E7CA8B77F5E55, 0xA42E74F3D032F525}, // 1e290
+ {0x28CE1BD2E55F35EB, 0xCD3A1230C43FB26F}, // 1e291
+ {0x7980D163CF5B81B3, 0x80444B5E7AA7CF85}, // 1e292
+ {0xD7E105BCC332621F, 0xA0555E361951C366}, // 1e293
+ {0x8DD9472BF3FEFAA7, 0xC86AB5C39FA63440}, // 1e294
+ {0xB14F98F6F0FEB951, 0xFA856334878FC150}, // 1e295
+ {0x6ED1BF9A569F33D3, 0x9C935E00D4B9D8D2}, // 1e296
+ {0x0A862F80EC4700C8, 0xC3B8358109E84F07}, // 1e297
+ {0xCD27BB612758C0FA, 0xF4A642E14C6262C8}, // 1e298
+ {0x8038D51CB897789C, 0x98E7E9CCCFBD7DBD}, // 1e299
+ {0xE0470A63E6BD56C3, 0xBF21E44003ACDD2C}, // 1e300
+ {0x1858CCFCE06CAC74, 0xEEEA5D5004981478}, // 1e301
+ {0x0F37801E0C43EBC8, 0x95527A5202DF0CCB}, // 1e302
+ {0xD30560258F54E6BA, 0xBAA718E68396CFFD}, // 1e303
+ {0x47C6B82EF32A2069, 0xE950DF20247C83FD}, // 1e304
+ {0x4CDC331D57FA5441, 0x91D28B7416CDD27E}, // 1e305
+ {0xE0133FE4ADF8E952, 0xB6472E511C81471D}, // 1e306
+ {0x58180FDDD97723A6, 0xE3D8F9E563A198E5}, // 1e307
+ {0x570F09EAA7EA7648, 0x8E679C2F5E44FF8F}, // 1e308
+ {0x2CD2CC6551E513DA, 0xB201833B35D63F73}, // 1e309
+ {0xF8077F7EA65E58D1, 0xDE81E40A034BCF4F}, // 1e310
+ {0xFB04AFAF27FAF782, 0x8B112E86420F6191}, // 1e311
+ {0x79C5DB9AF1F9B563, 0xADD57A27D29339F6}, // 1e312
+ {0x18375281AE7822BC, 0xD94AD8B1C7380874}, // 1e313
+ {0x8F2293910D0B15B5, 0x87CEC76F1C830548}, // 1e314
+ {0xB2EB3875504DDB22, 0xA9C2794AE3A3C69A}, // 1e315
+ {0x5FA60692A46151EB, 0xD433179D9C8CB841}, // 1e316
+ {0xDBC7C41BA6BCD333, 0x849FEEC281D7F328}, // 1e317
+ {0x12B9B522906C0800, 0xA5C7EA73224DEFF3}, // 1e318
+ {0xD768226B34870A00, 0xCF39E50FEAE16BEF}, // 1e319
+ {0xE6A1158300D46640, 0x81842F29F2CCE375}, // 1e320
+ {0x60495AE3C1097FD0, 0xA1E53AF46F801C53}, // 1e321
+ {0x385BB19CB14BDFC4, 0xCA5E89B18B602368}, // 1e322
+ {0x46729E03DD9ED7B5, 0xFCF62C1DEE382C42}, // 1e323
+ {0x6C07A2C26A8346D1, 0x9E19DB92B4E31BA9}, // 1e324
+ {0xC7098B7305241885, 0xC5A05277621BE293}, // 1e325
+ {0xB8CBEE4FC66D1EA7, 0xF70867153AA2DB38}, // 1e326
+ {0x737F74F1DC043328, 0x9A65406D44A5C903}, // 1e327
+ {0x505F522E53053FF2, 0xC0FE908895CF3B44}, // 1e328
+ {0x647726B9E7C68FEF, 0xF13E34AABB430A15}, // 1e329
+ {0x5ECA783430DC19F5, 0x96C6E0EAB509E64D}, // 1e330
+ {0xB67D16413D132072, 0xBC789925624C5FE0}, // 1e331
+ {0xE41C5BD18C57E88F, 0xEB96BF6EBADF77D8}, // 1e332
+ {0x8E91B962F7B6F159, 0x933E37A534CBAAE7}, // 1e333
+ {0x723627BBB5A4ADB0, 0xB80DC58E81FE95A1}, // 1e334
+ {0xCEC3B1AAA30DD91C, 0xE61136F2227E3B09}, // 1e335
+ {0x213A4F0AA5E8A7B1, 0x8FCAC257558EE4E6}, // 1e336
+ {0xA988E2CD4F62D19D, 0xB3BD72ED2AF29E1F}, // 1e337
+ {0x93EB1B80A33B8605, 0xE0ACCFA875AF45A7}, // 1e338
+ {0xBC72F130660533C3, 0x8C6C01C9498D8B88}, // 1e339
+ {0xEB8FAD7C7F8680B4, 0xAF87023B9BF0EE6A}, // 1e340
+ {0xA67398DB9F6820E1, 0xDB68C2CA82ED2A05}, // 1e341
+ {0x88083F8943A1148C, 0x892179BE91D43A43}, // 1e342
+ {0x6A0A4F6B948959B0, 0xAB69D82E364948D4}, // 1e343
+ {0x848CE34679ABB01C, 0xD6444E39C3DB9B09}, // 1e344
+ {0xF2D80E0C0C0B4E11, 0x85EAB0E41A6940E5}, // 1e345
+ {0x6F8E118F0F0E2195, 0xA7655D1D2103911F}, // 1e346
+ {0x4B7195F2D2D1A9FB, 0xD13EB46469447567}, // 1e347
+}
diff --git a/src/strconv/example_test.go b/src/strconv/example_test.go
new file mode 100644
index 0000000..428fde4
--- /dev/null
+++ b/src/strconv/example_test.go
@@ -0,0 +1,440 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ "fmt"
+ "log"
+ "strconv"
+)
+
+func ExampleAppendBool() {
+ b := []byte("bool:")
+ b = strconv.AppendBool(b, true)
+ fmt.Println(string(b))
+
+ // Output:
+ // bool:true
+}
+
+func ExampleAppendFloat() {
+ b32 := []byte("float32:")
+ b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32)
+ fmt.Println(string(b32))
+
+ b64 := []byte("float64:")
+ b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64)
+ fmt.Println(string(b64))
+
+ // Output:
+ // float32:3.1415927E+00
+ // float64:3.1415926535E+00
+}
+
+func ExampleAppendInt() {
+ b10 := []byte("int (base 10):")
+ b10 = strconv.AppendInt(b10, -42, 10)
+ fmt.Println(string(b10))
+
+ b16 := []byte("int (base 16):")
+ b16 = strconv.AppendInt(b16, -42, 16)
+ fmt.Println(string(b16))
+
+ // Output:
+ // int (base 10):-42
+ // int (base 16):-2a
+}
+
+func ExampleAppendQuote() {
+ b := []byte("quote:")
+ b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`)
+ fmt.Println(string(b))
+
+ // Output:
+ // quote:"\"Fran & Freddie's Diner\""
+}
+
+func ExampleAppendQuoteRune() {
+ b := []byte("rune:")
+ b = strconv.AppendQuoteRune(b, '☺')
+ fmt.Println(string(b))
+
+ // Output:
+ // rune:'☺'
+}
+
+func ExampleAppendQuoteRuneToASCII() {
+ b := []byte("rune (ascii):")
+ b = strconv.AppendQuoteRuneToASCII(b, '☺')
+ fmt.Println(string(b))
+
+ // Output:
+ // rune (ascii):'\u263a'
+}
+
+func ExampleAppendQuoteToASCII() {
+ b := []byte("quote (ascii):")
+ b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`)
+ fmt.Println(string(b))
+
+ // Output:
+ // quote (ascii):"\"Fran & Freddie's Diner\""
+}
+
+func ExampleAppendUint() {
+ b10 := []byte("uint (base 10):")
+ b10 = strconv.AppendUint(b10, 42, 10)
+ fmt.Println(string(b10))
+
+ b16 := []byte("uint (base 16):")
+ b16 = strconv.AppendUint(b16, 42, 16)
+ fmt.Println(string(b16))
+
+ // Output:
+ // uint (base 10):42
+ // uint (base 16):2a
+}
+
+func ExampleAtoi() {
+ v := "10"
+ if s, err := strconv.Atoi(v); err == nil {
+ fmt.Printf("%T, %v", s, s)
+ }
+
+ // Output:
+ // int, 10
+}
+
+func ExampleCanBackquote() {
+ fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺"))
+ fmt.Println(strconv.CanBackquote("`can't backquote this`"))
+
+ // Output:
+ // true
+ // false
+}
+
+func ExampleFormatBool() {
+ v := true
+ s := strconv.FormatBool(v)
+ fmt.Printf("%T, %v\n", s, s)
+
+ // Output:
+ // string, true
+}
+
+func ExampleFormatFloat() {
+ v := 3.1415926535
+
+ s32 := strconv.FormatFloat(v, 'E', -1, 32)
+ fmt.Printf("%T, %v\n", s32, s32)
+
+ s64 := strconv.FormatFloat(v, 'E', -1, 64)
+ fmt.Printf("%T, %v\n", s64, s64)
+
+ // fmt.Println uses these arguments to print floats
+ fmt64 := strconv.FormatFloat(v, 'g', -1, 64)
+ fmt.Printf("%T, %v\n", fmt64, fmt64)
+
+ // Output:
+ // string, 3.1415927E+00
+ // string, 3.1415926535E+00
+ // string, 3.1415926535
+}
+
+func ExampleFormatInt() {
+ v := int64(-42)
+
+ s10 := strconv.FormatInt(v, 10)
+ fmt.Printf("%T, %v\n", s10, s10)
+
+ s16 := strconv.FormatInt(v, 16)
+ fmt.Printf("%T, %v\n", s16, s16)
+
+ // Output:
+ // string, -42
+ // string, -2a
+}
+
+func ExampleFormatUint() {
+ v := uint64(42)
+
+ s10 := strconv.FormatUint(v, 10)
+ fmt.Printf("%T, %v\n", s10, s10)
+
+ s16 := strconv.FormatUint(v, 16)
+ fmt.Printf("%T, %v\n", s16, s16)
+
+ // Output:
+ // string, 42
+ // string, 2a
+}
+
+func ExampleIsGraphic() {
+ shamrock := strconv.IsGraphic('☘')
+ fmt.Println(shamrock)
+
+ a := strconv.IsGraphic('a')
+ fmt.Println(a)
+
+ bel := strconv.IsGraphic('\007')
+ fmt.Println(bel)
+
+ // Output:
+ // true
+ // true
+ // false
+}
+
+func ExampleIsPrint() {
+ c := strconv.IsPrint('\u263a')
+ fmt.Println(c)
+
+ bel := strconv.IsPrint('\007')
+ fmt.Println(bel)
+
+ // Output:
+ // true
+ // false
+}
+
+func ExampleItoa() {
+ i := 10
+ s := strconv.Itoa(i)
+ fmt.Printf("%T, %v\n", s, s)
+
+ // Output:
+ // string, 10
+}
+
+func ExampleParseBool() {
+ v := "true"
+ if s, err := strconv.ParseBool(v); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+
+ // Output:
+ // bool, true
+}
+
+func ExampleParseFloat() {
+ v := "3.1415926535"
+ if s, err := strconv.ParseFloat(v, 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ if s, err := strconv.ParseFloat(v, 64); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ if s, err := strconv.ParseFloat("NaN", 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ // ParseFloat is case insensitive
+ if s, err := strconv.ParseFloat("nan", 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ if s, err := strconv.ParseFloat("inf", 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ if s, err := strconv.ParseFloat("+Inf", 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ if s, err := strconv.ParseFloat("-Inf", 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ if s, err := strconv.ParseFloat("-0", 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ if s, err := strconv.ParseFloat("+0", 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+
+ // Output:
+ // float64, 3.1415927410125732
+ // float64, 3.1415926535
+ // float64, NaN
+ // float64, NaN
+ // float64, +Inf
+ // float64, +Inf
+ // float64, -Inf
+ // float64, -0
+ // float64, 0
+}
+
+func ExampleParseInt() {
+ v32 := "-354634382"
+ if s, err := strconv.ParseInt(v32, 10, 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ if s, err := strconv.ParseInt(v32, 16, 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+
+ v64 := "-3546343826724305832"
+ if s, err := strconv.ParseInt(v64, 10, 64); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ if s, err := strconv.ParseInt(v64, 16, 64); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+
+ // Output:
+ // int64, -354634382
+ // int64, -3546343826724305832
+}
+
+func ExampleParseUint() {
+ v := "42"
+ if s, err := strconv.ParseUint(v, 10, 32); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+ if s, err := strconv.ParseUint(v, 10, 64); err == nil {
+ fmt.Printf("%T, %v\n", s, s)
+ }
+
+ // Output:
+ // uint64, 42
+ // uint64, 42
+}
+
+func ExampleQuote() {
+ // This string literal contains a tab character.
+ s := strconv.Quote(`"Fran & Freddie's Diner ☺"`)
+ fmt.Println(s)
+
+ // Output:
+ // "\"Fran & Freddie's Diner\t☺\""
+}
+
+func ExampleQuoteRune() {
+ s := strconv.QuoteRune('☺')
+ fmt.Println(s)
+
+ // Output:
+ // '☺'
+}
+
+func ExampleQuoteRuneToASCII() {
+ s := strconv.QuoteRuneToASCII('☺')
+ fmt.Println(s)
+
+ // Output:
+ // '\u263a'
+}
+
+func ExampleQuoteRuneToGraphic() {
+ s := strconv.QuoteRuneToGraphic('☺')
+ fmt.Println(s)
+
+ s = strconv.QuoteRuneToGraphic('\u263a')
+ fmt.Println(s)
+
+ s = strconv.QuoteRuneToGraphic('\u000a')
+ fmt.Println(s)
+
+ s = strconv.QuoteRuneToGraphic(' ') // tab character
+ fmt.Println(s)
+
+ // Output:
+ // '☺'
+ // '☺'
+ // '\n'
+ // '\t'
+}
+
+func ExampleQuoteToASCII() {
+ // This string literal contains a tab character.
+ s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`)
+ fmt.Println(s)
+
+ // Output:
+ // "\"Fran & Freddie's Diner\t\u263a\""
+}
+
+func ExampleQuoteToGraphic() {
+ s := strconv.QuoteToGraphic("☺")
+ fmt.Println(s)
+
+ // This string literal contains a tab character.
+ s = strconv.QuoteToGraphic("This is a \u263a \u000a")
+ fmt.Println(s)
+
+ s = strconv.QuoteToGraphic(`" This is a ☺ \n "`)
+ fmt.Println(s)
+
+ // Output:
+ // "☺"
+ // "This is a ☺\t\n"
+ // "\" This is a ☺ \\n \""
+}
+
+func ExampleQuotedPrefix() {
+ s, err := strconv.QuotedPrefix("not a quoted string")
+ fmt.Printf("%q, %v\n", s, err)
+ s, err = strconv.QuotedPrefix("\"double-quoted string\" with trailing text")
+ fmt.Printf("%q, %v\n", s, err)
+ s, err = strconv.QuotedPrefix("`or backquoted` with more trailing text")
+ fmt.Printf("%q, %v\n", s, err)
+ s, err = strconv.QuotedPrefix("'\u263a' is also okay")
+ fmt.Printf("%q, %v\n", s, err)
+
+ // Output:
+ // "", invalid syntax
+ // "\"double-quoted string\"", <nil>
+ // "`or backquoted`", <nil>
+ // "'☺'", <nil>
+}
+
+func ExampleUnquote() {
+ s, err := strconv.Unquote("You can't unquote a string without quotes")
+ fmt.Printf("%q, %v\n", s, err)
+ s, err = strconv.Unquote("\"The string must be either double-quoted\"")
+ fmt.Printf("%q, %v\n", s, err)
+ s, err = strconv.Unquote("`or backquoted.`")
+ fmt.Printf("%q, %v\n", s, err)
+ s, err = strconv.Unquote("'\u263a'") // single character only allowed in single quotes
+ fmt.Printf("%q, %v\n", s, err)
+ s, err = strconv.Unquote("'\u2639\u2639'")
+ fmt.Printf("%q, %v\n", s, err)
+
+ // Output:
+ // "", invalid syntax
+ // "The string must be either double-quoted", <nil>
+ // "or backquoted.", <nil>
+ // "☺", <nil>
+ // "", invalid syntax
+}
+
+func ExampleUnquoteChar() {
+ v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"')
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ fmt.Println("value:", string(v))
+ fmt.Println("multibyte:", mb)
+ fmt.Println("tail:", t)
+
+ // Output:
+ // value: "
+ // multibyte: false
+ // tail: Fran & Freddie's Diner\"
+}
+
+func ExampleNumError() {
+ str := "Not a number"
+ if _, err := strconv.ParseFloat(str, 64); err != nil {
+ e := err.(*strconv.NumError)
+ fmt.Println("Func:", e.Func)
+ fmt.Println("Num:", e.Num)
+ fmt.Println("Err:", e.Err)
+ fmt.Println(err)
+ }
+
+ // Output:
+ // Func: ParseFloat
+ // Num: Not a number
+ // Err: invalid syntax
+ // strconv.ParseFloat: parsing "Not a number": invalid syntax
+}
diff --git a/src/strconv/export_test.go b/src/strconv/export_test.go
new file mode 100644
index 0000000..8c03a7f
--- /dev/null
+++ b/src/strconv/export_test.go
@@ -0,0 +1,10 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv
+
+var (
+ BitSizeError = bitSizeError
+ BaseError = baseError
+)
diff --git a/src/strconv/fp_test.go b/src/strconv/fp_test.go
new file mode 100644
index 0000000..fd73958
--- /dev/null
+++ b/src/strconv/fp_test.go
@@ -0,0 +1,142 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+ "strconv"
+ "strings"
+ "testing"
+)
+
+func pow2(i int) float64 {
+ switch {
+ case i < 0:
+ return 1 / pow2(-i)
+ case i == 0:
+ return 1
+ case i == 1:
+ return 2
+ }
+ return pow2(i/2) * pow2(i-i/2)
+}
+
+// Wrapper around strconv.ParseFloat(x, 64). Handles dddddp+ddd (binary exponent)
+// itself, passes the rest on to strconv.ParseFloat.
+func myatof64(s string) (f float64, ok bool) {
+ if mant, exp, ok := strings.Cut(s, "p"); ok {
+ n, err := strconv.ParseInt(mant, 10, 64)
+ if err != nil {
+ return 0, false
+ }
+ e, err1 := strconv.Atoi(exp)
+ if err1 != nil {
+ println("bad e", exp)
+ return 0, false
+ }
+ v := float64(n)
+ // We expect that v*pow2(e) fits in a float64,
+ // but pow2(e) by itself may not. Be careful.
+ if e <= -1000 {
+ v *= pow2(-1000)
+ e += 1000
+ for e < 0 {
+ v /= 2
+ e++
+ }
+ return v, true
+ }
+ if e >= 1000 {
+ v *= pow2(1000)
+ e -= 1000
+ for e > 0 {
+ v *= 2
+ e--
+ }
+ return v, true
+ }
+ return v * pow2(e), true
+ }
+ f1, err := strconv.ParseFloat(s, 64)
+ if err != nil {
+ return 0, false
+ }
+ return f1, true
+}
+
+// Wrapper around strconv.ParseFloat(x, 32). Handles dddddp+ddd (binary exponent)
+// itself, passes the rest on to strconv.ParseFloat.
+func myatof32(s string) (f float32, ok bool) {
+ if mant, exp, ok := strings.Cut(s, "p"); ok {
+ n, err := strconv.Atoi(mant)
+ if err != nil {
+ println("bad n", mant)
+ return 0, false
+ }
+ e, err1 := strconv.Atoi(exp)
+ if err1 != nil {
+ println("bad p", exp)
+ return 0, false
+ }
+ return float32(float64(n) * pow2(e)), true
+ }
+ f64, err1 := strconv.ParseFloat(s, 32)
+ f1 := float32(f64)
+ if err1 != nil {
+ return 0, false
+ }
+ return f1, true
+}
+
+func TestFp(t *testing.T) {
+ f, err := os.Open("testdata/testfp.txt")
+ if err != nil {
+ t.Fatal("testfp: open testdata/testfp.txt:", err)
+ }
+ defer f.Close()
+
+ s := bufio.NewScanner(f)
+
+ for lineno := 1; s.Scan(); lineno++ {
+ line := s.Text()
+ if len(line) == 0 || line[0] == '#' {
+ continue
+ }
+ a := strings.Split(line, " ")
+ if len(a) != 4 {
+ t.Error("testdata/testfp.txt:", lineno, ": wrong field count")
+ continue
+ }
+ var s string
+ var v float64
+ switch a[0] {
+ case "float64":
+ var ok bool
+ v, ok = myatof64(a[2])
+ if !ok {
+ t.Error("testdata/testfp.txt:", lineno, ": cannot atof64 ", a[2])
+ continue
+ }
+ s = fmt.Sprintf(a[1], v)
+ case "float32":
+ v1, ok := myatof32(a[2])
+ if !ok {
+ t.Error("testdata/testfp.txt:", lineno, ": cannot atof32 ", a[2])
+ continue
+ }
+ s = fmt.Sprintf(a[1], v1)
+ v = float64(v1)
+ }
+ if s != a[3] {
+ t.Error("testdata/testfp.txt:", lineno, ": ", a[0], " ", a[1], " ", a[2], " (", v, ") ",
+ "want ", a[3], " got ", s)
+ }
+ }
+ if s.Err() != nil {
+ t.Fatal("testfp: read testdata/testfp.txt: ", s.Err())
+ }
+}
diff --git a/src/strconv/ftoa.go b/src/strconv/ftoa.go
new file mode 100644
index 0000000..c514e66
--- /dev/null
+++ b/src/strconv/ftoa.go
@@ -0,0 +1,570 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Binary to decimal floating point conversion.
+// Algorithm:
+// 1) store mantissa in multiprecision decimal
+// 2) shift decimal by exponent
+// 3) read digits out & format
+
+package strconv
+
+import "math"
+
+// TODO: move elsewhere?
+type floatInfo struct {
+ mantbits uint
+ expbits uint
+ bias int
+}
+
+var float32info = floatInfo{23, 8, -127}
+var float64info = floatInfo{52, 11, -1023}
+
+// FormatFloat converts the floating-point number f to a string,
+// according to the format fmt and precision prec. It rounds the
+// result assuming that the original was obtained from a floating-point
+// value of bitSize bits (32 for float32, 64 for float64).
+//
+// The format fmt is one of
+// 'b' (-ddddp±ddd, a binary exponent),
+// 'e' (-d.dddde±dd, a decimal exponent),
+// 'E' (-d.ddddE±dd, a decimal exponent),
+// 'f' (-ddd.dddd, no exponent),
+// 'g' ('e' for large exponents, 'f' otherwise),
+// 'G' ('E' for large exponents, 'f' otherwise),
+// 'x' (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or
+// 'X' (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).
+//
+// The precision prec controls the number of digits (excluding the exponent)
+// printed by the 'e', 'E', 'f', 'g', 'G', 'x', and 'X' formats.
+// For 'e', 'E', 'f', 'x', and 'X', it is the number of digits after the decimal point.
+// For 'g' and 'G' it is the maximum number of significant digits (trailing
+// zeros are removed).
+// The special precision -1 uses the smallest number of digits
+// necessary such that ParseFloat will return f exactly.
+func FormatFloat(f float64, fmt byte, prec, bitSize int) string {
+ return string(genericFtoa(make([]byte, 0, max(prec+4, 24)), f, fmt, prec, bitSize))
+}
+
+// AppendFloat appends the string form of the floating-point number f,
+// as generated by FormatFloat, to dst and returns the extended buffer.
+func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte {
+ return genericFtoa(dst, f, fmt, prec, bitSize)
+}
+
+func genericFtoa(dst []byte, val float64, fmt byte, prec, bitSize int) []byte {
+ var bits uint64
+ var flt *floatInfo
+ switch bitSize {
+ case 32:
+ bits = uint64(math.Float32bits(float32(val)))
+ flt = &float32info
+ case 64:
+ bits = math.Float64bits(val)
+ flt = &float64info
+ default:
+ panic("strconv: illegal AppendFloat/FormatFloat bitSize")
+ }
+
+ neg := bits>>(flt.expbits+flt.mantbits) != 0
+ exp := int(bits>>flt.mantbits) & (1<<flt.expbits - 1)
+ mant := bits & (uint64(1)<<flt.mantbits - 1)
+
+ switch exp {
+ case 1<<flt.expbits - 1:
+ // Inf, NaN
+ var s string
+ switch {
+ case mant != 0:
+ s = "NaN"
+ case neg:
+ s = "-Inf"
+ default:
+ s = "+Inf"
+ }
+ return append(dst, s...)
+
+ case 0:
+ // denormalized
+ exp++
+
+ default:
+ // add implicit top bit
+ mant |= uint64(1) << flt.mantbits
+ }
+ exp += flt.bias
+
+ // Pick off easy binary, hex formats.
+ if fmt == 'b' {
+ return fmtB(dst, neg, mant, exp, flt)
+ }
+ if fmt == 'x' || fmt == 'X' {
+ return fmtX(dst, prec, fmt, neg, mant, exp, flt)
+ }
+
+ if !optimize {
+ return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
+ }
+
+ var digs decimalSlice
+ ok := false
+ // Negative precision means "only as much as needed to be exact."
+ shortest := prec < 0
+ if shortest {
+ // Use Ryu algorithm.
+ var buf [32]byte
+ digs.d = buf[:]
+ ryuFtoaShortest(&digs, mant, exp-int(flt.mantbits), flt)
+ ok = true
+ // Precision for shortest representation mode.
+ switch fmt {
+ case 'e', 'E':
+ prec = max(digs.nd-1, 0)
+ case 'f':
+ prec = max(digs.nd-digs.dp, 0)
+ case 'g', 'G':
+ prec = digs.nd
+ }
+ } else if fmt != 'f' {
+ // Fixed number of digits.
+ digits := prec
+ switch fmt {
+ case 'e', 'E':
+ digits++
+ case 'g', 'G':
+ if prec == 0 {
+ prec = 1
+ }
+ digits = prec
+ default:
+ // Invalid mode.
+ digits = 1
+ }
+ var buf [24]byte
+ if bitSize == 32 && digits <= 9 {
+ digs.d = buf[:]
+ ryuFtoaFixed32(&digs, uint32(mant), exp-int(flt.mantbits), digits)
+ ok = true
+ } else if digits <= 18 {
+ digs.d = buf[:]
+ ryuFtoaFixed64(&digs, mant, exp-int(flt.mantbits), digits)
+ ok = true
+ }
+ }
+ if !ok {
+ return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
+ }
+ return formatDigits(dst, shortest, neg, digs, prec, fmt)
+}
+
+// bigFtoa uses multiprecision computations to format a float.
+func bigFtoa(dst []byte, prec int, fmt byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte {
+ d := new(decimal)
+ d.Assign(mant)
+ d.Shift(exp - int(flt.mantbits))
+ var digs decimalSlice
+ shortest := prec < 0
+ if shortest {
+ roundShortest(d, mant, exp, flt)
+ digs = decimalSlice{d: d.d[:], nd: d.nd, dp: d.dp}
+ // Precision for shortest representation mode.
+ switch fmt {
+ case 'e', 'E':
+ prec = digs.nd - 1
+ case 'f':
+ prec = max(digs.nd-digs.dp, 0)
+ case 'g', 'G':
+ prec = digs.nd
+ }
+ } else {
+ // Round appropriately.
+ switch fmt {
+ case 'e', 'E':
+ d.Round(prec + 1)
+ case 'f':
+ d.Round(d.dp + prec)
+ case 'g', 'G':
+ if prec == 0 {
+ prec = 1
+ }
+ d.Round(prec)
+ }
+ digs = decimalSlice{d: d.d[:], nd: d.nd, dp: d.dp}
+ }
+ return formatDigits(dst, shortest, neg, digs, prec, fmt)
+}
+
+func formatDigits(dst []byte, shortest bool, neg bool, digs decimalSlice, prec int, fmt byte) []byte {
+ switch fmt {
+ case 'e', 'E':
+ return fmtE(dst, neg, digs, prec, fmt)
+ case 'f':
+ return fmtF(dst, neg, digs, prec)
+ case 'g', 'G':
+ // trailing fractional zeros in 'e' form will be trimmed.
+ eprec := prec
+ if eprec > digs.nd && digs.nd >= digs.dp {
+ eprec = digs.nd
+ }
+ // %e is used if the exponent from the conversion
+ // is less than -4 or greater than or equal to the precision.
+ // if precision was the shortest possible, use precision 6 for this decision.
+ if shortest {
+ eprec = 6
+ }
+ exp := digs.dp - 1
+ if exp < -4 || exp >= eprec {
+ if prec > digs.nd {
+ prec = digs.nd
+ }
+ return fmtE(dst, neg, digs, prec-1, fmt+'e'-'g')
+ }
+ if prec > digs.dp {
+ prec = digs.nd
+ }
+ return fmtF(dst, neg, digs, max(prec-digs.dp, 0))
+ }
+
+ // unknown format
+ return append(dst, '%', fmt)
+}
+
+// roundShortest rounds d (= mant * 2^exp) to the shortest number of digits
+// that will let the original floating point value be precisely reconstructed.
+func roundShortest(d *decimal, mant uint64, exp int, flt *floatInfo) {
+ // If mantissa is zero, the number is zero; stop now.
+ if mant == 0 {
+ d.nd = 0
+ return
+ }
+
+ // Compute upper and lower such that any decimal number
+ // between upper and lower (possibly inclusive)
+ // will round to the original floating point number.
+
+ // We may see at once that the number is already shortest.
+ //
+ // Suppose d is not denormal, so that 2^exp <= d < 10^dp.
+ // The closest shorter number is at least 10^(dp-nd) away.
+ // The lower/upper bounds computed below are at distance
+ // at most 2^(exp-mantbits).
+ //
+ // So the number is already shortest if 10^(dp-nd) > 2^(exp-mantbits),
+ // or equivalently log2(10)*(dp-nd) > exp-mantbits.
+ // It is true if 332/100*(dp-nd) >= exp-mantbits (log2(10) > 3.32).
+ minexp := flt.bias + 1 // minimum possible exponent
+ if exp > minexp && 332*(d.dp-d.nd) >= 100*(exp-int(flt.mantbits)) {
+ // The number is already shortest.
+ return
+ }
+
+ // d = mant << (exp - mantbits)
+ // Next highest floating point number is mant+1 << exp-mantbits.
+ // Our upper bound is halfway between, mant*2+1 << exp-mantbits-1.
+ upper := new(decimal)
+ upper.Assign(mant*2 + 1)
+ upper.Shift(exp - int(flt.mantbits) - 1)
+
+ // d = mant << (exp - mantbits)
+ // Next lowest floating point number is mant-1 << exp-mantbits,
+ // unless mant-1 drops the significant bit and exp is not the minimum exp,
+ // in which case the next lowest is mant*2-1 << exp-mantbits-1.
+ // Either way, call it mantlo << explo-mantbits.
+ // Our lower bound is halfway between, mantlo*2+1 << explo-mantbits-1.
+ var mantlo uint64
+ var explo int
+ if mant > 1<<flt.mantbits || exp == minexp {
+ mantlo = mant - 1
+ explo = exp
+ } else {
+ mantlo = mant*2 - 1
+ explo = exp - 1
+ }
+ lower := new(decimal)
+ lower.Assign(mantlo*2 + 1)
+ lower.Shift(explo - int(flt.mantbits) - 1)
+
+ // The upper and lower bounds are possible outputs only if
+ // the original mantissa is even, so that IEEE round-to-even
+ // would round to the original mantissa and not the neighbors.
+ inclusive := mant%2 == 0
+
+ // As we walk the digits we want to know whether rounding up would fall
+ // within the upper bound. This is tracked by upperdelta:
+ //
+ // If upperdelta == 0, the digits of d and upper are the same so far.
+ //
+ // If upperdelta == 1, we saw a difference of 1 between d and upper on a
+ // previous digit and subsequently only 9s for d and 0s for upper.
+ // (Thus rounding up may fall outside the bound, if it is exclusive.)
+ //
+ // If upperdelta == 2, then the difference is greater than 1
+ // and we know that rounding up falls within the bound.
+ var upperdelta uint8
+
+ // Now we can figure out the minimum number of digits required.
+ // Walk along until d has distinguished itself from upper and lower.
+ for ui := 0; ; ui++ {
+ // lower, d, and upper may have the decimal points at different
+ // places. In this case upper is the longest, so we iterate from
+ // ui==0 and start li and mi at (possibly) -1.
+ mi := ui - upper.dp + d.dp
+ if mi >= d.nd {
+ break
+ }
+ li := ui - upper.dp + lower.dp
+ l := byte('0') // lower digit
+ if li >= 0 && li < lower.nd {
+ l = lower.d[li]
+ }
+ m := byte('0') // middle digit
+ if mi >= 0 {
+ m = d.d[mi]
+ }
+ u := byte('0') // upper digit
+ if ui < upper.nd {
+ u = upper.d[ui]
+ }
+
+ // Okay to round down (truncate) if lower has a different digit
+ // or if lower is inclusive and is exactly the result of rounding
+ // down (i.e., and we have reached the final digit of lower).
+ okdown := l != m || inclusive && li+1 == lower.nd
+
+ switch {
+ case upperdelta == 0 && m+1 < u:
+ // Example:
+ // m = 12345xxx
+ // u = 12347xxx
+ upperdelta = 2
+ case upperdelta == 0 && m != u:
+ // Example:
+ // m = 12345xxx
+ // u = 12346xxx
+ upperdelta = 1
+ case upperdelta == 1 && (m != '9' || u != '0'):
+ // Example:
+ // m = 1234598x
+ // u = 1234600x
+ upperdelta = 2
+ }
+ // Okay to round up if upper has a different digit and either upper
+ // is inclusive or upper is bigger than the result of rounding up.
+ okup := upperdelta > 0 && (inclusive || upperdelta > 1 || ui+1 < upper.nd)
+
+ // If it's okay to do either, then round to the nearest one.
+ // If it's okay to do only one, do it.
+ switch {
+ case okdown && okup:
+ d.Round(mi + 1)
+ return
+ case okdown:
+ d.RoundDown(mi + 1)
+ return
+ case okup:
+ d.RoundUp(mi + 1)
+ return
+ }
+ }
+}
+
+type decimalSlice struct {
+ d []byte
+ nd, dp int
+}
+
+// %e: -d.ddddde±dd
+func fmtE(dst []byte, neg bool, d decimalSlice, prec int, fmt byte) []byte {
+ // sign
+ if neg {
+ dst = append(dst, '-')
+ }
+
+ // first digit
+ ch := byte('0')
+ if d.nd != 0 {
+ ch = d.d[0]
+ }
+ dst = append(dst, ch)
+
+ // .moredigits
+ if prec > 0 {
+ dst = append(dst, '.')
+ i := 1
+ m := min(d.nd, prec+1)
+ if i < m {
+ dst = append(dst, d.d[i:m]...)
+ i = m
+ }
+ for ; i <= prec; i++ {
+ dst = append(dst, '0')
+ }
+ }
+
+ // e±
+ dst = append(dst, fmt)
+ exp := d.dp - 1
+ if d.nd == 0 { // special case: 0 has exponent 0
+ exp = 0
+ }
+ if exp < 0 {
+ ch = '-'
+ exp = -exp
+ } else {
+ ch = '+'
+ }
+ dst = append(dst, ch)
+
+ // dd or ddd
+ switch {
+ case exp < 10:
+ dst = append(dst, '0', byte(exp)+'0')
+ case exp < 100:
+ dst = append(dst, byte(exp/10)+'0', byte(exp%10)+'0')
+ default:
+ dst = append(dst, byte(exp/100)+'0', byte(exp/10)%10+'0', byte(exp%10)+'0')
+ }
+
+ return dst
+}
+
+// %f: -ddddddd.ddddd
+func fmtF(dst []byte, neg bool, d decimalSlice, prec int) []byte {
+ // sign
+ if neg {
+ dst = append(dst, '-')
+ }
+
+ // integer, padded with zeros as needed.
+ if d.dp > 0 {
+ m := min(d.nd, d.dp)
+ dst = append(dst, d.d[:m]...)
+ for ; m < d.dp; m++ {
+ dst = append(dst, '0')
+ }
+ } else {
+ dst = append(dst, '0')
+ }
+
+ // fraction
+ if prec > 0 {
+ dst = append(dst, '.')
+ for i := 0; i < prec; i++ {
+ ch := byte('0')
+ if j := d.dp + i; 0 <= j && j < d.nd {
+ ch = d.d[j]
+ }
+ dst = append(dst, ch)
+ }
+ }
+
+ return dst
+}
+
+// %b: -ddddddddp±ddd
+func fmtB(dst []byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte {
+ // sign
+ if neg {
+ dst = append(dst, '-')
+ }
+
+ // mantissa
+ dst, _ = formatBits(dst, mant, 10, false, true)
+
+ // p
+ dst = append(dst, 'p')
+
+ // ±exponent
+ exp -= int(flt.mantbits)
+ if exp >= 0 {
+ dst = append(dst, '+')
+ }
+ dst, _ = formatBits(dst, uint64(exp), 10, exp < 0, true)
+
+ return dst
+}
+
+// %x: -0x1.yyyyyyyyp±ddd or -0x0p+0. (y is hex digit, d is decimal digit)
+func fmtX(dst []byte, prec int, fmt byte, neg bool, mant uint64, exp int, flt *floatInfo) []byte {
+ if mant == 0 {
+ exp = 0
+ }
+
+ // Shift digits so leading 1 (if any) is at bit 1<<60.
+ mant <<= 60 - flt.mantbits
+ for mant != 0 && mant&(1<<60) == 0 {
+ mant <<= 1
+ exp--
+ }
+
+ // Round if requested.
+ if prec >= 0 && prec < 15 {
+ shift := uint(prec * 4)
+ extra := (mant << shift) & (1<<60 - 1)
+ mant >>= 60 - shift
+ if extra|(mant&1) > 1<<59 {
+ mant++
+ }
+ mant <<= 60 - shift
+ if mant&(1<<61) != 0 {
+ // Wrapped around.
+ mant >>= 1
+ exp++
+ }
+ }
+
+ hex := lowerhex
+ if fmt == 'X' {
+ hex = upperhex
+ }
+
+ // sign, 0x, leading digit
+ if neg {
+ dst = append(dst, '-')
+ }
+ dst = append(dst, '0', fmt, '0'+byte((mant>>60)&1))
+
+ // .fraction
+ mant <<= 4 // remove leading 0 or 1
+ if prec < 0 && mant != 0 {
+ dst = append(dst, '.')
+ for mant != 0 {
+ dst = append(dst, hex[(mant>>60)&15])
+ mant <<= 4
+ }
+ } else if prec > 0 {
+ dst = append(dst, '.')
+ for i := 0; i < prec; i++ {
+ dst = append(dst, hex[(mant>>60)&15])
+ mant <<= 4
+ }
+ }
+
+ // p±
+ ch := byte('P')
+ if fmt == lower(fmt) {
+ ch = 'p'
+ }
+ dst = append(dst, ch)
+ if exp < 0 {
+ ch = '-'
+ exp = -exp
+ } else {
+ ch = '+'
+ }
+ dst = append(dst, ch)
+
+ // dd or ddd or dddd
+ switch {
+ case exp < 100:
+ dst = append(dst, byte(exp/10)+'0', byte(exp%10)+'0')
+ case exp < 1000:
+ dst = append(dst, byte(exp/100)+'0', byte((exp/10)%10)+'0', byte(exp%10)+'0')
+ default:
+ dst = append(dst, byte(exp/1000)+'0', byte(exp/100)%10+'0', byte((exp/10)%10)+'0', byte(exp%10)+'0')
+ }
+
+ return dst
+}
diff --git a/src/strconv/ftoa_test.go b/src/strconv/ftoa_test.go
new file mode 100644
index 0000000..3512ccf
--- /dev/null
+++ b/src/strconv/ftoa_test.go
@@ -0,0 +1,324 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ "math"
+ "math/rand"
+ . "strconv"
+ "testing"
+)
+
+type ftoaTest struct {
+ f float64
+ fmt byte
+ prec int
+ s string
+}
+
+func fdiv(a, b float64) float64 { return a / b }
+
+const (
+ below1e23 = 99999999999999974834176
+ above1e23 = 100000000000000008388608
+)
+
+var ftoatests = []ftoaTest{
+ {1, 'e', 5, "1.00000e+00"},
+ {1, 'f', 5, "1.00000"},
+ {1, 'g', 5, "1"},
+ {1, 'g', -1, "1"},
+ {1, 'x', -1, "0x1p+00"},
+ {1, 'x', 5, "0x1.00000p+00"},
+ {20, 'g', -1, "20"},
+ {20, 'x', -1, "0x1.4p+04"},
+ {1234567.8, 'g', -1, "1.2345678e+06"},
+ {1234567.8, 'x', -1, "0x1.2d687cccccccdp+20"},
+ {200000, 'g', -1, "200000"},
+ {200000, 'x', -1, "0x1.86ap+17"},
+ {200000, 'X', -1, "0X1.86AP+17"},
+ {2000000, 'g', -1, "2e+06"},
+ {1e10, 'g', -1, "1e+10"},
+
+ // g conversion and zero suppression
+ {400, 'g', 2, "4e+02"},
+ {40, 'g', 2, "40"},
+ {4, 'g', 2, "4"},
+ {.4, 'g', 2, "0.4"},
+ {.04, 'g', 2, "0.04"},
+ {.004, 'g', 2, "0.004"},
+ {.0004, 'g', 2, "0.0004"},
+ {.00004, 'g', 2, "4e-05"},
+ {.000004, 'g', 2, "4e-06"},
+
+ {0, 'e', 5, "0.00000e+00"},
+ {0, 'f', 5, "0.00000"},
+ {0, 'g', 5, "0"},
+ {0, 'g', -1, "0"},
+ {0, 'x', 5, "0x0.00000p+00"},
+
+ {-1, 'e', 5, "-1.00000e+00"},
+ {-1, 'f', 5, "-1.00000"},
+ {-1, 'g', 5, "-1"},
+ {-1, 'g', -1, "-1"},
+
+ {12, 'e', 5, "1.20000e+01"},
+ {12, 'f', 5, "12.00000"},
+ {12, 'g', 5, "12"},
+ {12, 'g', -1, "12"},
+
+ {123456700, 'e', 5, "1.23457e+08"},
+ {123456700, 'f', 5, "123456700.00000"},
+ {123456700, 'g', 5, "1.2346e+08"},
+ {123456700, 'g', -1, "1.234567e+08"},
+
+ {1.2345e6, 'e', 5, "1.23450e+06"},
+ {1.2345e6, 'f', 5, "1234500.00000"},
+ {1.2345e6, 'g', 5, "1.2345e+06"},
+
+ // Round to even
+ {1.2345e6, 'e', 3, "1.234e+06"},
+ {1.2355e6, 'e', 3, "1.236e+06"},
+ {1.2345, 'f', 3, "1.234"},
+ {1.2355, 'f', 3, "1.236"},
+ {1234567890123456.5, 'e', 15, "1.234567890123456e+15"},
+ {1234567890123457.5, 'e', 15, "1.234567890123458e+15"},
+ {108678236358137.625, 'g', -1, "1.0867823635813762e+14"},
+
+ {1e23, 'e', 17, "9.99999999999999916e+22"},
+ {1e23, 'f', 17, "99999999999999991611392.00000000000000000"},
+ {1e23, 'g', 17, "9.9999999999999992e+22"},
+
+ {1e23, 'e', -1, "1e+23"},
+ {1e23, 'f', -1, "100000000000000000000000"},
+ {1e23, 'g', -1, "1e+23"},
+
+ {below1e23, 'e', 17, "9.99999999999999748e+22"},
+ {below1e23, 'f', 17, "99999999999999974834176.00000000000000000"},
+ {below1e23, 'g', 17, "9.9999999999999975e+22"},
+
+ {below1e23, 'e', -1, "9.999999999999997e+22"},
+ {below1e23, 'f', -1, "99999999999999970000000"},
+ {below1e23, 'g', -1, "9.999999999999997e+22"},
+
+ {above1e23, 'e', 17, "1.00000000000000008e+23"},
+ {above1e23, 'f', 17, "100000000000000008388608.00000000000000000"},
+ {above1e23, 'g', 17, "1.0000000000000001e+23"},
+
+ {above1e23, 'e', -1, "1.0000000000000001e+23"},
+ {above1e23, 'f', -1, "100000000000000010000000"},
+ {above1e23, 'g', -1, "1.0000000000000001e+23"},
+
+ {fdiv(5e-304, 1e20), 'g', -1, "5e-324"}, // avoid constant arithmetic
+ {fdiv(-5e-304, 1e20), 'g', -1, "-5e-324"}, // avoid constant arithmetic
+
+ {32, 'g', -1, "32"},
+ {32, 'g', 0, "3e+01"},
+
+ {100, 'x', -1, "0x1.9p+06"},
+ {100, 'y', -1, "%y"},
+
+ {math.NaN(), 'g', -1, "NaN"},
+ {-math.NaN(), 'g', -1, "NaN"},
+ {math.Inf(0), 'g', -1, "+Inf"},
+ {math.Inf(-1), 'g', -1, "-Inf"},
+ {-math.Inf(0), 'g', -1, "-Inf"},
+
+ {-1, 'b', -1, "-4503599627370496p-52"},
+
+ // fixed bugs
+ {0.9, 'f', 1, "0.9"},
+ {0.09, 'f', 1, "0.1"},
+ {0.0999, 'f', 1, "0.1"},
+ {0.05, 'f', 1, "0.1"},
+ {0.05, 'f', 0, "0"},
+ {0.5, 'f', 1, "0.5"},
+ {0.5, 'f', 0, "0"},
+ {1.5, 'f', 0, "2"},
+
+ // https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
+ {2.2250738585072012e-308, 'g', -1, "2.2250738585072014e-308"},
+ // https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
+ {2.2250738585072011e-308, 'g', -1, "2.225073858507201e-308"},
+
+ // Issue 2625.
+ {383260575764816448, 'f', 0, "383260575764816448"},
+ {383260575764816448, 'g', -1, "3.8326057576481645e+17"},
+
+ // Issue 29491.
+ {498484681984085570, 'f', -1, "498484681984085570"},
+ {-5.8339553793802237e+23, 'g', -1, "-5.8339553793802237e+23"},
+
+ // Issue 52187
+ {123.45, '?', 0, "%?"},
+ {123.45, '?', 1, "%?"},
+ {123.45, '?', -1, "%?"},
+
+ // rounding
+ {2.275555555555555, 'x', -1, "0x1.23456789abcdep+01"},
+ {2.275555555555555, 'x', 0, "0x1p+01"},
+ {2.275555555555555, 'x', 2, "0x1.23p+01"},
+ {2.275555555555555, 'x', 16, "0x1.23456789abcde000p+01"},
+ {2.275555555555555, 'x', 21, "0x1.23456789abcde00000000p+01"},
+ {2.2755555510520935, 'x', -1, "0x1.2345678p+01"},
+ {2.2755555510520935, 'x', 6, "0x1.234568p+01"},
+ {2.275555431842804, 'x', -1, "0x1.2345668p+01"},
+ {2.275555431842804, 'x', 6, "0x1.234566p+01"},
+ {3.999969482421875, 'x', -1, "0x1.ffffp+01"},
+ {3.999969482421875, 'x', 4, "0x1.ffffp+01"},
+ {3.999969482421875, 'x', 3, "0x1.000p+02"},
+ {3.999969482421875, 'x', 2, "0x1.00p+02"},
+ {3.999969482421875, 'x', 1, "0x1.0p+02"},
+ {3.999969482421875, 'x', 0, "0x1p+02"},
+}
+
+func TestFtoa(t *testing.T) {
+ for i := 0; i < len(ftoatests); i++ {
+ test := &ftoatests[i]
+ s := FormatFloat(test.f, test.fmt, test.prec, 64)
+ if s != test.s {
+ t.Error("testN=64", test.f, string(test.fmt), test.prec, "want", test.s, "got", s)
+ }
+ x := AppendFloat([]byte("abc"), test.f, test.fmt, test.prec, 64)
+ if string(x) != "abc"+test.s {
+ t.Error("AppendFloat testN=64", test.f, string(test.fmt), test.prec, "want", "abc"+test.s, "got", string(x))
+ }
+ if float64(float32(test.f)) == test.f && test.fmt != 'b' {
+ s := FormatFloat(test.f, test.fmt, test.prec, 32)
+ if s != test.s {
+ t.Error("testN=32", test.f, string(test.fmt), test.prec, "want", test.s, "got", s)
+ }
+ x := AppendFloat([]byte("abc"), test.f, test.fmt, test.prec, 32)
+ if string(x) != "abc"+test.s {
+ t.Error("AppendFloat testN=32", test.f, string(test.fmt), test.prec, "want", "abc"+test.s, "got", string(x))
+ }
+ }
+ }
+}
+
+func TestFtoaPowersOfTwo(t *testing.T) {
+ for exp := -2048; exp <= 2048; exp++ {
+ f := math.Ldexp(1, exp)
+ if !math.IsInf(f, 0) {
+ s := FormatFloat(f, 'e', -1, 64)
+ if x, _ := ParseFloat(s, 64); x != f {
+ t.Errorf("failed roundtrip %v => %s => %v", f, s, x)
+ }
+ }
+ f32 := float32(f)
+ if !math.IsInf(float64(f32), 0) {
+ s := FormatFloat(float64(f32), 'e', -1, 32)
+ if x, _ := ParseFloat(s, 32); float32(x) != f32 {
+ t.Errorf("failed roundtrip %v => %s => %v", f32, s, float32(x))
+ }
+ }
+ }
+}
+
+func TestFtoaRandom(t *testing.T) {
+ N := int(1e4)
+ if testing.Short() {
+ N = 100
+ }
+ t.Logf("testing %d random numbers with fast and slow FormatFloat", N)
+ for i := 0; i < N; i++ {
+ bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
+ x := math.Float64frombits(bits)
+
+ shortFast := FormatFloat(x, 'g', -1, 64)
+ SetOptimize(false)
+ shortSlow := FormatFloat(x, 'g', -1, 64)
+ SetOptimize(true)
+ if shortSlow != shortFast {
+ t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
+ }
+
+ prec := rand.Intn(12) + 5
+ shortFast = FormatFloat(x, 'e', prec, 64)
+ SetOptimize(false)
+ shortSlow = FormatFloat(x, 'e', prec, 64)
+ SetOptimize(true)
+ if shortSlow != shortFast {
+ t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
+ }
+ }
+}
+
+func TestFormatFloatInvalidBitSize(t *testing.T) {
+ defer func() {
+ if r := recover(); r == nil {
+ t.Fatalf("expected panic due to invalid bitSize")
+ }
+ }()
+ _ = FormatFloat(3.14, 'g', -1, 100)
+}
+
+var ftoaBenches = []struct {
+ name string
+ float float64
+ fmt byte
+ prec int
+ bitSize int
+}{
+ {"Decimal", 33909, 'g', -1, 64},
+ {"Float", 339.7784, 'g', -1, 64},
+ {"Exp", -5.09e75, 'g', -1, 64},
+ {"NegExp", -5.11e-95, 'g', -1, 64},
+ {"LongExp", 1.234567890123456e-78, 'g', -1, 64},
+
+ {"Big", 123456789123456789123456789, 'g', -1, 64},
+ {"BinaryExp", -1, 'b', -1, 64},
+
+ {"32Integer", 33909, 'g', -1, 32},
+ {"32ExactFraction", 3.375, 'g', -1, 32},
+ {"32Point", 339.7784, 'g', -1, 32},
+ {"32Exp", -5.09e25, 'g', -1, 32},
+ {"32NegExp", -5.11e-25, 'g', -1, 32},
+ {"32Shortest", 1.234567e-8, 'g', -1, 32},
+ {"32Fixed8Hard", math.Ldexp(15961084, -125), 'e', 8, 32},
+ {"32Fixed9Hard", math.Ldexp(14855922, -83), 'e', 9, 32},
+
+ {"64Fixed1", 123456, 'e', 3, 64},
+ {"64Fixed2", 123.456, 'e', 3, 64},
+ {"64Fixed3", 1.23456e+78, 'e', 3, 64},
+ {"64Fixed4", 1.23456e-78, 'e', 3, 64},
+ {"64Fixed12", 1.23456e-78, 'e', 12, 64},
+ {"64Fixed16", 1.23456e-78, 'e', 16, 64},
+ // From testdata/testfp.txt
+ {"64Fixed12Hard", math.Ldexp(6965949469487146, -249), 'e', 12, 64},
+ {"64Fixed17Hard", math.Ldexp(8887055249355788, 665), 'e', 17, 64},
+ {"64Fixed18Hard", math.Ldexp(6994187472632449, 690), 'e', 18, 64},
+
+ // Trigger slow path (see issue #15672).
+ // The shortest is: 8.034137530808823e+43
+ {"Slowpath64", 8.03413753080882349e+43, 'e', -1, 64},
+ // This denormal is pathological because the lower/upper
+ // halfways to neighboring floats are:
+ // 622666234635.321003e-320 ~= 622666234635.321e-320
+ // 622666234635.321497e-320 ~= 622666234635.3215e-320
+ // making it hard to find the 3rd digit
+ {"SlowpathDenormal64", 622666234635.3213e-320, 'e', -1, 64},
+}
+
+func BenchmarkFormatFloat(b *testing.B) {
+ for _, c := range ftoaBenches {
+ b.Run(c.name, func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ FormatFloat(c.float, c.fmt, c.prec, c.bitSize)
+ }
+ })
+ }
+}
+
+func BenchmarkAppendFloat(b *testing.B) {
+ dst := make([]byte, 30)
+ for _, c := range ftoaBenches {
+ b.Run(c.name, func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ AppendFloat(dst[:0], c.float, c.fmt, c.prec, c.bitSize)
+ }
+ })
+ }
+}
diff --git a/src/strconv/ftoaryu.go b/src/strconv/ftoaryu.go
new file mode 100644
index 0000000..2e7bf71
--- /dev/null
+++ b/src/strconv/ftoaryu.go
@@ -0,0 +1,569 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv
+
+import (
+ "math/bits"
+)
+
+// binary to decimal conversion using the Ryū algorithm.
+//
+// See Ulf Adams, "Ryū: Fast Float-to-String Conversion" (doi:10.1145/3192366.3192369)
+//
+// Fixed precision formatting is a variant of the original paper's
+// algorithm, where a single multiplication by 10^k is required,
+// sharing the same rounding guarantees.
+
+// ryuFtoaFixed32 formats mant*(2^exp) with prec decimal digits.
+func ryuFtoaFixed32(d *decimalSlice, mant uint32, exp int, prec int) {
+ if prec < 0 {
+ panic("ryuFtoaFixed32 called with negative prec")
+ }
+ if prec > 9 {
+ panic("ryuFtoaFixed32 called with prec > 9")
+ }
+ // Zero input.
+ if mant == 0 {
+ d.nd, d.dp = 0, 0
+ return
+ }
+ // Renormalize to a 25-bit mantissa.
+ e2 := exp
+ if b := bits.Len32(mant); b < 25 {
+ mant <<= uint(25 - b)
+ e2 += b - 25
+ }
+ // Choose an exponent such that rounded mant*(2^e2)*(10^q) has
+ // at least prec decimal digits, i.e
+ // mant*(2^e2)*(10^q) >= 10^(prec-1)
+ // Because mant >= 2^24, it is enough to choose:
+ // 2^(e2+24) >= 10^(-q+prec-1)
+ // or q = -mulByLog2Log10(e2+24) + prec - 1
+ q := -mulByLog2Log10(e2+24) + prec - 1
+
+ // Now compute mant*(2^e2)*(10^q).
+ // Is it an exact computation?
+ // Only small positive powers of 10 are exact (5^28 has 66 bits).
+ exact := q <= 27 && q >= 0
+
+ di, dexp2, d0 := mult64bitPow10(mant, e2, q)
+ if dexp2 >= 0 {
+ panic("not enough significant bits after mult64bitPow10")
+ }
+ // As a special case, computation might still be exact, if exponent
+ // was negative and if it amounts to computing an exact division.
+ // In that case, we ignore all lower bits.
+ // Note that division by 10^11 cannot be exact as 5^11 has 26 bits.
+ if q < 0 && q >= -10 && divisibleByPower5(uint64(mant), -q) {
+ exact = true
+ d0 = true
+ }
+ // Remove extra lower bits and keep rounding info.
+ extra := uint(-dexp2)
+ extraMask := uint32(1<<extra - 1)
+
+ di, dfrac := di>>extra, di&extraMask
+ roundUp := false
+ if exact {
+ // If we computed an exact product, d + 1/2
+ // should round to d+1 if 'd' is odd.
+ roundUp = dfrac > 1<<(extra-1) ||
+ (dfrac == 1<<(extra-1) && !d0) ||
+ (dfrac == 1<<(extra-1) && d0 && di&1 == 1)
+ } else {
+ // otherwise, d+1/2 always rounds up because
+ // we truncated below.
+ roundUp = dfrac>>(extra-1) == 1
+ }
+ if dfrac != 0 {
+ d0 = false
+ }
+ // Proceed to the requested number of digits
+ formatDecimal(d, uint64(di), !d0, roundUp, prec)
+ // Adjust exponent
+ d.dp -= q
+}
+
+// ryuFtoaFixed64 formats mant*(2^exp) with prec decimal digits.
+func ryuFtoaFixed64(d *decimalSlice, mant uint64, exp int, prec int) {
+ if prec > 18 {
+ panic("ryuFtoaFixed64 called with prec > 18")
+ }
+ // Zero input.
+ if mant == 0 {
+ d.nd, d.dp = 0, 0
+ return
+ }
+ // Renormalize to a 55-bit mantissa.
+ e2 := exp
+ if b := bits.Len64(mant); b < 55 {
+ mant = mant << uint(55-b)
+ e2 += b - 55
+ }
+ // Choose an exponent such that rounded mant*(2^e2)*(10^q) has
+ // at least prec decimal digits, i.e
+ // mant*(2^e2)*(10^q) >= 10^(prec-1)
+ // Because mant >= 2^54, it is enough to choose:
+ // 2^(e2+54) >= 10^(-q+prec-1)
+ // or q = -mulByLog2Log10(e2+54) + prec - 1
+ //
+ // The minimal required exponent is -mulByLog2Log10(1025)+18 = -291
+ // The maximal required exponent is mulByLog2Log10(1074)+18 = 342
+ q := -mulByLog2Log10(e2+54) + prec - 1
+
+ // Now compute mant*(2^e2)*(10^q).
+ // Is it an exact computation?
+ // Only small positive powers of 10 are exact (5^55 has 128 bits).
+ exact := q <= 55 && q >= 0
+
+ di, dexp2, d0 := mult128bitPow10(mant, e2, q)
+ if dexp2 >= 0 {
+ panic("not enough significant bits after mult128bitPow10")
+ }
+ // As a special case, computation might still be exact, if exponent
+ // was negative and if it amounts to computing an exact division.
+ // In that case, we ignore all lower bits.
+ // Note that division by 10^23 cannot be exact as 5^23 has 54 bits.
+ if q < 0 && q >= -22 && divisibleByPower5(mant, -q) {
+ exact = true
+ d0 = true
+ }
+ // Remove extra lower bits and keep rounding info.
+ extra := uint(-dexp2)
+ extraMask := uint64(1<<extra - 1)
+
+ di, dfrac := di>>extra, di&extraMask
+ roundUp := false
+ if exact {
+ // If we computed an exact product, d + 1/2
+ // should round to d+1 if 'd' is odd.
+ roundUp = dfrac > 1<<(extra-1) ||
+ (dfrac == 1<<(extra-1) && !d0) ||
+ (dfrac == 1<<(extra-1) && d0 && di&1 == 1)
+ } else {
+ // otherwise, d+1/2 always rounds up because
+ // we truncated below.
+ roundUp = dfrac>>(extra-1) == 1
+ }
+ if dfrac != 0 {
+ d0 = false
+ }
+ // Proceed to the requested number of digits
+ formatDecimal(d, di, !d0, roundUp, prec)
+ // Adjust exponent
+ d.dp -= q
+}
+
+var uint64pow10 = [...]uint64{
+ 1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
+ 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
+}
+
+// formatDecimal fills d with at most prec decimal digits
+// of mantissa m. The boolean trunc indicates whether m
+// is truncated compared to the original number being formatted.
+func formatDecimal(d *decimalSlice, m uint64, trunc bool, roundUp bool, prec int) {
+ max := uint64pow10[prec]
+ trimmed := 0
+ for m >= max {
+ a, b := m/10, m%10
+ m = a
+ trimmed++
+ if b > 5 {
+ roundUp = true
+ } else if b < 5 {
+ roundUp = false
+ } else { // b == 5
+ // round up if there are trailing digits,
+ // or if the new value of m is odd (round-to-even convention)
+ roundUp = trunc || m&1 == 1
+ }
+ if b != 0 {
+ trunc = true
+ }
+ }
+ if roundUp {
+ m++
+ }
+ if m >= max {
+ // Happens if di was originally 99999....xx
+ m /= 10
+ trimmed++
+ }
+ // render digits (similar to formatBits)
+ n := uint(prec)
+ d.nd = prec
+ v := m
+ for v >= 100 {
+ var v1, v2 uint64
+ if v>>32 == 0 {
+ v1, v2 = uint64(uint32(v)/100), uint64(uint32(v)%100)
+ } else {
+ v1, v2 = v/100, v%100
+ }
+ n -= 2
+ d.d[n+1] = smallsString[2*v2+1]
+ d.d[n+0] = smallsString[2*v2+0]
+ v = v1
+ }
+ if v > 0 {
+ n--
+ d.d[n] = smallsString[2*v+1]
+ }
+ if v >= 10 {
+ n--
+ d.d[n] = smallsString[2*v]
+ }
+ for d.d[d.nd-1] == '0' {
+ d.nd--
+ trimmed++
+ }
+ d.dp = d.nd + trimmed
+}
+
+// ryuFtoaShortest formats mant*2^exp with prec decimal digits.
+func ryuFtoaShortest(d *decimalSlice, mant uint64, exp int, flt *floatInfo) {
+ if mant == 0 {
+ d.nd, d.dp = 0, 0
+ return
+ }
+ // If input is an exact integer with fewer bits than the mantissa,
+ // the previous and next integer are not admissible representations.
+ if exp <= 0 && bits.TrailingZeros64(mant) >= -exp {
+ mant >>= uint(-exp)
+ ryuDigits(d, mant, mant, mant, true, false)
+ return
+ }
+ ml, mc, mu, e2 := computeBounds(mant, exp, flt)
+ if e2 == 0 {
+ ryuDigits(d, ml, mc, mu, true, false)
+ return
+ }
+ // Find 10^q *larger* than 2^-e2
+ q := mulByLog2Log10(-e2) + 1
+
+ // We are going to multiply by 10^q using 128-bit arithmetic.
+ // The exponent is the same for all 3 numbers.
+ var dl, dc, du uint64
+ var dl0, dc0, du0 bool
+ if flt == &float32info {
+ var dl32, dc32, du32 uint32
+ dl32, _, dl0 = mult64bitPow10(uint32(ml), e2, q)
+ dc32, _, dc0 = mult64bitPow10(uint32(mc), e2, q)
+ du32, e2, du0 = mult64bitPow10(uint32(mu), e2, q)
+ dl, dc, du = uint64(dl32), uint64(dc32), uint64(du32)
+ } else {
+ dl, _, dl0 = mult128bitPow10(ml, e2, q)
+ dc, _, dc0 = mult128bitPow10(mc, e2, q)
+ du, e2, du0 = mult128bitPow10(mu, e2, q)
+ }
+ if e2 >= 0 {
+ panic("not enough significant bits after mult128bitPow10")
+ }
+ // Is it an exact computation?
+ if q > 55 {
+ // Large positive powers of ten are not exact
+ dl0, dc0, du0 = false, false, false
+ }
+ if q < 0 && q >= -24 {
+ // Division by a power of ten may be exact.
+ // (note that 5^25 is a 59-bit number so division by 5^25 is never exact).
+ if divisibleByPower5(ml, -q) {
+ dl0 = true
+ }
+ if divisibleByPower5(mc, -q) {
+ dc0 = true
+ }
+ if divisibleByPower5(mu, -q) {
+ du0 = true
+ }
+ }
+ // Express the results (dl, dc, du)*2^e2 as integers.
+ // Extra bits must be removed and rounding hints computed.
+ extra := uint(-e2)
+ extraMask := uint64(1<<extra - 1)
+ // Now compute the floored, integral base 10 mantissas.
+ dl, fracl := dl>>extra, dl&extraMask
+ dc, fracc := dc>>extra, dc&extraMask
+ du, fracu := du>>extra, du&extraMask
+ // Is it allowed to use 'du' as a result?
+ // It is always allowed when it is truncated, but also
+ // if it is exact and the original binary mantissa is even
+ // When disallowed, we can subtract 1.
+ uok := !du0 || fracu > 0
+ if du0 && fracu == 0 {
+ uok = mant&1 == 0
+ }
+ if !uok {
+ du--
+ }
+ // Is 'dc' the correctly rounded base 10 mantissa?
+ // The correct rounding might be dc+1
+ cup := false // don't round up.
+ if dc0 {
+ // If we computed an exact product, the half integer
+ // should round to next (even) integer if 'dc' is odd.
+ cup = fracc > 1<<(extra-1) ||
+ (fracc == 1<<(extra-1) && dc&1 == 1)
+ } else {
+ // otherwise, the result is a lower truncation of the ideal
+ // result.
+ cup = fracc>>(extra-1) == 1
+ }
+ // Is 'dl' an allowed representation?
+ // Only if it is an exact value, and if the original binary mantissa
+ // was even.
+ lok := dl0 && fracl == 0 && (mant&1 == 0)
+ if !lok {
+ dl++
+ }
+ // We need to remember whether the trimmed digits of 'dc' are zero.
+ c0 := dc0 && fracc == 0
+ // render digits
+ ryuDigits(d, dl, dc, du, c0, cup)
+ d.dp -= q
+}
+
+// mulByLog2Log10 returns math.Floor(x * log(2)/log(10)) for an integer x in
+// the range -1600 <= x && x <= +1600.
+//
+// The range restriction lets us work in faster integer arithmetic instead of
+// slower floating point arithmetic. Correctness is verified by unit tests.
+func mulByLog2Log10(x int) int {
+ // log(2)/log(10) ≈ 0.30102999566 ≈ 78913 / 2^18
+ return (x * 78913) >> 18
+}
+
+// mulByLog10Log2 returns math.Floor(x * log(10)/log(2)) for an integer x in
+// the range -500 <= x && x <= +500.
+//
+// The range restriction lets us work in faster integer arithmetic instead of
+// slower floating point arithmetic. Correctness is verified by unit tests.
+func mulByLog10Log2(x int) int {
+ // log(10)/log(2) ≈ 3.32192809489 ≈ 108853 / 2^15
+ return (x * 108853) >> 15
+}
+
+// computeBounds returns a floating-point vector (l, c, u)×2^e2
+// where the mantissas are 55-bit (or 26-bit) integers, describing the interval
+// represented by the input float64 or float32.
+func computeBounds(mant uint64, exp int, flt *floatInfo) (lower, central, upper uint64, e2 int) {
+ if mant != 1<<flt.mantbits || exp == flt.bias+1-int(flt.mantbits) {
+ // regular case (or denormals)
+ lower, central, upper = 2*mant-1, 2*mant, 2*mant+1
+ e2 = exp - 1
+ return
+ } else {
+ // border of an exponent
+ lower, central, upper = 4*mant-1, 4*mant, 4*mant+2
+ e2 = exp - 2
+ return
+ }
+}
+
+func ryuDigits(d *decimalSlice, lower, central, upper uint64,
+ c0, cup bool) {
+ lhi, llo := divmod1e9(lower)
+ chi, clo := divmod1e9(central)
+ uhi, ulo := divmod1e9(upper)
+ if uhi == 0 {
+ // only low digits (for denormals)
+ ryuDigits32(d, llo, clo, ulo, c0, cup, 8)
+ } else if lhi < uhi {
+ // truncate 9 digits at once.
+ if llo != 0 {
+ lhi++
+ }
+ c0 = c0 && clo == 0
+ cup = (clo > 5e8) || (clo == 5e8 && cup)
+ ryuDigits32(d, lhi, chi, uhi, c0, cup, 8)
+ d.dp += 9
+ } else {
+ d.nd = 0
+ // emit high part
+ n := uint(9)
+ for v := chi; v > 0; {
+ v1, v2 := v/10, v%10
+ v = v1
+ n--
+ d.d[n] = byte(v2 + '0')
+ }
+ d.d = d.d[n:]
+ d.nd = int(9 - n)
+ // emit low part
+ ryuDigits32(d, llo, clo, ulo,
+ c0, cup, d.nd+8)
+ }
+ // trim trailing zeros
+ for d.nd > 0 && d.d[d.nd-1] == '0' {
+ d.nd--
+ }
+ // trim initial zeros
+ for d.nd > 0 && d.d[0] == '0' {
+ d.nd--
+ d.dp--
+ d.d = d.d[1:]
+ }
+}
+
+// ryuDigits32 emits decimal digits for a number less than 1e9.
+func ryuDigits32(d *decimalSlice, lower, central, upper uint32,
+ c0, cup bool, endindex int) {
+ if upper == 0 {
+ d.dp = endindex + 1
+ return
+ }
+ trimmed := 0
+ // Remember last trimmed digit to check for round-up.
+ // c0 will be used to remember zeroness of following digits.
+ cNextDigit := 0
+ for upper > 0 {
+ // Repeatedly compute:
+ // l = Ceil(lower / 10^k)
+ // c = Round(central / 10^k)
+ // u = Floor(upper / 10^k)
+ // and stop when c goes out of the (l, u) interval.
+ l := (lower + 9) / 10
+ c, cdigit := central/10, central%10
+ u := upper / 10
+ if l > u {
+ // don't trim the last digit as it is forbidden to go below l
+ // other, trim and exit now.
+ break
+ }
+ // Check that we didn't cross the lower boundary.
+ // The case where l < u but c == l-1 is essentially impossible,
+ // but may happen if:
+ // lower = ..11
+ // central = ..19
+ // upper = ..31
+ // and means that 'central' is very close but less than
+ // an integer ending with many zeros, and usually
+ // the "round-up" logic hides the problem.
+ if l == c+1 && c < u {
+ c++
+ cdigit = 0
+ cup = false
+ }
+ trimmed++
+ // Remember trimmed digits of c
+ c0 = c0 && cNextDigit == 0
+ cNextDigit = int(cdigit)
+ lower, central, upper = l, c, u
+ }
+ // should we round up?
+ if trimmed > 0 {
+ cup = cNextDigit > 5 ||
+ (cNextDigit == 5 && !c0) ||
+ (cNextDigit == 5 && c0 && central&1 == 1)
+ }
+ if central < upper && cup {
+ central++
+ }
+ // We know where the number ends, fill directly
+ endindex -= trimmed
+ v := central
+ n := endindex
+ for n > d.nd {
+ v1, v2 := v/100, v%100
+ d.d[n] = smallsString[2*v2+1]
+ d.d[n-1] = smallsString[2*v2+0]
+ n -= 2
+ v = v1
+ }
+ if n == d.nd {
+ d.d[n] = byte(v + '0')
+ }
+ d.nd = endindex + 1
+ d.dp = d.nd + trimmed
+}
+
+// mult64bitPow10 takes a floating-point input with a 25-bit
+// mantissa and multiplies it with 10^q. The resulting mantissa
+// is m*P >> 57 where P is a 64-bit element of the detailedPowersOfTen tables.
+// It is typically 31 or 32-bit wide.
+// The returned boolean is true if all trimmed bits were zero.
+//
+// That is:
+//
+// m*2^e2 * round(10^q) = resM * 2^resE + ε
+// exact = ε == 0
+func mult64bitPow10(m uint32, e2, q int) (resM uint32, resE int, exact bool) {
+ if q == 0 {
+ // P == 1<<63
+ return m << 6, e2 - 6, true
+ }
+ if q < detailedPowersOfTenMinExp10 || detailedPowersOfTenMaxExp10 < q {
+ // This never happens due to the range of float32/float64 exponent
+ panic("mult64bitPow10: power of 10 is out of range")
+ }
+ pow := detailedPowersOfTen[q-detailedPowersOfTenMinExp10][1]
+ if q < 0 {
+ // Inverse powers of ten must be rounded up.
+ pow += 1
+ }
+ hi, lo := bits.Mul64(uint64(m), pow)
+ e2 += mulByLog10Log2(q) - 63 + 57
+ return uint32(hi<<7 | lo>>57), e2, lo<<7 == 0
+}
+
+// mult128bitPow10 takes a floating-point input with a 55-bit
+// mantissa and multiplies it with 10^q. The resulting mantissa
+// is m*P >> 119 where P is a 128-bit element of the detailedPowersOfTen tables.
+// It is typically 63 or 64-bit wide.
+// The returned boolean is true is all trimmed bits were zero.
+//
+// That is:
+//
+// m*2^e2 * round(10^q) = resM * 2^resE + ε
+// exact = ε == 0
+func mult128bitPow10(m uint64, e2, q int) (resM uint64, resE int, exact bool) {
+ if q == 0 {
+ // P == 1<<127
+ return m << 8, e2 - 8, true
+ }
+ if q < detailedPowersOfTenMinExp10 || detailedPowersOfTenMaxExp10 < q {
+ // This never happens due to the range of float32/float64 exponent
+ panic("mult128bitPow10: power of 10 is out of range")
+ }
+ pow := detailedPowersOfTen[q-detailedPowersOfTenMinExp10]
+ if q < 0 {
+ // Inverse powers of ten must be rounded up.
+ pow[0] += 1
+ }
+ e2 += mulByLog10Log2(q) - 127 + 119
+
+ // long multiplication
+ l1, l0 := bits.Mul64(m, pow[0])
+ h1, h0 := bits.Mul64(m, pow[1])
+ mid, carry := bits.Add64(l1, h0, 0)
+ h1 += carry
+ return h1<<9 | mid>>55, e2, mid<<9 == 0 && l0 == 0
+}
+
+func divisibleByPower5(m uint64, k int) bool {
+ if m == 0 {
+ return true
+ }
+ for i := 0; i < k; i++ {
+ if m%5 != 0 {
+ return false
+ }
+ m /= 5
+ }
+ return true
+}
+
+// divmod1e9 computes quotient and remainder of division by 1e9,
+// avoiding runtime uint64 division on 32-bit platforms.
+func divmod1e9(x uint64) (uint32, uint32) {
+ if !host32bit {
+ return uint32(x / 1e9), uint32(x % 1e9)
+ }
+ // Use the same sequence of operations as the amd64 compiler.
+ hi, _ := bits.Mul64(x>>1, 0x89705f4136b4a598) // binary digits of 1e-9
+ q := hi >> 28
+ return uint32(q), uint32(x - q*1e9)
+}
diff --git a/src/strconv/ftoaryu_test.go b/src/strconv/ftoaryu_test.go
new file mode 100644
index 0000000..9758619
--- /dev/null
+++ b/src/strconv/ftoaryu_test.go
@@ -0,0 +1,31 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ "math"
+ . "strconv"
+ "testing"
+)
+
+func TestMulByLog2Log10(t *testing.T) {
+ for x := -1600; x <= +1600; x++ {
+ iMath := MulByLog2Log10(x)
+ fMath := int(math.Floor(float64(x) * math.Ln2 / math.Ln10))
+ if iMath != fMath {
+ t.Errorf("mulByLog2Log10(%d) failed: %d vs %d\n", x, iMath, fMath)
+ }
+ }
+}
+
+func TestMulByLog10Log2(t *testing.T) {
+ for x := -500; x <= +500; x++ {
+ iMath := MulByLog10Log2(x)
+ fMath := int(math.Floor(float64(x) * math.Ln10 / math.Ln2))
+ if iMath != fMath {
+ t.Errorf("mulByLog10Log2(%d) failed: %d vs %d\n", x, iMath, fMath)
+ }
+ }
+}
diff --git a/src/strconv/internal_test.go b/src/strconv/internal_test.go
new file mode 100644
index 0000000..f2cceff
--- /dev/null
+++ b/src/strconv/internal_test.go
@@ -0,0 +1,31 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// export access to strconv internals for tests
+
+package strconv
+
+func NewDecimal(i uint64) *decimal {
+ d := new(decimal)
+ d.Assign(i)
+ return d
+}
+
+func SetOptimize(b bool) bool {
+ old := optimize
+ optimize = b
+ return old
+}
+
+func ParseFloatPrefix(s string, bitSize int) (float64, int, error) {
+ return parseFloatPrefix(s, bitSize)
+}
+
+func MulByLog2Log10(x int) int {
+ return mulByLog2Log10(x)
+}
+
+func MulByLog10Log2(x int) int {
+ return mulByLog10Log2(x)
+}
diff --git a/src/strconv/isprint.go b/src/strconv/isprint.go
new file mode 100644
index 0000000..baa14a6
--- /dev/null
+++ b/src/strconv/isprint.go
@@ -0,0 +1,752 @@
+// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Code generated by go run makeisprint.go -output isprint.go; DO NOT EDIT.
+
+package strconv
+
+// (424+133+112)*2 + (508)*4 = 3370 bytes
+
+var isPrint16 = []uint16{
+ 0x0020, 0x007e,
+ 0x00a1, 0x0377,
+ 0x037a, 0x037f,
+ 0x0384, 0x0556,
+ 0x0559, 0x058a,
+ 0x058d, 0x05c7,
+ 0x05d0, 0x05ea,
+ 0x05ef, 0x05f4,
+ 0x0606, 0x070d,
+ 0x0710, 0x074a,
+ 0x074d, 0x07b1,
+ 0x07c0, 0x07fa,
+ 0x07fd, 0x082d,
+ 0x0830, 0x085b,
+ 0x085e, 0x086a,
+ 0x0870, 0x088e,
+ 0x0898, 0x098c,
+ 0x098f, 0x0990,
+ 0x0993, 0x09b2,
+ 0x09b6, 0x09b9,
+ 0x09bc, 0x09c4,
+ 0x09c7, 0x09c8,
+ 0x09cb, 0x09ce,
+ 0x09d7, 0x09d7,
+ 0x09dc, 0x09e3,
+ 0x09e6, 0x09fe,
+ 0x0a01, 0x0a0a,
+ 0x0a0f, 0x0a10,
+ 0x0a13, 0x0a39,
+ 0x0a3c, 0x0a42,
+ 0x0a47, 0x0a48,
+ 0x0a4b, 0x0a4d,
+ 0x0a51, 0x0a51,
+ 0x0a59, 0x0a5e,
+ 0x0a66, 0x0a76,
+ 0x0a81, 0x0ab9,
+ 0x0abc, 0x0acd,
+ 0x0ad0, 0x0ad0,
+ 0x0ae0, 0x0ae3,
+ 0x0ae6, 0x0af1,
+ 0x0af9, 0x0b0c,
+ 0x0b0f, 0x0b10,
+ 0x0b13, 0x0b39,
+ 0x0b3c, 0x0b44,
+ 0x0b47, 0x0b48,
+ 0x0b4b, 0x0b4d,
+ 0x0b55, 0x0b57,
+ 0x0b5c, 0x0b63,
+ 0x0b66, 0x0b77,
+ 0x0b82, 0x0b8a,
+ 0x0b8e, 0x0b95,
+ 0x0b99, 0x0b9f,
+ 0x0ba3, 0x0ba4,
+ 0x0ba8, 0x0baa,
+ 0x0bae, 0x0bb9,
+ 0x0bbe, 0x0bc2,
+ 0x0bc6, 0x0bcd,
+ 0x0bd0, 0x0bd0,
+ 0x0bd7, 0x0bd7,
+ 0x0be6, 0x0bfa,
+ 0x0c00, 0x0c39,
+ 0x0c3c, 0x0c4d,
+ 0x0c55, 0x0c5a,
+ 0x0c5d, 0x0c5d,
+ 0x0c60, 0x0c63,
+ 0x0c66, 0x0c6f,
+ 0x0c77, 0x0cb9,
+ 0x0cbc, 0x0ccd,
+ 0x0cd5, 0x0cd6,
+ 0x0cdd, 0x0ce3,
+ 0x0ce6, 0x0cf3,
+ 0x0d00, 0x0d4f,
+ 0x0d54, 0x0d63,
+ 0x0d66, 0x0d96,
+ 0x0d9a, 0x0dbd,
+ 0x0dc0, 0x0dc6,
+ 0x0dca, 0x0dca,
+ 0x0dcf, 0x0ddf,
+ 0x0de6, 0x0def,
+ 0x0df2, 0x0df4,
+ 0x0e01, 0x0e3a,
+ 0x0e3f, 0x0e5b,
+ 0x0e81, 0x0ebd,
+ 0x0ec0, 0x0ed9,
+ 0x0edc, 0x0edf,
+ 0x0f00, 0x0f6c,
+ 0x0f71, 0x0fda,
+ 0x1000, 0x10c7,
+ 0x10cd, 0x10cd,
+ 0x10d0, 0x124d,
+ 0x1250, 0x125d,
+ 0x1260, 0x128d,
+ 0x1290, 0x12b5,
+ 0x12b8, 0x12c5,
+ 0x12c8, 0x1315,
+ 0x1318, 0x135a,
+ 0x135d, 0x137c,
+ 0x1380, 0x1399,
+ 0x13a0, 0x13f5,
+ 0x13f8, 0x13fd,
+ 0x1400, 0x169c,
+ 0x16a0, 0x16f8,
+ 0x1700, 0x1715,
+ 0x171f, 0x1736,
+ 0x1740, 0x1753,
+ 0x1760, 0x1773,
+ 0x1780, 0x17dd,
+ 0x17e0, 0x17e9,
+ 0x17f0, 0x17f9,
+ 0x1800, 0x1819,
+ 0x1820, 0x1878,
+ 0x1880, 0x18aa,
+ 0x18b0, 0x18f5,
+ 0x1900, 0x192b,
+ 0x1930, 0x193b,
+ 0x1940, 0x1940,
+ 0x1944, 0x196d,
+ 0x1970, 0x1974,
+ 0x1980, 0x19ab,
+ 0x19b0, 0x19c9,
+ 0x19d0, 0x19da,
+ 0x19de, 0x1a1b,
+ 0x1a1e, 0x1a7c,
+ 0x1a7f, 0x1a89,
+ 0x1a90, 0x1a99,
+ 0x1aa0, 0x1aad,
+ 0x1ab0, 0x1ace,
+ 0x1b00, 0x1b4c,
+ 0x1b50, 0x1bf3,
+ 0x1bfc, 0x1c37,
+ 0x1c3b, 0x1c49,
+ 0x1c4d, 0x1c88,
+ 0x1c90, 0x1cba,
+ 0x1cbd, 0x1cc7,
+ 0x1cd0, 0x1cfa,
+ 0x1d00, 0x1f15,
+ 0x1f18, 0x1f1d,
+ 0x1f20, 0x1f45,
+ 0x1f48, 0x1f4d,
+ 0x1f50, 0x1f7d,
+ 0x1f80, 0x1fd3,
+ 0x1fd6, 0x1fef,
+ 0x1ff2, 0x1ffe,
+ 0x2010, 0x2027,
+ 0x2030, 0x205e,
+ 0x2070, 0x2071,
+ 0x2074, 0x209c,
+ 0x20a0, 0x20c0,
+ 0x20d0, 0x20f0,
+ 0x2100, 0x218b,
+ 0x2190, 0x2426,
+ 0x2440, 0x244a,
+ 0x2460, 0x2b73,
+ 0x2b76, 0x2cf3,
+ 0x2cf9, 0x2d27,
+ 0x2d2d, 0x2d2d,
+ 0x2d30, 0x2d67,
+ 0x2d6f, 0x2d70,
+ 0x2d7f, 0x2d96,
+ 0x2da0, 0x2e5d,
+ 0x2e80, 0x2ef3,
+ 0x2f00, 0x2fd5,
+ 0x2ff0, 0x2ffb,
+ 0x3001, 0x3096,
+ 0x3099, 0x30ff,
+ 0x3105, 0x31e3,
+ 0x31f0, 0xa48c,
+ 0xa490, 0xa4c6,
+ 0xa4d0, 0xa62b,
+ 0xa640, 0xa6f7,
+ 0xa700, 0xa7ca,
+ 0xa7d0, 0xa7d9,
+ 0xa7f2, 0xa82c,
+ 0xa830, 0xa839,
+ 0xa840, 0xa877,
+ 0xa880, 0xa8c5,
+ 0xa8ce, 0xa8d9,
+ 0xa8e0, 0xa953,
+ 0xa95f, 0xa97c,
+ 0xa980, 0xa9d9,
+ 0xa9de, 0xaa36,
+ 0xaa40, 0xaa4d,
+ 0xaa50, 0xaa59,
+ 0xaa5c, 0xaac2,
+ 0xaadb, 0xaaf6,
+ 0xab01, 0xab06,
+ 0xab09, 0xab0e,
+ 0xab11, 0xab16,
+ 0xab20, 0xab6b,
+ 0xab70, 0xabed,
+ 0xabf0, 0xabf9,
+ 0xac00, 0xd7a3,
+ 0xd7b0, 0xd7c6,
+ 0xd7cb, 0xd7fb,
+ 0xf900, 0xfa6d,
+ 0xfa70, 0xfad9,
+ 0xfb00, 0xfb06,
+ 0xfb13, 0xfb17,
+ 0xfb1d, 0xfbc2,
+ 0xfbd3, 0xfd8f,
+ 0xfd92, 0xfdc7,
+ 0xfdcf, 0xfdcf,
+ 0xfdf0, 0xfe19,
+ 0xfe20, 0xfe6b,
+ 0xfe70, 0xfefc,
+ 0xff01, 0xffbe,
+ 0xffc2, 0xffc7,
+ 0xffca, 0xffcf,
+ 0xffd2, 0xffd7,
+ 0xffda, 0xffdc,
+ 0xffe0, 0xffee,
+ 0xfffc, 0xfffd,
+}
+
+var isNotPrint16 = []uint16{
+ 0x00ad,
+ 0x038b,
+ 0x038d,
+ 0x03a2,
+ 0x0530,
+ 0x0590,
+ 0x061c,
+ 0x06dd,
+ 0x083f,
+ 0x085f,
+ 0x08e2,
+ 0x0984,
+ 0x09a9,
+ 0x09b1,
+ 0x09de,
+ 0x0a04,
+ 0x0a29,
+ 0x0a31,
+ 0x0a34,
+ 0x0a37,
+ 0x0a3d,
+ 0x0a5d,
+ 0x0a84,
+ 0x0a8e,
+ 0x0a92,
+ 0x0aa9,
+ 0x0ab1,
+ 0x0ab4,
+ 0x0ac6,
+ 0x0aca,
+ 0x0b00,
+ 0x0b04,
+ 0x0b29,
+ 0x0b31,
+ 0x0b34,
+ 0x0b5e,
+ 0x0b84,
+ 0x0b91,
+ 0x0b9b,
+ 0x0b9d,
+ 0x0bc9,
+ 0x0c0d,
+ 0x0c11,
+ 0x0c29,
+ 0x0c45,
+ 0x0c49,
+ 0x0c57,
+ 0x0c8d,
+ 0x0c91,
+ 0x0ca9,
+ 0x0cb4,
+ 0x0cc5,
+ 0x0cc9,
+ 0x0cdf,
+ 0x0cf0,
+ 0x0d0d,
+ 0x0d11,
+ 0x0d45,
+ 0x0d49,
+ 0x0d80,
+ 0x0d84,
+ 0x0db2,
+ 0x0dbc,
+ 0x0dd5,
+ 0x0dd7,
+ 0x0e83,
+ 0x0e85,
+ 0x0e8b,
+ 0x0ea4,
+ 0x0ea6,
+ 0x0ec5,
+ 0x0ec7,
+ 0x0ecf,
+ 0x0f48,
+ 0x0f98,
+ 0x0fbd,
+ 0x0fcd,
+ 0x10c6,
+ 0x1249,
+ 0x1257,
+ 0x1259,
+ 0x1289,
+ 0x12b1,
+ 0x12bf,
+ 0x12c1,
+ 0x12d7,
+ 0x1311,
+ 0x1680,
+ 0x176d,
+ 0x1771,
+ 0x180e,
+ 0x191f,
+ 0x1a5f,
+ 0x1b7f,
+ 0x1f58,
+ 0x1f5a,
+ 0x1f5c,
+ 0x1f5e,
+ 0x1fb5,
+ 0x1fc5,
+ 0x1fdc,
+ 0x1ff5,
+ 0x208f,
+ 0x2b96,
+ 0x2d26,
+ 0x2da7,
+ 0x2daf,
+ 0x2db7,
+ 0x2dbf,
+ 0x2dc7,
+ 0x2dcf,
+ 0x2dd7,
+ 0x2ddf,
+ 0x2e9a,
+ 0x3040,
+ 0x3130,
+ 0x318f,
+ 0x321f,
+ 0xa7d2,
+ 0xa7d4,
+ 0xa9ce,
+ 0xa9ff,
+ 0xab27,
+ 0xab2f,
+ 0xfb37,
+ 0xfb3d,
+ 0xfb3f,
+ 0xfb42,
+ 0xfb45,
+ 0xfe53,
+ 0xfe67,
+ 0xfe75,
+ 0xffe7,
+}
+
+var isPrint32 = []uint32{
+ 0x010000, 0x01004d,
+ 0x010050, 0x01005d,
+ 0x010080, 0x0100fa,
+ 0x010100, 0x010102,
+ 0x010107, 0x010133,
+ 0x010137, 0x01019c,
+ 0x0101a0, 0x0101a0,
+ 0x0101d0, 0x0101fd,
+ 0x010280, 0x01029c,
+ 0x0102a0, 0x0102d0,
+ 0x0102e0, 0x0102fb,
+ 0x010300, 0x010323,
+ 0x01032d, 0x01034a,
+ 0x010350, 0x01037a,
+ 0x010380, 0x0103c3,
+ 0x0103c8, 0x0103d5,
+ 0x010400, 0x01049d,
+ 0x0104a0, 0x0104a9,
+ 0x0104b0, 0x0104d3,
+ 0x0104d8, 0x0104fb,
+ 0x010500, 0x010527,
+ 0x010530, 0x010563,
+ 0x01056f, 0x0105bc,
+ 0x010600, 0x010736,
+ 0x010740, 0x010755,
+ 0x010760, 0x010767,
+ 0x010780, 0x0107ba,
+ 0x010800, 0x010805,
+ 0x010808, 0x010838,
+ 0x01083c, 0x01083c,
+ 0x01083f, 0x01089e,
+ 0x0108a7, 0x0108af,
+ 0x0108e0, 0x0108f5,
+ 0x0108fb, 0x01091b,
+ 0x01091f, 0x010939,
+ 0x01093f, 0x01093f,
+ 0x010980, 0x0109b7,
+ 0x0109bc, 0x0109cf,
+ 0x0109d2, 0x010a06,
+ 0x010a0c, 0x010a35,
+ 0x010a38, 0x010a3a,
+ 0x010a3f, 0x010a48,
+ 0x010a50, 0x010a58,
+ 0x010a60, 0x010a9f,
+ 0x010ac0, 0x010ae6,
+ 0x010aeb, 0x010af6,
+ 0x010b00, 0x010b35,
+ 0x010b39, 0x010b55,
+ 0x010b58, 0x010b72,
+ 0x010b78, 0x010b91,
+ 0x010b99, 0x010b9c,
+ 0x010ba9, 0x010baf,
+ 0x010c00, 0x010c48,
+ 0x010c80, 0x010cb2,
+ 0x010cc0, 0x010cf2,
+ 0x010cfa, 0x010d27,
+ 0x010d30, 0x010d39,
+ 0x010e60, 0x010ead,
+ 0x010eb0, 0x010eb1,
+ 0x010efd, 0x010f27,
+ 0x010f30, 0x010f59,
+ 0x010f70, 0x010f89,
+ 0x010fb0, 0x010fcb,
+ 0x010fe0, 0x010ff6,
+ 0x011000, 0x01104d,
+ 0x011052, 0x011075,
+ 0x01107f, 0x0110c2,
+ 0x0110d0, 0x0110e8,
+ 0x0110f0, 0x0110f9,
+ 0x011100, 0x011147,
+ 0x011150, 0x011176,
+ 0x011180, 0x0111f4,
+ 0x011200, 0x011241,
+ 0x011280, 0x0112a9,
+ 0x0112b0, 0x0112ea,
+ 0x0112f0, 0x0112f9,
+ 0x011300, 0x01130c,
+ 0x01130f, 0x011310,
+ 0x011313, 0x011344,
+ 0x011347, 0x011348,
+ 0x01134b, 0x01134d,
+ 0x011350, 0x011350,
+ 0x011357, 0x011357,
+ 0x01135d, 0x011363,
+ 0x011366, 0x01136c,
+ 0x011370, 0x011374,
+ 0x011400, 0x011461,
+ 0x011480, 0x0114c7,
+ 0x0114d0, 0x0114d9,
+ 0x011580, 0x0115b5,
+ 0x0115b8, 0x0115dd,
+ 0x011600, 0x011644,
+ 0x011650, 0x011659,
+ 0x011660, 0x01166c,
+ 0x011680, 0x0116b9,
+ 0x0116c0, 0x0116c9,
+ 0x011700, 0x01171a,
+ 0x01171d, 0x01172b,
+ 0x011730, 0x011746,
+ 0x011800, 0x01183b,
+ 0x0118a0, 0x0118f2,
+ 0x0118ff, 0x011906,
+ 0x011909, 0x011909,
+ 0x01190c, 0x011938,
+ 0x01193b, 0x011946,
+ 0x011950, 0x011959,
+ 0x0119a0, 0x0119a7,
+ 0x0119aa, 0x0119d7,
+ 0x0119da, 0x0119e4,
+ 0x011a00, 0x011a47,
+ 0x011a50, 0x011aa2,
+ 0x011ab0, 0x011af8,
+ 0x011b00, 0x011b09,
+ 0x011c00, 0x011c45,
+ 0x011c50, 0x011c6c,
+ 0x011c70, 0x011c8f,
+ 0x011c92, 0x011cb6,
+ 0x011d00, 0x011d36,
+ 0x011d3a, 0x011d47,
+ 0x011d50, 0x011d59,
+ 0x011d60, 0x011d98,
+ 0x011da0, 0x011da9,
+ 0x011ee0, 0x011ef8,
+ 0x011f00, 0x011f3a,
+ 0x011f3e, 0x011f59,
+ 0x011fb0, 0x011fb0,
+ 0x011fc0, 0x011ff1,
+ 0x011fff, 0x012399,
+ 0x012400, 0x012474,
+ 0x012480, 0x012543,
+ 0x012f90, 0x012ff2,
+ 0x013000, 0x01342f,
+ 0x013440, 0x013455,
+ 0x014400, 0x014646,
+ 0x016800, 0x016a38,
+ 0x016a40, 0x016a69,
+ 0x016a6e, 0x016ac9,
+ 0x016ad0, 0x016aed,
+ 0x016af0, 0x016af5,
+ 0x016b00, 0x016b45,
+ 0x016b50, 0x016b77,
+ 0x016b7d, 0x016b8f,
+ 0x016e40, 0x016e9a,
+ 0x016f00, 0x016f4a,
+ 0x016f4f, 0x016f87,
+ 0x016f8f, 0x016f9f,
+ 0x016fe0, 0x016fe4,
+ 0x016ff0, 0x016ff1,
+ 0x017000, 0x0187f7,
+ 0x018800, 0x018cd5,
+ 0x018d00, 0x018d08,
+ 0x01aff0, 0x01b122,
+ 0x01b132, 0x01b132,
+ 0x01b150, 0x01b152,
+ 0x01b155, 0x01b155,
+ 0x01b164, 0x01b167,
+ 0x01b170, 0x01b2fb,
+ 0x01bc00, 0x01bc6a,
+ 0x01bc70, 0x01bc7c,
+ 0x01bc80, 0x01bc88,
+ 0x01bc90, 0x01bc99,
+ 0x01bc9c, 0x01bc9f,
+ 0x01cf00, 0x01cf2d,
+ 0x01cf30, 0x01cf46,
+ 0x01cf50, 0x01cfc3,
+ 0x01d000, 0x01d0f5,
+ 0x01d100, 0x01d126,
+ 0x01d129, 0x01d172,
+ 0x01d17b, 0x01d1ea,
+ 0x01d200, 0x01d245,
+ 0x01d2c0, 0x01d2d3,
+ 0x01d2e0, 0x01d2f3,
+ 0x01d300, 0x01d356,
+ 0x01d360, 0x01d378,
+ 0x01d400, 0x01d49f,
+ 0x01d4a2, 0x01d4a2,
+ 0x01d4a5, 0x01d4a6,
+ 0x01d4a9, 0x01d50a,
+ 0x01d50d, 0x01d546,
+ 0x01d54a, 0x01d6a5,
+ 0x01d6a8, 0x01d7cb,
+ 0x01d7ce, 0x01da8b,
+ 0x01da9b, 0x01daaf,
+ 0x01df00, 0x01df1e,
+ 0x01df25, 0x01df2a,
+ 0x01e000, 0x01e018,
+ 0x01e01b, 0x01e02a,
+ 0x01e030, 0x01e06d,
+ 0x01e08f, 0x01e08f,
+ 0x01e100, 0x01e12c,
+ 0x01e130, 0x01e13d,
+ 0x01e140, 0x01e149,
+ 0x01e14e, 0x01e14f,
+ 0x01e290, 0x01e2ae,
+ 0x01e2c0, 0x01e2f9,
+ 0x01e2ff, 0x01e2ff,
+ 0x01e4d0, 0x01e4f9,
+ 0x01e7e0, 0x01e8c4,
+ 0x01e8c7, 0x01e8d6,
+ 0x01e900, 0x01e94b,
+ 0x01e950, 0x01e959,
+ 0x01e95e, 0x01e95f,
+ 0x01ec71, 0x01ecb4,
+ 0x01ed01, 0x01ed3d,
+ 0x01ee00, 0x01ee24,
+ 0x01ee27, 0x01ee3b,
+ 0x01ee42, 0x01ee42,
+ 0x01ee47, 0x01ee54,
+ 0x01ee57, 0x01ee64,
+ 0x01ee67, 0x01ee9b,
+ 0x01eea1, 0x01eebb,
+ 0x01eef0, 0x01eef1,
+ 0x01f000, 0x01f02b,
+ 0x01f030, 0x01f093,
+ 0x01f0a0, 0x01f0ae,
+ 0x01f0b1, 0x01f0f5,
+ 0x01f100, 0x01f1ad,
+ 0x01f1e6, 0x01f202,
+ 0x01f210, 0x01f23b,
+ 0x01f240, 0x01f248,
+ 0x01f250, 0x01f251,
+ 0x01f260, 0x01f265,
+ 0x01f300, 0x01f6d7,
+ 0x01f6dc, 0x01f6ec,
+ 0x01f6f0, 0x01f6fc,
+ 0x01f700, 0x01f776,
+ 0x01f77b, 0x01f7d9,
+ 0x01f7e0, 0x01f7eb,
+ 0x01f7f0, 0x01f7f0,
+ 0x01f800, 0x01f80b,
+ 0x01f810, 0x01f847,
+ 0x01f850, 0x01f859,
+ 0x01f860, 0x01f887,
+ 0x01f890, 0x01f8ad,
+ 0x01f8b0, 0x01f8b1,
+ 0x01f900, 0x01fa53,
+ 0x01fa60, 0x01fa6d,
+ 0x01fa70, 0x01fa7c,
+ 0x01fa80, 0x01fa88,
+ 0x01fa90, 0x01fac5,
+ 0x01face, 0x01fadb,
+ 0x01fae0, 0x01fae8,
+ 0x01faf0, 0x01faf8,
+ 0x01fb00, 0x01fbca,
+ 0x01fbf0, 0x01fbf9,
+ 0x020000, 0x02a6df,
+ 0x02a700, 0x02b739,
+ 0x02b740, 0x02b81d,
+ 0x02b820, 0x02cea1,
+ 0x02ceb0, 0x02ebe0,
+ 0x02f800, 0x02fa1d,
+ 0x030000, 0x03134a,
+ 0x031350, 0x0323af,
+ 0x0e0100, 0x0e01ef,
+}
+
+var isNotPrint32 = []uint16{ // add 0x10000 to each entry
+ 0x000c,
+ 0x0027,
+ 0x003b,
+ 0x003e,
+ 0x018f,
+ 0x039e,
+ 0x057b,
+ 0x058b,
+ 0x0593,
+ 0x0596,
+ 0x05a2,
+ 0x05b2,
+ 0x05ba,
+ 0x0786,
+ 0x07b1,
+ 0x0809,
+ 0x0836,
+ 0x0856,
+ 0x08f3,
+ 0x0a04,
+ 0x0a14,
+ 0x0a18,
+ 0x0e7f,
+ 0x0eaa,
+ 0x10bd,
+ 0x1135,
+ 0x11e0,
+ 0x1212,
+ 0x1287,
+ 0x1289,
+ 0x128e,
+ 0x129e,
+ 0x1304,
+ 0x1329,
+ 0x1331,
+ 0x1334,
+ 0x133a,
+ 0x145c,
+ 0x1914,
+ 0x1917,
+ 0x1936,
+ 0x1c09,
+ 0x1c37,
+ 0x1ca8,
+ 0x1d07,
+ 0x1d0a,
+ 0x1d3b,
+ 0x1d3e,
+ 0x1d66,
+ 0x1d69,
+ 0x1d8f,
+ 0x1d92,
+ 0x1f11,
+ 0x246f,
+ 0x6a5f,
+ 0x6abf,
+ 0x6b5a,
+ 0x6b62,
+ 0xaff4,
+ 0xaffc,
+ 0xafff,
+ 0xd455,
+ 0xd49d,
+ 0xd4ad,
+ 0xd4ba,
+ 0xd4bc,
+ 0xd4c4,
+ 0xd506,
+ 0xd515,
+ 0xd51d,
+ 0xd53a,
+ 0xd53f,
+ 0xd545,
+ 0xd551,
+ 0xdaa0,
+ 0xe007,
+ 0xe022,
+ 0xe025,
+ 0xe7e7,
+ 0xe7ec,
+ 0xe7ef,
+ 0xe7ff,
+ 0xee04,
+ 0xee20,
+ 0xee23,
+ 0xee28,
+ 0xee33,
+ 0xee38,
+ 0xee3a,
+ 0xee48,
+ 0xee4a,
+ 0xee4c,
+ 0xee50,
+ 0xee53,
+ 0xee58,
+ 0xee5a,
+ 0xee5c,
+ 0xee5e,
+ 0xee60,
+ 0xee63,
+ 0xee6b,
+ 0xee73,
+ 0xee78,
+ 0xee7d,
+ 0xee7f,
+ 0xee8a,
+ 0xeea4,
+ 0xeeaa,
+ 0xf0c0,
+ 0xf0d0,
+ 0xfabe,
+ 0xfb93,
+}
+
+// isGraphic lists the graphic runes not matched by IsPrint.
+var isGraphic = []uint16{
+ 0x00a0,
+ 0x1680,
+ 0x2000,
+ 0x2001,
+ 0x2002,
+ 0x2003,
+ 0x2004,
+ 0x2005,
+ 0x2006,
+ 0x2007,
+ 0x2008,
+ 0x2009,
+ 0x200a,
+ 0x202f,
+ 0x205f,
+ 0x3000,
+}
diff --git a/src/strconv/itoa.go b/src/strconv/itoa.go
new file mode 100644
index 0000000..b0c2666
--- /dev/null
+++ b/src/strconv/itoa.go
@@ -0,0 +1,205 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv
+
+import "math/bits"
+
+const fastSmalls = true // enable fast path for small integers
+
+// FormatUint returns the string representation of i in the given base,
+// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
+// for digit values >= 10.
+func FormatUint(i uint64, base int) string {
+ if fastSmalls && i < nSmalls && base == 10 {
+ return small(int(i))
+ }
+ _, s := formatBits(nil, i, base, false, false)
+ return s
+}
+
+// FormatInt returns the string representation of i in the given base,
+// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
+// for digit values >= 10.
+func FormatInt(i int64, base int) string {
+ if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
+ return small(int(i))
+ }
+ _, s := formatBits(nil, uint64(i), base, i < 0, false)
+ return s
+}
+
+// Itoa is equivalent to FormatInt(int64(i), 10).
+func Itoa(i int) string {
+ return FormatInt(int64(i), 10)
+}
+
+// AppendInt appends the string form of the integer i,
+// as generated by FormatInt, to dst and returns the extended buffer.
+func AppendInt(dst []byte, i int64, base int) []byte {
+ if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
+ return append(dst, small(int(i))...)
+ }
+ dst, _ = formatBits(dst, uint64(i), base, i < 0, true)
+ return dst
+}
+
+// AppendUint appends the string form of the unsigned integer i,
+// as generated by FormatUint, to dst and returns the extended buffer.
+func AppendUint(dst []byte, i uint64, base int) []byte {
+ if fastSmalls && i < nSmalls && base == 10 {
+ return append(dst, small(int(i))...)
+ }
+ dst, _ = formatBits(dst, i, base, false, true)
+ return dst
+}
+
+// small returns the string for an i with 0 <= i < nSmalls.
+func small(i int) string {
+ if i < 10 {
+ return digits[i : i+1]
+ }
+ return smallsString[i*2 : i*2+2]
+}
+
+const nSmalls = 100
+
+const smallsString = "00010203040506070809" +
+ "10111213141516171819" +
+ "20212223242526272829" +
+ "30313233343536373839" +
+ "40414243444546474849" +
+ "50515253545556575859" +
+ "60616263646566676869" +
+ "70717273747576777879" +
+ "80818283848586878889" +
+ "90919293949596979899"
+
+const host32bit = ^uint(0)>>32 == 0
+
+const digits = "0123456789abcdefghijklmnopqrstuvwxyz"
+
+// formatBits computes the string representation of u in the given base.
+// If neg is set, u is treated as negative int64 value. If append_ is
+// set, the string is appended to dst and the resulting byte slice is
+// returned as the first result value; otherwise the string is returned
+// as the second result value.
+func formatBits(dst []byte, u uint64, base int, neg, append_ bool) (d []byte, s string) {
+ if base < 2 || base > len(digits) {
+ panic("strconv: illegal AppendInt/FormatInt base")
+ }
+ // 2 <= base && base <= len(digits)
+
+ var a [64 + 1]byte // +1 for sign of 64bit value in base 2
+ i := len(a)
+
+ if neg {
+ u = -u
+ }
+
+ // convert bits
+ // We use uint values where we can because those will
+ // fit into a single register even on a 32bit machine.
+ if base == 10 {
+ // common case: use constants for / because
+ // the compiler can optimize it into a multiply+shift
+
+ if host32bit {
+ // convert the lower digits using 32bit operations
+ for u >= 1e9 {
+ // Avoid using r = a%b in addition to q = a/b
+ // since 64bit division and modulo operations
+ // are calculated by runtime functions on 32bit machines.
+ q := u / 1e9
+ us := uint(u - q*1e9) // u % 1e9 fits into a uint
+ for j := 4; j > 0; j-- {
+ is := us % 100 * 2
+ us /= 100
+ i -= 2
+ a[i+1] = smallsString[is+1]
+ a[i+0] = smallsString[is+0]
+ }
+
+ // us < 10, since it contains the last digit
+ // from the initial 9-digit us.
+ i--
+ a[i] = smallsString[us*2+1]
+
+ u = q
+ }
+ // u < 1e9
+ }
+
+ // u guaranteed to fit into a uint
+ us := uint(u)
+ for us >= 100 {
+ is := us % 100 * 2
+ us /= 100
+ i -= 2
+ a[i+1] = smallsString[is+1]
+ a[i+0] = smallsString[is+0]
+ }
+
+ // us < 100
+ is := us * 2
+ i--
+ a[i] = smallsString[is+1]
+ if us >= 10 {
+ i--
+ a[i] = smallsString[is]
+ }
+
+ } else if isPowerOfTwo(base) {
+ // Use shifts and masks instead of / and %.
+ // Base is a power of 2 and 2 <= base <= len(digits) where len(digits) is 36.
+ // The largest power of 2 below or equal to 36 is 32, which is 1 << 5;
+ // i.e., the largest possible shift count is 5. By &-ind that value with
+ // the constant 7 we tell the compiler that the shift count is always
+ // less than 8 which is smaller than any register width. This allows
+ // the compiler to generate better code for the shift operation.
+ shift := uint(bits.TrailingZeros(uint(base))) & 7
+ b := uint64(base)
+ m := uint(base) - 1 // == 1<<shift - 1
+ for u >= b {
+ i--
+ a[i] = digits[uint(u)&m]
+ u >>= shift
+ }
+ // u < base
+ i--
+ a[i] = digits[uint(u)]
+ } else {
+ // general case
+ b := uint64(base)
+ for u >= b {
+ i--
+ // Avoid using r = a%b in addition to q = a/b
+ // since 64bit division and modulo operations
+ // are calculated by runtime functions on 32bit machines.
+ q := u / b
+ a[i] = digits[uint(u-q*b)]
+ u = q
+ }
+ // u < base
+ i--
+ a[i] = digits[uint(u)]
+ }
+
+ // add sign, if any
+ if neg {
+ i--
+ a[i] = '-'
+ }
+
+ if append_ {
+ d = append(dst, a[i:]...)
+ return
+ }
+ s = string(a[i:])
+ return
+}
+
+func isPowerOfTwo(x int) bool {
+ return x&(x-1) == 0
+}
diff --git a/src/strconv/itoa_test.go b/src/strconv/itoa_test.go
new file mode 100644
index 0000000..b8bc524
--- /dev/null
+++ b/src/strconv/itoa_test.go
@@ -0,0 +1,243 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ . "strconv"
+ "testing"
+)
+
+type itob64Test struct {
+ in int64
+ base int
+ out string
+}
+
+var itob64tests = []itob64Test{
+ {0, 10, "0"},
+ {1, 10, "1"},
+ {-1, 10, "-1"},
+ {12345678, 10, "12345678"},
+ {-987654321, 10, "-987654321"},
+ {1<<31 - 1, 10, "2147483647"},
+ {-1<<31 + 1, 10, "-2147483647"},
+ {1 << 31, 10, "2147483648"},
+ {-1 << 31, 10, "-2147483648"},
+ {1<<31 + 1, 10, "2147483649"},
+ {-1<<31 - 1, 10, "-2147483649"},
+ {1<<32 - 1, 10, "4294967295"},
+ {-1<<32 + 1, 10, "-4294967295"},
+ {1 << 32, 10, "4294967296"},
+ {-1 << 32, 10, "-4294967296"},
+ {1<<32 + 1, 10, "4294967297"},
+ {-1<<32 - 1, 10, "-4294967297"},
+ {1 << 50, 10, "1125899906842624"},
+ {1<<63 - 1, 10, "9223372036854775807"},
+ {-1<<63 + 1, 10, "-9223372036854775807"},
+ {-1 << 63, 10, "-9223372036854775808"},
+
+ {0, 2, "0"},
+ {10, 2, "1010"},
+ {-1, 2, "-1"},
+ {1 << 15, 2, "1000000000000000"},
+
+ {-8, 8, "-10"},
+ {057635436545, 8, "57635436545"},
+ {1 << 24, 8, "100000000"},
+
+ {16, 16, "10"},
+ {-0x123456789abcdef, 16, "-123456789abcdef"},
+ {1<<63 - 1, 16, "7fffffffffffffff"},
+ {1<<63 - 1, 2, "111111111111111111111111111111111111111111111111111111111111111"},
+ {-1 << 63, 2, "-1000000000000000000000000000000000000000000000000000000000000000"},
+
+ {16, 17, "g"},
+ {25, 25, "10"},
+ {(((((17*35+24)*35+21)*35+34)*35+12)*35+24)*35 + 32, 35, "holycow"},
+ {(((((17*36+24)*36+21)*36+34)*36+12)*36+24)*36 + 32, 36, "holycow"},
+}
+
+func TestItoa(t *testing.T) {
+ for _, test := range itob64tests {
+ s := FormatInt(test.in, test.base)
+ if s != test.out {
+ t.Errorf("FormatInt(%v, %v) = %v want %v",
+ test.in, test.base, s, test.out)
+ }
+ x := AppendInt([]byte("abc"), test.in, test.base)
+ if string(x) != "abc"+test.out {
+ t.Errorf("AppendInt(%q, %v, %v) = %q want %v",
+ "abc", test.in, test.base, x, test.out)
+ }
+
+ if test.in >= 0 {
+ s := FormatUint(uint64(test.in), test.base)
+ if s != test.out {
+ t.Errorf("FormatUint(%v, %v) = %v want %v",
+ test.in, test.base, s, test.out)
+ }
+ x := AppendUint(nil, uint64(test.in), test.base)
+ if string(x) != test.out {
+ t.Errorf("AppendUint(%q, %v, %v) = %q want %v",
+ "abc", uint64(test.in), test.base, x, test.out)
+ }
+ }
+
+ if test.base == 10 && int64(int(test.in)) == test.in {
+ s := Itoa(int(test.in))
+ if s != test.out {
+ t.Errorf("Itoa(%v) = %v want %v",
+ test.in, s, test.out)
+ }
+ }
+ }
+
+ // Override when base is illegal
+ defer func() {
+ if r := recover(); r == nil {
+ t.Fatalf("expected panic due to illegal base")
+ }
+ }()
+ FormatUint(12345678, 1)
+}
+
+type uitob64Test struct {
+ in uint64
+ base int
+ out string
+}
+
+var uitob64tests = []uitob64Test{
+ {1<<63 - 1, 10, "9223372036854775807"},
+ {1 << 63, 10, "9223372036854775808"},
+ {1<<63 + 1, 10, "9223372036854775809"},
+ {1<<64 - 2, 10, "18446744073709551614"},
+ {1<<64 - 1, 10, "18446744073709551615"},
+ {1<<64 - 1, 2, "1111111111111111111111111111111111111111111111111111111111111111"},
+}
+
+func TestUitoa(t *testing.T) {
+ for _, test := range uitob64tests {
+ s := FormatUint(test.in, test.base)
+ if s != test.out {
+ t.Errorf("FormatUint(%v, %v) = %v want %v",
+ test.in, test.base, s, test.out)
+ }
+ x := AppendUint([]byte("abc"), test.in, test.base)
+ if string(x) != "abc"+test.out {
+ t.Errorf("AppendUint(%q, %v, %v) = %q want %v",
+ "abc", test.in, test.base, x, test.out)
+ }
+
+ }
+}
+
+var varlenUints = []struct {
+ in uint64
+ out string
+}{
+ {1, "1"},
+ {12, "12"},
+ {123, "123"},
+ {1234, "1234"},
+ {12345, "12345"},
+ {123456, "123456"},
+ {1234567, "1234567"},
+ {12345678, "12345678"},
+ {123456789, "123456789"},
+ {1234567890, "1234567890"},
+ {12345678901, "12345678901"},
+ {123456789012, "123456789012"},
+ {1234567890123, "1234567890123"},
+ {12345678901234, "12345678901234"},
+ {123456789012345, "123456789012345"},
+ {1234567890123456, "1234567890123456"},
+ {12345678901234567, "12345678901234567"},
+ {123456789012345678, "123456789012345678"},
+ {1234567890123456789, "1234567890123456789"},
+ {12345678901234567890, "12345678901234567890"},
+}
+
+func TestFormatUintVarlen(t *testing.T) {
+ for _, test := range varlenUints {
+ s := FormatUint(test.in, 10)
+ if s != test.out {
+ t.Errorf("FormatUint(%v, 10) = %v want %v", test.in, s, test.out)
+ }
+ }
+}
+
+func BenchmarkFormatInt(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ for _, test := range itob64tests {
+ s := FormatInt(test.in, test.base)
+ BenchSink += len(s)
+ }
+ }
+}
+
+func BenchmarkAppendInt(b *testing.B) {
+ dst := make([]byte, 0, 30)
+ for i := 0; i < b.N; i++ {
+ for _, test := range itob64tests {
+ dst = AppendInt(dst[:0], test.in, test.base)
+ BenchSink += len(dst)
+ }
+ }
+}
+
+func BenchmarkFormatUint(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ for _, test := range uitob64tests {
+ s := FormatUint(test.in, test.base)
+ BenchSink += len(s)
+ }
+ }
+}
+
+func BenchmarkAppendUint(b *testing.B) {
+ dst := make([]byte, 0, 30)
+ for i := 0; i < b.N; i++ {
+ for _, test := range uitob64tests {
+ dst = AppendUint(dst[:0], test.in, test.base)
+ BenchSink += len(dst)
+ }
+ }
+}
+
+func BenchmarkFormatIntSmall(b *testing.B) {
+ smallInts := []int64{7, 42}
+ for _, smallInt := range smallInts {
+ b.Run(Itoa(int(smallInt)), func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ s := FormatInt(smallInt, 10)
+ BenchSink += len(s)
+ }
+ })
+ }
+}
+
+func BenchmarkAppendIntSmall(b *testing.B) {
+ dst := make([]byte, 0, 30)
+ const smallInt = 42
+ for i := 0; i < b.N; i++ {
+ dst = AppendInt(dst[:0], smallInt, 10)
+ BenchSink += len(dst)
+ }
+}
+
+func BenchmarkAppendUintVarlen(b *testing.B) {
+ for _, test := range varlenUints {
+ b.Run(test.out, func(b *testing.B) {
+ dst := make([]byte, 0, 30)
+ for j := 0; j < b.N; j++ {
+ dst = AppendUint(dst[:0], test.in, 10)
+ BenchSink += len(dst)
+ }
+ })
+ }
+}
+
+var BenchSink int // make sure compiler cannot optimize away benchmarks
diff --git a/src/strconv/makeisprint.go b/src/strconv/makeisprint.go
new file mode 100644
index 0000000..ff361e7
--- /dev/null
+++ b/src/strconv/makeisprint.go
@@ -0,0 +1,203 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build ignore
+
+//
+// usage:
+//
+// go run makeisprint.go -output isprint.go
+//
+
+package main
+
+import (
+ "bytes"
+ "flag"
+ "fmt"
+ "go/format"
+ "log"
+ "os"
+ "unicode"
+)
+
+var filename = flag.String("output", "isprint.go", "output file name")
+
+var (
+ range16 []uint16
+ except16 []uint16
+ range32 []uint32
+ except32 []uint32
+)
+
+// bsearch16 returns the smallest i such that a[i] >= x.
+// If there is no such i, bsearch16 returns len(a).
+func bsearch16(a []uint16, x uint16) int {
+ i, j := 0, len(a)
+ for i < j {
+ h := i + (j-i)>>1
+ if a[h] < x {
+ i = h + 1
+ } else {
+ j = h
+ }
+ }
+ return i
+}
+
+// bsearch32 returns the smallest i such that a[i] >= x.
+// If there is no such i, bsearch32 returns len(a).
+func bsearch32(a []uint32, x uint32) int {
+ i, j := 0, len(a)
+ for i < j {
+ h := i + (j-i)>>1
+ if a[h] < x {
+ i = h + 1
+ } else {
+ j = h
+ }
+ }
+ return i
+}
+
+func isPrint(r rune) bool {
+ // Same algorithm, either on uint16 or uint32 value.
+ // First, find first i such that rang[i] >= x.
+ // This is the index of either the start or end of a pair that might span x.
+ // The start is even (rang[i&^1]) and the end is odd (rang[i|1]).
+ // If we find x in a range, make sure x is not in exception list.
+
+ if 0 <= r && r < 1<<16 {
+ rr, rang, except := uint16(r), range16, except16
+ i := bsearch16(rang, rr)
+ if i >= len(rang) || rr < rang[i&^1] || rang[i|1] < rr {
+ return false
+ }
+ j := bsearch16(except, rr)
+ return j >= len(except) || except[j] != rr
+ }
+
+ rr, rang, except := uint32(r), range32, except32
+ i := bsearch32(rang, rr)
+ if i >= len(rang) || rr < rang[i&^1] || rang[i|1] < rr {
+ return false
+ }
+ j := bsearch32(except, rr)
+ return j >= len(except) || except[j] != rr
+}
+
+func scan(min, max rune) (rang, except []uint32) {
+ lo := rune(-1)
+ for i := min; ; i++ {
+ if (i > max || !unicode.IsPrint(i)) && lo >= 0 {
+ // End range, but avoid flip flop.
+ if i+1 <= max && unicode.IsPrint(i+1) {
+ except = append(except, uint32(i))
+ continue
+ }
+ rang = append(rang, uint32(lo), uint32(i-1))
+ lo = -1
+ }
+ if i > max {
+ break
+ }
+ if lo < 0 && unicode.IsPrint(i) {
+ lo = i
+ }
+ }
+ return
+}
+
+func to16(x []uint32) []uint16 {
+ var y []uint16
+ for _, v := range x {
+ if uint32(uint16(v)) != v {
+ panic("bad 32->16 conversion")
+ }
+ y = append(y, uint16(v))
+ }
+ return y
+}
+
+func main() {
+ flag.Parse()
+
+ rang, except := scan(0, 0xFFFF)
+ range16 = to16(rang)
+ except16 = to16(except)
+ range32, except32 = scan(0x10000, unicode.MaxRune)
+
+ for i := rune(0); i <= unicode.MaxRune; i++ {
+ if isPrint(i) != unicode.IsPrint(i) {
+ log.Fatalf("%U: isPrint=%v, want %v\n", i, isPrint(i), unicode.IsPrint(i))
+ }
+ }
+
+ var buf bytes.Buffer
+
+ fmt.Fprintf(&buf, `// Copyright 2013 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.`+"\n\n")
+ fmt.Fprintf(&buf, "// Code generated by go run makeisprint.go -output isprint.go; DO NOT EDIT.\n\n")
+ fmt.Fprintf(&buf, "package strconv\n\n")
+
+ fmt.Fprintf(&buf, "// (%d+%d+%d)*2 + (%d)*4 = %d bytes\n\n",
+ len(range16), len(except16), len(except32),
+ len(range32),
+ (len(range16)+len(except16)+len(except32))*2+
+ (len(range32))*4)
+
+ fmt.Fprintf(&buf, "var isPrint16 = []uint16{\n")
+ for i := 0; i < len(range16); i += 2 {
+ fmt.Fprintf(&buf, "\t%#04x, %#04x,\n", range16[i], range16[i+1])
+ }
+ fmt.Fprintf(&buf, "}\n\n")
+
+ fmt.Fprintf(&buf, "var isNotPrint16 = []uint16{\n")
+ for _, r := range except16 {
+ fmt.Fprintf(&buf, "\t%#04x,\n", r)
+ }
+ fmt.Fprintf(&buf, "}\n\n")
+
+ fmt.Fprintf(&buf, "var isPrint32 = []uint32{\n")
+ for i := 0; i < len(range32); i += 2 {
+ fmt.Fprintf(&buf, "\t%#06x, %#06x,\n", range32[i], range32[i+1])
+ }
+ fmt.Fprintf(&buf, "}\n\n")
+
+ fmt.Fprintf(&buf, "var isNotPrint32 = []uint16{ // add 0x10000 to each entry\n")
+ for _, r := range except32 {
+ if r >= 0x20000 {
+ log.Fatalf("%U too big for isNotPrint32\n", r)
+ }
+ fmt.Fprintf(&buf, "\t%#04x,\n", r-0x10000)
+ }
+ fmt.Fprintf(&buf, "}\n\n")
+
+ // The list of graphic but not "printable" runes is short. Just make one easy table.
+ fmt.Fprintf(&buf, "// isGraphic lists the graphic runes not matched by IsPrint.\n")
+ fmt.Fprintf(&buf, "var isGraphic = []uint16{\n")
+ for r := rune(0); r <= unicode.MaxRune; r++ {
+ if unicode.IsPrint(r) != unicode.IsGraphic(r) {
+ // Sanity check.
+ if !unicode.IsGraphic(r) {
+ log.Fatalf("%U is printable but not graphic\n", r)
+ }
+ if r > 0xFFFF { // We expect only 16-bit values.
+ log.Fatalf("%U too big for isGraphic\n", r)
+ }
+ fmt.Fprintf(&buf, "\t%#04x,\n", r)
+ }
+ }
+ fmt.Fprintf(&buf, "}\n")
+
+ data, err := format.Source(buf.Bytes())
+ if err != nil {
+ log.Fatal(err)
+ }
+ err = os.WriteFile(*filename, data, 0644)
+ if err != nil {
+ log.Fatal(err)
+ }
+}
diff --git a/src/strconv/quote.go b/src/strconv/quote.go
new file mode 100644
index 0000000..7c38433
--- /dev/null
+++ b/src/strconv/quote.go
@@ -0,0 +1,599 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:generate go run makeisprint.go -output isprint.go
+
+package strconv
+
+import (
+ "unicode/utf8"
+)
+
+const (
+ lowerhex = "0123456789abcdef"
+ upperhex = "0123456789ABCDEF"
+)
+
+// contains reports whether the string contains the byte c.
+func contains(s string, c byte) bool {
+ return index(s, c) != -1
+}
+
+func quoteWith(s string, quote byte, ASCIIonly, graphicOnly bool) string {
+ return string(appendQuotedWith(make([]byte, 0, 3*len(s)/2), s, quote, ASCIIonly, graphicOnly))
+}
+
+func quoteRuneWith(r rune, quote byte, ASCIIonly, graphicOnly bool) string {
+ return string(appendQuotedRuneWith(nil, r, quote, ASCIIonly, graphicOnly))
+}
+
+func appendQuotedWith(buf []byte, s string, quote byte, ASCIIonly, graphicOnly bool) []byte {
+ // Often called with big strings, so preallocate. If there's quoting,
+ // this is conservative but still helps a lot.
+ if cap(buf)-len(buf) < len(s) {
+ nBuf := make([]byte, len(buf), len(buf)+1+len(s)+1)
+ copy(nBuf, buf)
+ buf = nBuf
+ }
+ buf = append(buf, quote)
+ for width := 0; len(s) > 0; s = s[width:] {
+ r := rune(s[0])
+ width = 1
+ if r >= utf8.RuneSelf {
+ r, width = utf8.DecodeRuneInString(s)
+ }
+ if width == 1 && r == utf8.RuneError {
+ buf = append(buf, `\x`...)
+ buf = append(buf, lowerhex[s[0]>>4])
+ buf = append(buf, lowerhex[s[0]&0xF])
+ continue
+ }
+ buf = appendEscapedRune(buf, r, quote, ASCIIonly, graphicOnly)
+ }
+ buf = append(buf, quote)
+ return buf
+}
+
+func appendQuotedRuneWith(buf []byte, r rune, quote byte, ASCIIonly, graphicOnly bool) []byte {
+ buf = append(buf, quote)
+ if !utf8.ValidRune(r) {
+ r = utf8.RuneError
+ }
+ buf = appendEscapedRune(buf, r, quote, ASCIIonly, graphicOnly)
+ buf = append(buf, quote)
+ return buf
+}
+
+func appendEscapedRune(buf []byte, r rune, quote byte, ASCIIonly, graphicOnly bool) []byte {
+ if r == rune(quote) || r == '\\' { // always backslashed
+ buf = append(buf, '\\')
+ buf = append(buf, byte(r))
+ return buf
+ }
+ if ASCIIonly {
+ if r < utf8.RuneSelf && IsPrint(r) {
+ buf = append(buf, byte(r))
+ return buf
+ }
+ } else if IsPrint(r) || graphicOnly && isInGraphicList(r) {
+ return utf8.AppendRune(buf, r)
+ }
+ switch r {
+ case '\a':
+ buf = append(buf, `\a`...)
+ case '\b':
+ buf = append(buf, `\b`...)
+ case '\f':
+ buf = append(buf, `\f`...)
+ case '\n':
+ buf = append(buf, `\n`...)
+ case '\r':
+ buf = append(buf, `\r`...)
+ case '\t':
+ buf = append(buf, `\t`...)
+ case '\v':
+ buf = append(buf, `\v`...)
+ default:
+ switch {
+ case r < ' ' || r == 0x7f:
+ buf = append(buf, `\x`...)
+ buf = append(buf, lowerhex[byte(r)>>4])
+ buf = append(buf, lowerhex[byte(r)&0xF])
+ case !utf8.ValidRune(r):
+ r = 0xFFFD
+ fallthrough
+ case r < 0x10000:
+ buf = append(buf, `\u`...)
+ for s := 12; s >= 0; s -= 4 {
+ buf = append(buf, lowerhex[r>>uint(s)&0xF])
+ }
+ default:
+ buf = append(buf, `\U`...)
+ for s := 28; s >= 0; s -= 4 {
+ buf = append(buf, lowerhex[r>>uint(s)&0xF])
+ }
+ }
+ }
+ return buf
+}
+
+// Quote returns a double-quoted Go string literal representing s. The
+// returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
+// control characters and non-printable characters as defined by
+// IsPrint.
+func Quote(s string) string {
+ return quoteWith(s, '"', false, false)
+}
+
+// AppendQuote appends a double-quoted Go string literal representing s,
+// as generated by Quote, to dst and returns the extended buffer.
+func AppendQuote(dst []byte, s string) []byte {
+ return appendQuotedWith(dst, s, '"', false, false)
+}
+
+// QuoteToASCII returns a double-quoted Go string literal representing s.
+// The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
+// non-ASCII characters and non-printable characters as defined by IsPrint.
+func QuoteToASCII(s string) string {
+ return quoteWith(s, '"', true, false)
+}
+
+// AppendQuoteToASCII appends a double-quoted Go string literal representing s,
+// as generated by QuoteToASCII, to dst and returns the extended buffer.
+func AppendQuoteToASCII(dst []byte, s string) []byte {
+ return appendQuotedWith(dst, s, '"', true, false)
+}
+
+// QuoteToGraphic returns a double-quoted Go string literal representing s.
+// The returned string leaves Unicode graphic characters, as defined by
+// IsGraphic, unchanged and uses Go escape sequences (\t, \n, \xFF, \u0100)
+// for non-graphic characters.
+func QuoteToGraphic(s string) string {
+ return quoteWith(s, '"', false, true)
+}
+
+// AppendQuoteToGraphic appends a double-quoted Go string literal representing s,
+// as generated by QuoteToGraphic, to dst and returns the extended buffer.
+func AppendQuoteToGraphic(dst []byte, s string) []byte {
+ return appendQuotedWith(dst, s, '"', false, true)
+}
+
+// QuoteRune returns a single-quoted Go character literal representing the
+// rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100)
+// for control characters and non-printable characters as defined by IsPrint.
+// If r is not a valid Unicode code point, it is interpreted as the Unicode
+// replacement character U+FFFD.
+func QuoteRune(r rune) string {
+ return quoteRuneWith(r, '\'', false, false)
+}
+
+// AppendQuoteRune appends a single-quoted Go character literal representing the rune,
+// as generated by QuoteRune, to dst and returns the extended buffer.
+func AppendQuoteRune(dst []byte, r rune) []byte {
+ return appendQuotedRuneWith(dst, r, '\'', false, false)
+}
+
+// QuoteRuneToASCII returns a single-quoted Go character literal representing
+// the rune. The returned string uses Go escape sequences (\t, \n, \xFF,
+// \u0100) for non-ASCII characters and non-printable characters as defined
+// by IsPrint.
+// If r is not a valid Unicode code point, it is interpreted as the Unicode
+// replacement character U+FFFD.
+func QuoteRuneToASCII(r rune) string {
+ return quoteRuneWith(r, '\'', true, false)
+}
+
+// AppendQuoteRuneToASCII appends a single-quoted Go character literal representing the rune,
+// as generated by QuoteRuneToASCII, to dst and returns the extended buffer.
+func AppendQuoteRuneToASCII(dst []byte, r rune) []byte {
+ return appendQuotedRuneWith(dst, r, '\'', true, false)
+}
+
+// QuoteRuneToGraphic returns a single-quoted Go character literal representing
+// the rune. If the rune is not a Unicode graphic character,
+// as defined by IsGraphic, the returned string will use a Go escape sequence
+// (\t, \n, \xFF, \u0100).
+// If r is not a valid Unicode code point, it is interpreted as the Unicode
+// replacement character U+FFFD.
+func QuoteRuneToGraphic(r rune) string {
+ return quoteRuneWith(r, '\'', false, true)
+}
+
+// AppendQuoteRuneToGraphic appends a single-quoted Go character literal representing the rune,
+// as generated by QuoteRuneToGraphic, to dst and returns the extended buffer.
+func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte {
+ return appendQuotedRuneWith(dst, r, '\'', false, true)
+}
+
+// CanBackquote reports whether the string s can be represented
+// unchanged as a single-line backquoted string without control
+// characters other than tab.
+func CanBackquote(s string) bool {
+ for len(s) > 0 {
+ r, wid := utf8.DecodeRuneInString(s)
+ s = s[wid:]
+ if wid > 1 {
+ if r == '\ufeff' {
+ return false // BOMs are invisible and should not be quoted.
+ }
+ continue // All other multibyte runes are correctly encoded and assumed printable.
+ }
+ if r == utf8.RuneError {
+ return false
+ }
+ if (r < ' ' && r != '\t') || r == '`' || r == '\u007F' {
+ return false
+ }
+ }
+ return true
+}
+
+func unhex(b byte) (v rune, ok bool) {
+ c := rune(b)
+ switch {
+ case '0' <= c && c <= '9':
+ return c - '0', true
+ case 'a' <= c && c <= 'f':
+ return c - 'a' + 10, true
+ case 'A' <= c && c <= 'F':
+ return c - 'A' + 10, true
+ }
+ return
+}
+
+// UnquoteChar decodes the first character or byte in the escaped string
+// or character literal represented by the string s.
+// It returns four values:
+//
+// 1. value, the decoded Unicode code point or byte value;
+// 2. multibyte, a boolean indicating whether the decoded character requires a multibyte UTF-8 representation;
+// 3. tail, the remainder of the string after the character; and
+// 4. an error that will be nil if the character is syntactically valid.
+//
+// The second argument, quote, specifies the type of literal being parsed
+// and therefore which escaped quote character is permitted.
+// If set to a single quote, it permits the sequence \' and disallows unescaped '.
+// If set to a double quote, it permits \" and disallows unescaped ".
+// If set to zero, it does not permit either escape and allows both quote characters to appear unescaped.
+func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error) {
+ // easy cases
+ if len(s) == 0 {
+ err = ErrSyntax
+ return
+ }
+ switch c := s[0]; {
+ case c == quote && (quote == '\'' || quote == '"'):
+ err = ErrSyntax
+ return
+ case c >= utf8.RuneSelf:
+ r, size := utf8.DecodeRuneInString(s)
+ return r, true, s[size:], nil
+ case c != '\\':
+ return rune(s[0]), false, s[1:], nil
+ }
+
+ // hard case: c is backslash
+ if len(s) <= 1 {
+ err = ErrSyntax
+ return
+ }
+ c := s[1]
+ s = s[2:]
+
+ switch c {
+ case 'a':
+ value = '\a'
+ case 'b':
+ value = '\b'
+ case 'f':
+ value = '\f'
+ case 'n':
+ value = '\n'
+ case 'r':
+ value = '\r'
+ case 't':
+ value = '\t'
+ case 'v':
+ value = '\v'
+ case 'x', 'u', 'U':
+ n := 0
+ switch c {
+ case 'x':
+ n = 2
+ case 'u':
+ n = 4
+ case 'U':
+ n = 8
+ }
+ var v rune
+ if len(s) < n {
+ err = ErrSyntax
+ return
+ }
+ for j := 0; j < n; j++ {
+ x, ok := unhex(s[j])
+ if !ok {
+ err = ErrSyntax
+ return
+ }
+ v = v<<4 | x
+ }
+ s = s[n:]
+ if c == 'x' {
+ // single-byte string, possibly not UTF-8
+ value = v
+ break
+ }
+ if !utf8.ValidRune(v) {
+ err = ErrSyntax
+ return
+ }
+ value = v
+ multibyte = true
+ case '0', '1', '2', '3', '4', '5', '6', '7':
+ v := rune(c) - '0'
+ if len(s) < 2 {
+ err = ErrSyntax
+ return
+ }
+ for j := 0; j < 2; j++ { // one digit already; two more
+ x := rune(s[j]) - '0'
+ if x < 0 || x > 7 {
+ err = ErrSyntax
+ return
+ }
+ v = (v << 3) | x
+ }
+ s = s[2:]
+ if v > 255 {
+ err = ErrSyntax
+ return
+ }
+ value = v
+ case '\\':
+ value = '\\'
+ case '\'', '"':
+ if c != quote {
+ err = ErrSyntax
+ return
+ }
+ value = rune(c)
+ default:
+ err = ErrSyntax
+ return
+ }
+ tail = s
+ return
+}
+
+// QuotedPrefix returns the quoted string (as understood by Unquote) at the prefix of s.
+// If s does not start with a valid quoted string, QuotedPrefix returns an error.
+func QuotedPrefix(s string) (string, error) {
+ out, _, err := unquote(s, false)
+ return out, err
+}
+
+// Unquote interprets s as a single-quoted, double-quoted,
+// or backquoted Go string literal, returning the string value
+// that s quotes. (If s is single-quoted, it would be a Go
+// character literal; Unquote returns the corresponding
+// one-character string.)
+func Unquote(s string) (string, error) {
+ out, rem, err := unquote(s, true)
+ if len(rem) > 0 {
+ return "", ErrSyntax
+ }
+ return out, err
+}
+
+// unquote parses a quoted string at the start of the input,
+// returning the parsed prefix, the remaining suffix, and any parse errors.
+// If unescape is true, the parsed prefix is unescaped,
+// otherwise the input prefix is provided verbatim.
+func unquote(in string, unescape bool) (out, rem string, err error) {
+ // Determine the quote form and optimistically find the terminating quote.
+ if len(in) < 2 {
+ return "", in, ErrSyntax
+ }
+ quote := in[0]
+ end := index(in[1:], quote)
+ if end < 0 {
+ return "", in, ErrSyntax
+ }
+ end += 2 // position after terminating quote; may be wrong if escape sequences are present
+
+ switch quote {
+ case '`':
+ switch {
+ case !unescape:
+ out = in[:end] // include quotes
+ case !contains(in[:end], '\r'):
+ out = in[len("`") : end-len("`")] // exclude quotes
+ default:
+ // Carriage return characters ('\r') inside raw string literals
+ // are discarded from the raw string value.
+ buf := make([]byte, 0, end-len("`")-len("\r")-len("`"))
+ for i := len("`"); i < end-len("`"); i++ {
+ if in[i] != '\r' {
+ buf = append(buf, in[i])
+ }
+ }
+ out = string(buf)
+ }
+ // NOTE: Prior implementations did not verify that raw strings consist
+ // of valid UTF-8 characters and we continue to not verify it as such.
+ // The Go specification does not explicitly require valid UTF-8,
+ // but only mention that it is implicitly valid for Go source code
+ // (which must be valid UTF-8).
+ return out, in[end:], nil
+ case '"', '\'':
+ // Handle quoted strings without any escape sequences.
+ if !contains(in[:end], '\\') && !contains(in[:end], '\n') {
+ var valid bool
+ switch quote {
+ case '"':
+ valid = utf8.ValidString(in[len(`"`) : end-len(`"`)])
+ case '\'':
+ r, n := utf8.DecodeRuneInString(in[len("'") : end-len("'")])
+ valid = len("'")+n+len("'") == end && (r != utf8.RuneError || n != 1)
+ }
+ if valid {
+ out = in[:end]
+ if unescape {
+ out = out[1 : end-1] // exclude quotes
+ }
+ return out, in[end:], nil
+ }
+ }
+
+ // Handle quoted strings with escape sequences.
+ var buf []byte
+ in0 := in
+ in = in[1:] // skip starting quote
+ if unescape {
+ buf = make([]byte, 0, 3*end/2) // try to avoid more allocations
+ }
+ for len(in) > 0 && in[0] != quote {
+ // Process the next character,
+ // rejecting any unescaped newline characters which are invalid.
+ r, multibyte, rem, err := UnquoteChar(in, quote)
+ if in[0] == '\n' || err != nil {
+ return "", in0, ErrSyntax
+ }
+ in = rem
+
+ // Append the character if unescaping the input.
+ if unescape {
+ if r < utf8.RuneSelf || !multibyte {
+ buf = append(buf, byte(r))
+ } else {
+ buf = utf8.AppendRune(buf, r)
+ }
+ }
+
+ // Single quoted strings must be a single character.
+ if quote == '\'' {
+ break
+ }
+ }
+
+ // Verify that the string ends with a terminating quote.
+ if !(len(in) > 0 && in[0] == quote) {
+ return "", in0, ErrSyntax
+ }
+ in = in[1:] // skip terminating quote
+
+ if unescape {
+ return string(buf), in, nil
+ }
+ return in0[:len(in0)-len(in)], in, nil
+ default:
+ return "", in, ErrSyntax
+ }
+}
+
+// bsearch16 returns the smallest i such that a[i] >= x.
+// If there is no such i, bsearch16 returns len(a).
+func bsearch16(a []uint16, x uint16) int {
+ i, j := 0, len(a)
+ for i < j {
+ h := i + (j-i)>>1
+ if a[h] < x {
+ i = h + 1
+ } else {
+ j = h
+ }
+ }
+ return i
+}
+
+// bsearch32 returns the smallest i such that a[i] >= x.
+// If there is no such i, bsearch32 returns len(a).
+func bsearch32(a []uint32, x uint32) int {
+ i, j := 0, len(a)
+ for i < j {
+ h := i + (j-i)>>1
+ if a[h] < x {
+ i = h + 1
+ } else {
+ j = h
+ }
+ }
+ return i
+}
+
+// TODO: IsPrint is a local implementation of unicode.IsPrint, verified by the tests
+// to give the same answer. It allows this package not to depend on unicode,
+// and therefore not pull in all the Unicode tables. If the linker were better
+// at tossing unused tables, we could get rid of this implementation.
+// That would be nice.
+
+// IsPrint reports whether the rune is defined as printable by Go, with
+// the same definition as unicode.IsPrint: letters, numbers, punctuation,
+// symbols and ASCII space.
+func IsPrint(r rune) bool {
+ // Fast check for Latin-1
+ if r <= 0xFF {
+ if 0x20 <= r && r <= 0x7E {
+ // All the ASCII is printable from space through DEL-1.
+ return true
+ }
+ if 0xA1 <= r && r <= 0xFF {
+ // Similarly for ¡ through ÿ...
+ return r != 0xAD // ...except for the bizarre soft hyphen.
+ }
+ return false
+ }
+
+ // Same algorithm, either on uint16 or uint32 value.
+ // First, find first i such that isPrint[i] >= x.
+ // This is the index of either the start or end of a pair that might span x.
+ // The start is even (isPrint[i&^1]) and the end is odd (isPrint[i|1]).
+ // If we find x in a range, make sure x is not in isNotPrint list.
+
+ if 0 <= r && r < 1<<16 {
+ rr, isPrint, isNotPrint := uint16(r), isPrint16, isNotPrint16
+ i := bsearch16(isPrint, rr)
+ if i >= len(isPrint) || rr < isPrint[i&^1] || isPrint[i|1] < rr {
+ return false
+ }
+ j := bsearch16(isNotPrint, rr)
+ return j >= len(isNotPrint) || isNotPrint[j] != rr
+ }
+
+ rr, isPrint, isNotPrint := uint32(r), isPrint32, isNotPrint32
+ i := bsearch32(isPrint, rr)
+ if i >= len(isPrint) || rr < isPrint[i&^1] || isPrint[i|1] < rr {
+ return false
+ }
+ if r >= 0x20000 {
+ return true
+ }
+ r -= 0x10000
+ j := bsearch16(isNotPrint, uint16(r))
+ return j >= len(isNotPrint) || isNotPrint[j] != uint16(r)
+}
+
+// IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such
+// characters include letters, marks, numbers, punctuation, symbols, and
+// spaces, from categories L, M, N, P, S, and Zs.
+func IsGraphic(r rune) bool {
+ if IsPrint(r) {
+ return true
+ }
+ return isInGraphicList(r)
+}
+
+// isInGraphicList reports whether the rune is in the isGraphic list. This separation
+// from IsGraphic allows quoteWith to avoid two calls to IsPrint.
+// Should be called only if IsPrint fails.
+func isInGraphicList(r rune) bool {
+ // We know r must fit in 16 bits - see makeisprint.go.
+ if r > 0xFFFF {
+ return false
+ }
+ rr := uint16(r)
+ i := bsearch16(isGraphic, rr)
+ return i < len(isGraphic) && rr == isGraphic[i]
+}
diff --git a/src/strconv/quote_test.go b/src/strconv/quote_test.go
new file mode 100644
index 0000000..fc000de
--- /dev/null
+++ b/src/strconv/quote_test.go
@@ -0,0 +1,384 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ . "strconv"
+ "strings"
+ "testing"
+ "unicode"
+)
+
+// Verify that our IsPrint agrees with unicode.IsPrint.
+func TestIsPrint(t *testing.T) {
+ n := 0
+ for r := rune(0); r <= unicode.MaxRune; r++ {
+ if IsPrint(r) != unicode.IsPrint(r) {
+ t.Errorf("IsPrint(%U)=%t incorrect", r, IsPrint(r))
+ n++
+ if n > 10 {
+ return
+ }
+ }
+ }
+}
+
+// Verify that our IsGraphic agrees with unicode.IsGraphic.
+func TestIsGraphic(t *testing.T) {
+ n := 0
+ for r := rune(0); r <= unicode.MaxRune; r++ {
+ if IsGraphic(r) != unicode.IsGraphic(r) {
+ t.Errorf("IsGraphic(%U)=%t incorrect", r, IsGraphic(r))
+ n++
+ if n > 10 {
+ return
+ }
+ }
+ }
+}
+
+type quoteTest struct {
+ in string
+ out string
+ ascii string
+ graphic string
+}
+
+var quotetests = []quoteTest{
+ {"\a\b\f\r\n\t\v", `"\a\b\f\r\n\t\v"`, `"\a\b\f\r\n\t\v"`, `"\a\b\f\r\n\t\v"`},
+ {"\\", `"\\"`, `"\\"`, `"\\"`},
+ {"abc\xffdef", `"abc\xffdef"`, `"abc\xffdef"`, `"abc\xffdef"`},
+ {"\u263a", `"☺"`, `"\u263a"`, `"☺"`},
+ {"\U0010ffff", `"\U0010ffff"`, `"\U0010ffff"`, `"\U0010ffff"`},
+ {"\x04", `"\x04"`, `"\x04"`, `"\x04"`},
+ // Some non-printable but graphic runes. Final column is double-quoted.
+ {"!\u00a0!\u2000!\u3000!", `"!\u00a0!\u2000!\u3000!"`, `"!\u00a0!\u2000!\u3000!"`, "\"!\u00a0!\u2000!\u3000!\""},
+ {"\x7f", `"\x7f"`, `"\x7f"`, `"\x7f"`},
+}
+
+func TestQuote(t *testing.T) {
+ for _, tt := range quotetests {
+ if out := Quote(tt.in); out != tt.out {
+ t.Errorf("Quote(%s) = %s, want %s", tt.in, out, tt.out)
+ }
+ if out := AppendQuote([]byte("abc"), tt.in); string(out) != "abc"+tt.out {
+ t.Errorf("AppendQuote(%q, %s) = %s, want %s", "abc", tt.in, out, "abc"+tt.out)
+ }
+ }
+}
+
+func TestQuoteToASCII(t *testing.T) {
+ for _, tt := range quotetests {
+ if out := QuoteToASCII(tt.in); out != tt.ascii {
+ t.Errorf("QuoteToASCII(%s) = %s, want %s", tt.in, out, tt.ascii)
+ }
+ if out := AppendQuoteToASCII([]byte("abc"), tt.in); string(out) != "abc"+tt.ascii {
+ t.Errorf("AppendQuoteToASCII(%q, %s) = %s, want %s", "abc", tt.in, out, "abc"+tt.ascii)
+ }
+ }
+}
+
+func TestQuoteToGraphic(t *testing.T) {
+ for _, tt := range quotetests {
+ if out := QuoteToGraphic(tt.in); out != tt.graphic {
+ t.Errorf("QuoteToGraphic(%s) = %s, want %s", tt.in, out, tt.graphic)
+ }
+ if out := AppendQuoteToGraphic([]byte("abc"), tt.in); string(out) != "abc"+tt.graphic {
+ t.Errorf("AppendQuoteToGraphic(%q, %s) = %s, want %s", "abc", tt.in, out, "abc"+tt.graphic)
+ }
+ }
+}
+
+func BenchmarkQuote(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Quote("\a\b\f\r\n\t\v\a\b\f\r\n\t\v\a\b\f\r\n\t\v")
+ }
+}
+
+func BenchmarkQuoteRune(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ QuoteRune('\a')
+ }
+}
+
+var benchQuoteBuf []byte
+
+func BenchmarkAppendQuote(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ benchQuoteBuf = AppendQuote(benchQuoteBuf[:0], "\a\b\f\r\n\t\v\a\b\f\r\n\t\v\a\b\f\r\n\t\v")
+ }
+}
+
+var benchQuoteRuneBuf []byte
+
+func BenchmarkAppendQuoteRune(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ benchQuoteRuneBuf = AppendQuoteRune(benchQuoteRuneBuf[:0], '\a')
+ }
+}
+
+type quoteRuneTest struct {
+ in rune
+ out string
+ ascii string
+ graphic string
+}
+
+var quoterunetests = []quoteRuneTest{
+ {'a', `'a'`, `'a'`, `'a'`},
+ {'\a', `'\a'`, `'\a'`, `'\a'`},
+ {'\\', `'\\'`, `'\\'`, `'\\'`},
+ {0xFF, `'ÿ'`, `'\u00ff'`, `'ÿ'`},
+ {0x263a, `'☺'`, `'\u263a'`, `'☺'`},
+ {0xdead, `'�'`, `'\ufffd'`, `'�'`},
+ {0xfffd, `'�'`, `'\ufffd'`, `'�'`},
+ {0x0010ffff, `'\U0010ffff'`, `'\U0010ffff'`, `'\U0010ffff'`},
+ {0x0010ffff + 1, `'�'`, `'\ufffd'`, `'�'`},
+ {0x04, `'\x04'`, `'\x04'`, `'\x04'`},
+ // Some differences between graphic and printable. Note the last column is double-quoted.
+ {'\u00a0', `'\u00a0'`, `'\u00a0'`, "'\u00a0'"},
+ {'\u2000', `'\u2000'`, `'\u2000'`, "'\u2000'"},
+ {'\u3000', `'\u3000'`, `'\u3000'`, "'\u3000'"},
+}
+
+func TestQuoteRune(t *testing.T) {
+ for _, tt := range quoterunetests {
+ if out := QuoteRune(tt.in); out != tt.out {
+ t.Errorf("QuoteRune(%U) = %s, want %s", tt.in, out, tt.out)
+ }
+ if out := AppendQuoteRune([]byte("abc"), tt.in); string(out) != "abc"+tt.out {
+ t.Errorf("AppendQuoteRune(%q, %U) = %s, want %s", "abc", tt.in, out, "abc"+tt.out)
+ }
+ }
+}
+
+func TestQuoteRuneToASCII(t *testing.T) {
+ for _, tt := range quoterunetests {
+ if out := QuoteRuneToASCII(tt.in); out != tt.ascii {
+ t.Errorf("QuoteRuneToASCII(%U) = %s, want %s", tt.in, out, tt.ascii)
+ }
+ if out := AppendQuoteRuneToASCII([]byte("abc"), tt.in); string(out) != "abc"+tt.ascii {
+ t.Errorf("AppendQuoteRuneToASCII(%q, %U) = %s, want %s", "abc", tt.in, out, "abc"+tt.ascii)
+ }
+ }
+}
+
+func TestQuoteRuneToGraphic(t *testing.T) {
+ for _, tt := range quoterunetests {
+ if out := QuoteRuneToGraphic(tt.in); out != tt.graphic {
+ t.Errorf("QuoteRuneToGraphic(%U) = %s, want %s", tt.in, out, tt.graphic)
+ }
+ if out := AppendQuoteRuneToGraphic([]byte("abc"), tt.in); string(out) != "abc"+tt.graphic {
+ t.Errorf("AppendQuoteRuneToGraphic(%q, %U) = %s, want %s", "abc", tt.in, out, "abc"+tt.graphic)
+ }
+ }
+}
+
+type canBackquoteTest struct {
+ in string
+ out bool
+}
+
+var canbackquotetests = []canBackquoteTest{
+ {"`", false},
+ {string(rune(0)), false},
+ {string(rune(1)), false},
+ {string(rune(2)), false},
+ {string(rune(3)), false},
+ {string(rune(4)), false},
+ {string(rune(5)), false},
+ {string(rune(6)), false},
+ {string(rune(7)), false},
+ {string(rune(8)), false},
+ {string(rune(9)), true}, // \t
+ {string(rune(10)), false},
+ {string(rune(11)), false},
+ {string(rune(12)), false},
+ {string(rune(13)), false},
+ {string(rune(14)), false},
+ {string(rune(15)), false},
+ {string(rune(16)), false},
+ {string(rune(17)), false},
+ {string(rune(18)), false},
+ {string(rune(19)), false},
+ {string(rune(20)), false},
+ {string(rune(21)), false},
+ {string(rune(22)), false},
+ {string(rune(23)), false},
+ {string(rune(24)), false},
+ {string(rune(25)), false},
+ {string(rune(26)), false},
+ {string(rune(27)), false},
+ {string(rune(28)), false},
+ {string(rune(29)), false},
+ {string(rune(30)), false},
+ {string(rune(31)), false},
+ {string(rune(0x7F)), false},
+ {`' !"#$%&'()*+,-./:;<=>?@[\]^_{|}~`, true},
+ {`0123456789`, true},
+ {`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, true},
+ {`abcdefghijklmnopqrstuvwxyz`, true},
+ {`☺`, true},
+ {"\x80", false},
+ {"a\xe0\xa0z", false},
+ {"\ufeffabc", false},
+ {"a\ufeffz", false},
+}
+
+func TestCanBackquote(t *testing.T) {
+ for _, tt := range canbackquotetests {
+ if out := CanBackquote(tt.in); out != tt.out {
+ t.Errorf("CanBackquote(%q) = %v, want %v", tt.in, out, tt.out)
+ }
+ }
+}
+
+type unQuoteTest struct {
+ in string
+ out string
+}
+
+var unquotetests = []unQuoteTest{
+ {`""`, ""},
+ {`"a"`, "a"},
+ {`"abc"`, "abc"},
+ {`"☺"`, "☺"},
+ {`"hello world"`, "hello world"},
+ {`"\xFF"`, "\xFF"},
+ {`"\377"`, "\377"},
+ {`"\u1234"`, "\u1234"},
+ {`"\U00010111"`, "\U00010111"},
+ {`"\U0001011111"`, "\U0001011111"},
+ {`"\a\b\f\n\r\t\v\\\""`, "\a\b\f\n\r\t\v\\\""},
+ {`"'"`, "'"},
+
+ {`'a'`, "a"},
+ {`'☹'`, "☹"},
+ {`'\a'`, "\a"},
+ {`'\x10'`, "\x10"},
+ {`'\377'`, "\377"},
+ {`'\u1234'`, "\u1234"},
+ {`'\U00010111'`, "\U00010111"},
+ {`'\t'`, "\t"},
+ {`' '`, " "},
+ {`'\''`, "'"},
+ {`'"'`, "\""},
+
+ {"``", ``},
+ {"`a`", `a`},
+ {"`abc`", `abc`},
+ {"`☺`", `☺`},
+ {"`hello world`", `hello world`},
+ {"`\\xFF`", `\xFF`},
+ {"`\\377`", `\377`},
+ {"`\\`", `\`},
+ {"`\n`", "\n"},
+ {"` `", ` `},
+ {"` `", ` `},
+ {"`a\rb`", "ab"},
+}
+
+var misquoted = []string{
+ ``,
+ `"`,
+ `"a`,
+ `"'`,
+ `b"`,
+ `"\"`,
+ `"\9"`,
+ `"\19"`,
+ `"\129"`,
+ `'\'`,
+ `'\9'`,
+ `'\19'`,
+ `'\129'`,
+ `'ab'`,
+ `"\x1!"`,
+ `"\U12345678"`,
+ `"\z"`,
+ "`",
+ "`xxx",
+ "``x\r",
+ "`\"",
+ `"\'"`,
+ `'\"'`,
+ "\"\n\"",
+ "\"\\n\n\"",
+ "'\n'",
+ `"\udead"`,
+ `"\ud83d\ude4f"`,
+}
+
+func TestUnquote(t *testing.T) {
+ for _, tt := range unquotetests {
+ testUnquote(t, tt.in, tt.out, nil)
+ }
+ for _, tt := range quotetests {
+ testUnquote(t, tt.out, tt.in, nil)
+ }
+ for _, s := range misquoted {
+ testUnquote(t, s, "", ErrSyntax)
+ }
+}
+
+// Issue 23685: invalid UTF-8 should not go through the fast path.
+func TestUnquoteInvalidUTF8(t *testing.T) {
+ tests := []struct {
+ in string
+
+ // one of:
+ want string
+ wantErr error
+ }{
+ {in: `"foo"`, want: "foo"},
+ {in: `"foo`, wantErr: ErrSyntax},
+ {in: `"` + "\xc0" + `"`, want: "\xef\xbf\xbd"},
+ {in: `"a` + "\xc0" + `"`, want: "a\xef\xbf\xbd"},
+ {in: `"\t` + "\xc0" + `"`, want: "\t\xef\xbf\xbd"},
+ }
+ for _, tt := range tests {
+ testUnquote(t, tt.in, tt.want, tt.wantErr)
+ }
+}
+
+func testUnquote(t *testing.T, in, want string, wantErr error) {
+ // Test Unquote.
+ got, gotErr := Unquote(in)
+ if got != want || gotErr != wantErr {
+ t.Errorf("Unquote(%q) = (%q, %v), want (%q, %v)", in, got, gotErr, want, wantErr)
+ }
+
+ // Test QuotedPrefix.
+ // Adding an arbitrary suffix should not change the result of QuotedPrefix
+ // assume that the suffix doesn't accidentally terminate a truncated input.
+ if gotErr == nil {
+ want = in
+ }
+ suffix := "\n\r\\\"`'" // special characters for quoted strings
+ if len(in) > 0 {
+ suffix = strings.ReplaceAll(suffix, in[:1], "")
+ }
+ in += suffix
+ got, gotErr = QuotedPrefix(in)
+ if gotErr == nil && wantErr != nil {
+ _, wantErr = Unquote(got) // original input had trailing junk, reparse with only valid prefix
+ want = got
+ }
+ if got != want || gotErr != wantErr {
+ t.Errorf("QuotedPrefix(%q) = (%q, %v), want (%q, %v)", in, got, gotErr, want, wantErr)
+ }
+}
+
+func BenchmarkUnquoteEasy(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Unquote(`"Give me a rock, paper and scissors and I will move the world."`)
+ }
+}
+
+func BenchmarkUnquoteHard(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Unquote(`"\x47ive me a \x72ock, \x70aper and \x73cissors and \x49 will move the world."`)
+ }
+}
diff --git a/src/strconv/strconv_test.go b/src/strconv/strconv_test.go
new file mode 100644
index 0000000..41b8fa7
--- /dev/null
+++ b/src/strconv/strconv_test.go
@@ -0,0 +1,160 @@
+// Copyright 2012 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package strconv_test
+
+import (
+ "runtime"
+ . "strconv"
+ "strings"
+ "testing"
+)
+
+var (
+ globalBuf [64]byte
+ nextToOne = "1.00000000000000011102230246251565404236316680908203125" + strings.Repeat("0", 10000) + "1"
+
+ mallocTest = []struct {
+ count int
+ desc string
+ fn func()
+ }{
+ {0, `AppendInt(localBuf[:0], 123, 10)`, func() {
+ var localBuf [64]byte
+ AppendInt(localBuf[:0], 123, 10)
+ }},
+ {0, `AppendInt(globalBuf[:0], 123, 10)`, func() { AppendInt(globalBuf[:0], 123, 10) }},
+ {0, `AppendFloat(localBuf[:0], 1.23, 'g', 5, 64)`, func() {
+ var localBuf [64]byte
+ AppendFloat(localBuf[:0], 1.23, 'g', 5, 64)
+ }},
+ {0, `AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64)`, func() { AppendFloat(globalBuf[:0], 1.23, 'g', 5, 64) }},
+ // In practice we see 7 for the next one, but allow some slop.
+ // Before pre-allocation in appendQuotedWith, we saw 39.
+ {10, `AppendQuoteToASCII(nil, oneMB)`, func() { AppendQuoteToASCII(nil, string(oneMB)) }},
+ {0, `ParseFloat("123.45", 64)`, func() { ParseFloat("123.45", 64) }},
+ {0, `ParseFloat("123.456789123456789", 64)`, func() { ParseFloat("123.456789123456789", 64) }},
+ {0, `ParseFloat("1.000000000000000111022302462515654042363166809082031251", 64)`, func() {
+ ParseFloat("1.000000000000000111022302462515654042363166809082031251", 64)
+ }},
+ {0, `ParseFloat("1.0000000000000001110223024625156540423631668090820312500...001", 64)`, func() {
+ ParseFloat(nextToOne, 64)
+ }},
+ }
+)
+
+var oneMB []byte // Will be allocated to 1MB of random data by TestCountMallocs.
+
+func TestCountMallocs(t *testing.T) {
+ if testing.Short() {
+ t.Skip("skipping malloc count in short mode")
+ }
+ if runtime.GOMAXPROCS(0) > 1 {
+ t.Skip("skipping; GOMAXPROCS>1")
+ }
+ // Allocate a big messy buffer for AppendQuoteToASCII's test.
+ oneMB = make([]byte, 1e6)
+ for i := range oneMB {
+ oneMB[i] = byte(i)
+ }
+ for _, mt := range mallocTest {
+ allocs := testing.AllocsPerRun(100, mt.fn)
+ if max := float64(mt.count); allocs > max {
+ t.Errorf("%s: %v allocs, want <=%v", mt.desc, allocs, max)
+ }
+ }
+}
+
+// Sink makes sure the compiler cannot optimize away the benchmarks.
+var Sink struct {
+ Bool bool
+ Int int
+ Int64 int64
+ Uint64 uint64
+ Float64 float64
+ Complex128 complex128
+ Error error
+ Bytes []byte
+}
+
+func TestAllocationsFromBytes(t *testing.T) {
+ const runsPerTest = 100
+ bytes := struct{ Bool, Number, String, Buffer []byte }{
+ Bool: []byte("false"),
+ Number: []byte("123456789"),
+ String: []byte("hello, world!"),
+ Buffer: make([]byte, 1024),
+ }
+
+ checkNoAllocs := func(f func()) func(t *testing.T) {
+ return func(t *testing.T) {
+ t.Helper()
+ if allocs := testing.AllocsPerRun(runsPerTest, f); allocs != 0 {
+ t.Errorf("got %v allocs, want 0 allocs", allocs)
+ }
+ }
+ }
+
+ t.Run("Atoi", checkNoAllocs(func() {
+ Sink.Int, Sink.Error = Atoi(string(bytes.Number))
+ }))
+ t.Run("ParseBool", checkNoAllocs(func() {
+ Sink.Bool, Sink.Error = ParseBool(string(bytes.Bool))
+ }))
+ t.Run("ParseInt", checkNoAllocs(func() {
+ Sink.Int64, Sink.Error = ParseInt(string(bytes.Number), 10, 64)
+ }))
+ t.Run("ParseUint", checkNoAllocs(func() {
+ Sink.Uint64, Sink.Error = ParseUint(string(bytes.Number), 10, 64)
+ }))
+ t.Run("ParseFloat", checkNoAllocs(func() {
+ Sink.Float64, Sink.Error = ParseFloat(string(bytes.Number), 64)
+ }))
+ t.Run("ParseComplex", checkNoAllocs(func() {
+ Sink.Complex128, Sink.Error = ParseComplex(string(bytes.Number), 128)
+ }))
+ t.Run("CanBackquote", checkNoAllocs(func() {
+ Sink.Bool = CanBackquote(string(bytes.String))
+ }))
+ t.Run("AppendQuote", checkNoAllocs(func() {
+ Sink.Bytes = AppendQuote(bytes.Buffer[:0], string(bytes.String))
+ }))
+ t.Run("AppendQuoteToASCII", checkNoAllocs(func() {
+ Sink.Bytes = AppendQuoteToASCII(bytes.Buffer[:0], string(bytes.String))
+ }))
+ t.Run("AppendQuoteToGraphic", checkNoAllocs(func() {
+ Sink.Bytes = AppendQuoteToGraphic(bytes.Buffer[:0], string(bytes.String))
+ }))
+}
+
+func TestErrorPrefixes(t *testing.T) {
+ _, errInt := Atoi("INVALID")
+ _, errBool := ParseBool("INVALID")
+ _, errFloat := ParseFloat("INVALID", 64)
+ _, errInt64 := ParseInt("INVALID", 10, 64)
+ _, errUint64 := ParseUint("INVALID", 10, 64)
+
+ vectors := []struct {
+ err error // Input error
+ want string // Function name wanted
+ }{
+ {errInt, "Atoi"},
+ {errBool, "ParseBool"},
+ {errFloat, "ParseFloat"},
+ {errInt64, "ParseInt"},
+ {errUint64, "ParseUint"},
+ }
+
+ for _, v := range vectors {
+ nerr, ok := v.err.(*NumError)
+ if !ok {
+ t.Errorf("test %s, error was not a *NumError", v.want)
+ continue
+ }
+ if got := nerr.Func; got != v.want {
+ t.Errorf("mismatching Func: got %s, want %s", got, v.want)
+ }
+ }
+
+}
diff --git a/src/strconv/testdata/testfp.txt b/src/strconv/testdata/testfp.txt
new file mode 100644
index 0000000..08d3c4e
--- /dev/null
+++ b/src/strconv/testdata/testfp.txt
@@ -0,0 +1,181 @@
+# Floating-point conversion test cases.
+# Empty lines and lines beginning with # are ignored.
+# The rest have four fields per line: type, format, input, and output.
+# The input is given either in decimal or binary scientific notation.
+# The output is the string that should be produced by formatting the
+# input with the given format.
+#
+# The formats are as in C's printf, except that %b means print
+# binary scientific notation: NpE = N x 2^E.
+
+# TODO:
+# Powers of 10.
+# Powers of 2.
+# %.20g versions.
+# random sources
+# random targets
+# random targets ± half a ULP
+
+# Difficult boundary cases, derived from tables given in
+# Vern Paxson, A Program for Testing IEEE Decimal-Binary Conversion
+# ftp://ftp.ee.lbl.gov/testbase-report.ps.Z
+
+# Table 1: Stress Inputs for Conversion to 53-bit Binary, < 1/2 ULP
+float64 %b 5e+125 6653062250012735p+365
+float64 %b 69e+267 4705683757438170p+841
+float64 %b 999e-026 6798841691080350p-129
+float64 %b 7861e-034 8975675289889240p-153
+float64 %b 75569e-254 6091718967192243p-880
+float64 %b 928609e-261 7849264900213743p-900
+float64 %b 9210917e+080 8341110837370930p+236
+float64 %b 84863171e+114 4625202867375927p+353
+float64 %b 653777767e+273 5068902999763073p+884
+float64 %b 5232604057e-298 5741343011915040p-1010
+float64 %b 27235667517e-109 6707124626673586p-380
+float64 %b 653532977297e-123 7078246407265384p-422
+float64 %b 3142213164987e-294 8219991337640559p-988
+float64 %b 46202199371337e-072 5224462102115359p-246
+float64 %b 231010996856685e-073 5224462102115359p-247
+float64 %b 9324754620109615e+212 5539753864394442p+705
+float64 %b 78459735791271921e+049 8388176519442766p+166
+float64 %b 272104041512242479e+200 5554409530847367p+670
+float64 %b 6802601037806061975e+198 5554409530847367p+668
+float64 %b 20505426358836677347e-221 4524032052079546p-722
+float64 %b 836168422905420598437e-234 5070963299887562p-760
+float64 %b 4891559871276714924261e+222 6452687840519111p+757
+
+# Table 2: Stress Inputs for Conversion to 53-bit Binary, > 1/2 ULP
+float64 %b 9e-265 8168427841980010p-930
+float64 %b 85e-037 6360455125664090p-169
+float64 %b 623e+100 6263531988747231p+289
+float64 %b 3571e+263 6234526311072170p+833
+float64 %b 81661e+153 6696636728760206p+472
+float64 %b 920657e-023 5975405561110124p-109
+float64 %b 4603285e-024 5975405561110124p-110
+float64 %b 87575437e-309 8452160731874668p-1053
+float64 %b 245540327e+122 4985336549131723p+381
+float64 %b 6138508175e+120 4985336549131723p+379
+float64 %b 83356057653e+193 5986732817132056p+625
+float64 %b 619534293513e+124 4798406992060657p+399
+float64 %b 2335141086879e+218 5419088166961646p+713
+float64 %b 36167929443327e-159 8135819834632444p-536
+float64 %b 609610927149051e-255 4576664294594737p-850
+float64 %b 3743626360493413e-165 6898586531774201p-549
+float64 %b 94080055902682397e-242 6273271706052298p-800
+float64 %b 899810892172646163e+283 7563892574477827p+947
+float64 %b 7120190517612959703e+120 5385467232557565p+409
+float64 %b 25188282901709339043e-252 5635662608542340p-825
+float64 %b 308984926168550152811e-052 5644774693823803p-157
+float64 %b 6372891218502368041059e+064 4616868614322430p+233
+
+# Table 3: Stress Inputs for Converting 53-bit Binary to Decimal, < 1/2 ULP
+float64 %.0e 8511030020275656p-342 9e-88
+float64 %.1e 5201988407066741p-824 4.6e-233
+float64 %.2e 6406892948269899p+237 1.41e+87
+float64 %.3e 8431154198732492p+72 3.981e+37
+float64 %.4e 6475049196144587p+99 4.1040e+45
+float64 %.5e 8274307542972842p+726 2.92084e+234
+float64 %.6e 5381065484265332p-456 2.891946e-122
+float64 %.7e 6761728585499734p-1057 4.3787718e-303
+float64 %.8e 7976538478610756p+376 1.22770163e+129
+float64 %.9e 5982403858958067p+377 1.841552452e+129
+float64 %.10e 5536995190630837p+93 5.4835744350e+43
+float64 %.11e 7225450889282194p+710 3.89190181146e+229
+float64 %.12e 7225450889282194p+709 1.945950905732e+229
+float64 %.13e 8703372741147379p+117 1.4460958381605e+51
+float64 %.14e 8944262675275217p-1001 4.17367747458531e-286
+float64 %.15e 7459803696087692p-707 1.107950772878888e-197
+float64 %.16e 6080469016670379p-381 1.2345501366327440e-99
+float64 %.17e 8385515147034757p+721 9.25031711960365024e+232
+float64 %.18e 7514216811389786p-828 4.198047150284889840e-234
+float64 %.19e 8397297803260511p-345 1.1716315319786511046e-88
+float64 %.20e 6733459239310543p+202 4.32810072844612493629e+76
+float64 %.21e 8091450587292794p-473 3.317710118160031081518e-127
+
+# Table 4: Stress Inputs for Converting 53-bit Binary to Decimal, > 1/2 ULP
+float64 %.0e 6567258882077402p+952 3e+302
+float64 %.1e 6712731423444934p+535 7.6e+176
+float64 %.2e 6712731423444934p+534 3.78e+176
+float64 %.3e 5298405411573037p-957 4.350e-273
+float64 %.4e 5137311167659507p-144 2.3037e-28
+float64 %.5e 6722280709661868p+363 1.26301e+125
+float64 %.6e 5344436398034927p-169 7.142211e-36
+float64 %.7e 8369123604277281p-853 1.3934574e-241
+float64 %.8e 8995822108487663p-780 1.41463449e-219
+float64 %.9e 8942832835564782p-383 4.539277920e-100
+float64 %.10e 8942832835564782p-384 2.2696389598e-100
+float64 %.11e 8942832835564782p-385 1.13481947988e-100
+float64 %.12e 6965949469487146p-249 7.700366561890e-60
+float64 %.13e 6965949469487146p-250 3.8501832809448e-60
+float64 %.14e 6965949469487146p-251 1.92509164047238e-60
+float64 %.15e 7487252720986826p+548 6.898586531774201e+180
+float64 %.16e 5592117679628511p+164 1.3076622631878654e+65
+float64 %.17e 8887055249355788p+665 1.36052020756121240e+216
+float64 %.18e 6994187472632449p+690 3.592810217475959676e+223
+float64 %.19e 8797576579012143p+588 8.9125197712484551899e+192
+float64 %.20e 7363326733505337p+272 5.58769757362301140950e+97
+float64 %.21e 8549497411294502p-448 1.176257830728540379990e-119
+
+# Table 14: Stress Inputs for Conversion to 24-bit Binary, <1/2 ULP
+# NOTE: The lines with exponent p-149 have been changed from the
+# paper. Those entries originally read p-150 and had a mantissa
+# twice as large (and even), but IEEE single-precision has no p-150:
+# that's the start of the denormals.
+float32 %b 5e-20 15474250p-88
+float32 %b 67e+14 12479722p+29
+float32 %b 985e+15 14333636p+36
+# float32 %b 7693e-42 10979816p-150
+float32 %b 7693e-42 5489908p-149
+float32 %b 55895e-16 12888509p-61
+# float32 %b 996622e-44 14224264p-150
+float32 %b 996622e-44 7112132p-149
+float32 %b 7038531e-32 11420669p-107
+# float32 %b 60419369e-46 8623340p-150
+float32 %b 60419369e-46 4311670p-149
+float32 %b 702990899e-20 16209866p-61
+# float32 %b 6930161142e-48 9891056p-150
+float32 %b 6930161142e-48 4945528p-149
+float32 %b 25933168707e+13 14395800p+54
+float32 %b 596428896559e+20 12333860p+82
+
+# Table 15: Stress Inputs for Conversion to 24-bit Binary, >1/2 ULP
+float32 %b 3e-23 9507380p-98
+float32 %b 57e+18 12960300p+42
+float32 %b 789e-35 10739312p-130
+float32 %b 2539e-18 11990089p-72
+float32 %b 76173e+28 9845130p+86
+float32 %b 887745e-11 9760860p-40
+float32 %b 5382571e-37 11447463p-124
+float32 %b 82381273e-35 8554961p-113
+float32 %b 750486563e-38 9975678p-120
+float32 %b 3752432815e-39 9975678p-121
+float32 %b 75224575729e-45 13105970p-137
+float32 %b 459926601011e+15 12466336p+65
+
+# Table 16: Stress Inputs for Converting 24-bit Binary to Decimal, < 1/2 ULP
+float32 %.0e 12676506p-102 2e-24
+float32 %.1e 12676506p-103 1.2e-24
+float32 %.2e 15445013p+86 1.19e+33
+float32 %.3e 13734123p-138 3.941e-35
+float32 %.4e 12428269p-130 9.1308e-33
+float32 %.5e 15334037p-146 1.71900e-37
+float32 %.6e 11518287p-41 5.237910e-06
+float32 %.7e 12584953p-145 2.8216440e-37
+float32 %.8e 15961084p-125 3.75243281e-31
+float32 %.9e 14915817p-146 1.672120916e-37
+float32 %.10e 10845484p-102 2.1388945814e-24
+float32 %.11e 16431059p-61 7.12583594561e-12
+
+# Table 17: Stress Inputs for Converting 24-bit Binary to Decimal, > 1/2 ULP
+float32 %.0e 16093626p+69 1e+28
+float32 %.1e 9983778p+25 3.4e+14
+float32 %.2e 12745034p+104 2.59e+38
+float32 %.3e 12706553p+72 6.001e+28
+float32 %.4e 11005028p+45 3.8721e+20
+float32 %.5e 15059547p+71 3.55584e+28
+float32 %.6e 16015691p-99 2.526831e-23
+float32 %.7e 8667859p+56 6.2458507e+23
+float32 %.8e 14855922p-82 3.07213267e-18
+float32 %.9e 14855922p-83 1.536066333e-18
+float32 %.10e 10144164p-110 7.8147796834e-27
+float32 %.11e 13248074p+95 5.24810279937e+35