diff --git a/number.go b/number.go index a58dc4d..4f709fa 100644 --- a/number.go +++ b/number.go @@ -414,7 +414,7 @@ func parseInt[T integer](s string) (T, error) { } func parseUint[T unsigned](s string) (T, error) { - v, err := strconv.ParseUint(strings.TrimLeft(trimDecimal(s), "+"), 0, 0) + v, err := strconv.ParseUint(strings.TrimPrefix(trimDecimal(s), "+"), 0, 0) if err != nil { return 0, err } @@ -529,6 +529,10 @@ func trimDecimal(s string) string { return s } + if s == "." || s == "+." || s == "-." { + return s + } + matches := stringNumberRe.FindStringSubmatch(s) if matches != nil { // matches[1] is the captured integer part with sign diff --git a/number_internal_test.go b/number_internal_test.go index 6d333d0..abda2f2 100644 --- a/number_internal_test.go +++ b/number_internal_test.go @@ -69,6 +69,9 @@ func TestTrimDecimal(t *testing.T) { {"10.foobar", "10.foobar"}, {"10.0i", "10.0i"}, {"10.0E9", "10.0E9"}, + {".", "."}, + {"+.", "+."}, + {"-.", "-."}, } for _, testCase := range testCases { diff --git a/number_test.go b/number_test.go index 9d84d18..933cfc9 100644 --- a/number_test.go +++ b/number_test.go @@ -464,3 +464,15 @@ func BenchmarkNumber(b *testing.B) { }) } } + +func TestInvalidNumberStrings(t *testing.T) { + c := qt.New(t) + + for _, s := range []string{".", "-.", "+.", "++5", "+-5"} { + _, err := cast.ToIntE(s) + c.Assert(err, qt.IsNotNil, qt.Commentf("ToIntE(%q)", s)) + + _, err = cast.ToUintE(s) + c.Assert(err, qt.IsNotNil, qt.Commentf("ToUintE(%q)", s)) + } +}