From ba25d37d7a2f16d1497677558c4bcb1ad874ba36 Mon Sep 17 00:00:00 2001 From: xgfone Date: Wed, 12 Jul 2023 17:11:11 +0800 Subject: [PATCH] fix the integer string check --- cast.go | 15 +++++++++++++-- cast_test.go | 4 ++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cast.go b/cast.go index 037f112..f244ef8 100644 --- a/cast.go +++ b/cast.go @@ -889,9 +889,20 @@ func tryReflectToTimeInLocation(src reflect.Value, loc *time.Location, } func isIntegerString(s string) bool { - for i, _len := 0, len(s); i < _len; i++ { + _len := len(s) + if _len == 0 { + return false + } + + switch s[0] { + case '-', '+': + s = s[1:] + _len-- + } + + for i := 0; i < _len; i++ { switch s[i] { - case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-': + case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': default: return false } diff --git a/cast_test.go b/cast_test.go index b42d649..2572f9b 100644 --- a/cast_test.go +++ b/cast_test.go @@ -242,4 +242,8 @@ func TestIsIntegerString(t *testing.T) { } else if v != -1000 { t.Errorf("expect %d, but got %d", -1000, v) } + + if isIntegerString("2006-01-02") { + t.Error("2006-01-02: expect false, but got true") + } }