Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(643)

Unified Diff: go/src/infra/libs/infra_util/buf.go

Issue 662113003: Drover's back, baby! (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git/+/master
Patch Set: Lots of fixes Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698