2010-12-03 05:34:57 +01:00
|
|
|
// 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
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
import "errors"
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-02 20:34:41 +01:00
|
|
|
// ErrRange indicates that a value is out of range for the target type.
|
2011-12-03 03:17:34 +01:00
|
|
|
var ErrRange = errors.New("value out of range")
|
2011-12-02 20:34:41 +01:00
|
|
|
|
|
|
|
// ErrSyntax indicates that a value does not have the right syntax for the target type.
|
2011-12-03 03:17:34 +01:00
|
|
|
var ErrSyntax = errors.New("invalid syntax")
|
2011-12-02 20:34:41 +01:00
|
|
|
|
|
|
|
// A NumError records a failed conversion.
|
2010-12-03 05:34:57 +01:00
|
|
|
type NumError struct {
|
2012-01-12 02:31:45 +01:00
|
|
|
Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat)
|
|
|
|
Num string // the input
|
|
|
|
Err error // the reason the conversion failed (ErrRange, ErrSyntax)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 02:31:45 +01:00
|
|
|
func (e *NumError) Error() string {
|
2013-07-16 08:54:42 +02:00
|
|
|
return "strconv." + e.Func + ": " + "parsing " + Quote(e.Num) + ": " + e.Err.Error()
|
2012-01-12 02:31:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func syntaxError(fn, str string) *NumError {
|
|
|
|
return &NumError{fn, str, ErrSyntax}
|
|
|
|
}
|
|
|
|
|
|
|
|
func rangeError(fn, str string) *NumError {
|
|
|
|
return &NumError{fn, str, ErrRange}
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
const intSize = 32 << uint(^uint(0)>>63)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2013-11-06 20:49:01 +01:00
|
|
|
// IntSize is the size in bits of an int or uint value.
|
|
|
|
const IntSize = intSize
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Return the first number n such that n*base >= 1<<64.
|
|
|
|
func cutoff64(base int) uint64 {
|
|
|
|
if base < 2 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return (1<<64-1)/uint64(base) + 1
|
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// ParseUint is like ParseInt but for unsigned numbers.
|
2012-10-03 07:27:36 +02:00
|
|
|
func ParseUint(s string, base int, bitSize int) (n uint64, err error) {
|
2011-12-14 16:41:54 +01:00
|
|
|
var cutoff, maxVal uint64
|
|
|
|
|
|
|
|
if bitSize == 0 {
|
|
|
|
bitSize = int(IntSize)
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
s0 := s
|
|
|
|
switch {
|
|
|
|
case len(s) < 1:
|
2011-12-02 20:34:41 +01:00
|
|
|
err = ErrSyntax
|
2010-12-03 05:34:57 +01:00
|
|
|
goto Error
|
|
|
|
|
2012-10-03 07:27:36 +02:00
|
|
|
case 2 <= base && base <= 36:
|
2010-12-03 05:34:57 +01:00
|
|
|
// valid base; nothing to do
|
|
|
|
|
2012-10-03 07:27:36 +02:00
|
|
|
case base == 0:
|
2010-12-03 05:34:57 +01:00
|
|
|
// Look for octal, hex prefix.
|
|
|
|
switch {
|
|
|
|
case s[0] == '0' && len(s) > 1 && (s[1] == 'x' || s[1] == 'X'):
|
2012-10-03 07:27:36 +02:00
|
|
|
base = 16
|
2010-12-03 05:34:57 +01:00
|
|
|
s = s[2:]
|
|
|
|
if len(s) < 1 {
|
2011-12-02 20:34:41 +01:00
|
|
|
err = ErrSyntax
|
2010-12-03 05:34:57 +01:00
|
|
|
goto Error
|
|
|
|
}
|
|
|
|
case s[0] == '0':
|
2012-10-03 07:27:36 +02:00
|
|
|
base = 8
|
2010-12-03 05:34:57 +01:00
|
|
|
default:
|
2012-10-03 07:27:36 +02:00
|
|
|
base = 10
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2012-10-03 07:27:36 +02:00
|
|
|
err = errors.New("invalid base " + Itoa(base))
|
2010-12-03 05:34:57 +01:00
|
|
|
goto Error
|
|
|
|
}
|
|
|
|
|
|
|
|
n = 0
|
2012-10-03 07:27:36 +02:00
|
|
|
cutoff = cutoff64(base)
|
2011-12-14 16:41:54 +01:00
|
|
|
maxVal = 1<<uint(bitSize) - 1
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
var v byte
|
|
|
|
d := s[i]
|
|
|
|
switch {
|
|
|
|
case '0' <= d && d <= '9':
|
|
|
|
v = d - '0'
|
|
|
|
case 'a' <= d && d <= 'z':
|
|
|
|
v = d - 'a' + 10
|
|
|
|
case 'A' <= d && d <= 'Z':
|
|
|
|
v = d - 'A' + 10
|
|
|
|
default:
|
|
|
|
n = 0
|
2011-12-02 20:34:41 +01:00
|
|
|
err = ErrSyntax
|
2010-12-03 05:34:57 +01:00
|
|
|
goto Error
|
|
|
|
}
|
2012-10-03 07:27:36 +02:00
|
|
|
if int(v) >= base {
|
2010-12-03 05:34:57 +01:00
|
|
|
n = 0
|
2011-12-02 20:34:41 +01:00
|
|
|
err = ErrSyntax
|
2010-12-03 05:34:57 +01:00
|
|
|
goto Error
|
|
|
|
}
|
|
|
|
|
|
|
|
if n >= cutoff {
|
2012-10-03 07:27:36 +02:00
|
|
|
// n*base overflows
|
2010-12-03 05:34:57 +01:00
|
|
|
n = 1<<64 - 1
|
2011-12-02 20:34:41 +01:00
|
|
|
err = ErrRange
|
2010-12-03 05:34:57 +01:00
|
|
|
goto Error
|
|
|
|
}
|
2012-10-03 07:27:36 +02:00
|
|
|
n *= uint64(base)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
n1 := n + uint64(v)
|
2011-12-14 16:41:54 +01:00
|
|
|
if n1 < n || n1 > maxVal {
|
2010-12-03 05:34:57 +01:00
|
|
|
// n+v overflows
|
|
|
|
n = 1<<64 - 1
|
2011-12-02 20:34:41 +01:00
|
|
|
err = ErrRange
|
2010-12-03 05:34:57 +01:00
|
|
|
goto Error
|
|
|
|
}
|
|
|
|
n = n1
|
|
|
|
}
|
|
|
|
|
|
|
|
return n, nil
|
|
|
|
|
|
|
|
Error:
|
2012-01-12 02:31:45 +01:00
|
|
|
return n, &NumError{"ParseUint", s0, err}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-01-12 02:31:45 +01:00
|
|
|
// ParseInt interprets a string s in the given base (2 to 36) and
|
|
|
|
// returns the corresponding value i. If base == 0, the base is
|
|
|
|
// implied by the string's prefix: base 16 for "0x", base 8 for
|
|
|
|
// "0", and base 10 otherwise.
|
2010-12-03 05:34:57 +01:00
|
|
|
//
|
2011-12-14 16:41:54 +01:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// The errors that ParseInt returns have concrete type *NumError
|
|
|
|
// and include err.Num = s. If s is empty or contains invalid
|
2014-07-19 10:53:52 +02:00
|
|
|
// 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.
|
2011-12-14 16:41:54 +01:00
|
|
|
func ParseInt(s string, base int, bitSize int) (i int64, err error) {
|
2012-01-12 02:31:45 +01:00
|
|
|
const fnParseInt = "ParseInt"
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
if bitSize == 0 {
|
|
|
|
bitSize = int(IntSize)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Empty string bad.
|
|
|
|
if len(s) == 0 {
|
2012-01-12 02:31:45 +01:00
|
|
|
return 0, syntaxError(fnParseInt, s)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2011-12-14 16:41:54 +01:00
|
|
|
un, err = ParseUint(s, base, bitSize)
|
2011-12-03 03:17:34 +01:00
|
|
|
if err != nil && err.(*NumError).Err != ErrRange {
|
2012-01-12 02:31:45 +01:00
|
|
|
err.(*NumError).Func = fnParseInt
|
2010-12-03 05:34:57 +01:00
|
|
|
err.(*NumError).Num = s0
|
|
|
|
return 0, err
|
|
|
|
}
|
2011-12-14 16:41:54 +01:00
|
|
|
cutoff := uint64(1 << uint(bitSize-1))
|
|
|
|
if !neg && un >= cutoff {
|
2012-01-12 02:31:45 +01:00
|
|
|
return int64(cutoff - 1), rangeError(fnParseInt, s0)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-12-14 16:41:54 +01:00
|
|
|
if neg && un > cutoff {
|
2012-01-12 02:31:45 +01:00
|
|
|
return -int64(cutoff), rangeError(fnParseInt, s0)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
n := int64(un)
|
|
|
|
if neg {
|
|
|
|
n = -n
|
|
|
|
}
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2011-12-14 16:41:54 +01:00
|
|
|
// Atoi is shorthand for ParseInt(s, 10, 0).
|
2011-12-03 03:17:34 +01:00
|
|
|
func Atoi(s string) (i int, err error) {
|
2011-12-14 16:41:54 +01:00
|
|
|
i64, err := ParseInt(s, 10, 0)
|
|
|
|
return int(i64), err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|