aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/binary/varint.go
diff options
context:
space:
mode:
authorJoe Tsai <joetsai@digital-static.net>2022-04-13 13:21:30 -0700
committerJoseph Tsai <joetsai@digital-static.net>2022-04-15 01:19:37 +0000
commit35a92f92bd0ce15c658dd6794238ca90b71e4422 (patch)
treeacc7417aac6fed9129f6c623843531fe72280f69 /src/encoding/binary/varint.go
parent5a4f0b6f1e6d3c022ee30884590526ab7d3f580b (diff)
downloadgo-35a92f92bd0ce15c658dd6794238ca90b71e4422.tar.xz
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 <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Run-TryBot: Joseph Tsai <joetsai@digital-static.net> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/encoding/binary/varint.go')
-rw-r--r--src/encoding/binary/varint.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/encoding/binary/varint.go b/src/encoding/binary/varint.go
index 1b07e2541d..c807d15f44 100644
--- a/src/encoding/binary/varint.go
+++ b/src/encoding/binary/varint.go
@@ -36,6 +36,16 @@ const (
MaxVarintLen64 = 10
)
+// AppendUvarint appends the varint-encoded form of x,
+// as generated by PutUvarint, to buf and returns the extended buffer.
+func AppendUvarint(buf []byte, x uint64) []byte {
+ for x >= 0x80 {
+ buf = append(buf, byte(x)|0x80)
+ x >>= 7
+ }
+ return append(buf, byte(x))
+}
+
// PutUvarint encodes a uint64 into buf and returns the number of bytes written.
// If the buffer is too small, PutUvarint will panic.
func PutUvarint(buf []byte, x uint64) int {
@@ -77,6 +87,16 @@ func Uvarint(buf []byte) (uint64, int) {
return 0, 0
}
+// AppendVarint appends the varint-encoded form of x,
+// as generated by PutVarint, to buf and returns the extended buffer.
+func AppendVarint(buf []byte, x int64) []byte {
+ ux := uint64(x) << 1
+ if x < 0 {
+ ux = ^ux
+ }
+ return AppendUvarint(buf, ux)
+}
+
// PutVarint encodes an int64 into buf and returns the number of bytes written.
// If the buffer is too small, PutVarint will panic.
func PutVarint(buf []byte, x int64) int {