另客网go项目公用的代码库

gen-dec-array.go.tmpl 2.8KB

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