Chromium Code Reviews| Index: go/src/infra/gae/libs/wrapper/memory/binutils.go |
| diff --git a/go/src/infra/gae/libs/wrapper/memory/binutils.go b/go/src/infra/gae/libs/wrapper/memory/binutils.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b66a22e85e3048c70e35e977303f18c1a10c5738 |
| --- /dev/null |
| +++ b/go/src/infra/gae/libs/wrapper/memory/binutils.go |
| @@ -0,0 +1,79 @@ |
| +// Copyright 2015 The Chromium 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 memory |
| + |
| +import ( |
| + "bytes" |
| + "encoding/binary" |
| + "fmt" |
| + "math" |
| + |
| + "github.com/luci/luci-go/common/funnybase" |
| +) |
| + |
| +func writeString(buf *bytes.Buffer, s string) { |
| + funnybase.WriteUint(buf, uint64(len(s))) |
| + buf.WriteString(s) |
|
Vadim Sh.
2015/05/24 19:43:26
writeBytes(buf, []byte(s))
to be symmetric with r
iannucci
2015/05/24 20:33:54
I did this to avoid a copy (which I think is what
|
| +} |
| + |
| +func readString(buf *bytes.Buffer) (string, error) { |
| + b, err := readBytes(buf) |
| + if err != nil { |
| + return "", err |
| + } |
| + return string(b), nil |
| +} |
| + |
| +func writeBytes(buf *bytes.Buffer, b []byte) { |
| + funnybase.WriteUint(buf, uint64(len(b))) |
| + buf.Write(b) |
| +} |
| + |
| +func readBytes(buf *bytes.Buffer) ([]byte, error) { |
| + val, err := funnybase.ReadUint(buf) |
|
Vadim Sh.
2015/05/24 19:43:26
I don't fully understand yet where this function i
iannucci
2015/05/24 20:33:54
Good idea. It can actually be 2MB I think (since i
|
| + if err != nil { |
| + return nil, err |
| + } |
| + retBuf := make([]byte, val) |
| + n, _ := buf.Read(retBuf) // err is either io.EOF or nil for bytes.Buffer |
| + if uint64(n) != val { |
| + return nil, fmt.Errorf("readBytes: expected %d bytes but read %d", val, n) |
| + } |
| + return retBuf, err |
| +} |
| + |
| +func writeFloat64(buf *bytes.Buffer, v float64) { |
| + // byte-ordered floats http://stereopsis.com/radix.html |
| + bits := math.Float64bits(v) |
| + bits = bits ^ (-(bits >> 63) | (1 << 63)) |
| + data := make([]byte, 8) |
| + binary.BigEndian.PutUint64(data, bits) |
| + buf.Write(data) |
| +} |
| + |
| +func readFloat64(buf *bytes.Buffer) (float64, error) { |
| + // byte-ordered floats http://stereopsis.com/radix.html |
| + data := make([]byte, 8) |
| + _, err := buf.Read(data) |
| + if err != nil { |
| + return 0, err |
| + } |
| + bits := binary.BigEndian.Uint64(data) |
| + return math.Float64frombits(bits ^ (((bits >> 63) - 1) | (1 << 63))), nil |
| +} |
| + |
| +func btoi(b bool) byte { |
| + if b { |
| + return 1 |
| + } |
| + return 0 |
| +} |
| + |
| +func itob(v byte) bool { |
| + if v != 0 { |
| + return true |
| + } |
| + return false |
| +} |