aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/http/server.go
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2011-10-03 15:19:04 -0700
committerBrad Fitzpatrick <bradfitz@golang.org>2011-10-03 15:19:04 -0700
commitbba7396fbd4c3245dbedd3cc2a1fb25137331ebb (patch)
tree042d2580b79df727ae9f01e6de5cda1e093f1b9e /src/pkg/http/server.go
parente419535f2ae5c8aef1f64cdb207049c8712ffb48 (diff)
downloadgo-bba7396fbd4c3245dbedd3cc2a1fb25137331ebb.tar.xz
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
Diffstat (limited to 'src/pkg/http/server.go')
-rw-r--r--src/pkg/http/server.go15
1 files changed, 9 insertions, 6 deletions
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(
+ "&", "&amp;",
+ "<", "&lt;",
+ ">", "&gt;",
+ `"`, "&quot;",
+ "'", "&apos;",
+)
+
func htmlEscape(s string) string {
- s = strings.Replace(s, "&", "&amp;", -1)
- s = strings.Replace(s, "<", "&lt;", -1)
- s = strings.Replace(s, ">", "&gt;", -1)
- s = strings.Replace(s, "\"", "&quot;", -1)
- s = strings.Replace(s, "'", "&apos;", -1)
- return s
+ return htmlReplacer.Replace(s)
}
// Redirect to a fixed URL