forked from mirror/go-json
Remove escaped optype
This commit is contained in:
parent
4cfb919183
commit
9c5f6ec0e6
|
@ -14,7 +14,6 @@ import (
|
|||
type opType struct {
|
||||
Op string
|
||||
Code string
|
||||
Escaped func() string
|
||||
HeadToPtrHead func() string
|
||||
HeadToNPtrHead func() string
|
||||
HeadToAnonymousHead func() string
|
||||
|
@ -27,10 +26,6 @@ type opType struct {
|
|||
FieldToStringTagField func() string
|
||||
}
|
||||
|
||||
func (t opType) IsEscaped() bool {
|
||||
return t.Op != t.Escaped()
|
||||
}
|
||||
|
||||
func (t opType) IsHeadToPtrHead() bool {
|
||||
return t.Op != t.HeadToPtrHead()
|
||||
}
|
||||
|
@ -75,7 +70,6 @@ func createOpType(op, code string) opType {
|
|||
return opType{
|
||||
Op: op,
|
||||
Code: code,
|
||||
Escaped: func() string { return op },
|
||||
HeadToPtrHead: func() string { return op },
|
||||
HeadToNPtrHead: func() string { return op },
|
||||
HeadToAnonymousHead: func() string { return op },
|
||||
|
@ -158,31 +152,6 @@ func (t opType) codeType() codeType {
|
|||
return codeOp
|
||||
}
|
||||
|
||||
func (t opType) toEscaped() opType {
|
||||
if strings.Index(t.String(), "Escaped") > 0 {
|
||||
return t
|
||||
}
|
||||
if t.String() == "String" {
|
||||
return opType(int(t) + 1)
|
||||
}
|
||||
|
||||
fieldHeadIdx := strings.Index(t.String(), "Head")
|
||||
if fieldHeadIdx > 0 && strings.Contains(t.String(), "Struct") {
|
||||
const toEscapedHeadOffset = 36
|
||||
return opType(int(t) + toEscapedHeadOffset)
|
||||
}
|
||||
fieldIdx := strings.Index(t.String(), "Field")
|
||||
if fieldIdx > 0 && strings.Contains(t.String(), "Struct") {
|
||||
const toEscapedFieldOffset = 3
|
||||
return opType(int(t) + toEscapedFieldOffset)
|
||||
}
|
||||
if strings.Contains(t.String(), "StructEnd") {
|
||||
const toEscapedEndOffset = 3
|
||||
return opType(int(t) + toEscapedEndOffset)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
func (t opType) headToPtrHead() opType {
|
||||
if strings.Index(t.String(), "PtrHead") > 0 {
|
||||
return t
|
||||
|
@ -325,16 +294,16 @@ func (t opType) fieldToStringTagField() opType {
|
|||
primitiveTypes := []string{
|
||||
"int", "int8", "int16", "int32", "int64",
|
||||
"uint", "uint8", "uint16", "uint32", "uint64",
|
||||
"float32", "float64", "bool", "string", "escapedString", "bytes",
|
||||
"float32", "float64", "bool", "string", "bytes",
|
||||
"array", "map", "mapLoad", "slice", "struct", "MarshalJSON", "MarshalText", "recursive",
|
||||
"intString", "int8String", "int16String", "int32String", "int64String",
|
||||
"uintString", "uint8String", "uint16String", "uint32String", "uint64String",
|
||||
"intPtr", "int8Ptr", "int16Ptr", "int32Ptr", "int64Ptr",
|
||||
"uintPtr", "uint8Ptr", "uint16Ptr", "uint32Ptr", "uint64Ptr",
|
||||
"float32Ptr", "float64Ptr", "boolPtr", "stringPtr", "escapedStringPtr", "bytesPtr",
|
||||
"float32Ptr", "float64Ptr", "boolPtr", "stringPtr", "bytesPtr",
|
||||
"intNPtr", "int8NPtr", "int16NPtr", "int32NPtr", "int64NPtr",
|
||||
"uintNPtr", "uint8NPtr", "uint16NPtr", "uint32NPtr", "uint64NPtr",
|
||||
"float32NPtr", "float64NPtr", "boolNPtr", "stringNPtr", "escapedStringNPtr", "bytesNPtr",
|
||||
"float32NPtr", "float64NPtr", "boolNPtr", "stringNPtr", "bytesNPtr",
|
||||
}
|
||||
primitiveTypesUpper := []string{}
|
||||
for _, typ := range primitiveTypes {
|
||||
|
@ -364,41 +333,21 @@ func (t opType) fieldToStringTagField() opType {
|
|||
}
|
||||
for _, typ := range primitiveTypesUpper {
|
||||
typ := typ
|
||||
optype := createOpType(typ, "Op")
|
||||
switch typ {
|
||||
case "String", "StringPtr", "StringNPtr":
|
||||
optype.Escaped = func() string {
|
||||
return fmt.Sprintf("Escaped%s", typ)
|
||||
}
|
||||
}
|
||||
opTypes = append(opTypes, optype)
|
||||
opTypes = append(opTypes, createOpType(typ, "Op"))
|
||||
}
|
||||
for _, typ := range append(primitiveTypesUpper, "") {
|
||||
if typ == "EscapedString" || typ == "EscapedStringPtr" || typ == "EscapedStringNPtr" {
|
||||
continue
|
||||
}
|
||||
for _, escapedOrNot := range []string{"", "Escaped"} {
|
||||
for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} {
|
||||
for _, headType := range []string{"", "Anonymous"} {
|
||||
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
||||
for _, onlyOrNot := range []string{"", "Only"} {
|
||||
escapedOrNot := escapedOrNot
|
||||
ptrOrNot := ptrOrNot
|
||||
headType := headType
|
||||
opt := opt
|
||||
typ := typ
|
||||
onlyOrNot := onlyOrNot
|
||||
|
||||
isEscaped := escapedOrNot != ""
|
||||
isString := typ == "String" || typ == "StringPtr" || typ == "StringNPtr"
|
||||
|
||||
if isEscaped && isString {
|
||||
typ = "Escaped" + typ
|
||||
}
|
||||
|
||||
op := fmt.Sprintf(
|
||||
"Struct%sField%s%sHead%s%s%s",
|
||||
escapedOrNot,
|
||||
"StructField%s%sHead%s%s%s",
|
||||
ptrOrNot,
|
||||
headType,
|
||||
opt,
|
||||
|
@ -408,31 +357,9 @@ func (t opType) fieldToStringTagField() opType {
|
|||
opTypes = append(opTypes, opType{
|
||||
Op: op,
|
||||
Code: "StructField",
|
||||
Escaped: func() string {
|
||||
switch typ {
|
||||
case "String", "StringPtr", "StringNPtr":
|
||||
return fmt.Sprintf(
|
||||
"StructEscapedField%s%sHead%sEscaped%s%s",
|
||||
ptrOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"StructEscapedField%s%sHead%s%s%s",
|
||||
ptrOrNot,
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
onlyOrNot,
|
||||
)
|
||||
},
|
||||
HeadToPtrHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sFieldPtr%sHead%s%s%s",
|
||||
escapedOrNot,
|
||||
"StructFieldPtr%sHead%s%s%s",
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
|
@ -441,8 +368,7 @@ func (t opType) fieldToStringTagField() opType {
|
|||
},
|
||||
HeadToNPtrHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sFieldNPtr%sHead%s%s%s",
|
||||
escapedOrNot,
|
||||
"StructFieldNPtr%sHead%s%s%s",
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
|
@ -451,8 +377,7 @@ func (t opType) fieldToStringTagField() opType {
|
|||
},
|
||||
HeadToAnonymousHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%sAnonymousHead%s%s%s",
|
||||
escapedOrNot,
|
||||
"StructField%sAnonymousHead%s%s%s",
|
||||
ptrOrNot,
|
||||
opt,
|
||||
typ,
|
||||
|
@ -461,8 +386,7 @@ func (t opType) fieldToStringTagField() opType {
|
|||
},
|
||||
HeadToOmitEmptyHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%s%sHeadOmitEmpty%s%s",
|
||||
escapedOrNot,
|
||||
"StructField%s%sHeadOmitEmpty%s%s",
|
||||
ptrOrNot,
|
||||
headType,
|
||||
typ,
|
||||
|
@ -471,8 +395,7 @@ func (t opType) fieldToStringTagField() opType {
|
|||
},
|
||||
HeadToStringTagHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%s%sHeadStringTag%s%s",
|
||||
escapedOrNot,
|
||||
"StructField%s%sHeadStringTag%s%s",
|
||||
ptrOrNot,
|
||||
headType,
|
||||
typ,
|
||||
|
@ -488,8 +411,7 @@ func (t opType) fieldToStringTagField() opType {
|
|||
return op
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%s%sHead%s%sOnly",
|
||||
escapedOrNot,
|
||||
"StructField%s%sHead%s%sOnly",
|
||||
ptrOrNot,
|
||||
headType,
|
||||
opt,
|
||||
|
@ -498,8 +420,7 @@ func (t opType) fieldToStringTagField() opType {
|
|||
},
|
||||
PtrHeadToHead: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sField%sHead%s%s%s",
|
||||
escapedOrNot,
|
||||
"StructField%sHead%s%s%s",
|
||||
headType,
|
||||
opt,
|
||||
typ,
|
||||
|
@ -515,48 +436,19 @@ func (t opType) fieldToStringTagField() opType {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, typ := range append(primitiveTypesUpper, "") {
|
||||
if typ == "EscapedString" || typ == "EscapedStringPtr" || typ == "EscapedStringNPtr" {
|
||||
continue
|
||||
}
|
||||
for _, escapedOrNot := range []string{"", "Escaped"} {
|
||||
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
||||
escapedOrNot := escapedOrNot
|
||||
opt := opt
|
||||
typ := typ
|
||||
|
||||
isEscaped := escapedOrNot != ""
|
||||
isString := typ == "String" || typ == "StringPtr" || typ == "StringNPtr"
|
||||
|
||||
if isEscaped && isString {
|
||||
typ = "Escaped" + typ
|
||||
}
|
||||
|
||||
op := fmt.Sprintf(
|
||||
"Struct%sField%s%s",
|
||||
escapedOrNot,
|
||||
"StructField%s%s",
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
opTypes = append(opTypes, opType{
|
||||
Op: op,
|
||||
Code: "StructField",
|
||||
Escaped: func() string {
|
||||
switch typ {
|
||||
case "String", "StringPtr", "StringNPtr":
|
||||
return fmt.Sprintf(
|
||||
"StructEscapedField%sEscaped%s",
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"StructEscapedField%s%s",
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
},
|
||||
HeadToPtrHead: func() string { return op },
|
||||
HeadToNPtrHead: func() string { return op },
|
||||
HeadToAnonymousHead: func() string { return op },
|
||||
|
@ -570,71 +462,39 @@ func (t opType) fieldToStringTagField() opType {
|
|||
return op
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"Struct%sEnd%s%s",
|
||||
escapedOrNot,
|
||||
"StructEnd%s%s",
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
},
|
||||
FieldToOmitEmptyField: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sFieldOmitEmpty%s",
|
||||
escapedOrNot,
|
||||
"StructFieldOmitEmpty%s",
|
||||
typ,
|
||||
)
|
||||
},
|
||||
FieldToStringTagField: func() string {
|
||||
return fmt.Sprintf(
|
||||
"Struct%sFieldStringTag%s",
|
||||
escapedOrNot,
|
||||
"StructFieldStringTag%s",
|
||||
typ,
|
||||
)
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, typ := range append(primitiveTypesUpper, "") {
|
||||
if typ == "EscapedString" || typ == "EscapedStringPtr" || typ == "EscapedStringNPtr" {
|
||||
continue
|
||||
}
|
||||
for _, escapedOrNot := range []string{"", "Escaped"} {
|
||||
for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
|
||||
escapedOrNot := escapedOrNot
|
||||
opt := opt
|
||||
typ := typ
|
||||
|
||||
isEscaped := escapedOrNot != ""
|
||||
isString := typ == "String" || typ == "StringPtr" || typ == "StringNPtr"
|
||||
|
||||
if isEscaped && isString {
|
||||
typ = "Escaped" + typ
|
||||
}
|
||||
|
||||
op := fmt.Sprintf(
|
||||
"Struct%sEnd%s%s",
|
||||
escapedOrNot,
|
||||
"StructEnd%s%s",
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
opTypes = append(opTypes, opType{
|
||||
Op: op,
|
||||
Code: "StructEnd",
|
||||
Escaped: func() string {
|
||||
switch typ {
|
||||
case "String", "StringPtr", "StringNPtr":
|
||||
return fmt.Sprintf(
|
||||
"StructEscapedEnd%sEscaped%s",
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
}
|
||||
return fmt.Sprintf(
|
||||
"StructEscapedEnd%s%s",
|
||||
opt,
|
||||
typ,
|
||||
)
|
||||
},
|
||||
HeadToPtrHead: func() string { return op },
|
||||
HeadToNPtrHead: func() string { return op },
|
||||
HeadToAnonymousHead: func() string { return op },
|
||||
|
@ -648,7 +508,6 @@ func (t opType) fieldToStringTagField() opType {
|
|||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
var b bytes.Buffer
|
||||
if err := tmpl.Execute(&b, struct {
|
||||
CodeTypes []string
|
||||
|
|
10
encode.go
10
encode.go
|
@ -42,7 +42,6 @@ const (
|
|||
)
|
||||
|
||||
type opcodeSet struct {
|
||||
escapedCode *opcode
|
||||
code *opcode
|
||||
codeLength int
|
||||
}
|
||||
|
@ -198,13 +197,13 @@ func (e *Encoder) encode(header *interfaceHeader, isNil bool) ([]byte, error) {
|
|||
|
||||
if e.enabledIndent {
|
||||
if e.enabledHTMLEscape {
|
||||
return e.runEscapedIndent(ctx, b, codeSet.escapedCode)
|
||||
return e.runEscapedIndent(ctx, b, codeSet.code)
|
||||
} else {
|
||||
return e.runIndent(ctx, b, codeSet.code)
|
||||
}
|
||||
}
|
||||
if e.enabledHTMLEscape {
|
||||
return e.runEscaped(ctx, b, codeSet.escapedCode)
|
||||
return e.runEscaped(ctx, b, codeSet.code)
|
||||
}
|
||||
return e.run(ctx, b, codeSet.code)
|
||||
}
|
||||
|
@ -223,7 +222,6 @@ func (e *Encoder) encode(header *interfaceHeader, isNil bool) ([]byte, error) {
|
|||
code = copyOpcode(code)
|
||||
codeLength := code.totalLength()
|
||||
codeSet := &opcodeSet{
|
||||
escapedCode: toEscaped(code),
|
||||
code: code,
|
||||
codeLength: codeLength,
|
||||
}
|
||||
|
@ -235,13 +233,13 @@ func (e *Encoder) encode(header *interfaceHeader, isNil bool) ([]byte, error) {
|
|||
|
||||
if e.enabledIndent {
|
||||
if e.enabledHTMLEscape {
|
||||
return e.runEscapedIndent(ctx, b, codeSet.escapedCode)
|
||||
return e.runEscapedIndent(ctx, b, codeSet.code)
|
||||
} else {
|
||||
return e.runIndent(ctx, b, codeSet.code)
|
||||
}
|
||||
}
|
||||
if e.enabledHTMLEscape {
|
||||
return e.runEscaped(ctx, b, codeSet.escapedCode)
|
||||
return e.runEscaped(ctx, b, codeSet.code)
|
||||
}
|
||||
return e.run(ctx, b, codeSet.code)
|
||||
}
|
||||
|
|
|
@ -53,20 +53,6 @@ func copyOpcode(code *opcode) *opcode {
|
|||
return code.copy(codeMap)
|
||||
}
|
||||
|
||||
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 {
|
||||
return &opcode{
|
||||
op: op,
|
||||
|
|
13914
encode_optype.go
13914
encode_optype.go
File diff suppressed because it is too large
Load Diff
1047
encode_vm_escaped.go
1047
encode_vm_escaped.go
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue