Skip to content

Commit

Permalink
golint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Mar 5, 2020
1 parent be6360a commit 5f7828c
Showing 1 changed file with 5 additions and 53 deletions.
58 changes: 5 additions & 53 deletions util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math/rand"
"reflect"
"strconv"
"strings"
"time"
)

Expand All @@ -29,57 +28,10 @@ const (
RESET = "\033[0m"
)

// IsNumeric is_numeric()
// Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part.
// Thus +0123.45e6 is a valid numeric value.
func IsNumeric(val interface{}) bool {
switch val.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return true
case float32, float64, complex64, complex128:
return true
case string:
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
// IsNumber 判断字符串是否为数字
func IsNumber(input string) bool {
_, err := strconv.Atoi(input)
return err == nil
}

// RandString 随机字符串
Expand Down Expand Up @@ -131,7 +83,7 @@ func LoopInput(tip string, choices interface{}, print bool) int {
}
if inputString == "" {
return -1
} else if !IsNumeric(inputString) {
} else if !IsNumber(inputString) {
fmt.Println("输入有误,请重新输入")
continue
}
Expand Down

0 comments on commit 5f7828c

Please sign in to comment.