-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers.go
63 lines (60 loc) · 1.14 KB
/
numbers.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
package strings
import (
"golang.org/x/exp/constraints"
"strconv"
"strings"
"unicode"
)
// ExtractNumbers returns a string with the digits contained in the given string.
func ExtractNumbers(in string) string {
return strings.Map(func(r rune) rune {
if unicode.IsNumber(r) {
return r
}
return -1
}, in)
}
// AtoI is a convenience script for converting a string to various types of signed integers.
// An invalid input will return zero, including if the input overflows the max size of the integer type.
func AtoI[T constraints.Integer](s string) T {
var t T
signed := true
bitSize := 0
switch any(t).(type) {
case uint:
signed = false
case uint8:
signed = false
bitSize = 8
case uint16:
signed = false
bitSize = 16
case uint32:
signed = false
bitSize = 32
case uint64:
signed = false
bitSize = 64
case int8:
bitSize = 8
case int16:
bitSize = 16
case int32:
bitSize = 32
case int64:
bitSize = 64
}
if signed {
v, err := strconv.ParseInt(s, 10, bitSize)
if err != nil {
return 0
}
return T(v)
} else {
v, err := strconv.ParseUint(s, 10, 0)
if err != nil {
return 0
}
return T(v)
}
}