From bba7396fbd4c3245dbedd3cc2a1fb25137331ebb Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 3 Oct 2011 15:19:04 -0700 Subject: strings: implement a faster byte->string Replacer This implements a replacer for when all old strings are single bytes, but new values are not. BenchmarkHTMLEscapeNew 1000000 1090 ns/op BenchmarkHTMLEscapeOld 1000000 2049 ns/op R=rsc CC=golang-dev https://golang.org/cl/5176043 --- src/pkg/http/server.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/pkg/http/server.go') diff --git a/src/pkg/http/server.go b/src/pkg/http/server.go index 8326ff8be1..e8e23087e0 100644 --- a/src/pkg/http/server.go +++ b/src/pkg/http/server.go @@ -752,13 +752,16 @@ func Redirect(w ResponseWriter, r *Request, urlStr string, code int) { } } +var htmlReplacer = strings.NewReplacer( + "&", "&", + "<", "<", + ">", ">", + `"`, """, + "'", "'", +) + func htmlEscape(s string) string { - s = strings.Replace(s, "&", "&", -1) - s = strings.Replace(s, "<", "<", -1) - s = strings.Replace(s, ">", ">", -1) - s = strings.Replace(s, "\"", """, -1) - s = strings.Replace(s, "'", "'", -1) - return s + return htmlReplacer.Replace(s) } // Redirect to a fixed URL -- cgit v1.3-6-g1900