-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnum.go
86 lines (77 loc) · 1.56 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
package kutils
import (
"fmt"
"strconv"
"golang.org/x/exp/constraints"
)
func Itoa[T constraints.Signed](i T) string {
return strconv.FormatInt(int64(i), 10)
}
func Utoa[T constraints.Unsigned](u T) string {
return strconv.FormatUint(uint64(u), 10)
}
func Atoi[T constraints.Signed](a string) (i T, err error) {
bitSize := 0
switch any(i).(type) {
case int:
bitSize = strconv.IntSize
case int8:
bitSize = 8
case int16:
bitSize = 16
case int32:
bitSize = 32
case int64:
bitSize = 64
}
if i, err := strconv.ParseInt(a, 10, bitSize); err != nil {
return 0, err
} else if bitSize == 0 && int64(T(i)) != i {
return 0, &strconv.NumError{Func: "ParseInt", Num: a, Err: strconv.ErrRange}
} else {
return T(i), nil
}
}
func Atou[T constraints.Unsigned](a string) (u T, err error) {
bitSize := 0
switch any(u).(type) {
case uint, uintptr:
bitSize = strconv.IntSize
case uint8:
bitSize = 8
case uint16:
bitSize = 16
case uint32:
bitSize = 32
case uint64:
bitSize = 64
}
if u, err := strconv.ParseUint(a, 10, bitSize); err != nil {
return 0, err
} else if bitSize == 0 && uint64(T(u)) != u {
return 0, &strconv.NumError{Func: "ParseUint", Num: a, Err: strconv.ErrRange}
} else {
return T(u), nil
}
}
func Min[T constraints.Ordered](a, b T) T {
if b < a {
return b
}
return a
}
func Max[T constraints.Ordered](a, b T) T {
if b > a {
return b
}
return a
}
func Abs[T constraints.Signed | constraints.Float](a T) T {
if a < 0 {
if a-1 > 0 {
panic(fmt.Sprintf("Abs result of %v will overflow", a))
}
return -a
}
return a
}