Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrix-X committed Aug 19, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent ccbbf64 commit 5d62ae4
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions object/number.go
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
}

0 comments on commit 5d62ae4

Please sign in to comment.