-
Notifications
You must be signed in to change notification settings - Fork 0
/
Num.go
182 lines (160 loc) · 4.1 KB
/
Num.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package gohelper
import (
"fmt"
"math"
"math/rand"
"reflect"
"regexp"
"strconv"
"time"
"unicode"
)
// RandInt generates a random integer within the specified range.
//
// min: the minimum value of the range
// max: the maximum value of the range
// int: the random integer within the specified range
func RandInt(min int, max int) int {
if max-min <= 0 {
panic("invalid range: max must be greater than min")
}
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
return min + r.Intn(max-min)
}
// IsDigit checks if a string contains only digits.
//
// s: the string to be checked
// bool: returns true if the string contains only digits, false otherwise.
func IsDigit(s string) bool {
for _, c := range s {
if !unicode.IsDigit(c) {
return false
}
}
return true
}
// IsNumber checks if the input string is a number.
//
// s: the string to be checked
// bool: returns true if the string is a number, false otherwise.
func IsNumber(s string) bool {
re := regexp.MustCompile(`^-?\d+(\.\d+)?$`)
return re.MatchString(s)
}
// IsFloat checks if a string can be parsed into a float64.
//
// s: the string to be checked
// bool: returns true if the string can be parsed into a float64, false otherwise.
func IsFloat(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}
// IsInteger checks if the given value is an integer.
//
// T is the type of the value.
// value is the value to check.
// bool is the return type indicating if the value is an integer.
func IsInteger[T any](value T) bool {
var v any = value
switch v.(type) {
case int, int8, int16, int32, int64:
return true
case uint, uint8, uint16, uint32, uint64:
return true
case float32, float64:
return math.Remainder(reflect.ValueOf(v).Float(), 1) == 0
case bool:
return true
case string:
f, err := strconv.ParseFloat(v.(string), 64)
if err != nil {
return false
}
return math.Remainder(f, 1) == 0
default:
panic(fmt.Sprintf("unhandled type: %T", value))
}
}
// IsNatural checks if the given value is a natural number.
//
// T is the type of the value.
// value is the value to check.
// bool is the return type indicating if the value is a natural number.
func IsNatural[T any](value T) bool {
return IsInteger(value) && IsPositive(value)
}
// IsPositive checks if the given value is positive.
//
// T is the type of the value.
// value is the value to check.
// positive is the return type indicating if the value is positive.
// It returns a boolean value.
func IsPositive[T any](value T) (positive bool) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("%v", r)
}
panic(fmt.Sprintf("IsPositive panicked: %v", err))
}
}()
var v any = value
switch v.(type) {
case int, int8, int16, int32, int64:
positive = v.(int) >= 0
case uint, uint8, uint16, uint32, uint64:
positive = true
case float32, float64:
positive = math.Remainder(reflect.ValueOf(v).Float(), 1) == 0
case bool:
positive = If(v.(bool), true, false)
case string:
f, err := strconv.ParseFloat(v.(string), 64)
if err != nil {
return false
}
_, frac := math.Modf(f)
positive = frac == 0
default:
panic(fmt.Sprintf("unhandled type: %T", value))
}
return
}
// IsNegative checks if the given value is negative.
//
// T is the type of the value.
// value is the value to check.
// negative is the return type indicating if the value is negative.
// It returns a boolean value.
func IsNegative[T any](value T) (negative bool) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(error)
if !ok {
err = fmt.Errorf("%v", r)
}
panic(fmt.Sprintf("IsNegative panicked: %v", err))
}
}()
var v any = value
switch v.(type) {
case int, int8, int16, int32, int64:
negative = v.(int) >= 0
case uint, uint8, uint16, uint32, uint64:
negative = true
case float32, float64:
negative = math.Remainder(reflect.ValueOf(v).Float(), 1) == 0
case string:
f, err := strconv.ParseFloat(v.(string), 64)
if err != nil {
return false
}
_, frac := math.Modf(f)
negative = frac == 0
default:
panic(fmt.Sprintf("unhandled type: %T", value))
}
return
}