feat: 增加中文的一些处理

This commit is contained in:
tiglog 2023-09-20 16:49:31 +08:00
parent f60bf1ee22
commit b57b32944a
2 changed files with 51 additions and 0 deletions

View File

@ -162,3 +162,43 @@ func Uniq(l int) string {
// log.Println("ret=", ret, ", pl=", pl, ", l=", l) // log.Println("ret=", ret, ", pl=", pl, ", l=", l)
return ret[0:l] return ret[0:l]
} }
func GetCjkRange(code int) string {
var result string
if code >= 0x3400 && code <= 0x4DBF {
result = "EXT-A"
} else if code >= 0x20000 && code <= 0x2A6DF {
result = "EXT-B"
} else if code >= 0x2A700 && code <= 0x2B739 {
result = "EXT-C"
} else if code >= 0x2B740 && code <= 0x2B81D {
result = "EXT-D"
} else if code >= 0x2B820 && code <= 0x2CEA1 {
result = "EXT-E"
} else if code >= 0x2CEB0 && code <= 0x2EBE0 {
result = "EXT-F"
} else if code >= 0x30000 && code <= 0x3134A {
result = "EXT-G"
} else if code >= 0x31350 && code <= 0x323AF {
result = "EXT-H"
} else if code >= 0xE000 && code <= 0xF8FF {
result = "PUA"
} else if code >= 0x4e00 && code <= 0x9FFF {
result = "GBK"
} else {
result = "Other"
}
return result
}
// 获取某个字的 unicode 值
func Ord(in string) int {
s := []rune(in)
return int(s[0])
}
// 把 unicode 值转成字符串
func Chr(code int) string {
return string(rune(code))
}

View File

@ -130,3 +130,14 @@ func TestCamel(t *testing.T) {
r1 := helper.CamelString(s1) r1 := helper.CamelString(s1)
gtest.Equal(t, "XxYy", r1) gtest.Equal(t, "XxYy", r1)
} }
func TestOrd(t *testing.T) {
s := "中"
r := helper.Ord(s)
gtest.Equal(t, 20013, r)
}
func TestChr(t *testing.T) {
r := helper.Chr(20013)
gtest.Equal(t, "中", r)
}