Skip to content

Commit

Permalink
add KStr.HasEmoji/KStr.RemoveEmoji
Browse files Browse the repository at this point in the history
  • Loading branch information
kakuilan committed May 20, 2021
1 parent 60f3786 commit 80f70f6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
10 changes: 10 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2120,3 +2120,13 @@ func (ks *LkkString) CountWords(str string) (int, map[string]int) {

return total, mp
}

// HasEmoji 字符串是否含有表情符.
func (ks *LkkString) HasEmoji(str string) bool {
return str != "" && RegEmoji.MatchString(str)
}

// RemoveEmoji 移除字符串中的表情符(使用正则,效率较低).
func (ks *LkkString) RemoveEmoji(str string) string {
return RegEmoji.ReplaceAllString(str, "")
}
29 changes: 29 additions & 0 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3252,3 +3252,32 @@ func BenchmarkString_EndsWiths(b *testing.B) {
KStr.EndsWiths(helloOther2, []string{"", "World 世界!"}, false)
}
}

func TestString_HasEmoji_RemoveEmoji(t *testing.T) {
var res string
var chk bool

chk = KStr.HasEmoji(strHello)
assert.False(t, chk)

chk = KStr.HasEmoji(tesEmoji1)
assert.True(t, chk)

res = KStr.RemoveEmoji(tesEmoji1)
chk = KStr.HasEmoji(res)
assert.False(t, chk)
}

func BenchmarkString_HasEmoji(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
KStr.HasEmoji(strHello)
}
}

func BenchmarkString_RemoveEmoji(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
KStr.RemoveEmoji(tesEmoji1)
}
}
32 changes: 32 additions & 0 deletions testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,38 @@ var tesUrl37 = "rtmp://foobar.com"
var tesUrl38 = "xyz://foobar.com"
var tesUrl39 = "https://www.baidu.com/"

//表情符
var tesEmoji1 = `Lorem ipsum 🥊dolor 🤒sit amet, consectetur adipiscing 🍂 elit. 🍁🍃🍂🌰🍁🌿🌾🌼🌻سلام تست شد hell中文
😀😁😂😃😄😅😆😉😊😋😎😍😘😗😙😚☺😇😐😑😶😏😣😥😮😯😪😫😴😌😛😜😝😒😓😔😕😲😷😖😞😟😤😢😭😦😧😨😬😰😱😳😵😡😠
👦👧👨👩👴👵👶👱👮👲👳👷👸💂🎅👰👼💆💇🙍🙎🙅🙆💁🙋🙇🙌🙏👤👥🚶🏃👯💃👫👬👭💏💑👪
💪👈👉☝👆👇✌✋👌👍👎✊👊👋👏👐✍
👣👀👂👃👅👄💋👓👔👕👖👗👘👙👚👛👜👝🎒💼👞👟👠👡👢👑👒🎩🎓💄💅💍🌂
📱📲📶📳📴☎📞📟📠
♻🏧🚮🚰♿🚹🚺🚻🚼🚾⚠🚸⛔🚫🚳🚭🚯🚱🚷🔞💈
🙈🙉🙊🐵🐒🐶🐕🐩🐺🐱😺😸😹😻😼😽🙀😿😾🐈🐯🐅🐆🐴🐎🐮🐂🐃🐄🐷🐖🐗🐽🐏🐑🐐🐪🐫🐘🐭🐁🐀🐹🐰🐇🐻🐨🐼🐾🐔🐓🐣🐤🐥🐦🐧🐸🐊🐢🐍🐲🐉🐳🐋🐬🐟🐠🐡🐙🐚🐌🐛🐜🐝🐞
💐🌸💮🌹🌺🌻🌼🌷🌱🌲🌳🌴🌵🌾🌿🍀🍁🍂🍃
🌍🌎🌏🌐🌑🌒🌓🌔🌕🌖🌗🌘🌙🌚🌛🌜☀🌝🌞⭐🌟🌠☁⛅☔⚡❄🔥💧🌊
🍇🍈🍉🍊🍋🍌🍍🍎🍏🍐🍑🍒🍓🍅🍆🌽🍄🌰🍞🍖🍗🍔🍟🍕🍳🍲🍱🍘🍙🍚🍛🍜🍝🍠🍢🍣🍤🍥🍡🍦🍧🍨🍩🍪🎂🍰🍫🍬🍭🍮🍯🍼☕🍵🍶🍷🍸🍹🍺🍻🍴
🎪🎭🎨🎰🚣🛀🎫🏆⚽⚾🏀🏈🏉🎾🎱🎳⛳🎣🎽🎿🏂🏄🏇🏊🚴🚵🎯🎮🎲🎷🎸🎺🎻🎬
😈👿👹👺💀☠👻👽👾💣
🌋🗻🏠🏡🏢🏣🏤🏥🏦🏨🏩🏪🏫🏬🏭🏯🏰💒🗼🗽⛪⛲🌁🌃🌆🌇🌉🌌🎠🎡🎢🚂🚃🚄🚅🚆🚇🚈🚉🚊🚝🚞🚋🚌🚍🚎🚏🚐🚑🚒🚓🚔🚕🚖🚗🚘🚚🚛🚜🚲⛽🚨🚥🚦🚧⚓⛵🚤🚢✈💺🚁🚟🚠🚡🚀🎑🗿🛂🛃🛄🛅
💌💎🔪💈🚪🚽🚿🛁⌛⏳⌚⏰🎈🎉🎊🎎🎏🎐🎀🎁📯📻📱📲☎📞📟📠🔋🔌💻💽💾💿📀🎥📺📷📹📼🔍🔎🔬🔭📡💡🔦🏮📔📕📖📗📘📙📚📓📃📜📄📰📑🔖💰💴💵💶💷💸💳✉📧📨📩📤📥📦📫📪📬📭📮✏✒📝📁📂📅📆📇📈📉📊📋📌📍📎📏📐✂🔒🔓🔏🔐🔑🔨🔫🔧🔩🔗💉💊🚬🔮🚩🎌💦💨
♠♥♦♣🀄🎴🔇🔈🔉🔊📢📣💤💢💬💭♨🌀🔔🔕✡✝🔯📛🔰🔱⭕✅☑✔✖❌❎➕➖➗➰➿〽✳✴❇‼⁉❓❔❕❗©®™🎦🔅🔆💯🔠🔡🔢🔣🔤🅰🆎🅱🆑🆒🆓ℹ🆔Ⓜ🆕🆖🅾🆗🅿🆘🆙🆚🈁🈂🈷🈶🈯🉐🈹🈚🈲🉑🈸🈴🈳㊗㊙🈺🈵▪▫◻◼◽◾⬛⬜🔶🔷🔸🔹🔺🔻💠🔲🔳⚪⚫🔴🔵
🐁🐂🐅🐇🐉🐍🐎🐐🐒🐓🐕🐖
♈♉♊♋♌♍♎♏♐♑♒♓⛎
🕛🕧🕐🕜🕑🕝🕒🕞🕓🕟🕔🕠🕕🕡🕖🕢🕗🕣🕘🕤🕙🕥🕚🕦⌛⏳⌚⏰⏱⏲🕰
💘❤💓💔💕💖💗💙💚💛💜💝💞💟❣
💐🌸💮🌹🌺🌻🌼🌷🌱🌿🍀
🌿🍀🍁🍂🍃
🌑🌒🌓🌔🌕🌖🌗🌘🌙🌚🌛🌜🌝
🍇🍈🍉🍊🍋🍌🍍🍎🍏🍐🍑🍒🍓
💴💵💶💷💰💸💳
🚂🚃🚄🚅🚆🚇🚈🚉🚊🚝🚞🚋🚌🚍🚎🚏🚐🚑🚒🚓🚔🚕🚖🚗🚘🚚🚛🚜🚲⛽🚨🚥🚦🚧⚓⛵🚣🚤🚢✈💺🚁🚟🚠🚡🚀
🏠🏡🏢🏣🏤🏥🏦🏨🏩🏪🏫🏬🏭🏯🏰💒🗼🗽⛪🌆🌇🌉
📱📲☎📞📟📠🔋🔌💻💽💾💿📀🎥📺📷📹📼🔍🔎🔬🔭📡📔📕📖📗📘📙📚📓📃📜📄📰📑🔖💳✉📧📨📩📤📥📦📫📪📬📭📮✏✒📝📁📂📅📆📇📈📉📊📋📌📍📎📏📐✂🔒🔓🔏🔐🔑
⬆↗➡↘⬇↙⬅↖↕↔↩↪⤴⤵🔃🔄🔙🔚🔛🔜🔝`
var tesEmoji2 = `Hi!😀👽😀☂❤华み원❤This is a string 😄 🐷 with some 👍🏻 🙈 emoji! 🐷 🏃🏿‍♂️`

func init() {
gofakeit.Struct(&personS1)
gofakeit.Struct(&personS2)
Expand Down

0 comments on commit 80f70f6

Please sign in to comment.