OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Package binary implements translation between | 5 // Package binary implements translation between |
6 // unsigned integer values and byte sequences | 6 // unsigned integer values and byte sequences |
7 // and the reading and writing of fixed-size values. | 7 // and the reading and writing of fixed-size values. |
8 package binary | 8 package binary |
9 | 9 |
10 import ( | 10 import ( |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } | 162 } |
163 buf := make([]byte, size) | 163 buf := make([]byte, size) |
164 e := &encoder{order: order, buf: buf} | 164 e := &encoder{order: order, buf: buf} |
165 e.value(v) | 165 e.value(v) |
166 _, err := w.Write(buf) | 166 _, err := w.Write(buf) |
167 return err | 167 return err |
168 } | 168 } |
169 | 169 |
170 func TotalSize(v reflect.Value) int { | 170 func TotalSize(v reflect.Value) int { |
171 if sv, ok := v.(*reflect.SliceValue); ok { | 171 if sv, ok := v.(*reflect.SliceValue); ok { |
172 » » elem := sizeof(v.Type().(*reflect.SliceType).Elem()) | 172 » » elem := sizeof(v.Type().Elem()) |
173 if elem < 0 { | 173 if elem < 0 { |
174 return -1 | 174 return -1 |
175 } | 175 } |
176 return sv.Len() * elem | 176 return sv.Len() * elem |
177 } | 177 } |
178 return sizeof(v.Type()) | 178 return sizeof(v.Type()) |
179 } | 179 } |
180 | 180 |
181 func sizeof(v reflect.Type) int { | 181 func sizeof(v reflect.Type) int { |
182 » switch t := v.(type) { | 182 » switch t := v; t.Kind() { |
183 » case *reflect.ArrayType: | 183 » case reflect.Array: |
184 n := sizeof(t.Elem()) | 184 n := sizeof(t.Elem()) |
185 if n < 0 { | 185 if n < 0 { |
186 return -1 | 186 return -1 |
187 } | 187 } |
188 return t.Len() * n | 188 return t.Len() * n |
189 | 189 |
190 » case *reflect.StructType: | 190 » case reflect.Struct: |
191 sum := 0 | 191 sum := 0 |
192 for i, n := 0, t.NumField(); i < n; i++ { | 192 for i, n := 0, t.NumField(); i < n; i++ { |
193 s := sizeof(t.Field(i).Type) | 193 s := sizeof(t.Field(i).Type) |
194 if s < 0 { | 194 if s < 0 { |
195 return -1 | 195 return -1 |
196 } | 196 } |
197 sum += s | 197 sum += s |
198 } | 198 } |
199 return sum | 199 return sum |
200 | 200 |
201 » case *reflect.UintType, *reflect.IntType, *reflect.FloatType, *reflect.C
omplexType: | 201 » case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflec
t.Uint64, reflect.Uintptr, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int
32, reflect.Int64, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.
Complex128: |
202 switch t := t.Kind(); t { | 202 switch t := t.Kind(); t { |
203 case reflect.Int, reflect.Uint, reflect.Uintptr: | 203 case reflect.Int, reflect.Uint, reflect.Uintptr: |
204 return -1 | 204 return -1 |
205 } | 205 } |
206 return int(v.Size()) | 206 return int(v.Size()) |
207 } | 207 } |
208 return -1 | 208 return -1 |
209 } | 209 } |
210 | 210 |
211 type decoder struct { | 211 type decoder struct { |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 x := v.Get() | 401 x := v.Get() |
402 e.uint32(math.Float32bits(float32(real(x)))) | 402 e.uint32(math.Float32bits(float32(real(x)))) |
403 e.uint32(math.Float32bits(float32(imag(x)))) | 403 e.uint32(math.Float32bits(float32(imag(x)))) |
404 case reflect.Complex128: | 404 case reflect.Complex128: |
405 x := v.Get() | 405 x := v.Get() |
406 e.uint64(math.Float64bits(real(x))) | 406 e.uint64(math.Float64bits(real(x))) |
407 e.uint64(math.Float64bits(imag(x))) | 407 e.uint64(math.Float64bits(imag(x))) |
408 } | 408 } |
409 } | 409 } |
410 } | 410 } |
OLD | NEW |