diff options
author | Ian Lance Taylor <iant@golang.org> | 2020-01-02 15:05:27 -0800 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2020-01-21 23:53:22 -0800 |
commit | 5a8ea165926cb0737ab03bc48c18dc5198ab5305 (patch) | |
tree | 962dc3357c57f019f85658f99e2e753e30201c27 /libgo/go/strconv | |
parent | 6ac6529e155c9baa0aaaed7aca06bd38ebda5b43 (diff) | |
download | gcc-5a8ea165926cb0737ab03bc48c18dc5198ab5305.zip gcc-5a8ea165926cb0737ab03bc48c18dc5198ab5305.tar.gz gcc-5a8ea165926cb0737ab03bc48c18dc5198ab5305.tar.bz2 |
libgo: update to Go1.14beta1
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/214297
Diffstat (limited to 'libgo/go/strconv')
-rw-r--r-- | libgo/go/strconv/atof.go | 37 | ||||
-rw-r--r-- | libgo/go/strconv/atoi.go | 20 | ||||
-rw-r--r-- | libgo/go/strconv/atoi_test.go | 7 | ||||
-rw-r--r-- | libgo/go/strconv/example_test.go | 9 | ||||
-rw-r--r-- | libgo/go/strconv/isprint.go | 86 |
5 files changed, 97 insertions, 62 deletions
diff --git a/libgo/go/strconv/atof.go b/libgo/go/strconv/atof.go index d531bda..4dbe68b 100644 --- a/libgo/go/strconv/atof.go +++ b/libgo/go/strconv/atof.go @@ -85,7 +85,7 @@ func (b *decimal) set(s string) (ok bool) { for ; i < len(s); i++ { switch { case s[i] == '_': - // underscoreOK already called + // readFloat already checked underscores continue case s[i] == '.': if sawdot { @@ -141,7 +141,7 @@ func (b *decimal) set(s string) (ok bool) { e := 0 for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i++ { if s[i] == '_' { - // underscoreOK already called + // readFloat already checked underscores continue } if e < 10000 { @@ -160,10 +160,11 @@ func (b *decimal) set(s string) (ok bool) { } // readFloat reads a decimal mantissa and exponent from a float -// string representation. It returns ok==false if the number could -// not fit return types or is invalid. +// string representation. It returns ok==false if the number +// is invalid. func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex, ok bool) { i := 0 + underscores := false // optional sign if i >= len(s) { @@ -196,7 +197,7 @@ func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex, ok bool) { for ; i < len(s); i++ { switch c := s[i]; true { case c == '_': - // underscoreOK already called + underscores = true continue case c == '.': @@ -272,7 +273,7 @@ func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex, ok bool) { e := 0 for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i++ { if s[i] == '_' { - // underscoreOK already called + underscores = true continue } if e < 10000 { @@ -292,6 +293,11 @@ func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex, ok bool) { if mantissa != 0 { exp = dp - ndMant } + + if underscores && !underscoreOK(s) { + return + } + ok = true return } @@ -560,12 +566,16 @@ func atof32(s string) (f float32, err error) { } mantissa, exp, neg, trunc, hex, ok := readFloat(s) - if hex && ok { + if !ok { + return 0, syntaxError(fnParseFloat, s) + } + + if hex { f, err := atofHex(s, &float32info, mantissa, exp, neg, trunc) return float32(f), err } - if optimize && ok { + if optimize { // Try pure floating-point arithmetic conversion. if !trunc { if f, ok := atof32exact(mantissa, exp, neg); ok { @@ -603,11 +613,15 @@ func atof64(s string) (f float64, err error) { } mantissa, exp, neg, trunc, hex, ok := readFloat(s) - if hex && ok { + if !ok { + return 0, syntaxError(fnParseFloat, s) + } + + if hex { return atofHex(s, &float64info, mantissa, exp, neg, trunc) } - if optimize && ok { + if optimize { // Try pure floating-point arithmetic conversion. if !trunc { if f, ok := atof64exact(mantissa, exp, neg); ok { @@ -664,9 +678,6 @@ func atof64(s string) (f float64, err error) { // ParseFloat recognizes the strings "NaN", "+Inf", and "-Inf" as their // respective special floating point values. It ignores case when matching. func ParseFloat(s string, bitSize int) (float64, error) { - if !underscoreOK(s) { - return 0, syntaxError(fnParseFloat, s) - } if bitSize == 32 { f, err := atof32(s) return float64(f), err diff --git a/libgo/go/strconv/atoi.go b/libgo/go/strconv/atoi.go index 0233f14..a4a8a37 100644 --- a/libgo/go/strconv/atoi.go +++ b/libgo/go/strconv/atoi.go @@ -31,6 +31,8 @@ func (e *NumError) Error() string { return "strconv." + e.Func + ": " + "parsing " + Quote(e.Num) + ": " + e.Err.Error() } +func (e *NumError) Unwrap() error { return e.Err } + func syntaxError(fn, str string) *NumError { return &NumError{fn, str, ErrSyntax} } @@ -58,7 +60,7 @@ const maxUint64 = 1<<64 - 1 func ParseUint(s string, base int, bitSize int) (uint64, error) { const fnParseUint = "ParseUint" - if s == "" || !underscoreOK(s) { + if s == "" { return 0, syntaxError(fnParseUint, s) } @@ -113,12 +115,13 @@ func ParseUint(s string, base int, bitSize int) (uint64, error) { maxVal := uint64(1)<<uint(bitSize) - 1 + underscores := false var n uint64 for _, c := range []byte(s) { var d byte switch { case c == '_' && base0: - // underscoreOK already called + underscores = true continue case '0' <= c && c <= '9': d = c - '0' @@ -146,17 +149,20 @@ func ParseUint(s string, base int, bitSize int) (uint64, error) { 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. // -// If base == 0, the base is implied by the string's prefix: -// base 2 for "0b", base 8 for "0" or "0o", base 16 for "0x", -// and base 10 otherwise. Also, for base == 0 only, underscore -// characters are permitted per the Go integer literal syntax. -// If base is below 0, is 1, or is above 36, an error is returned. +// If the base argument is 0, the true base is implied by the string's +// prefix: 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 diff --git a/libgo/go/strconv/atoi_test.go b/libgo/go/strconv/atoi_test.go index b167c96..178fb01 100644 --- a/libgo/go/strconv/atoi_test.go +++ b/libgo/go/strconv/atoi_test.go @@ -592,6 +592,13 @@ func TestNumError(t *testing.T) { } } +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) diff --git a/libgo/go/strconv/example_test.go b/libgo/go/strconv/example_test.go index 50f6b20..3b4cedb 100644 --- a/libgo/go/strconv/example_test.go +++ b/libgo/go/strconv/example_test.go @@ -294,7 +294,8 @@ func ExampleParseUint() { } func ExampleQuote() { - s := strconv.Quote(`"Fran & Freddie's Diner ☺"`) // there is a tab character inside the string literal + // This string literal contains a tab character. + s := strconv.Quote(`"Fran & Freddie's Diner ☺"`) fmt.Println(s) // Output: @@ -338,7 +339,8 @@ func ExampleQuoteRuneToGraphic() { } func ExampleQuoteToASCII() { - s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`) // there is a tab character inside the string literal + // This string literal contains a tab character. + s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`) fmt.Println(s) // Output: @@ -349,7 +351,8 @@ func ExampleQuoteToGraphic() { s := strconv.QuoteToGraphic("☺") fmt.Println(s) - s = strconv.QuoteToGraphic("This is a \u263a \u000a") // there is a tab character inside the string literal + // 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 "`) diff --git a/libgo/go/strconv/isprint.go b/libgo/go/strconv/isprint.go index a8dfdb2..7ada2d1 100644 --- a/libgo/go/strconv/isprint.go +++ b/libgo/go/strconv/isprint.go @@ -6,7 +6,7 @@ package strconv -// (448+137+90)*2 + (418)*4 = 3022 bytes +// (442+132+90)*2 + (450)*4 = 3128 bytes var isPrint16 = []uint16{ 0x0020, 0x007e, @@ -75,7 +75,7 @@ var isPrint16 = []uint16{ 0x0c55, 0x0c5a, 0x0c60, 0x0c63, 0x0c66, 0x0c6f, - 0x0c78, 0x0cb9, + 0x0c77, 0x0cb9, 0x0cbc, 0x0ccd, 0x0cd5, 0x0cd6, 0x0cde, 0x0ce3, @@ -92,11 +92,7 @@ var isPrint16 = []uint16{ 0x0df2, 0x0df4, 0x0e01, 0x0e3a, 0x0e3f, 0x0e5b, - 0x0e81, 0x0e84, - 0x0e87, 0x0e8a, - 0x0e8d, 0x0e8d, - 0x0e94, 0x0ea7, - 0x0eaa, 0x0ebd, + 0x0e81, 0x0ebd, 0x0ec0, 0x0ecd, 0x0ed0, 0x0ed9, 0x0edc, 0x0edf, @@ -151,7 +147,7 @@ var isPrint16 = []uint16{ 0x1c4d, 0x1c88, 0x1c90, 0x1cba, 0x1cbd, 0x1cc7, - 0x1cd0, 0x1cf9, + 0x1cd0, 0x1cfa, 0x1d00, 0x1f15, 0x1f18, 0x1f1d, 0x1f20, 0x1f45, @@ -177,7 +173,7 @@ var isPrint16 = []uint16{ 0x2d30, 0x2d67, 0x2d6f, 0x2d70, 0x2d7f, 0x2d96, - 0x2da0, 0x2e4e, + 0x2da0, 0x2e4f, 0x2e80, 0x2ef3, 0x2f00, 0x2fd5, 0x2ff0, 0x2ffb, @@ -191,7 +187,8 @@ var isPrint16 = []uint16{ 0xa490, 0xa4c6, 0xa4d0, 0xa62b, 0xa640, 0xa6f7, - 0xa700, 0xa7b9, + 0xa700, 0xa7bf, + 0xa7c2, 0xa7c6, 0xa7f7, 0xa82b, 0xa830, 0xa839, 0xa840, 0xa877, @@ -208,7 +205,7 @@ var isPrint16 = []uint16{ 0xab01, 0xab06, 0xab09, 0xab0e, 0xab11, 0xab16, - 0xab20, 0xab65, + 0xab20, 0xab67, 0xab70, 0xabed, 0xabf0, 0xabf9, 0xac00, 0xd7a3, @@ -302,13 +299,10 @@ var isNotPrint16 = []uint16{ 0x0dd5, 0x0dd7, 0x0e83, - 0x0e89, - 0x0e98, - 0x0ea0, + 0x0e85, + 0x0e8b, 0x0ea4, 0x0ea6, - 0x0eac, - 0x0eba, 0x0ec5, 0x0ec7, 0x0f48, @@ -341,8 +335,6 @@ var isNotPrint16 = []uint16{ 0x1fdc, 0x1ff5, 0x208f, - 0x2bc9, - 0x2bff, 0x2c2f, 0x2c5f, 0x2d26, @@ -435,6 +427,7 @@ var isPrint32 = []uint32{ 0x010e60, 0x010e7e, 0x010f00, 0x010f27, 0x010f30, 0x010f59, + 0x010fe0, 0x010ff6, 0x011000, 0x01104d, 0x011052, 0x01106f, 0x01107f, 0x0110c1, @@ -458,7 +451,7 @@ var isPrint32 = []uint32{ 0x01135d, 0x011363, 0x011366, 0x01136c, 0x011370, 0x011374, - 0x011400, 0x01145e, + 0x011400, 0x01145f, 0x011480, 0x0114c7, 0x0114d0, 0x0114d9, 0x011580, 0x0115b5, @@ -466,7 +459,7 @@ var isPrint32 = []uint32{ 0x011600, 0x011644, 0x011650, 0x011659, 0x011660, 0x01166c, - 0x011680, 0x0116b7, + 0x011680, 0x0116b8, 0x0116c0, 0x0116c9, 0x011700, 0x01171a, 0x01171d, 0x01172b, @@ -474,9 +467,11 @@ var isPrint32 = []uint32{ 0x011800, 0x01183b, 0x0118a0, 0x0118f2, 0x0118ff, 0x0118ff, + 0x0119a0, 0x0119a7, + 0x0119aa, 0x0119d7, + 0x0119da, 0x0119e4, 0x011a00, 0x011a47, - 0x011a50, 0x011a83, - 0x011a86, 0x011aa2, + 0x011a50, 0x011aa2, 0x011ac0, 0x011af8, 0x011c00, 0x011c45, 0x011c50, 0x011c6c, @@ -488,7 +483,8 @@ var isPrint32 = []uint32{ 0x011d60, 0x011d98, 0x011da0, 0x011da9, 0x011ee0, 0x011ef8, - 0x012000, 0x012399, + 0x011fc0, 0x011ff1, + 0x011fff, 0x012399, 0x012400, 0x012474, 0x012480, 0x012543, 0x013000, 0x01342e, @@ -502,13 +498,15 @@ var isPrint32 = []uint32{ 0x016b50, 0x016b77, 0x016b7d, 0x016b8f, 0x016e40, 0x016e9a, - 0x016f00, 0x016f44, - 0x016f50, 0x016f7e, + 0x016f00, 0x016f4a, + 0x016f4f, 0x016f87, 0x016f8f, 0x016f9f, - 0x016fe0, 0x016fe1, - 0x017000, 0x0187f1, + 0x016fe0, 0x016fe3, + 0x017000, 0x0187f7, 0x018800, 0x018af2, 0x01b000, 0x01b11e, + 0x01b150, 0x01b152, + 0x01b164, 0x01b167, 0x01b170, 0x01b2fb, 0x01bc00, 0x01bc6a, 0x01bc70, 0x01bc7c, @@ -534,12 +532,19 @@ var isPrint32 = []uint32{ 0x01da9b, 0x01daaf, 0x01e000, 0x01e018, 0x01e01b, 0x01e02a, + 0x01e100, 0x01e12c, + 0x01e130, 0x01e13d, + 0x01e140, 0x01e149, + 0x01e14e, 0x01e14f, + 0x01e2c0, 0x01e2f9, + 0x01e2ff, 0x01e2ff, 0x01e800, 0x01e8c4, 0x01e8c7, 0x01e8d6, - 0x01e900, 0x01e94a, + 0x01e900, 0x01e94b, 0x01e950, 0x01e959, 0x01e95e, 0x01e95f, 0x01ec71, 0x01ecb4, + 0x01ed01, 0x01ed3d, 0x01ee00, 0x01ee24, 0x01ee27, 0x01ee3b, 0x01ee42, 0x01ee42, @@ -553,31 +558,34 @@ var isPrint32 = []uint32{ 0x01f0a0, 0x01f0ae, 0x01f0b1, 0x01f0f5, 0x01f100, 0x01f10c, - 0x01f110, 0x01f16b, + 0x01f110, 0x01f16c, 0x01f170, 0x01f1ac, 0x01f1e6, 0x01f202, 0x01f210, 0x01f23b, 0x01f240, 0x01f248, 0x01f250, 0x01f251, 0x01f260, 0x01f265, - 0x01f300, 0x01f6d4, + 0x01f300, 0x01f6d5, 0x01f6e0, 0x01f6ec, - 0x01f6f0, 0x01f6f9, + 0x01f6f0, 0x01f6fa, 0x01f700, 0x01f773, 0x01f780, 0x01f7d8, + 0x01f7e0, 0x01f7eb, 0x01f800, 0x01f80b, 0x01f810, 0x01f847, 0x01f850, 0x01f859, 0x01f860, 0x01f887, 0x01f890, 0x01f8ad, - 0x01f900, 0x01f90b, - 0x01f910, 0x01f970, - 0x01f973, 0x01f976, + 0x01f900, 0x01f976, 0x01f97a, 0x01f9a2, - 0x01f9b0, 0x01f9b9, - 0x01f9c0, 0x01f9c2, - 0x01f9d0, 0x01f9ff, + 0x01f9a5, 0x01f9aa, + 0x01f9ae, 0x01f9ca, + 0x01f9cd, 0x01fa53, 0x01fa60, 0x01fa6d, + 0x01fa70, 0x01fa73, + 0x01fa78, 0x01fa7a, + 0x01fa80, 0x01fa82, + 0x01fa90, 0x01fa95, 0x020000, 0x02a6d6, 0x02a700, 0x02b734, 0x02b740, 0x02b81d, @@ -676,8 +684,8 @@ var isNotPrint32 = []uint16{ // add 0x10000 to each entry 0xeeaa, 0xf0c0, 0xf0d0, - 0xf93f, - 0xf97b, + 0xf90c, + 0xf972, } // isGraphic lists the graphic runes not matched by IsPrint. |