| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package infra_util |
| 6 |
| 7 import ( |
| 8 "io" |
| 9 ) |
| 10 |
| 11 type Nomable interface { |
| 12 ReadString(delim byte) (string, error) |
| 13 } |
| 14 |
| 15 // Returns `func(byte) string` which will read from |buf| until the byte, |
| 16 // returning the string read. If an error is encountered, this will panic. |
| 17 func Nom(buf Nomable) func(byte) string { |
| 18 return func(delim byte) string { |
| 19 ret, err := buf.ReadString(delim) |
| 20 if err != nil { |
| 21 panic(err) |
| 22 } |
| 23 return ret[:len(ret)-1] |
| 24 } |
| 25 } |
| 26 |
| 27 // Returns `func (int) []byte` which will read the specified number of bytes |
| 28 // from |buf|, or panic. |
| 29 func Yoink(buf io.Reader) func(int) []byte { |
| 30 return func(num int) []byte { |
| 31 ret := make([]byte, num) |
| 32 i, err := io.ReadFull(buf, ret) |
| 33 if err != nil { |
| 34 panic(err) |
| 35 } |
| 36 if i != len(ret) { |
| 37 panic("yoink: failed to read enough data") |
| 38 } |
| 39 return ret |
| 40 } |
| 41 } |
| OLD | NEW |