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:
Ilia Choly 2025-10-31 20:11:04 +00:00 committed by Gopher Robot
parent 361d51a6b5
commit 5132158ac2
4 changed files with 43 additions and 0 deletions

View file

@ -531,6 +531,34 @@ func TestReadString(t *testing.T) {
}
}
var peekTests = []struct {
buffer string
n int
expected string
err error
}{
{"", 0, "", nil},
{"aaa", 3, "aaa", nil},
{"foobar", 2, "fo", nil},
{"a", 2, "a", io.EOF},
}
func TestPeek(t *testing.T) {
for _, test := range peekTests {
buf := NewBufferString(test.buffer)
bytes, err := buf.Peek(test.n)
if string(bytes) != test.expected {
t.Errorf("expected %q, got %q", test.expected, bytes)
}
if err != test.err {
t.Errorf("expected error %v, got %v", test.err, err)
}
if buf.Len() != len(test.buffer) {
t.Errorf("bad length after peek: %d, want %d", buf.Len(), len(test.buffer))
}
}
}
func BenchmarkReadString(b *testing.B) {
const n = 32 << 10