From 35a92f92bd0ce15c658dd6794238ca90b71e4422 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Wed, 13 Apr 2022 13:21:30 -0700 Subject: encoding/binary: add AppendVarint AppendUvarint This adds a straight-forward implementation of the functionality. A more performant version could be added that unrolls the loop as is done in google.golang.org/protobuf/encoding/protowire, but usages that demand high performance can use that package instead. Fixes #51644 Change-Id: I9d3b615a60cdff47e5200e7e5d2276adf4c93783 Reviewed-on: https://go-review.googlesource.com/c/go/+/400176 Reviewed-by: Ian Lance Taylor Reviewed-by: Dmitri Shuralyov Run-TryBot: Joseph Tsai TryBot-Result: Gopher Robot --- src/encoding/binary/varint_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/encoding/binary/varint_test.go') diff --git a/src/encoding/binary/varint_test.go b/src/encoding/binary/varint_test.go index d025a67538..080a2148f0 100644 --- a/src/encoding/binary/varint_test.go +++ b/src/encoding/binary/varint_test.go @@ -36,6 +36,12 @@ func testVarint(t *testing.T, x int64) { t.Errorf("Varint(%d): got n = %d; want %d", x, m, n) } + buf2 := []byte("prefix") + buf2 = AppendVarint(buf2, x) + if string(buf2) != "prefix"+string(buf[:n]) { + t.Errorf("AppendVarint(%d): got %q, want %q", x, buf2, "prefix"+string(buf[:n])) + } + y, err := ReadVarint(bytes.NewReader(buf)) if err != nil { t.Errorf("ReadVarint(%d): %s", x, err) @@ -56,6 +62,12 @@ func testUvarint(t *testing.T, x uint64) { t.Errorf("Uvarint(%d): got n = %d; want %d", x, m, n) } + buf2 := []byte("prefix") + buf2 = AppendUvarint(buf2, x) + if string(buf2) != "prefix"+string(buf[:n]) { + t.Errorf("AppendUvarint(%d): got %q, want %q", x, buf2, "prefix"+string(buf[:n])) + } + y, err := ReadUvarint(bytes.NewReader(buf)) if err != nil { t.Errorf("ReadUvarint(%d): %s", x, err) -- cgit v1.3