Merge pull request #43 from suntong/master
simplify FormatEmoji() algorithm
This commit is contained in:
commit
bec899ef78
8
emoji.go
8
emoji.go
@ -1,7 +1,7 @@
|
||||
package openwechat
|
||||
|
||||
import (
|
||||
"html"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -294,13 +294,11 @@ func FormatEmoji(text string) string {
|
||||
}
|
||||
value := item[0]
|
||||
emojiCodeStr := item[1]
|
||||
emojiCode, err := strconv.ParseInt(emojiCodeStr, 16, 64)
|
||||
emojiCode, err := strconv.ParseInt(emojiCodeStr, 16, 16)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
emojiStr := html.UnescapeString("&#" + strconv.FormatInt(emojiCode, 10) + ";")
|
||||
text = strings.Replace(text, value, emojiStr, -1)
|
||||
text = strings.Replace(text, value, fmt.Sprintf("%c", emojiCode), -1)
|
||||
}
|
||||
|
||||
return text
|
||||
|
@ -1,6 +1,9 @@
|
||||
package openwechat
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormatEmoji(t *testing.T) {
|
||||
t.Log(FormatEmoji(`多吃点苹果<span class="emoji emoji1f34f"></span>`))
|
||||
@ -17,5 +20,27 @@ func TestSendEmoji(t *testing.T) {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
_ , _= f.SendText(Emoji.Dagger)
|
||||
_, _ = f.SendText(Emoji.Dagger)
|
||||
}
|
||||
|
||||
func BenchmarkFormatEmojiString(b *testing.B) {
|
||||
str := `多吃点苹果<span class="emoji emoji1f34f"></span>高兴<span class="emoji emoji1f604"></span> 生气<span class="emoji emoji1f64e"></span> 点赞<span class="emoji emoji1f44d"></span>`
|
||||
b.SetBytes(int64(len(str)))
|
||||
// b.N会根据函数的运行时间取一个合适的值
|
||||
for i := 0; i < b.N; i++ {
|
||||
FormatEmoji(str)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFormatEmojiBlock(b *testing.B) {
|
||||
str := ""
|
||||
for ii := 0x1F301; ii <= 0x1F53D; ii++ {
|
||||
str += fmt.Sprintf(`<span class="emoji emoji%x"></span> `, ii)
|
||||
}
|
||||
b.SetBytes(int64(len(str)))
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
FormatEmoji(str)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user