gen-dec-array.go.tmpl 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. {{var "v"}} := {{if not isArray}}*{{end}}{{ .Varname }}
  2. {{var "h"}}, {{var "l"}} := z.DecSliceHelperStart() {{/* // helper, containerLenS */}}{{if not isArray}}
  3. var {{var "c"}} bool {{/* // changed */}}
  4. _ = {{var "c"}}{{end}}
  5. if {{var "l"}} == 0 {
  6. {{if isSlice }}if {{var "v"}} == nil {
  7. {{var "v"}} = []{{ .Typ }}{}
  8. {{var "c"}} = true
  9. } else if len({{var "v"}}) != 0 {
  10. {{var "v"}} = {{var "v"}}[:0]
  11. {{var "c"}} = true
  12. } {{else if isChan }}if {{var "v"}} == nil {
  13. {{var "v"}} = make({{ .CTyp }}, 0)
  14. {{var "c"}} = true
  15. }
  16. {{end -}}
  17. } else {
  18. {{var "hl"}} := {{var "l"}} > 0
  19. var {{var "rl"}} int
  20. _ = {{var "rl"}}
  21. {{if isSlice }} if {{var "hl"}} {
  22. if {{var "l"}} > cap({{var "v"}}) {
  23. {{var "rl"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  24. if {{var "rl"}} <= cap({{var "v"}}) {
  25. {{var "v"}} = {{var "v"}}[:{{var "rl"}}]
  26. } else {
  27. {{var "v"}} = make([]{{ .Typ }}, {{var "rl"}})
  28. }
  29. {{var "c"}} = true
  30. } else if {{var "l"}} != len({{var "v"}}) {
  31. {{var "v"}} = {{var "v"}}[:{{var "l"}}]
  32. {{var "c"}} = true
  33. }
  34. }
  35. {{end -}}
  36. var {{var "j"}} int
  37. {{/* // var {{var "dn"}} bool */ -}}
  38. for {{var "j"}} = 0; ({{var "hl"}} && {{var "j"}} < {{var "l"}}) || !({{var "hl"}} || r.CheckBreak()); {{var "j"}}++ { // bounds-check-elimination
  39. {{if not isArray}} if {{var "j"}} == 0 && {{var "v"}} == nil {
  40. if {{var "hl"}} {
  41. {{var "rl"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  42. } else {
  43. {{var "rl"}} = {{if isSlice}}8{{else if isChan}}64{{end}}
  44. }
  45. {{var "v"}} = make({{if isSlice}}[]{{ .Typ }}{{else if isChan}}{{.CTyp}}{{end}}, {{var "rl"}})
  46. {{var "c"}} = true
  47. }
  48. {{end -}}
  49. {{var "h"}}.ElemContainerState({{var "j"}})
  50. {{/* {{var "dn"}} = r.TryDecodeAsNil() */}}{{/* commented out, as decLineVar handles this already each time */ -}}
  51. {{if isChan}}{{ $x := printf "%[1]vvcx%[2]v" .TempVar .Rand }}var {{$x}} {{ .Typ }}
  52. {{ decLineVar $x -}}
  53. {{var "v"}} <- {{ $x }}
  54. // println(">>>> sending ", {{ $x }}, " into ", {{var "v"}}) // TODO: remove this
  55. {{else}}{{/* // if indefinite, etc, then expand the slice if necessary */ -}}
  56. var {{var "db"}} bool
  57. if {{var "j"}} >= len({{var "v"}}) {
  58. {{if isSlice }} {{var "v"}} = append({{var "v"}}, {{ zero }})
  59. {{var "c"}} = true
  60. {{else}} z.DecArrayCannotExpand(len(v), {{var "j"}}+1); {{var "db"}} = true
  61. {{end -}}
  62. }
  63. if {{var "db"}} {
  64. z.DecSwallow()
  65. } else {
  66. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x -}}
  67. }
  68. {{end -}}
  69. }
  70. {{if isSlice}} if {{var "j"}} < len({{var "v"}}) {
  71. {{var "v"}} = {{var "v"}}[:{{var "j"}}]
  72. {{var "c"}} = true
  73. } else if {{var "j"}} == 0 && {{var "v"}} == nil {
  74. {{var "v"}} = make([]{{ .Typ }}, 0)
  75. {{var "c"}} = true
  76. }
  77. {{end -}}
  78. }
  79. {{var "h"}}.End()
  80. {{if not isArray }}if {{var "c"}} {
  81. *{{ .Varname }} = {{var "v"}}
  82. }
  83. {{end -}}