// 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)
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
// 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.
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 {