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.159 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.158 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.157 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.156 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.155 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.154 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.153 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.152 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.151 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.150 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.149 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.148 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.147 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.146 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.145 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.144 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.143 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.142 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.141 tracks/go/exercises/variable-length-quantity/example.go
trackler-2.2.1.140 tracks/go/exercises/variable-length-quantity/example.go