137 lines
2.8 KiB
Go
137 lines
2.8 KiB
Go
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
package str
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"math/rand"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
NUMBERSTRING = "0123456789"
|
|
CHARSTRING = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
)
|
|
|
|
// get random number string with length.
|
|
var GetRandomString = GetRandomNumString
|
|
|
|
// get random number string with length.
|
|
func GetRandomNumString(l int) string {
|
|
str := NUMBERSTRING
|
|
bytes := []byte(str)
|
|
result := []byte{}
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
for i := 0; i < l; i++ {
|
|
result = append(result, bytes[r.Intn(len(bytes))])
|
|
}
|
|
return string(result)
|
|
}
|
|
|
|
// get random number+alphabate string with length.
|
|
func GetRandomCharString(l int) string {
|
|
str := CHARSTRING
|
|
bytes := []byte(str)
|
|
result := []byte{}
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
for i := 0; i < l; i++ {
|
|
result = append(result, bytes[r.Intn(len(bytes))])
|
|
}
|
|
return string(result)
|
|
}
|
|
|
|
// skip
|
|
func Skip(s string, sep string, n int) string {
|
|
seplen := len(sep)
|
|
if seplen == 0 || n < 0 {
|
|
return s
|
|
}
|
|
|
|
for {
|
|
i := strings.Index(s, sep)
|
|
if i < 0 {
|
|
return s
|
|
} else {
|
|
s = s[i+seplen:]
|
|
n = n - 1
|
|
if n <= 0 {
|
|
return s
|
|
}
|
|
}
|
|
}
|
|
return s
|
|
}
|
|
|
|
// skip line
|
|
func SkipLine(s string, n int) string {
|
|
return Skip(s, "\n", n)
|
|
}
|
|
|
|
// get a map's keys as array.
|
|
func MapsKeys(imap interface{}) (keys []string) {
|
|
switch imap.(type) {
|
|
case map[string]string:
|
|
m := imap.(map[string]string)
|
|
for k, _ := range m {
|
|
keys = append(keys, k)
|
|
}
|
|
case map[string]interface{}:
|
|
m := imap.(map[string]interface{})
|
|
for k, _ := range m {
|
|
keys = append(keys, k)
|
|
}
|
|
default:
|
|
panic("Unkown Type")
|
|
}
|
|
return keys
|
|
}
|
|
|
|
// get a map's keys and join it to string.
|
|
func MapsKeysString(imap interface{}) string {
|
|
keys := MapsKeys(imap)
|
|
return strings.Join(keys, ",")
|
|
}
|
|
|
|
// string json to string map to string
|
|
func JsonstrToStrMap(jsonstr string) (m map[string]string) {
|
|
err := json.Unmarshal([]byte(jsonstr), &m)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Jsonstr[%s] To MapString failed:%s", jsonstr, err.Error()))
|
|
}
|
|
return m
|
|
}
|
|
|
|
// string to string map to string
|
|
//
|
|
// example: "K1:V1;K2:V2"
|
|
// => {K1:V1, K2:V2}
|
|
func StringToStrMap(str string, sep1, sep2 string) (m map[string]string) {
|
|
m = map[string]string{}
|
|
for _, kvstr := range strings.Split(str, sep1) {
|
|
if kv := strings.SplitN(kvstr, sep2, 2); len(kv) == 2 {
|
|
m[kv[0]] = kv[1]
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
|
|
// get os env with default
|
|
func GetEnvDefault(key, defval string) string {
|
|
if val, ok := os.LookupEnv(key); ok {
|
|
return val
|
|
} else {
|
|
return defval
|
|
}
|
|
}
|
|
|
|
// fix hex string to no 0x prefix
|
|
func FixToNo0x(hexstr string) string {
|
|
if strings.HasPrefix(hexstr, "0x") {
|
|
return hexstr[2:]
|
|
}
|
|
return hexstr
|
|
}
|