Sha256: 3f0c824968eb73808db15403e6f9c0a1a2696d5943f5a8323bdd9127513f6855

Contents?: true

Size: 1.84 KB

Versions: 122

Compression:

Stored size: 1.84 KB

Contents

package variablelengthquantity

import "errors"

var ErrUnterminatedSequence = errors.New("unterminated sequence")

// encodeInt returns the varint encoding of x.
func encodeInt(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),
		}
	}

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

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

// decodeInt 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 decodeInt(buf []byte) (x uint32, n int, err error) {
	if len(buf) < 1 {
		return 0, 0, nil
	}

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

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

	return x, 0, ErrUnterminatedSequence
}

// EncodeVarint encodes a slice of uint32 into a var-int encoded bytes.
func EncodeVarint(xa []uint32) []byte {
	result := make([]byte, 0)
	for _, x := range xa {
		result = append(result, encodeInt(x)...)
	}
	return result
}

// DecodeVarint decodes a buffer of var-int encoded values into
// a slice of uint32 values; an error is returned if buf doesn't
// decode var-int successfully.
func DecodeVarint(buf []byte) (ra []uint32, err error) {
	if len(buf) == 0 {
		return []uint32{0}, nil
	}
	usedBytes := 0
	ra = make([]uint32, 0)
	for usedBytes < len(buf) {
		r, nUsed, err := decodeInt(buf[usedBytes:])
		if err != nil {
			return nil, err
		}
		ra = append(ra, r)
		usedBytes += nUsed
	}
	return ra, nil
}

Version data entries

122 entries across 122 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.118 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.117 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.116 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.115 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.114 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.113 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.111 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.110 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.109 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.108 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.107 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.106 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.105 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.104 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.103 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.102 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.101 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.100 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.99 tracks/go/exercises/variable-length-quantity/example.go