From b57b32944aabc4b36fb19d2c6c5bcf0ee509cc91 Mon Sep 17 00:00:00 2001 From: tiglog Date: Wed, 20 Sep 2023 16:49:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=B8=AD=E6=96=87?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E4=BA=9B=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/str_helper.go | 40 +++++++++++++++++++++++++++++++++++++++ helper/str_helper_test.go | 11 +++++++++++ 2 files changed, 51 insertions(+) diff --git a/helper/str_helper.go b/helper/str_helper.go index eeb0773..62686b5 100644 --- a/helper/str_helper.go +++ b/helper/str_helper.go @@ -162,3 +162,43 @@ func Uniq(l int) string { // log.Println("ret=", ret, ", pl=", pl, ", l=", 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)) +} diff --git a/helper/str_helper_test.go b/helper/str_helper_test.go index 5df5b4f..fadb8ea 100644 --- a/helper/str_helper_test.go +++ b/helper/str_helper_test.go @@ -130,3 +130,14 @@ func TestCamel(t *testing.T) { r1 := helper.CamelString(s1) 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) +}