aboutsummaryrefslogtreecommitdiffstats
path: root/utils/binary/read.go
diff options
context:
space:
mode:
authorArran Walker <arran.walker@fiveturns.org>2019-04-22 17:10:29 +0000
committerArran Walker <arran.walker@fiveturns.org>2019-04-22 17:10:29 +0000
commit262179b985108f0cbabc046b315e4feb3311a199 (patch)
tree7d2af0e060e3ed1e4550889214f01562895e258d /utils/binary/read.go
parent5598068360a39a61d97887522e474323d8f78f97 (diff)
downloadgo-git-262179b985108f0cbabc046b315e4feb3311a199.tar.gz
utils: binary reader, add ReadUntilFromBufioReader()
Signed-off-by: Arran Walker <arran.walker@fiveturns.org>
Diffstat (limited to 'utils/binary/read.go')
-rw-r--r--utils/binary/read.go18
1 files changed, 12 insertions, 6 deletions
diff --git a/utils/binary/read.go b/utils/binary/read.go
index ac55609..12e57c3 100644
--- a/utils/binary/read.go
+++ b/utils/binary/read.go
@@ -26,12 +26,7 @@ func Read(r io.Reader, data ...interface{}) error {
// ReadUntil reads from r untin delim is found
func ReadUntil(r io.Reader, delim byte) ([]byte, error) {
if bufr, ok := r.(*bufio.Reader); ok {
- value, err := bufr.ReadBytes(delim)
- if err != nil || len(value) == 0 {
- return nil, err
- }
-
- return value[:len(value)-1], nil
+ return ReadUntilFromBufioReader(bufr, delim)
}
var buf [1]byte
@@ -53,6 +48,17 @@ func ReadUntil(r io.Reader, delim byte) ([]byte, error) {
}
}
+// ReadUntilFromBufioReader is like bufio.ReadBytes but drops the delimiter
+// from the result.
+func ReadUntilFromBufioReader(r *bufio.Reader, delim byte) ([]byte, error) {
+ value, err := r.ReadBytes(delim)
+ if err != nil || len(value) == 0 {
+ return nil, err
+ }
+
+ return value[:len(value)-1], nil
+}
+
// ReadVariableWidthInt reads and returns an int in Git VLQ special format:
//
// Ordinary VLQ has some redundancies, example: the number 358 can be