-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Matrix-X
committed
Aug 19, 2021
1 parent
ccbbf64
commit 5d62ae4
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package object | ||
|
||
import "strings" | ||
|
||
func IsNumeric(val interface{}, trueNumber bool) bool { | ||
switch val.(type) { | ||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: | ||
case float32, float64, complex64, complex128: | ||
return true | ||
case string: | ||
|
||
if trueNumber{ | ||
return false | ||
} | ||
|
||
str := val.(string) | ||
if str == "" { | ||
return false | ||
} | ||
// Trim any whitespace | ||
str = strings.TrimSpace(str) | ||
if str[0] == '-' || str[0] == '+' { | ||
if len(str) == 1 { | ||
return false | ||
} | ||
str = str[1:] } | ||
// hex | ||
if len(str) > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X') { | ||
for _, h := range str[2:] { | ||
if !((h >= '0' && h <= '9') || (h >= 'a' && h <= 'f') || (h >= 'A' && h <= 'F')) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
// 0-9,Point,Scientific | ||
p, s, l := 0, 0, len(str) | ||
for i, v := range str { | ||
if v == '.' { // Point | ||
if p > 0 || s > 0 || i+1 == l { | ||
return false | ||
} | ||
p = i | ||
} else if v == 'e' || v == 'E' { // Scientific | ||
if i == 0 || s > 0 || i+1 == l { | ||
return false | ||
} | ||
s = i | ||
} else if v < '0' || v > '9' { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
return false | ||
} |