Reduce code size

This commit is contained in:
Masaaki Goshima 2021-01-11 00:48:20 +09:00
parent b7f79b13eb
commit 35a58e5b5f
2 changed files with 10127 additions and 66398 deletions

View File

@ -134,12 +134,37 @@ func (t opType) codeType() codeType {
return t.toNotIndent().codeType() return t.toNotIndent().codeType()
} }
switch t { if strings.Contains(t.String(), "Struct") {
{{- range $type := .OpNotIndentTypes }} if strings.Contains(t.String(), "End") {
case op{{ $type.Op }}: return codeStructEnd
return code{{ $type.Code }} }
{{- end }} return codeStructField
} }
if strings.Contains(t.String(), "ArrayHead") {
return codeArrayHead
}
if strings.Contains(t.String(), "ArrayElem") {
return codeArrayElem
}
if strings.Contains(t.String(), "SliceHead") {
return codeSliceHead
}
if strings.Contains(t.String(), "SliceElem") {
return codeSliceElem
}
if strings.Contains(t.String(), "MapHead") {
return codeMapHead
}
if strings.Contains(t.String(), "MapKey") {
return codeMapKey
}
if strings.Contains(t.String(), "MapValue") {
return codeMapValue
}
if strings.Contains(t.String(), "MapEnd") {
return codeMapEnd
}
return codeOp return codeOp
} }
@ -161,14 +186,26 @@ func (t opType) toEscaped() opType {
if int(t) >= {{ .OpLen }} { if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().toEscaped()) + {{ .OpLen }}) return opType(int(t.toNotIndent().toEscaped()) + {{ .OpLen }})
} }
if strings.Index(t.String(), "Escaped") > 0 {
return t
}
if t.String() == "String" {
return opType(int(t) + 1)
}
switch t { fieldHeadIdx := strings.Index(t.String(), "Head")
{{- range $type := .OpNotIndentTypes }} if fieldHeadIdx > 0 && strings.Contains(t.String(), "Struct") {
{{- if $type.IsEscaped }} const toEscapedHeadOffset = 36
case op{{ $type.Op }}: return opType(int(t) + toEscapedHeadOffset)
return op{{ call $type.Escaped }} }
{{- end }} fieldIdx := strings.Index(t.String(), "Field")
{{- end }} 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 return t
} }
@ -177,14 +214,22 @@ func (t opType) headToPtrHead() opType {
if int(t) >= {{ .OpLen }} { if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().headToPtrHead()) + {{ .OpLen }}) return opType(int(t.toNotIndent().headToPtrHead()) + {{ .OpLen }})
} }
if strings.Index(t.String(), "PtrHead") > 0 {
return t
}
if strings.Index(t.String(), "PtrAnonymousHead") > 0 {
return t
}
switch t { idx := strings.Index(t.String(), "Field")
{{- range $type := .OpNotIndentTypes }} if idx == -1 {
{{- if $type.IsHeadToPtrHead }} return t
case op{{ $type.Op }}: }
return op{{ call $type.HeadToPtrHead }} suffix := "Ptr"+t.String()[idx+len("Field"):]
{{- end }}
{{- end }} const toPtrOffset = 12
if strings.Contains(opType(int(t) + toPtrOffset).String(), suffix) {
return opType(int(t) + toPtrOffset)
} }
return t return t
} }
@ -193,14 +238,22 @@ func (t opType) headToNPtrHead() opType {
if int(t) >= {{ .OpLen }} { if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().headToNPtrHead()) + {{ .OpLen }}) return opType(int(t.toNotIndent().headToNPtrHead()) + {{ .OpLen }})
} }
if strings.Index(t.String(), "PtrHead") > 0 {
return t
}
if strings.Index(t.String(), "PtrAnonymousHead") > 0 {
return t
}
switch t { idx := strings.Index(t.String(), "Field")
{{- range $type := .OpNotIndentTypes }} if idx == -1 {
{{- if $type.IsHeadToNPtrHead }} return t
case op{{ $type.Op }}: }
return op{{ call $type.HeadToNPtrHead }} suffix := "NPtr"+t.String()[idx+len("Field"):]
{{- end }}
{{- end }} const toPtrOffset = 24
if strings.Contains(opType(int(t) + toPtrOffset).String(), suffix) {
return opType(int(t) + toPtrOffset)
} }
return t return t
} }
@ -210,13 +263,9 @@ func (t opType) headToAnonymousHead() opType {
return opType(int(t.toNotIndent().headToAnonymousHead()) + {{ .OpLen }}) return opType(int(t.toNotIndent().headToAnonymousHead()) + {{ .OpLen }})
} }
switch t { const toAnonymousOffset = 6
{{- range $type := .OpNotIndentTypes }} if strings.Contains(opType(int(t) + toAnonymousOffset).String(), "Anonymous") {
{{- if $type.IsHeadToAnonymousHead }} return opType(int(t) + toAnonymousOffset)
case op{{ $type.Op }}:
return op{{ call $type.HeadToAnonymousHead }}
{{- end }}
{{- end }}
} }
return t return t
} }
@ -226,14 +275,11 @@ func (t opType) headToOmitEmptyHead() opType {
return opType(int(t.toNotIndent().headToOmitEmptyHead()) + {{ .OpLen }}) return opType(int(t.toNotIndent().headToOmitEmptyHead()) + {{ .OpLen }})
} }
switch t { const toOmitEmptyOffset = 2
{{- range $type := .OpNotIndentTypes }} if strings.Contains(opType(int(t) + toOmitEmptyOffset).String(), "OmitEmpty") {
{{- if $type.IsHeadToOmitEmptyHead }} return opType(int(t) + toOmitEmptyOffset)
case op{{ $type.Op }}:
return op{{ call $type.HeadToOmitEmptyHead }}
{{- end }}
{{- end }}
} }
return t return t
} }
@ -242,13 +288,9 @@ func (t opType) headToStringTagHead() opType {
return opType(int(t.toNotIndent().headToStringTagHead()) + {{ .OpLen }}) return opType(int(t.toNotIndent().headToStringTagHead()) + {{ .OpLen }})
} }
switch t { const toStringTagOffset = 4
{{- range $type := .OpNotIndentTypes }} if strings.Contains(opType(int(t) + toStringTagOffset).String(), "StringTag") {
{{- if $type.IsHeadToStringTagHead }} return opType(int(t) + toStringTagOffset)
case op{{ $type.Op }}:
return op{{ call $type.HeadToStringTagHead }}
{{- end }}
{{- end }}
} }
return t return t
} }
@ -273,14 +315,15 @@ func (t opType) ptrHeadToHead() opType {
if int(t) >= {{ .OpLen }} { if int(t) >= {{ .OpLen }} {
return opType(int(t.toNotIndent().ptrHeadToHead()) + {{ .OpLen }}) return opType(int(t.toNotIndent().ptrHeadToHead()) + {{ .OpLen }})
} }
idx := strings.Index(t.String(), "Ptr")
if idx == -1 {
return t
}
suffix := t.String()[idx+len("Ptr"):]
switch t { const toPtrOffset = 12
{{- range $type := .OpNotIndentTypes }} if strings.Contains(opType(int(t) - toPtrOffset).String(), suffix) {
{{- if $type.IsPtrHeadToHead }} return opType(int(t) - toPtrOffset)
case op{{ $type.Op }}:
return op{{ call $type.PtrHeadToHead }}
{{- end }}
{{- end }}
} }
return t return t
} }
@ -306,13 +349,9 @@ func (t opType) fieldToOmitEmptyField() opType {
return opType(int(t.toNotIndent().fieldToOmitEmptyField()) + {{ .OpLen }}) return opType(int(t.toNotIndent().fieldToOmitEmptyField()) + {{ .OpLen }})
} }
switch t { const toOmitEmptyOffset = 1
{{- range $type := .OpNotIndentTypes }} if strings.Contains(opType(int(t) + toOmitEmptyOffset).String(), "OmitEmpty") {
{{- if $type.IsFieldToOmitEmptyField }} return opType(int(t) + toOmitEmptyOffset)
case op{{ $type.Op }}:
return op{{ call $type.FieldToOmitEmptyField }}
{{- end }}
{{- end }}
} }
return t return t
} }
@ -322,13 +361,9 @@ func (t opType) fieldToStringTagField() opType {
return opType(int(t.toNotIndent().fieldToStringTagField()) + {{ .OpLen }}) return opType(int(t.toNotIndent().fieldToStringTagField()) + {{ .OpLen }})
} }
switch t { const toStringTagOffset = 2
{{- range $type := .OpNotIndentTypes }} if strings.Contains(opType(int(t) + toStringTagOffset).String(), "StringTag") {
{{- if $type.IsFieldToStringTagField }} return opType(int(t) + toStringTagOffset)
case op{{ $type.Op }}:
return op{{ call $type.FieldToStringTagField }}
{{- end }}
{{- end }}
} }
return t return t
} }
@ -403,6 +438,9 @@ func (t opType) fieldToStringTagField() opType {
opTypes = append(opTypes, optype) opTypes = append(opTypes, optype)
} }
for _, typ := range append(primitiveTypesUpper, "") { for _, typ := range append(primitiveTypesUpper, "") {
if typ == "EscapedString" || typ == "EscapedStringPtr" || typ == "EscapedStringNPtr" {
continue
}
for _, escapedOrNot := range []string{"", "Escaped"} { for _, escapedOrNot := range []string{"", "Escaped"} {
for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} { for _, ptrOrNot := range []string{"", "Ptr", "NPtr"} {
for _, headType := range []string{"", "Anonymous"} { for _, headType := range []string{"", "Anonymous"} {
@ -415,19 +453,11 @@ func (t opType) fieldToStringTagField() opType {
typ := typ typ := typ
onlyOrNot := onlyOrNot onlyOrNot := onlyOrNot
if onlyOrNot == "Only" { isEscaped := escapedOrNot != ""
/* isString := typ == "String" || typ == "StringPtr" || typ == "StringNPtr"
if typ == "" && ptrOrNot == "Ptr" {
continue if isEscaped && isString {
} typ = "Escaped" + typ
switch typ {
case "Array", "Map", "MapLoad", "Slice",
"Struct", "Recursive", "MarshalJSON", "MarshalText",
"IntString", "Int8String", "Int16String", "Int32String", "Int64String",
"UintString", "Uint8String", "Uint16String", "Uint32String", "Uint64String":
continue
}
*/
} }
op := fmt.Sprintf( op := fmt.Sprintf(
@ -552,12 +582,22 @@ func (t opType) fieldToStringTagField() opType {
} }
} }
for _, typ := range append(primitiveTypesUpper, "") { for _, typ := range append(primitiveTypesUpper, "") {
if typ == "EscapedString" || typ == "EscapedStringPtr" || typ == "EscapedStringNPtr" {
continue
}
for _, escapedOrNot := range []string{"", "Escaped"} { for _, escapedOrNot := range []string{"", "Escaped"} {
for _, opt := range []string{"", "OmitEmpty", "StringTag"} { for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
escapedOrNot := escapedOrNot escapedOrNot := escapedOrNot
opt := opt opt := opt
typ := typ typ := typ
isEscaped := escapedOrNot != ""
isString := typ == "String" || typ == "StringPtr" || typ == "StringNPtr"
if isEscaped && isString {
typ = "Escaped" + typ
}
op := fmt.Sprintf( op := fmt.Sprintf(
"Struct%sField%s%s", "Struct%sField%s%s",
escapedOrNot, escapedOrNot,
@ -621,12 +661,22 @@ func (t opType) fieldToStringTagField() opType {
} }
} }
for _, typ := range append(primitiveTypesUpper, "") { for _, typ := range append(primitiveTypesUpper, "") {
if typ == "EscapedString" || typ == "EscapedStringPtr" || typ == "EscapedStringNPtr" {
continue
}
for _, escapedOrNot := range []string{"", "Escaped"} { for _, escapedOrNot := range []string{"", "Escaped"} {
for _, opt := range []string{"", "OmitEmpty", "StringTag"} { for _, opt := range []string{"", "OmitEmpty", "StringTag"} {
escapedOrNot := escapedOrNot escapedOrNot := escapedOrNot
opt := opt opt := opt
typ := typ typ := typ
isEscaped := escapedOrNot != ""
isString := typ == "String" || typ == "StringPtr" || typ == "StringNPtr"
if isEscaped && isString {
typ = "Escaped" + typ
}
op := fmt.Sprintf( op := fmt.Sprintf(
"Struct%sEnd%s%s", "Struct%sEnd%s%s",
escapedOrNot, escapedOrNot,

File diff suppressed because it is too large Load Diff