github.com/chainreactors/fingers@v1.2.1/resources/utils.go (about) 1 package resources 2 3 import ( 4 "bytes" 5 "compress/gzip" 6 "encoding/json" 7 "io/ioutil" 8 9 "strings" 10 11 "github.com/chainreactors/utils/encode" 12 "github.com/mozillazg/go-pinyin" 13 ) 14 15 var pinyinArgs = pinyin.NewArgs() 16 17 // UnmarshalData 自动检测并解压 gzip 数据,然后进行 JSON 反序列化 18 func UnmarshalData(data []byte, v interface{}) error { 19 var err error 20 // 自动检测并解压 gzip 数据 21 if bytes.HasPrefix(data, []byte{0x1f, 0x8b}) { 22 data, err = encode.GzipDecompress(data) 23 if err != nil { 24 return err 25 } 26 } 27 28 return json.Unmarshal(data, &v) 29 } 30 31 // ConvertChineseToPinyin converts Chinese characters to Pinyin. 32 func ConvertChineseToPinyin(input string) string { 33 var s strings.Builder 34 for _, i := range input { 35 if i >= 0x4e00 && i <= 0x9fa5 { 36 if py := pinyin.SinglePinyin(i, pinyinArgs); len(py) > 0 { 37 s.WriteString(py[0]) 38 } else { 39 s.WriteRune(i) 40 } 41 } else { 42 s.WriteRune(i) 43 } 44 } 45 return s.String() 46 } 47 48 // NormalizeString performs normalization on the input string. 49 func NormalizeString(s string) string { 50 // Convert Chinese to Pinyin 51 s = ConvertChineseToPinyin(s) 52 53 // Convert to lower case 54 s = strings.ToLower(s) 55 56 // Replace '-' with '_' 57 s = strings.Replace(s, "-", "", -1) 58 59 s = strings.Replace(s, "_", "", -1) 60 61 // Remove spaces 62 s = strings.Replace(s, " ", "", -1) 63 64 return s 65 } 66 67 // DecompressGzip 解压缩gzip格式的数据 68 func DecompressGzip(data []byte) ([]byte, error) { 69 reader, err := gzip.NewReader(bytes.NewReader(data)) 70 if err != nil { 71 return nil, err 72 } 73 defer reader.Close() 74 75 return ioutil.ReadAll(reader) 76 }