| Index: go/src/infra/libs/infra_util/buf.go
|
| diff --git a/go/src/infra/libs/infra_util/buf.go b/go/src/infra/libs/infra_util/buf.go
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..fda55b6b474a94b5e165723dedb04c5e159024cc
|
| --- /dev/null
|
| +++ b/go/src/infra/libs/infra_util/buf.go
|
| @@ -0,0 +1,41 @@
|
| +// Copyright 2014 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 infra_util
|
| +
|
| +import (
|
| + "io"
|
| +)
|
| +
|
| +type Nomable interface {
|
| + ReadString(delim byte) (string, error)
|
| +}
|
| +
|
| +// Returns `func(byte) string` which will read from |buf| until the byte,
|
| +// returning the string read. If an error is encountered, this will panic.
|
| +func Nom(buf Nomable) func(byte) string {
|
| + return func(delim byte) string {
|
| + ret, err := buf.ReadString(delim)
|
| + if err != nil {
|
| + panic(err)
|
| + }
|
| + return ret[:len(ret)-1]
|
| + }
|
| +}
|
| +
|
| +// Returns `func (int) []byte` which will read the specified number of bytes
|
| +// from |buf|, or panic.
|
| +func Yoink(buf io.Reader) func(int) []byte {
|
| + return func(num int) []byte {
|
| + ret := make([]byte, num)
|
| + i, err := io.ReadFull(buf, ret)
|
| + if err != nil {
|
| + panic(err)
|
| + }
|
| + if i != len(ret) {
|
| + panic("yoink: failed to read enough data")
|
| + }
|
| + return ret
|
| + }
|
| +}
|
|
|