vlq

MoonBit library for encoding and decoding Variable Length Quantities

vlq
encoding
decoding
moon add CAIMEOX/vlq@0.1.0
Download zip
Author
Version
0.1.0
License
Apache-2.0
Last updated
last year
Downloads
27
README

#VLQ (Variable Length Quantities)

A MoonBit library for encoding and decoding Variable Length Quantities (VLQ) and Base64 VLQ.

#Overview

Variable Length Quantities (VLQ) is a universal code that uses an arbitrary number of bytes to encode arbitrarily large integers. It's commonly used in:

  • MIDI files for encoding timing information
  • Source maps for encoding mapping information
  • Protocol buffers and other binary serialization formats

This library provides:

  • Standard VLQ encoding/decoding for unsigned integers
  • Base64 VLQ encoding/decoding for signed integers (commonly used in source maps)

#Features

  • Standard VLQ: Encode/decode arrays of unsigned integers to/from byte arrays
  • Base64 VLQ: Encode/decode arrays of signed integers to/from Base64 strings
  • Error handling: Comprehensive error types for overflow and incomplete sequences
  • Memory efficient: Direct array operations without intermediate allocations

#Usage

#Standard VLQ

// Encode unsigned integers to VLQ bytes
let numbers = [0x7FU, 0x4000U, 0x01FFFFFFU]
let encoded = to_vlq(numbers)
// encoded = [0x7F, 0x81, 0x80, 0x00, 0xFF, 0xFF, 0xFF, 0x7F]

// Decode VLQ bytes back to unsigned integers
let decoded = try! from_vlq(encoded)
// decoded = Ok([0x7F, 0x4000, 0x01FFFFFF])

#Base64 VLQ

// Encode signed integers to Base64 VLQ string
let numbers = [1, -1, 16]
let encoded = to_base64_vlq(numbers)
// encoded = "CDgB"

// Decode Base64 VLQ string back to signed integers
let decoded = try! from_base64_vlq("CDgB")
// decoded = Ok([1, -1, 16])

#API Reference

#Error Handling

The library defines three error types:

  • Overflow: When decoding a VLQ sequence that would overflow the maximum supported value
  • IncompleteSequence: When a VLQ sequence is incomplete (missing continuation bytes)
  • InvalidBase64Character: When an invalid character is encountered in Base64 VLQ decoding

#Functions

  • to_vlq(Array[UInt]) -> Array[Byte]: Encode unsigned integers to VLQ bytes
  • from_vlq(Array[Byte]) -> Array[UInt] raise VlqError: Decode VLQ bytes to unsigned integers
  • to_base64_vlq(Array[Int]) -> String: Encode signed integers to Base64 VLQ string
  • from_base64_vlq(String) -> Array[Int] raise VlqError: Decode Base64 VLQ string to signed integers

#
VlqError

pub(all) enum VlqError {
Overflow
IncompleteSequence
InvalidBase64Character(Char)
}
Error types for VLQ operations.

  • Overflow: The decoded number exceeds the maximum supported value (0x01FFFFFF)
  • IncompleteSequence: The VLQ sequence is incomplete (missing continuation bytes)
  • InvalidBase64Character: An invalid character was encountered during Base64 VLQ decoding
impl Eq for VlqError
impl Show for VlqError

#
from_base64_vlq

fn from_base64_vlq(s : String) -> Array[Int] raise VlqError
Decodes a Base64 VLQ string back into signed integers.

Example

let encoded = "CDgB"
let decoded = try! from_base64_vlq(encoded)
inspect(decoded, content="Ok([1, -1, 16])")

Errors

  • IncompleteSequence: If the string ends with a continuation character
  • InvalidBase64Character: If an invalid Base64 character is encountered

#
from_vlq

fn from_vlq(bytes : Array[Byte]) -> Array[UInt] raise VlqError
Decodes Variable Length Quantity (VLQ) bytes back into unsigned integers.

Errors:

  • Overflow: If a number exceeds 0x01FFFFFF (28 bits)
  • IncompleteSequence: If the sequence ends with a continuation byte

#
to_base64_vlq

fn to_base64_vlq(numbers : Array[Int]) -> String
Encodes an array of signed integers into Base64 VLQ format.

Example

let numbers = [1, -1, 16]
let encoded = to_base64_vlq(numbers)
inspect(encoded, content="CDgB")

#
to_vlq

fn to_vlq(numbers : Array[UInt]) -> Array[Byte]
Encodes an array of unsigned integers into Variable Length Quantity (VLQ) byte format.

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io