| OLD | NEW |
| (Empty) | |
| 1 package infra_util |
| 2 |
| 3 import "io" |
| 4 |
| 5 type Nomable interface { |
| 6 ReadString(delim byte) (string, error) |
| 7 } |
| 8 |
| 9 // Returns `func(byte) string` which will read from |buf| until the byte, |
| 10 // returning the string read. If an error is encountered, this will panic. |
| 11 func Nom(buf Nomable) func(byte) string { |
| 12 return func(delim byte) string { |
| 13 ret, err := buf.ReadString(delim) |
| 14 if err != nil { |
| 15 panic(err) |
| 16 } |
| 17 return ret[:len(ret)-1] |
| 18 } |
| 19 } |
| 20 |
| 21 // Returns `func (int) []byte` which will read the specified number of bytes |
| 22 // from |buf|, or panic. |
| 23 func Yoink(buf io.Reader) func(int) []byte { |
| 24 return func(num int) []byte { |
| 25 ret := make([]byte, num) |
| 26 i, err := io.ReadFull(buf, ret) |
| 27 if err != nil { |
| 28 panic(err) |
| 29 } |
| 30 if i != len(ret) { |
| 31 panic("yoink: failed to read enough data") |
| 32 } |
| 33 return ret |
| 34 } |
| 35 } |
| OLD | NEW |