Help us shape the future of Go! Take the 2026 Go Developer Survey

vlq

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2020 License: MIT Imports: 1 Imported by: 1

README

Variable Length Quantity

A go implementation of the VLQ type. VLQ is an encoding scheme to compress unsigned integers.

Library Usage

func readme_example_normal() {
    // Standard operation

    value := Rvlq(30000)
    byteCount := value.EncodedSize()
    buffer := make([]byte, byteCount)
    value.EncodeTo(buffer)
    fmt.Printf("Encoded %v into the byte sequence %v\n", value, buffer)

    decodedValue, bytesUsed, isComplete := DecodeRvlqFrom(buffer)
    if !isComplete {
        // TODO: Error handling
    }
    fmt.Printf("Decoded value %v from %v bytes\n", decodedValue, bytesUsed)
}

Result:

Encoded 30000 into the byte sequence [129 234 48]
Decoded value 30000 from 3 bytes
func readme_example_split_reads() {
    // Simulate the VLQ being split across two reads

    value := Rvlq(30000)
    byteCount := value.EncodedSize()
    buffer := make([]byte, byteCount)
    value.EncodeTo(buffer)

    buffer1 := buffer[:2]
    buffer2 := buffer[2:]

    var decodedValue Rvlq
    bytesUsed, isComplete := decodedValue.DecodeFrom(buffer1)
    fmt.Printf("Used %v bytes decoding VLQ value. Is complete = %v\n", bytesUsed, isComplete)

    bytesUsed, isComplete = decodedValue.DecodeFrom(buffer2)
    fmt.Printf("Used %v bytes decoding VLQ value. Is complete = %v\n", bytesUsed, isComplete)

    fmt.Printf("Decoded value: %v\n", decodedValue)
}

Result:

Used 2 bytes decoding VLQ value. Is complete = false
Used 1 bytes decoding VLQ value. Is complete = true
Decoded value: 30000
func readme_example_reversed() {
	// Reversed mode, which allows reading the value from the end of a buffer

	value := Rvlq(30000)
	buffer := make([]byte, 10)
	value.EncodeReversedTo(buffer)
	fmt.Printf("Encoded %v into the byte sequence %v\n", value, buffer)

	decodedValue, bytesUsed, ok := DecodeRvlqReversedFrom(buffer)
	if !ok {
		// TODO: Didn't find end of sequence
	}
	fmt.Printf("Decoded value %v from %v bytes\n", decodedValue, bytesUsed)
}

Result:

Encoded 30000 into the byte sequence [0 0 0 0 0 0 0 48 234 129]
Decoded value 30000 from 3 bytes

Documentation

Overview

VLQ (Variable Length Quantity) is an unsigned integer encoding scheme designed for the MIDI file format.

It encodes a value into a sequence of bytes where the lower 7 bits contain data and the high bit is used as a "continuation" bit. A decoder reads encoded bytes, filling a decoded unsigned integer 7 bits at a time in big endian order, until it encounters a byte with the high "continuation" bit cleared.

Index

Constants

View Source
const MaxEncodeLength = 10

Maximum byte length that this library will encode (for a 64-bit value)

Variables

This section is empty.

Functions

func Extend

func Extend(buffer []byte, groupCount int) (bytesEncoded int, err error)

Adds VLQ extension groups to a buffer.

func IsExtended

func IsExtended(buffer []byte) bool

Returns true if the encoded VLQ is extended (prepended with 0x80)

func MaxValueInBytes

func MaxValueInBytes(byteCount int) uint64

Get the maximum value that can be stored in a RVLQ of the selected byte count

Types

type Lvlq

type Lvlq uint64

func DecodeLvlqFrom

func DecodeLvlqFrom(buffer []byte) (value Lvlq, decodedByteCount int, isComplete bool)

func DecodeLvlqReversedFrom

func DecodeLvlqReversedFrom(buffer []byte) (value Lvlq, decodedByteCount int, ok bool)

Decode a RVLQ value in reverse order from the end of a buffer. Unlike DecodeFrom(), the reversed version must have all encoded bytes present in the buffer to decode.

func (*Lvlq) DecodeFrom

func (this *Lvlq) DecodeFrom(buffer []byte) (decodedByteCount int, isComplete bool)

Decode this LVLQ from a buffer. Returns true for isComplete once the VLQ is fully decoded (i.e. it has encountered a byte with the high bit cleared). This allows for progressive decoding of the VLQ value across multiple buffers.

func (*Lvlq) DecodeReversedFrom

func (this *Lvlq) DecodeReversedFrom(buffer []byte) (decodedByteCount int, ok bool)

Decode this RVLQ in reverse order from the end of a buffer. Unlike DecodeFrom(), the reversed version must have all encoded bytes present in the buffer to decode.

func (Lvlq) EncodeReversedTo

func (this Lvlq) EncodeReversedTo(buffer []byte) (encodedByteCount int, ok bool)

Encode this LVLQ in reverse order to the end of a buffer. Returns false and the number of bytes it attempted to write if the buffer isn't big enough.

func (Lvlq) EncodeTo

func (this Lvlq) EncodeTo(buffer []byte) (encodedByteCount int, ok bool)

Encode this LVLQ to a buffer. Returns false and the number of bytes it attempted to write if the buffer isn't big enough.

func (Lvlq) EncodedSize

func (this Lvlq) EncodedSize() int

Get the number of bytes required to encode this RVLQ

type Rvlq

type Rvlq uint64

func DecodeRvlqFrom

func DecodeRvlqFrom(buffer []byte) (value Rvlq, decodedByteCount int, isComplete bool)

func DecodeRvlqReversedFrom

func DecodeRvlqReversedFrom(buffer []byte) (value Rvlq, decodedByteCount int, ok bool)

Decode a RVLQ value in reverse order from the end of a buffer. Unlike DecodeFrom(), the reversed version must have all encoded bytes present in the buffer to decode.

func (*Rvlq) DecodeFrom

func (this *Rvlq) DecodeFrom(buffer []byte) (decodedByteCount int, isComplete bool)

Decode this RVLQ from a buffer. Returns true for isComplete once the VLQ is fully decoded (i.e. it has encountered a byte with the high bit cleared). This allows for progressive decoding of the VLQ value across multiple buffers.

func (*Rvlq) DecodeReversedFrom

func (this *Rvlq) DecodeReversedFrom(buffer []byte) (decodedByteCount int, ok bool)

Decode this RVLQ in reverse order from the end of a buffer. Unlike DecodeFrom(), the reversed version must have all encoded bytes present in the buffer to decode.

func (Rvlq) EncodeReversedTo

func (this Rvlq) EncodeReversedTo(buffer []byte) (encodedByteCount int, ok bool)

Encode this RVLQ in reverse order to the end of a buffer. Returns false and the number of bytes it attempted to write if the buffer isn't big enough.

func (Rvlq) EncodeTo

func (this Rvlq) EncodeTo(buffer []byte) (encodedByteCount int, ok bool)

Encode this RVLQ to a buffer. Returns false and the number of bytes it attempted to write if the buffer isn't big enough.

func (Rvlq) EncodedSize

func (this Rvlq) EncodedSize() int

Get the number of bytes required to encode this RVLQ

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL