Chromium Code Reviews| 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..c24840cb09c2aca2cc2b5e35f23985bb7a32771d |
| --- /dev/null |
| +++ b/go/src/infra/libs/infra_util/buf.go |
| @@ -0,0 +1,40 @@ |
| +// 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 { |
|
Vadim Sh.
2014/10/21 15:27:00
"
An exclamation that, when uttered in conjunction
|
| + 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 |
| + } |
| +} |