diff options
author | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-01-12 01:31:45 +0000 |
---|---|---|
committer | ian <ian@138bc75d-0d04-0410-961f-82ee72b054a4> | 2012-01-12 01:31:45 +0000 |
commit | 26ae0101ebafafea33beadccea3056f4b5e135e8 (patch) | |
tree | 86a3b8019380d5fad53258c4baba3dd9e1e7c736 /libgo/go/strconv/atoi.go | |
parent | c8142b20a95ae9c6bd0bac8c5a545b27402836fe (diff) | |
download | gcc-26ae0101ebafafea33beadccea3056f4b5e135e8.tar.gz |
libgo: Update to weekly.2011-12-14.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@183118 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libgo/go/strconv/atoi.go')
-rw-r--r-- | libgo/go/strconv/atoi.go | 36 |
1 files changed, 25 insertions, 11 deletions
diff --git a/libgo/go/strconv/atoi.go b/libgo/go/strconv/atoi.go index 438d496948d..59ef264d17c 100644 --- a/libgo/go/strconv/atoi.go +++ b/libgo/go/strconv/atoi.go @@ -14,11 +14,22 @@ var ErrSyntax = errors.New("invalid syntax") // A NumError records a failed conversion. type NumError struct { - Num string // the input - Err error // the reason the conversion failed (ErrRange, ErrSyntax) + Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat) + Num string // the input + Err error // the reason the conversion failed (ErrRange, ErrSyntax) } -func (e *NumError) Error() string { return `parsing "` + e.Num + `": ` + e.Err.Error() } +func (e *NumError) Error() string { + return "strconv." + e.Func + ": " + `parsing "` + e.Num + `": ` + e.Err.Error() +} + +func syntaxError(fn, str string) *NumError { + return &NumError{fn, str, ErrSyntax} +} + +func rangeError(fn, str string) *NumError { + return &NumError{fn, str, ErrRange} +} const intSize = 32 << uint(^uint(0)>>63) @@ -116,13 +127,13 @@ func ParseUint(s string, b int, bitSize int) (n uint64, err error) { return n, nil Error: - return n, &NumError{s0, err} + return n, &NumError{"ParseUint", s0, err} } -// ParseInt interprets a string s in an arbitrary base b (2 to 36) -// and returns the corresponding value n. If b == 0, the base -// is taken from the string prefix: base 16 for "0x", base 8 for "0", -// and base 10 otherwise. +// 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. // // The bitSize argument specifies the integer type // that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 @@ -134,13 +145,15 @@ Error: // to s cannot be represented by a signed integer of the // given size, err.Error = ErrRange. func ParseInt(s string, base int, bitSize int) (i int64, err error) { + const fnParseInt = "ParseInt" + if bitSize == 0 { bitSize = int(IntSize) } // Empty string bad. if len(s) == 0 { - return 0, &NumError{s, ErrSyntax} + return 0, syntaxError(fnParseInt, s) } // Pick off leading sign. @@ -157,15 +170,16 @@ func ParseInt(s string, base int, bitSize int) (i int64, err error) { var un uint64 un, err = ParseUint(s, base, bitSize) if err != nil && err.(*NumError).Err != ErrRange { + err.(*NumError).Func = fnParseInt err.(*NumError).Num = s0 return 0, err } cutoff := uint64(1 << uint(bitSize-1)) if !neg && un >= cutoff { - return int64(cutoff - 1), &NumError{s0, ErrRange} + return int64(cutoff - 1), rangeError(fnParseInt, s0) } if neg && un > cutoff { - return -int64(cutoff), &NumError{s0, ErrRange} + return -int64(cutoff), rangeError(fnParseInt, s0) } n := int64(un) if neg { |