mirror of https://github.com/goccy/go-json.git
Optimize HTML escape operation
This commit is contained in:
parent
5741c733a6
commit
b8f43ca445
49
encode.go
49
encode.go
|
@ -34,9 +34,18 @@ const (
|
||||||
bufSize = 1024
|
bufSize = 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
opCodeEscapedType = iota
|
||||||
|
opCodeEscapedIndentType
|
||||||
|
opCodeNoEscapeType
|
||||||
|
opCodeNoEscapeIndentType
|
||||||
|
)
|
||||||
|
|
||||||
type opcodeSet struct {
|
type opcodeSet struct {
|
||||||
codeIndent *opcode
|
escapedCode *opcode
|
||||||
|
escapedCodeIndent *opcode
|
||||||
code *opcode
|
code *opcode
|
||||||
|
codeIndent *opcode
|
||||||
codeLength int
|
codeLength int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,10 +196,18 @@ func (e *Encoder) encode(v interface{}) ([]byte, error) {
|
||||||
if codeSet, exists := opcodeMap[typeptr]; exists {
|
if codeSet, exists := opcodeMap[typeptr]; exists {
|
||||||
var code *opcode
|
var code *opcode
|
||||||
if e.enabledIndent {
|
if e.enabledIndent {
|
||||||
|
if e.enabledHTMLEscape {
|
||||||
|
code = codeSet.escapedCodeIndent
|
||||||
|
} else {
|
||||||
code = codeSet.codeIndent
|
code = codeSet.codeIndent
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if e.enabledHTMLEscape {
|
||||||
|
code = codeSet.escapedCode
|
||||||
} else {
|
} else {
|
||||||
code = codeSet.code
|
code = codeSet.code
|
||||||
}
|
}
|
||||||
|
}
|
||||||
ctx := e.ctx
|
ctx := e.ctx
|
||||||
p := uintptr(header.ptr)
|
p := uintptr(header.ptr)
|
||||||
ctx.init(p, codeSet.codeLength)
|
ctx.init(p, codeSet.codeLength)
|
||||||
|
@ -212,8 +229,10 @@ func (e *Encoder) encode(v interface{}) ([]byte, error) {
|
||||||
codeIndent := toIndent(code)
|
codeIndent := toIndent(code)
|
||||||
codeLength := code.totalLength()
|
codeLength := code.totalLength()
|
||||||
codeSet := &opcodeSet{
|
codeSet := &opcodeSet{
|
||||||
codeIndent: codeIndent,
|
escapedCode: toEscaped(code),
|
||||||
|
escapedCodeIndent: toEscaped(codeIndent),
|
||||||
code: code,
|
code: code,
|
||||||
|
codeIndent: codeIndent,
|
||||||
codeLength: codeLength,
|
codeLength: codeLength,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,9 +243,17 @@ func (e *Encoder) encode(v interface{}) ([]byte, error) {
|
||||||
|
|
||||||
var c *opcode
|
var c *opcode
|
||||||
if e.enabledIndent {
|
if e.enabledIndent {
|
||||||
c = codeIndent
|
if e.enabledHTMLEscape {
|
||||||
|
c = codeSet.escapedCodeIndent
|
||||||
} else {
|
} else {
|
||||||
c = code
|
c = codeSet.codeIndent
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if e.enabledHTMLEscape {
|
||||||
|
c = codeSet.escapedCode
|
||||||
|
} else {
|
||||||
|
c = codeSet.code
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err = e.run(ctx, b, c)
|
b, err = e.run(ctx, b, c)
|
||||||
|
@ -285,20 +312,6 @@ func encodeIndentComma(b []byte) []byte {
|
||||||
return append(b, ',', '\n')
|
return append(b, ',', '\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) encodeKey(b []byte, code *opcode) []byte {
|
|
||||||
if e.enabledHTMLEscape {
|
|
||||||
return append(b, code.escapedKey...)
|
|
||||||
}
|
|
||||||
return append(b, code.key...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Encoder) encodeString(b []byte, s string) []byte {
|
|
||||||
if e.enabledHTMLEscape {
|
|
||||||
return encodeEscapedString(b, s)
|
|
||||||
}
|
|
||||||
return encodeNoEscapedString(b, s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func encodeByteSlice(b []byte, src []byte) []byte {
|
func encodeByteSlice(b []byte, src []byte) []byte {
|
||||||
encodedLen := base64.StdEncoding.EncodedLen(len(src))
|
encodedLen := base64.StdEncoding.EncodedLen(len(src))
|
||||||
b = append(b, '"')
|
b = append(b, '"')
|
||||||
|
|
|
@ -67,6 +67,20 @@ func toIndent(c *opcode) *opcode {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func toEscaped(c *opcode) *opcode {
|
||||||
|
c = copyOpcode(c)
|
||||||
|
for code := c; code.op != opEnd; {
|
||||||
|
code.op = code.op.toEscaped()
|
||||||
|
switch code.op.codeType() {
|
||||||
|
case codeArrayElem, codeSliceElem, codeMapKey:
|
||||||
|
code = code.end
|
||||||
|
default:
|
||||||
|
code = code.next
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
func newOpCodeWithNext(ctx *encodeCompileContext, op opType, next *opcode) *opcode {
|
func newOpCodeWithNext(ctx *encodeCompileContext, op opType, next *opcode) *opcode {
|
||||||
return &opcode{
|
return &opcode{
|
||||||
op: op,
|
op: op,
|
||||||
|
|
6152
encode_vm.go
6152
encode_vm.go
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue