Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize slackutilsx.EscapeMessage function #1041

Merged
merged 1 commit into from Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
}
}