Sha256: 980c41c30ccf3bac0b0421c8b56b517125b310f07f0bc0026af5182c6ae41326

Contents?: true

Size: 921 Bytes

Versions: 32

Compression:

Stored size: 921 Bytes

Contents

package variablelengthquantity

const testVersion = 2

// EncodeVarint returns the varint encoding of x.
func EncodeVarint(x uint32) []byte {
	if x>>7 == 0 {
		return []byte{
			byte(x),
		}
	}

	if x>>14 == 0 {
		return []byte{
			byte(0x80 | x>>7),
			byte(127 & x),
		}
	}

	if x>>21 == 0 {
		return []byte{
			byte(0x80 | x>>14),
			byte(0x80 | x>>7),
			byte(127 & x),
		}
	}

	return []byte{
		byte(0x80 | x>>21),
		byte(0x80 | x>>14),
		byte(0x80 | x>>7),
		byte(127 & x),
	}
}

// DecodeVarint reads a varint-encoded integer from the slice.
// It returns the integer and the number of bytes consumed, or
// zero if there is not enough.
func DecodeVarint(buf []byte) (x uint32, n int) {
	if len(buf) < 1 {
		return 0, 0
	}

	if buf[0] <= 0x80 {
		return uint32(buf[0]), 1
	}

	var b byte
	for n, b = range buf {
		x = x << 7
		x |= uint32(b) & 0x7F
		if (b & 0x80) == 0 {
			return x, n + 1
		}
	}

	return x, 0
}

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
trackler-2.1.0.18 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.17 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.16 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.15 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.14 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.13 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.12 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.11 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.10 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.9 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.8 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.7 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.6 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.5 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.4 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.3 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.2 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.1 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.1.0.0 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.55 tracks/go/exercises/variable-length-quantity/example.go