q5/strutils.go
aozhiwei 88b284f7de 1
2020-09-10 11:24:57 +08:00

48 lines
780 B
Go

package q5
import "io"
import "strings"
import "crypto/md5"
import "encoding/hex"
import "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
}
}
}