{"1e", "0", ErrSyntax},
{"1e-", "0", ErrSyntax},
{".e-1", "0", ErrSyntax},
+ {"1\x00.2", "0", ErrSyntax},
// http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
{"2.2250738585072012e-308", "2.2250738585072014e-308", nil},
}
func (e *NumError) Error() string {
- return "strconv." + e.Func + ": " + `parsing "` + e.Num + `": ` + e.Err.Error()
+ return "strconv." + e.Func + ": " + "parsing " + Quote(e.Num) + ": " + e.Err.Error()
}
func syntaxError(fn, str string) *NumError {
package strconv_test
import (
+ "errors"
"reflect"
. "strconv"
"testing"
{"-2147483649", -1 << 31, ErrRange},
}
+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 atoi routines return NumErrors wrapping
// the error and the string. Convert the tables above.
}
}
+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 BenchmarkAtoi(b *testing.B) {
for i := 0; i < b.N; i++ {
ParseInt("12345678", 10, 0)