+FilterEmoji

This commit is contained in:
gazebo
2019-10-16 16:37:59 +08:00
parent 444b0583d0
commit ae275c8645
2 changed files with 20 additions and 4 deletions

View File

@@ -225,6 +225,22 @@ func FilterMb4(content string) string {
return newContent.String()
}
// 是否是修饰符比如控制EMOJI的颜色控制符ZWJ
func IsDecorateRune(value rune) bool {
return value == 0xFE0F || value == 0xFE0E || value == 0x200D
}
func FilterEmoji(content string) string {
newContent := &strings.Builder{}
for _, value := range []rune(content) {
size := utf8.RuneLen(value)
if size >= 1 && size <= 3 && !(value >= 0x2300 && value < 0x32A0 || value >= 0x1F000 && value < 0x1F9F0) && !IsDecorateRune(value) {
newContent.WriteRune(value)
}
}
return newContent.String()
}
func TrimBlankChar(str string) string {
return strings.Trim(str, "\n\r\t ")
}

View File

@@ -128,7 +128,7 @@ func BenchmarkFilterMb4(b *testing.B) {
}
}
func TestFilterMb4(t *testing.T) {
func TestFilterEmoji(t *testing.T) {
for _, v := range [][]string{
[]string{
`
@@ -143,11 +143,11 @@ func TestFilterMb4(t *testing.T) {
"abcd中国人",
},
[]string{
"日配冷藏❄❄",
"日配冷藏❄❄",
"a❄日配冷藏❄❄🀄A❄",
"a日配冷藏A",
},
} {
str := FilterMb4(v[0])
str := FilterEmoji(v[0])
if str != v[1] {
t.Fatalf("%s failed\nshould be:%s\nbut it's:%s", v[0], v[1], str)
}