Sha256: 16e2e6f088d1c202834047b5e5667a83a2947bf61155fb18d251c3704e787983

Contents?: true

Size: 917 Bytes

Versions: 53

Compression:

Stored size: 917 Bytes

Contents

package variablelengthquantity

const testVersion = 1

// 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
		}
	}

	return x, n
}

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
trackler-2.0.8.42 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.41 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.40 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.39 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.38 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.37 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.36 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.35 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.34 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.33 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.32 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.31 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.30 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.29 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.28 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.27 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.26 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.24 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.23 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.0.8.22 tracks/go/exercises/variable-length-quantity/example.go