forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
93 lines (81 loc) · 2.44 KB
/
check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package mathutil
import "github.com/gookit/goutil/comdef"
// IsNumeric returns true if the given character is a numeric, otherwise false.
func IsNumeric(c byte) bool {
return c >= '0' && c <= '9'
}
// Compare any intX,floatX value by given op. returns `first op(=,!=,<,<=,>,>=) second`
//
// Usage:
//
// mathutil.Compare(2, 3, ">") // false
// mathutil.Compare(2, 1.3, ">") // true
// mathutil.Compare(2.2, 1.3, ">") // true
// mathutil.Compare(2.1, 2, ">") // true
func Compare(first, second any, op string) bool {
if first == nil || second == nil {
return false
}
switch fVal := first.(type) {
case float64:
if sVal, err := ToFloat(second); err == nil {
return CompFloat(fVal, sVal, op)
}
case float32:
if sVal, err := ToFloat(second); err == nil {
return CompFloat(float64(fVal), sVal, op)
}
default: // as int64
if int1, err := ToInt64(first); err == nil {
if int2, err := ToInt64(second); err == nil {
return CompInt64(int1, int2, op)
}
}
}
return false
}
// CompInt compare all intX,uintX type value. returns `first op(=,!=,<,<=,>,>=) second`
func CompInt[T comdef.Xint](first, second T, op string) (ok bool) {
return CompValue(first, second, op)
}
// CompInt64 compare int64 value. returns `first op(=,!=,<,<=,>,>=) second`
func CompInt64(first, second int64, op string) bool {
return CompValue(first, second, op)
}
// CompFloat compare float64,float32 value. returns `first op(=,!=,<,<=,>,>=) second`
func CompFloat[T comdef.Float](first, second T, op string) (ok bool) {
return CompValue(first, second, op)
}
// CompValue compare intX,uintX,floatX value. returns `first op(=,!=,<,<=,>,>=) second`
func CompValue[T comdef.XintOrFloat](first, second T, op string) (ok bool) {
switch op {
case "<", "lt":
ok = first < second
case "<=", "lte":
ok = first <= second
case ">", "gt":
ok = first > second
case ">=", "gte":
ok = first >= second
case "=", "eq":
ok = first == second
case "!=", "ne", "neq":
ok = first != second
}
return
}
// InRange check if val in int/float range [min, max]
func InRange[T comdef.IntOrFloat](val, min, max T) bool {
return val >= min && val <= max
}
// OutRange check if val not in int/float range [min, max]
func OutRange[T comdef.IntOrFloat](val, min, max T) bool {
return val < min || val > max
}
// InUintRange check if val in unit range [min, max]
func InUintRange[T comdef.Uint](val, min, max T) bool {
if max == 0 {
return val >= min
}
return val >= min && val <= max
}