Skip to content

Commit

Permalink
fix the integer string check
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Jul 12, 2023
1 parent 335363e commit ba25d37
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 13 additions & 2 deletions cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit ba25d37

Please sign in to comment.