q5/strutils.go
aozhiwei a7c34b44d7 1
2020-12-04 16:47:45 +08:00

67 lines
1.1 KiB
Go

package q5
import (
"io"
"strings"
"crypto/md5"
"encoding/hex"
"encoding/json"
"hash/crc32"
)
const (
JSON_NONE = 0
JSON_ARRAY = iota
JSON_OBJECT = iota
)
func Md5Str(data string) string {
h := md5.New()
h.Write([]byte(data))
return hex.EncodeToString(h.Sum(nil))
}
func Crc32(data string) uint32 {
ieee := crc32.NewIEEE()
io.WriteString(ieee, data)
code := ieee.Sum32()
return code
}
func JsonStrType(data string) int32 {
arrayIdx := strings.IndexByte(data, '[')
objectIdx := strings.IndexByte(data, '{')
if arrayIdx < 0 && objectIdx < 0 {
return JSON_NONE
} else {
if arrayIdx < 0 {
return JSON_OBJECT
}
if objectIdx < 0 {
return JSON_ARRAY
}
if arrayIdx < objectIdx {
return JSON_ARRAY
} else {
return JSON_OBJECT
}
}
}
func EncodeJson(p interface{}) string {
if jsonBytes, err := json.Marshal(p); err == nil {
return string(jsonBytes)
} else {
return ""
}
}
func StrSplit(s string, sep string) []string {
return strings.Split(s, sep)
}
func StrContains(s string, substr string) bool {
return strings.Contains(s, substr)
}