-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertCode.go
76 lines (69 loc) · 1.63 KB
/
convertCode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"C"
"crypto/md5"
"encoding/hex"
"unicode"
"github.com/mozillazg/go-pinyin"
)
//export convertCode
func convertCode(value string) *C.char {
stringSlice := []rune(value)
convertedCode := ""
// Support Chinese words in quotation
quotationFlag := false
tempID := ""
keywords := []string{"字符", "其他", "枚举", "如果", "整型", "返回", "占用空间", "循环当", "打开",
"读取", "关闭", "打印", "内存分配", "内存设置", "内存拷贝", "退出", "空值", "主函数"}
for i := 0; i < len(stringSlice); i++ {
ch := string(stringSlice[i])
if ch == "\"" {
quotationFlag = !quotationFlag
convertedCode += ch
continue
} else if quotationFlag == true {
convertedCode += ch
continue
}
if unicode.Is(unicode.Scripts["Han"], stringSlice[i]) || unicode.IsLetter(stringSlice[i]) || ch == "_" {
tempID += ch
} else {
if tempID != "" {
if unicode.IsDigit(stringSlice[i]) {
tempID += ch
continue
}
if stringsContains(keywords, tempID) == -1 {
convertedCode += "_" + getMD5HashCode(tempID)
} else {
for _, h := range pinyin.LazyConvert(tempID, nil) {
convertedCode += h
}
}
}
convertedCode += ch
tempID = ""
continue
}
}
return C.CString(convertedCode)
}
func stringsContains(array []string, val string) (index int) {
index = -1
for i := 0; i < len(array); i++ {
if array[i] == val {
index = i
return
}
}
return
}
func getMD5HashCode(tempID string) string {
hash := md5.New()
hash.Write([]byte(tempID))
bytes := hash.Sum(nil)
hashCode := hex.EncodeToString(bytes)
return hashCode
}
func main() {
}