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 // This package implements translation between | 5 // This package implements translation between |
6 // unsigned integer values and byte sequences. | 6 // unsigned integer values and byte sequences. |
7 package binary | 7 package binary |
8 | 8 |
9 import ( | 9 import ( |
10 "math"; | 10 "math"; |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 buf []byte; | 203 buf []byte; |
204 } | 204 } |
205 | 205 |
206 type encoder struct { | 206 type encoder struct { |
207 order ByteOrder; | 207 order ByteOrder; |
208 buf []byte; | 208 buf []byte; |
209 } | 209 } |
210 | 210 |
211 func (d *decoder) uint8() uint8 { | 211 func (d *decoder) uint8() uint8 { |
212 x := d.buf[0]; | 212 x := d.buf[0]; |
213 » d.buf = d.buf[1:len(d.buf)]; | 213 » d.buf = d.buf[1:]; |
214 return x; | 214 return x; |
215 } | 215 } |
216 | 216 |
217 func (e *encoder) uint8(x uint8) { | 217 func (e *encoder) uint8(x uint8) { |
218 e.buf[0] = x; | 218 e.buf[0] = x; |
219 » e.buf = e.buf[1:len(e.buf)]; | 219 » e.buf = e.buf[1:]; |
220 } | 220 } |
221 | 221 |
222 func (d *decoder) uint16() uint16 { | 222 func (d *decoder) uint16() uint16 { |
223 x := d.order.Uint16(d.buf[0:2]); | 223 x := d.order.Uint16(d.buf[0:2]); |
224 » d.buf = d.buf[2:len(d.buf)]; | 224 » d.buf = d.buf[2:]; |
225 return x; | 225 return x; |
226 } | 226 } |
227 | 227 |
228 func (e *encoder) uint16(x uint16) { | 228 func (e *encoder) uint16(x uint16) { |
229 e.order.PutUint16(e.buf[0:2], x); | 229 e.order.PutUint16(e.buf[0:2], x); |
230 » e.buf = e.buf[2:len(e.buf)]; | 230 » e.buf = e.buf[2:]; |
231 } | 231 } |
232 | 232 |
233 func (d *decoder) uint32() uint32 { | 233 func (d *decoder) uint32() uint32 { |
234 x := d.order.Uint32(d.buf[0:4]); | 234 x := d.order.Uint32(d.buf[0:4]); |
235 » d.buf = d.buf[4:len(d.buf)]; | 235 » d.buf = d.buf[4:]; |
236 return x; | 236 return x; |
237 } | 237 } |
238 | 238 |
239 func (e *encoder) uint32(x uint32) { | 239 func (e *encoder) uint32(x uint32) { |
240 e.order.PutUint32(e.buf[0:4], x); | 240 e.order.PutUint32(e.buf[0:4], x); |
241 » e.buf = e.buf[4:len(e.buf)]; | 241 » e.buf = e.buf[4:]; |
242 } | 242 } |
243 | 243 |
244 func (d *decoder) uint64() uint64 { | 244 func (d *decoder) uint64() uint64 { |
245 x := d.order.Uint64(d.buf[0:8]); | 245 x := d.order.Uint64(d.buf[0:8]); |
246 » d.buf = d.buf[8:len(d.buf)]; | 246 » d.buf = d.buf[8:]; |
247 return x; | 247 return x; |
248 } | 248 } |
249 | 249 |
250 func (e *encoder) uint64(x uint64) { | 250 func (e *encoder) uint64(x uint64) { |
251 e.order.PutUint64(e.buf[0:8], x); | 251 e.order.PutUint64(e.buf[0:8], x); |
252 » e.buf = e.buf[8:len(e.buf)]; | 252 » e.buf = e.buf[8:]; |
253 } | 253 } |
254 | 254 |
255 func (d *decoder) int8() int8 { return int8(d.uint8()) } | 255 func (d *decoder) int8() int8 { return int8(d.uint8()) } |
256 | 256 |
257 func (e *encoder) int8(x int8) { e.uint8(uint8(x)) } | 257 func (e *encoder) int8(x int8) { e.uint8(uint8(x)) } |
258 | 258 |
259 func (d *decoder) int16() int16 { return int16(d.uint16()) } | 259 func (d *decoder) int16() int16 { return int16(d.uint16()) } |
260 | 260 |
261 func (e *encoder) int16(x int16) { e.uint16(uint16(x)) } | 261 func (e *encoder) int16(x int16) { e.uint16(uint16(x)) } |
262 | 262 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 case *reflect.Int32Value: | 332 case *reflect.Int32Value: |
333 e.int32(v.Get()) | 333 e.int32(v.Get()) |
334 case *reflect.Int64Value: | 334 case *reflect.Int64Value: |
335 e.int64(v.Get()) | 335 e.int64(v.Get()) |
336 case *reflect.Float32Value: | 336 case *reflect.Float32Value: |
337 e.uint32(math.Float32bits(v.Get())) | 337 e.uint32(math.Float32bits(v.Get())) |
338 case *reflect.Float64Value: | 338 case *reflect.Float64Value: |
339 e.uint64(math.Float64bits(v.Get())) | 339 e.uint64(math.Float64bits(v.Get())) |
340 } | 340 } |
341 } | 341 } |
OLD | NEW |