Remove indent optype

This commit is contained in:
Masaaki Goshima 2021-01-11 19:05:06 +09:00
parent 06906637f2
commit 4cfb919183
6 changed files with 6322 additions and 11978 deletions

View File

@ -14,7 +14,6 @@ import (
type opType struct { type opType struct {
Op string Op string
Code string Code string
Indent func() string
Escaped func() string Escaped func() string
HeadToPtrHead func() string HeadToPtrHead func() string
HeadToNPtrHead func() string HeadToNPtrHead func() string
@ -76,7 +75,6 @@ func createOpType(op, code string) opType {
return opType{ return opType{
Op: op, Op: op,
Code: code, Code: code,
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
Escaped: func() string { return op }, Escaped: func() string { return op },
HeadToPtrHead: func() string { return op }, HeadToPtrHead: func() string { return op },
HeadToNPtrHead: func() string { return op }, HeadToNPtrHead: func() string { return op },
@ -116,12 +114,8 @@ const (
) )
func (t opType) String() string { func (t opType) String() string {
if int(t) >= {{ .OpLen }} {
return t.toNotIndent().String() + "Indent"
}
switch t { switch t {
{{- range $type := .OpNotIndentTypes }} {{- range $type := .OpTypes }}
case op{{ $type.Op }}: case op{{ $type.Op }}:
return "{{ $type.Op }}" return "{{ $type.Op }}"
{{- end }} {{- end }}
@ -130,10 +124,6 @@ func (t opType) String() string {
} }
func (t opType) codeType() codeType { func (t opType) codeType() codeType {
if int(t) >= {{ .OpLen }} {
return t.toNotIndent().codeType()
}
if strings.Contains(t.String(), "Struct") { if strings.Contains(t.String(), "Struct") {
if strings.Contains(t.String(), "End") { if strings.Contains(t.String(), "End") {
return codeStructEnd return codeStructEnd
@ -168,24 +158,7 @@ func (t opType) codeType() codeType {
return codeOp return codeOp
} }
func (t opType) toNotIndent() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t) - {{ .OpLen }})
}
return t
}
func (t opType) toIndent() opType {
if int(t) >= {{ .OpLen }} {
return t
}
return opType(int(t) + {{ .OpLen }})
}
func (t opType) toEscaped() opType { func (t opType) toEscaped() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().toEscaped()) + {{ .OpLen }})
}
if strings.Index(t.String(), "Escaped") > 0 { if strings.Index(t.String(), "Escaped") > 0 {
return t return t
} }
@ -211,9 +184,6 @@ func (t opType) toEscaped() opType {
} }
func (t opType) headToPtrHead() opType { func (t opType) headToPtrHead() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().headToPtrHead()) + {{ .OpLen }})
}
if strings.Index(t.String(), "PtrHead") > 0 { if strings.Index(t.String(), "PtrHead") > 0 {
return t return t
} }
@ -235,9 +205,6 @@ func (t opType) headToPtrHead() opType {
} }
func (t opType) headToNPtrHead() opType { func (t opType) headToNPtrHead() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().headToNPtrHead()) + {{ .OpLen }})
}
if strings.Index(t.String(), "PtrHead") > 0 { if strings.Index(t.String(), "PtrHead") > 0 {
return t return t
} }
@ -259,10 +226,6 @@ func (t opType) headToNPtrHead() opType {
} }
func (t opType) headToAnonymousHead() opType { func (t opType) headToAnonymousHead() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().headToAnonymousHead()) + {{ .OpLen }})
}
const toAnonymousOffset = 6 const toAnonymousOffset = 6
if strings.Contains(opType(int(t) + toAnonymousOffset).String(), "Anonymous") { if strings.Contains(opType(int(t) + toAnonymousOffset).String(), "Anonymous") {
return opType(int(t) + toAnonymousOffset) return opType(int(t) + toAnonymousOffset)
@ -271,10 +234,6 @@ func (t opType) headToAnonymousHead() opType {
} }
func (t opType) headToOmitEmptyHead() opType { func (t opType) headToOmitEmptyHead() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().headToOmitEmptyHead()) + {{ .OpLen }})
}
const toOmitEmptyOffset = 2 const toOmitEmptyOffset = 2
if strings.Contains(opType(int(t) + toOmitEmptyOffset).String(), "OmitEmpty") { if strings.Contains(opType(int(t) + toOmitEmptyOffset).String(), "OmitEmpty") {
return opType(int(t) + toOmitEmptyOffset) return opType(int(t) + toOmitEmptyOffset)
@ -284,10 +243,6 @@ func (t opType) headToOmitEmptyHead() opType {
} }
func (t opType) headToStringTagHead() opType { func (t opType) headToStringTagHead() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().headToStringTagHead()) + {{ .OpLen }})
}
const toStringTagOffset = 4 const toStringTagOffset = 4
if strings.Contains(opType(int(t) + toStringTagOffset).String(), "StringTag") { if strings.Contains(opType(int(t) + toStringTagOffset).String(), "StringTag") {
return opType(int(t) + toStringTagOffset) return opType(int(t) + toStringTagOffset)
@ -296,10 +251,6 @@ func (t opType) headToStringTagHead() opType {
} }
func (t opType) headToOnlyHead() opType { func (t opType) headToOnlyHead() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().headToOnlyHead()) + {{ .OpLen }})
}
if strings.HasSuffix(t.String(), "Head") || strings.HasSuffix(t.String(), "HeadOmitEmpty") || strings.HasSuffix(t.String(), "HeadStringTag") { if strings.HasSuffix(t.String(), "Head") || strings.HasSuffix(t.String(), "HeadOmitEmpty") || strings.HasSuffix(t.String(), "HeadStringTag") {
return t return t
} }
@ -312,9 +263,6 @@ func (t opType) headToOnlyHead() opType {
} }
func (t opType) ptrHeadToHead() opType { func (t opType) ptrHeadToHead() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().ptrHeadToHead()) + {{ .OpLen }})
}
idx := strings.Index(t.String(), "Ptr") idx := strings.Index(t.String(), "Ptr")
if idx == -1 { if idx == -1 {
return t return t
@ -329,12 +277,8 @@ func (t opType) ptrHeadToHead() opType {
} }
func (t opType) fieldToEnd() opType { func (t opType) fieldToEnd() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().fieldToEnd()) + {{ .OpLen }})
}
switch t { switch t {
{{- range $type := .OpNotIndentTypes }} {{- range $type := .OpTypes }}
{{- if $type.IsFieldToEnd }} {{- if $type.IsFieldToEnd }}
case op{{ $type.Op }}: case op{{ $type.Op }}:
return op{{ call $type.FieldToEnd }} return op{{ call $type.FieldToEnd }}
@ -345,10 +289,6 @@ func (t opType) fieldToEnd() opType {
} }
func (t opType) fieldToOmitEmptyField() opType { func (t opType) fieldToOmitEmptyField() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().fieldToOmitEmptyField()) + {{ .OpLen }})
}
const toOmitEmptyOffset = 1 const toOmitEmptyOffset = 1
if strings.Contains(opType(int(t) + toOmitEmptyOffset).String(), "OmitEmpty") { if strings.Contains(opType(int(t) + toOmitEmptyOffset).String(), "OmitEmpty") {
return opType(int(t) + toOmitEmptyOffset) return opType(int(t) + toOmitEmptyOffset)
@ -357,10 +297,6 @@ func (t opType) fieldToOmitEmptyField() opType {
} }
func (t opType) fieldToStringTagField() opType { func (t opType) fieldToStringTagField() opType {
if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().fieldToStringTagField()) + {{ .OpLen }})
}
const toStringTagOffset = 2 const toStringTagOffset = 2
if strings.Contains(opType(int(t) + toStringTagOffset).String(), "StringTag") { if strings.Contains(opType(int(t) + toStringTagOffset).String(), "StringTag") {
return opType(int(t) + toStringTagOffset) return opType(int(t) + toStringTagOffset)
@ -472,7 +408,6 @@ func (t opType) fieldToStringTagField() opType {
opTypes = append(opTypes, opType{ opTypes = append(opTypes, opType{
Op: op, Op: op,
Code: "StructField", Code: "StructField",
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
Escaped: func() string { Escaped: func() string {
switch typ { switch typ {
case "String", "StringPtr", "StringNPtr": case "String", "StringPtr", "StringNPtr":
@ -607,7 +542,6 @@ func (t opType) fieldToStringTagField() opType {
opTypes = append(opTypes, opType{ opTypes = append(opTypes, opType{
Op: op, Op: op,
Code: "StructField", Code: "StructField",
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
Escaped: func() string { Escaped: func() string {
switch typ { switch typ {
case "String", "StringPtr", "StringNPtr": case "String", "StringPtr", "StringNPtr":
@ -686,7 +620,6 @@ func (t opType) fieldToStringTagField() opType {
opTypes = append(opTypes, opType{ opTypes = append(opTypes, opType{
Op: op, Op: op,
Code: "StructEnd", Code: "StructEnd",
Indent: func() string { return fmt.Sprintf("%sIndent", op) },
Escaped: func() string { Escaped: func() string {
switch typ { switch typ {
case "String", "StringPtr", "StringNPtr": case "String", "StringPtr", "StringNPtr":
@ -716,59 +649,15 @@ func (t opType) fieldToStringTagField() opType {
} }
} }
} }
indentOpTypes := []opType{}
for _, typ := range opTypes {
typ := typ
indentOpTypes = append(indentOpTypes, opType{
Op: fmt.Sprintf("%sIndent", typ.Op),
Code: typ.Code,
Indent: func() string { return fmt.Sprintf("%sIndent", typ.Op) },
Escaped: func() string { return fmt.Sprintf("%sIndent", typ.Escaped()) },
HeadToPtrHead: func() string {
return fmt.Sprintf("%sIndent", typ.HeadToPtrHead())
},
HeadToNPtrHead: func() string {
return fmt.Sprintf("%sIndent", typ.HeadToNPtrHead())
},
HeadToAnonymousHead: func() string {
return fmt.Sprintf("%sIndent", typ.HeadToAnonymousHead())
},
HeadToOmitEmptyHead: func() string {
return fmt.Sprintf("%sIndent", typ.HeadToOmitEmptyHead())
},
HeadToStringTagHead: func() string {
return fmt.Sprintf("%sIndent", typ.HeadToStringTagHead())
},
HeadToOnlyHead: func() string {
return fmt.Sprintf("%sIndent", typ.HeadToOnlyHead())
},
PtrHeadToHead: func() string {
return fmt.Sprintf("%sIndent", typ.PtrHeadToHead())
},
FieldToOmitEmptyField: func() string {
return fmt.Sprintf("%sIndent", typ.FieldToOmitEmptyField())
},
FieldToStringTagField: func() string {
return fmt.Sprintf("%sIndent", typ.FieldToStringTagField())
},
FieldToEnd: func() string {
return fmt.Sprintf("%sIndent", typ.FieldToEnd())
},
})
}
var b bytes.Buffer var b bytes.Buffer
if err := tmpl.Execute(&b, struct { if err := tmpl.Execute(&b, struct {
CodeTypes []string CodeTypes []string
OpTypes []opType OpTypes []opType
OpNotIndentTypes []opType
OpLen int OpLen int
OpIndentLen int
}{ }{
CodeTypes: codeTypes, CodeTypes: codeTypes,
OpTypes: append(opTypes, indentOpTypes...), OpTypes: opTypes,
OpNotIndentTypes: opTypes,
OpLen: len(opTypes), OpLen: len(opTypes),
OpIndentLen: len(indentOpTypes),
}); err != nil { }); err != nil {
return err return err
} }

View File

@ -43,9 +43,7 @@ const (
type opcodeSet struct { type opcodeSet struct {
escapedCode *opcode escapedCode *opcode
escapedCodeIndent *opcode
code *opcode code *opcode
codeIndent *opcode
codeLength int codeLength int
} }
@ -200,9 +198,9 @@ func (e *Encoder) encode(header *interfaceHeader, isNil bool) ([]byte, error) {
if e.enabledIndent { if e.enabledIndent {
if e.enabledHTMLEscape { if e.enabledHTMLEscape {
return e.runEscapedIndent(ctx, b, codeSet.escapedCodeIndent) return e.runEscapedIndent(ctx, b, codeSet.escapedCode)
} else { } else {
return e.runIndent(ctx, b, codeSet.codeIndent) return e.runIndent(ctx, b, codeSet.code)
} }
} }
if e.enabledHTMLEscape { if e.enabledHTMLEscape {
@ -223,13 +221,10 @@ func (e *Encoder) encode(header *interfaceHeader, isNil bool) ([]byte, error) {
return nil, err return nil, err
} }
code = copyOpcode(code) code = copyOpcode(code)
codeIndent := toIndent(code)
codeLength := code.totalLength() codeLength := code.totalLength()
codeSet := &opcodeSet{ codeSet := &opcodeSet{
escapedCode: toEscaped(code), escapedCode: toEscaped(code),
escapedCodeIndent: toEscaped(codeIndent),
code: code, code: code,
codeIndent: codeIndent,
codeLength: codeLength, codeLength: codeLength,
} }
@ -240,9 +235,9 @@ func (e *Encoder) encode(header *interfaceHeader, isNil bool) ([]byte, error) {
if e.enabledIndent { if e.enabledIndent {
if e.enabledHTMLEscape { if e.enabledHTMLEscape {
return e.runEscapedIndent(ctx, b, codeSet.escapedCodeIndent) return e.runEscapedIndent(ctx, b, codeSet.escapedCode)
} else { } else {
return e.runIndent(ctx, b, codeSet.codeIndent) return e.runIndent(ctx, b, codeSet.code)
} }
} }
if e.enabledHTMLEscape { if e.enabledHTMLEscape {

View File

@ -53,20 +53,6 @@ func copyOpcode(code *opcode) *opcode {
return code.copy(codeMap) return code.copy(codeMap)
} }
func toIndent(c *opcode) *opcode {
c = copyOpcode(c)
for code := c; code.op != opEnd; {
code.op = code.op.toIndent()
switch code.op.codeType() {
case codeArrayElem, codeSliceElem, codeMapKey:
code = code.end
default:
code = code.next
}
}
return c
}
func toEscaped(c *opcode) *opcode { func toEscaped(c *opcode) *opcode {
c = copyOpcode(c) c = copyOpcode(c)
for code := c; code.op != opEnd; { for code := c; code.op != opEnd; {

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff