marshal and unmarshal in place in arrays

that avoids a copy of each item.
This commit is contained in:
Nicolas S. Dade
2016-09-23 01:35:27 -07:00
parent bc1638cf78
commit 0f22c066ac
2 changed files with 3 additions and 7 deletions
+1 -4
View File
@@ -124,7 +124,6 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
}
case reflect.Array:
tmpVar := g.uniqueVarName()
iterVar := g.uniqueVarName()
elem := t.Elem()
@@ -145,11 +144,9 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
fmt.Fprintln(g.out, ws+" in.Delim('[')")
fmt.Fprintln(g.out, ws+" "+iterVar+" := 0")
fmt.Fprintln(g.out, ws+" for !in.IsDelim(']') && "+iterVar+" < "+fmt.Sprint(length)+" {")
fmt.Fprintln(g.out, ws+" var "+tmpVar+" "+g.getType(elem))
g.genTypeDecoder(elem, tmpVar, tags, indent+2)
g.genTypeDecoder(elem, out+"["+iterVar+"]", tags, indent+2)
fmt.Fprintln(g.out, ws+" "+out+"["+iterVar+"] = "+tmpVar)
fmt.Fprintln(g.out, ws+" "+iterVar+"++")
fmt.Fprintln(g.out, ws+" in.WantComma()")
fmt.Fprintln(g.out, ws+" }")
+2 -3
View File
@@ -140,18 +140,17 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT
case reflect.Array:
elem := t.Elem()
iVar := g.uniqueVarName()
vVar := g.uniqueVarName()
if t.Elem().Kind() == reflect.Uint8 {
fmt.Fprintln(g.out, ws+"out.Base64Bytes("+in+"[:])")
} else {
fmt.Fprintln(g.out, ws+"out.RawByte('[')")
fmt.Fprintln(g.out, ws+"for "+iVar+", "+vVar+" := range "+in+" {")
fmt.Fprintln(g.out, ws+"for "+iVar+" := range "+in+" {")
fmt.Fprintln(g.out, ws+" if "+iVar+" > 0 {")
fmt.Fprintln(g.out, ws+" out.RawByte(',')")
fmt.Fprintln(g.out, ws+" }")
g.genTypeEncoder(elem, vVar, tags, indent+1)
g.genTypeEncoder(elem, in+"["+iVar+"]", tags, indent+1)
fmt.Fprintln(g.out, ws+"}")
fmt.Fprintln(g.out, ws+"out.RawByte(']')")