gen-dec-array.go.tmpl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. {{var "v"}} := {{if not isArray}}*{{end}}{{ .Varname }}
  2. {{var "h"}}, {{var "l"}} := z.DecSliceHelperStart() {{/* // helper, containerLenS */}}
  3. var {{var "c"}} bool {{/* // changed */}}
  4. if {{var "l"}} == 0 {
  5. {{if isSlice }}if {{var "v"}} == nil {
  6. {{var "v"}} = []{{ .Typ }}{}
  7. {{var "c"}} = true
  8. } else if len({{var "v"}}) != 0 {
  9. {{var "v"}} = {{var "v"}}[:0]
  10. {{var "c"}} = true
  11. } {{end}} {{if isChan }}if {{var "v"}} == nil {
  12. {{var "v"}} = make({{ .CTyp }}, 0)
  13. {{var "c"}} = true
  14. } {{end}}
  15. } else if {{var "l"}} > 0 {
  16. {{if isChan }}if {{var "v"}} == nil {
  17. {{var "rl"}}, _ = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  18. {{var "v"}} = make({{ .CTyp }}, {{var "rl"}})
  19. {{var "c"}} = true
  20. }
  21. for {{var "r"}} := 0; {{var "r"}} < {{var "l"}}; {{var "r"}}++ {
  22. {{var "h"}}.ElemContainerState({{var "r"}})
  23. var {{var "t"}} {{ .Typ }}
  24. {{ $x := printf "%st%s" .TempVar .Rand }}{{ decLineVar $x }}
  25. {{var "v"}} <- {{var "t"}}
  26. }
  27. {{ else }} var {{var "rr"}}, {{var "rl"}} int {{/* // num2read, length of slice/array/chan */}}
  28. var {{var "rt"}} bool {{/* truncated */}}
  29. if {{var "l"}} > cap({{var "v"}}) {
  30. {{if isArray }}z.DecArrayCannotExpand(len({{var "v"}}), {{var "l"}})
  31. {{ else }}{{if not .Immutable }}
  32. {{var "rg"}} := len({{var "v"}}) > 0
  33. {{var "v2"}} := {{var "v"}} {{end}}
  34. {{var "rl"}}, {{var "rt"}} = z.DecInferLen({{var "l"}}, z.DecBasicHandle().MaxInitLen, {{ .Size }})
  35. if {{var "rt"}} {
  36. if {{var "rl"}} <= cap({{var "v"}}) {
  37. {{var "v"}} = {{var "v"}}[:{{var "rl"}}]
  38. } else {
  39. {{var "v"}} = make([]{{ .Typ }}, {{var "rl"}})
  40. }
  41. } else {
  42. {{var "v"}} = make([]{{ .Typ }}, {{var "rl"}})
  43. }
  44. {{var "c"}} = true
  45. {{var "rr"}} = len({{var "v"}}) {{if not .Immutable }}
  46. if {{var "rg"}} { copy({{var "v"}}, {{var "v2"}}) } {{end}} {{end}}{{/* end not Immutable, isArray */}}
  47. } {{if isSlice }} else if {{var "l"}} != len({{var "v"}}) {
  48. {{var "v"}} = {{var "v"}}[:{{var "l"}}]
  49. {{var "c"}} = true
  50. } {{end}} {{/* end isSlice:47 */}}
  51. {{var "j"}} := 0
  52. for ; {{var "j"}} < {{var "rr"}} ; {{var "j"}}++ {
  53. {{var "h"}}.ElemContainerState({{var "j"}})
  54. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
  55. }
  56. {{if isArray }}for ; {{var "j"}} < {{var "l"}} ; {{var "j"}}++ {
  57. {{var "h"}}.ElemContainerState({{var "j"}})
  58. z.DecSwallow()
  59. }
  60. {{ else }}if {{var "rt"}} {
  61. for ; {{var "j"}} < {{var "l"}} ; {{var "j"}}++ {
  62. {{var "v"}} = append({{var "v"}}, {{ zero}})
  63. {{var "h"}}.ElemContainerState({{var "j"}})
  64. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
  65. }
  66. } {{end}} {{/* end isArray:56 */}}
  67. {{end}} {{/* end isChan:16 */}}
  68. } else { {{/* len < 0 */}}
  69. {{var "j"}} := 0
  70. for ; !r.CheckBreak(); {{var "j"}}++ {
  71. {{if isChan }}
  72. {{var "h"}}.ElemContainerState({{var "j"}})
  73. var {{var "t"}} {{ .Typ }}
  74. {{ $x := printf "%st%s" .TempVar .Rand }}{{ decLineVar $x }}
  75. {{var "v"}} <- {{var "t"}}
  76. {{ else }}
  77. if {{var "j"}} >= len({{var "v"}}) {
  78. {{if isArray }}z.DecArrayCannotExpand(len({{var "v"}}), {{var "j"}}+1)
  79. {{ else }}{{var "v"}} = append({{var "v"}}, {{zero}})// var {{var "z"}} {{ .Typ }}
  80. {{var "c"}} = true {{end}}
  81. }
  82. {{var "h"}}.ElemContainerState({{var "j"}})
  83. if {{var "j"}} < len({{var "v"}}) {
  84. {{ $x := printf "%[1]vv%[2]v[%[1]vj%[2]v]" .TempVar .Rand }}{{ decLineVar $x }}
  85. } else {
  86. z.DecSwallow()
  87. }
  88. {{end}}
  89. }
  90. {{if isSlice }}if {{var "j"}} < len({{var "v"}}) {
  91. {{var "v"}} = {{var "v"}}[:{{var "j"}}]
  92. {{var "c"}} = true
  93. } else if {{var "j"}} == 0 && {{var "v"}} == nil {
  94. {{var "v"}} = []{{ .Typ }}{}
  95. {{var "c"}} = true
  96. }{{end}}
  97. }
  98. {{var "h"}}.End()
  99. {{if not isArray }}if {{var "c"}} {
  100. *{{ .Varname }} = {{var "v"}}
  101. }{{end}}