diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2019-04-22 20:37:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-22 20:37:50 +0200 |
commit | 7b3220f1790e35ed1596540363599099c2821b9f (patch) | |
tree | e2881d61cb341218395e75e35344cb5835df98aa /utils/binary/read.go | |
parent | 6d4408addbaec52b861012688df0f09b79362e42 (diff) | |
parent | 262179b985108f0cbabc046b315e4feb3311a199 (diff) | |
download | go-git-7b3220f1790e35ed1596540363599099c2821b9f.tar.gz |
Merge pull request #1126 from saracen/index-performance-improvements
plumbing: format/index perf, buffered reads, reflection removal
Diffstat (limited to 'utils/binary/read.go')
-rw-r--r-- | utils/binary/read.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/utils/binary/read.go b/utils/binary/read.go index 50da1ff..12e57c3 100644 --- a/utils/binary/read.go +++ b/utils/binary/read.go @@ -25,6 +25,10 @@ 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 { + return ReadUntilFromBufioReader(bufr, delim) + } + var buf [1]byte value := make([]byte, 0, 16) for { @@ -44,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 |