Merge pull request #28 from goccy/feature/improve-performance

Refactor encodeEscapedString ( for improvement performance )
This commit is contained in:
Masaaki Goshima 2020-08-15 23:14:26 +09:00 committed by GitHub
commit a45bb76d99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 4 deletions

View File

@ -219,21 +219,21 @@ var hex = "0123456789abcdef"
func (e *Encoder) encodeEscapedString(s string) {
valLen := len(s)
e.buf = append(e.buf, '"')
// write string, the fast path, without utf8 and escape support
i := 0
for ; i < valLen; i++ {
c := s[i]
if c < utf8.RuneSelf && htmlSafeSet[c] {
e.buf = append(e.buf, c)
} else {
if c >= utf8.RuneSelf || !htmlSafeSet[c] {
break
}
}
e.buf = append(e.buf, '"')
if i == valLen {
e.buf = append(e.buf, s...)
e.buf = append(e.buf, '"')
return
}
e.buf = append(e.buf, s[:i]...)
e.writeStringSlowPathWithHTMLEscaped(i, s, valLen)
}