Skip to content

Commit

Permalink
Merge pull request #1041 from brainexe/optimize_escape
Browse files Browse the repository at this point in the history
optimize slackutilsx.EscapeMessage function
  • Loading branch information
zchee committed Mar 11, 2022
2 parents b5149b1 + 60ea6a8 commit 6ae6da1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 4 additions & 2 deletions slackutilsx/slackutilsx.go
Expand Up @@ -50,10 +50,12 @@ func DetectChannelType(channelID string) ChannelType {
}
}

// initialize replacer only once (if needed)
var escapeReplacer = strings.NewReplacer("&", "&amp;", "<", "&lt;", ">", "&gt;")

// EscapeMessage text
func EscapeMessage(message string) string {
replacer := strings.NewReplacer("&", "&amp;", "<", "&lt;", ">", "&gt;")
return replacer.Replace(message)
return escapeReplacer.Replace(message)
}

// Retryable errors return true.
Expand Down
6 changes: 6 additions & 0 deletions slackutilsx/slackutilsx_test.go
Expand Up @@ -28,3 +28,9 @@ func TestEscapeMessage(t *testing.T) {
test("A < B", "A &lt; B")
test("A > B", "A &gt; B")
}

func BenchmarkEscapeMessage(b *testing.B) {
for i := 0; i < b.N; i++ {
EscapeMessage("A & B")
}
}

0 comments on commit 6ae6da1

Please sign in to comment.