2023-11-02 22:04:28 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2023-11-04 16:33:24 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"math"
|
|
|
|
"path"
|
2023-11-06 22:43:10 +00:00
|
|
|
"reflect"
|
2023-11-02 22:04:28 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2023-11-04 16:33:24 +00:00
|
|
|
|
|
|
|
"golang.org/x/text/cases"
|
|
|
|
"golang.org/x/text/language"
|
2023-11-02 22:04:28 +00:00
|
|
|
)
|
|
|
|
|
2023-11-02 23:36:07 +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-02 23:36:07 +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-02 23:36:07 +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-02 23:36:07 +00:00
|
|
|
func Str2int(str string, base int, fallback int) int {
|
|
|
|
return int(Str2int64(str, base, int64(fallback)))
|
2023-11-02 22:04:28 +00:00
|
|
|
}
|
|
|
|
|
2023-11-02 23:36:07 +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-02 23:36:07 +00:00
|
|
|
var f int = Str2int(elm, 10, 0)
|
2023-11-02 22:04:28 +00:00
|
|
|
a[i] = f
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
2023-11-04 16:33:24 +00:00
|
|
|
|
|
|
|
func PrettyByteSize(b int64) string {
|
|
|
|
bf := float64(b)
|
2023-11-19 23:15:38 +00:00
|
|
|
for _, unit := range []string{"", "Kio", "Mio", "Gio", "Tio", "Pio", "Eio", "Zio"} {
|
2023-11-04 16:33:24 +00:00
|
|
|
if math.Abs(bf) < 1024.0 {
|
|
|
|
return fmt.Sprintf("%3.2f%s", bf, unit)
|
|
|
|
}
|
|
|
|
bf /= 1024.0
|
|
|
|
}
|
2023-11-19 23:15:38 +00:00
|
|
|
return fmt.Sprintf("%.2fYio", bf)
|
2023-11-04 16:33:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Ucfirst(str *string) {
|
|
|
|
caser := cases.Title(language.English)
|
|
|
|
*str = caser.String(*str)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveExtension(fileName string) string {
|
|
|
|
return strings.TrimSuffix(fileName, path.Ext(fileName))
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetBytesAsHex(key []byte) []byte {
|
|
|
|
dst := make([]byte, hex.EncodedLen(len(key)))
|
|
|
|
hex.Encode(dst, key)
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetBytesFromHex(key []byte) []byte {
|
|
|
|
dst := make([]byte, hex.DecodedLen(len(key)))
|
|
|
|
n, err := hex.Decode(dst, key)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return dst[:n]
|
|
|
|
}
|
|
|
|
|
|
|
|
func Escape(str string, c rune) string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, char := range str {
|
|
|
|
switch char {
|
|
|
|
case c:
|
|
|
|
buf.WriteRune('\\')
|
|
|
|
}
|
|
|
|
buf.WriteRune(char)
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func EscapeBt(str string) string {
|
|
|
|
return Escape(str, '`')
|
|
|
|
}
|
|
|
|
|
|
|
|
func EscapeDquote(str string) string {
|
|
|
|
return Escape(str, '"')
|
|
|
|
}
|
|
|
|
|
|
|
|
func EscapeSquote(str string) string {
|
|
|
|
return Escape(str, '\'')
|
|
|
|
}
|
2023-11-06 22:43:10 +00:00
|
|
|
|
|
|
|
func InspectStruct(a any) (map[string]string, bool) {
|
|
|
|
done := false
|
|
|
|
rs := make(map[string]string)
|
|
|
|
n := reflect.ValueOf(a)
|
|
|
|
for {
|
|
|
|
if n.Kind() == reflect.Ptr {
|
|
|
|
n = n.Elem()
|
|
|
|
} else if n.Kind() == reflect.Struct {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
return rs, done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i := 0; i < n.NumField(); i++ {
|
|
|
|
rs[n.Type().Field(i).Name] = n.Field(i).Type().String()
|
|
|
|
}
|
|
|
|
done = true
|
|
|
|
return rs, done
|
|
|
|
}
|
2023-11-10 20:01:31 +00:00
|
|
|
|
|
|
|
func InArray(v interface{}, arr interface{}) int {
|
|
|
|
values := reflect.ValueOf(arr)
|
|
|
|
if reflect.TypeOf(arr).Kind() == reflect.Slice || values.Len() > 0 {
|
|
|
|
for i := 0; i < values.Len(); i++ {
|
|
|
|
if reflect.DeepEqual(v, values.Index(i).Interface()) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|