From e46f01d320bfd0d488a4b257ee96685e79cd3725 Mon Sep 17 00:00:00 2001 From: ZhangYunHao Date: Thu, 17 Mar 2022 21:52:47 +0800 Subject: [PATCH] Imporve AppendHTMLEscape fast path (#1249) --- bytesconv.go | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/bytesconv.go b/bytesconv.go index a35b6068b6..a12bb95979 100644 --- a/bytesconv.go +++ b/bytesconv.go @@ -11,7 +11,6 @@ import ( "math" "net" "reflect" - "strings" "sync" "time" "unsafe" @@ -19,19 +18,11 @@ import ( // AppendHTMLEscape appends html-escaped s to dst and returns the extended dst. func AppendHTMLEscape(dst []byte, s string) []byte { - if strings.IndexByte(s, '&') < 0 && - strings.IndexByte(s, '<') < 0 && - strings.IndexByte(s, '>') < 0 && - strings.IndexByte(s, '"') < 0 && - strings.IndexByte(s, '\'') < 0 { - - // fast path - nothing to escape - return append(dst, s...) - } + var ( + prev int + sub string + ) - // slow path - var prev int - var sub string for i, n := 0, len(s); i < n; i++ { sub = "" switch s[i] {