mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
bytes: add Buffer.Peek
Fixes #73794
Change-Id: I0a57db05aacfa805213fe8278fc727e76eb8a65e
GitHub-Last-Rev: 3494d93f80
GitHub-Pull-Request: golang/go#73795
Reviewed-on: https://go-review.googlesource.com/c/go/+/674415
Reviewed-by: Sean Liao <sean@liao.dev>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
This commit is contained in:
parent
361d51a6b5
commit
5132158ac2
4 changed files with 43 additions and 0 deletions
|
|
@ -77,6 +77,18 @@ func (b *Buffer) String() string {
|
|||
return string(b.buf[b.off:])
|
||||
}
|
||||
|
||||
// Peek returns the next n bytes without advancing the buffer.
|
||||
// If Peek returns fewer than n bytes, it also returns [io.EOF].
|
||||
// The slice is only valid until the next call to a read or write method.
|
||||
// The slice aliases the buffer content at least until the next buffer modification,
|
||||
// so immediate changes to the slice will affect the result of future reads.
|
||||
func (b *Buffer) Peek(n int) ([]byte, error) {
|
||||
if b.Len() < n {
|
||||
return b.buf[b.off:], io.EOF
|
||||
}
|
||||
return b.buf[b.off:n], nil
|
||||
}
|
||||
|
||||
// empty reports whether the unread portion of the buffer is empty.
|
||||
func (b *Buffer) empty() bool { return len(b.buf) <= b.off }
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue