mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
integer encode/decode
R=rsc DELTA=185 (175 added, 10 deleted, 0 changed) OCL=30863 CL=30871
This commit is contained in:
parent
8f9a953760
commit
b948c437a1
4 changed files with 178 additions and 10 deletions
40
src/pkg/gob/decode.go
Normal file
40
src/pkg/gob/decode.go
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
// Copyright 2009 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package gob
|
||||
|
||||
import (
|
||||
"io";
|
||||
"os";
|
||||
)
|
||||
|
||||
// DecodeUint reads an encoded unsigned integer from r.
|
||||
func DecodeUint(r io.Reader) (x uint64, err os.Error) {
|
||||
var buf [1]byte;
|
||||
for shift := uint(0);; shift += 7 {
|
||||
n, err := r.Read(&buf);
|
||||
if n != 1 {
|
||||
return 0, err
|
||||
}
|
||||
b := uint64(buf[0]);
|
||||
x |= b << shift;
|
||||
if b&0x80 != 0 {
|
||||
x &^= 0x80 << shift;
|
||||
break
|
||||
}
|
||||
}
|
||||
return x, nil;
|
||||
}
|
||||
|
||||
// DecodeInt reads an encoded signed integer from r.
|
||||
func DecodeInt(r io.Reader) (i int64, err os.Error) {
|
||||
x, err := DecodeUint(r);
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if x & 1 != 0 {
|
||||
return ^int64(x>>1), nil
|
||||
}
|
||||
return int64(x >> 1), nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue