Skip to content

Commit

Permalink
Imporve AppendHTMLEscape fast path (valyala#1249)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyunhao116 authored and u5surf committed Mar 23, 2022
1 parent d1753f7 commit ab56e44
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions bytesconv.go
Expand Up @@ -11,27 +11,18 @@ import (
"math"
"net"
"reflect"
"strings"
"sync"
"time"
"unsafe"
)

// 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] {
Expand Down

0 comments on commit ab56e44

Please sign in to comment.