core/util/util.go

41 lines
754 B
Go
Raw Normal View History

2023-11-02 22:04:28 +00:00
package util
import (
"strconv"
"strings"
)
2023-11-03 01:03:11 +00:00
func PrependToSliceStr(strs *[]string, prefix string) {
2023-11-02 22:04:28 +00:00
for i, elm := range *strs {
(*strs)[i] = prefix + elm
}
}
2023-11-03 01:03:11 +00:00
func AppendToSliceStr(strs *[]string, suffix string) {
2023-11-02 22:04:28 +00:00
for i, elm := range *strs {
(*strs)[i] = elm + suffix
}
}
2023-11-03 01:03:11 +00:00
func Str2int64(str string, base int, fallback int64) int64 {
2023-11-02 22:04:28 +00:00
str = strings.TrimSuffix(str, "\n")
num, err := strconv.ParseInt(str, base, 64)
if err != nil {
num = fallback
}
return num
}
2023-11-03 01:03:11 +00:00
func Str2int(str string, base int, fallback int) int {
2023-11-03 01:09:33 +00:00
return int(Str2int64(str, base, int64(fallback)))
2023-11-02 22:04:28 +00:00
}
2023-11-03 01:03:11 +00:00
func CastStrings2ints(strs *[]string) []int {
2023-11-02 22:04:28 +00:00
a := make([]int, len(*strs))
for i, elm := range *strs {
2023-11-03 01:09:33 +00:00
var f int = Str2int(elm, 10, 0)
2023-11-02 22:04:28 +00:00
a[i] = f
}
return a
}