2023-10-26 00:03:43 +08:00
|
|
|
package strutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strconv"
|
2023-12-13 12:22:41 +08:00
|
|
|
"strings"
|
2023-10-26 00:03:43 +08:00
|
|
|
sliceutil "tinyrdm/backend/utils/slice"
|
|
|
|
)
|
|
|
|
|
2023-11-21 20:06:28 +08:00
|
|
|
func AnyToString(value interface{}, prefix string, layer int) (s string) {
|
2023-10-26 00:03:43 +08:00
|
|
|
if value == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch value.(type) {
|
|
|
|
case float64:
|
|
|
|
ft := value.(float64)
|
|
|
|
s = strconv.FormatFloat(ft, 'f', -1, 64)
|
|
|
|
case float32:
|
|
|
|
ft := value.(float32)
|
|
|
|
s = strconv.FormatFloat(float64(ft), 'f', -1, 64)
|
|
|
|
case int:
|
|
|
|
it := value.(int)
|
|
|
|
s = strconv.Itoa(it)
|
|
|
|
case uint:
|
|
|
|
it := value.(uint)
|
|
|
|
s = strconv.Itoa(int(it))
|
|
|
|
case int8:
|
|
|
|
it := value.(int8)
|
|
|
|
s = strconv.Itoa(int(it))
|
|
|
|
case uint8:
|
|
|
|
it := value.(uint8)
|
|
|
|
s = strconv.Itoa(int(it))
|
|
|
|
case int16:
|
|
|
|
it := value.(int16)
|
|
|
|
s = strconv.Itoa(int(it))
|
|
|
|
case uint16:
|
|
|
|
it := value.(uint16)
|
|
|
|
s = strconv.Itoa(int(it))
|
|
|
|
case int32:
|
|
|
|
it := value.(int32)
|
|
|
|
s = strconv.Itoa(int(it))
|
|
|
|
case uint32:
|
|
|
|
it := value.(uint32)
|
|
|
|
s = strconv.Itoa(int(it))
|
|
|
|
case int64:
|
|
|
|
it := value.(int64)
|
|
|
|
s = strconv.FormatInt(it, 10)
|
|
|
|
case uint64:
|
|
|
|
it := value.(uint64)
|
|
|
|
s = strconv.FormatUint(it, 10)
|
|
|
|
case string:
|
2023-11-21 20:06:28 +08:00
|
|
|
if layer > 0 {
|
|
|
|
s = "\"" + value.(string) + "\""
|
|
|
|
} else {
|
|
|
|
s = value.(string)
|
|
|
|
}
|
2023-10-26 00:03:43 +08:00
|
|
|
case bool:
|
|
|
|
val, _ := value.(bool)
|
|
|
|
if val {
|
|
|
|
s = "True"
|
|
|
|
} else {
|
|
|
|
s = "False"
|
|
|
|
}
|
|
|
|
case []byte:
|
2023-11-21 20:06:28 +08:00
|
|
|
s = prefix + string(value.([]byte))
|
2023-10-26 00:03:43 +08:00
|
|
|
case []string:
|
|
|
|
ss := value.([]string)
|
|
|
|
anyStr := sliceutil.Map(ss, func(i int) string {
|
2023-11-21 20:06:28 +08:00
|
|
|
str := AnyToString(ss[i], prefix, layer+1)
|
|
|
|
return prefix + strconv.Itoa(i+1) + ") " + str
|
2023-10-26 00:03:43 +08:00
|
|
|
})
|
2023-11-21 20:06:28 +08:00
|
|
|
s = prefix + sliceutil.JoinString(anyStr, "\r\n")
|
2023-10-26 00:03:43 +08:00
|
|
|
case []any:
|
|
|
|
as := value.([]any)
|
|
|
|
anyItems := sliceutil.Map(as, func(i int) string {
|
2023-11-21 20:06:28 +08:00
|
|
|
str := AnyToString(as[i], prefix, layer+1)
|
|
|
|
return prefix + strconv.Itoa(i+1) + ") " + str
|
2023-10-26 00:03:43 +08:00
|
|
|
})
|
|
|
|
s = sliceutil.JoinString(anyItems, "\r\n")
|
2023-11-21 20:06:28 +08:00
|
|
|
case map[any]any:
|
|
|
|
am := value.(map[any]any)
|
|
|
|
var items []string
|
|
|
|
index := 0
|
|
|
|
for k, v := range am {
|
|
|
|
kk := prefix + strconv.Itoa(index+1) + ") " + AnyToString(k, prefix, layer+1)
|
|
|
|
vv := prefix + strconv.Itoa(index+2) + ") " + AnyToString(v, "\t", layer+1)
|
|
|
|
if layer > 0 {
|
|
|
|
indent := layer
|
|
|
|
if index == 0 {
|
|
|
|
indent -= 1
|
|
|
|
}
|
|
|
|
for i := 0; i < indent; i++ {
|
|
|
|
vv = " " + vv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
index += 2
|
|
|
|
items = append(items, kk, vv)
|
|
|
|
}
|
|
|
|
s = sliceutil.JoinString(items, "\r\n")
|
2023-10-26 00:03:43 +08:00
|
|
|
default:
|
|
|
|
b, _ := json.Marshal(value)
|
2023-11-21 20:06:28 +08:00
|
|
|
s = prefix + string(b)
|
2023-10-26 00:03:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//func AnyToHex(val any) (string, bool) {
|
|
|
|
// var src string
|
|
|
|
// switch val.(type) {
|
|
|
|
// case string:
|
|
|
|
// src = val.(string)
|
|
|
|
// case []byte:
|
|
|
|
// src = string(val.([]byte))
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// if len(src) <= 0 {
|
|
|
|
// return "", false
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// var output strings.Builder
|
|
|
|
// for i := range src {
|
|
|
|
// if !utf8.ValidString(src[i : i+1]) {
|
|
|
|
// output.WriteString(fmt.Sprintf("\\x%02x", src[i:i+1]))
|
|
|
|
// } else {
|
|
|
|
// output.WriteString(src[i : i+1])
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return output.String(), true
|
|
|
|
//}
|
2023-12-13 12:22:41 +08:00
|
|
|
|
|
|
|
func SplitCmd(cmd string) []string {
|
2024-04-01 17:14:39 +08:00
|
|
|
var result []string
|
|
|
|
var curStr strings.Builder
|
|
|
|
var preChar int32
|
|
|
|
inQuotes := false
|
|
|
|
|
|
|
|
for _, char := range cmd {
|
|
|
|
if char == '"' && preChar != '\\' {
|
|
|
|
inQuotes = !inQuotes
|
|
|
|
} else if char == ' ' && !inQuotes {
|
|
|
|
if curStr.Len() > 0 {
|
|
|
|
if part, e := strconv.Unquote(`"` + curStr.String() + `"`); e == nil {
|
|
|
|
result = append(result, part)
|
|
|
|
}
|
|
|
|
curStr.Reset()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
curStr.WriteByte(byte(char))
|
2023-12-13 12:22:41 +08:00
|
|
|
}
|
2024-04-01 17:14:39 +08:00
|
|
|
preChar = char
|
|
|
|
}
|
|
|
|
|
|
|
|
if curStr.Len() > 0 {
|
|
|
|
if part, e := strconv.Unquote(`"` + curStr.String() + `"`); e == nil {
|
|
|
|
result = append(result, part)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
2023-12-13 12:22:41 +08:00
|
|
|
}
|