uvarint

package module
v0.0.0-...-c3f9e62 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2016 License: MIT Imports: 0 Imported by: 0

README

uvarint

Build Status

Varint is SQLite4's variable-length integers: an encoding of 64-bit unsigned integers into between 1 and 9 bytes.

The Encode and Decode functions, PutUvarint and Uvarint, respectively, also document the rules for encoding/decoding as documented on http://www.sqlite.org/src4/doc/trunk/www/varint.wiki. The rules are copied directly from that page.

For more information about SQLite4's varint, please refer to the above link.

When encoding, the passed byte slice must be have a len of n bytes where n is the number of bytes that the number will encode to, up to a max of 9 bytes. A panic will occur if the byte slice is not long enough to hold the encoded number. The number to encode must be of type uint64. The number of bytes encoded will be returned.

Uint64 takes a byte slice and returns the decoded number as a uint64 and the number of bytes read. A panic will occur if the byte slice length is less than expected. The expected length is determined by the value of the first byte.

Usage

package main

import "github.com/mohae/uvarint"

func main() {
        buf := make([]byte, 9)
        n := uvarint.PutUvarint(buf, 42)
        fmt.Printf("Encoded %d bytes: %#v\n", buf)

        v, n := uvarint.Uvarint(buf)
        fmt.Printf("Decoded %d bytes: %d\n", n, v)
}

Output:

Encoded 1 bytes: 0x2a
Decoded 1 bytes: 42

Notes:

Two wrapper functions are also exposed: Encode and Decode. Encode is a wrapper to PutUvarint, while Decode wraps Uvarint.

Doc

https://godoc.org/github.com/mohae/uvarint

Documentation

Overview

Package varint encodes and decodes SQLite4 variable length integers which is an encoding of 64-bit unsigned integers into 1-9 bytes (varuint). See: http://www.sqlite.org/src4/doc/trunk/www/varint.wiki

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(buf []byte) (uint64, int)

Wrapper to Uvarint().

func Encode

func Encode(buf []byte, x uint64) int

Wrapper to PutUvarint().

func PutUvarint

func PutUvarint(buf []byte, x uint64) int

PutUvarint encodes the received uint64 into varint using the minimum necessary bytes. The number of bytes written is returned.

From: http://www.sqlite.org/src4/doc/trunk/www/varint.wiki

Encode:

Let the input value be V.

If V<=240 then output a single by A0 equal to V.
If V<=2287 then output A0 as (V-240)/256 + 241 and A1 as (V-240)%256.
If V<=67823 then output A0 as 249, A1 as (V-2288)/256, and A2 as (V-2288)%256.
If V<=16777215 then output A0 as 250 and A1 through A3 as a big-endian 3-byte integer.
If V<=4294967295 then output A0 as 251 and A1..A4 as a big-endian 4-byte integer.
If V<=1099511627775 then output A0 as 252 and A1..A5 as a big-endian 5-byte integer.
If V<=281474976710655 then output A0 as 253 and A1..A6 as a big-endian 6-byte integer.
If V<=72057594037927935 then output A0 as 254 and A1..A7 as a big-endian 7-byte integer.
Otherwise then output A0 as 255 and A1..A8 as a big-endian 8-byte integer.

func Uvarint

func Uvarint(buf []byte) (uint64, int)

Uvarint decodes the received varint encoded byte slice; returning the value and the amount of bytes used.

From: http://www.sqlite.org/src4/doc/trunk/www/varint.wiki

Decode:

If A0 is between 0 and 240 inclusive, then the result is the value of A0.
If A0 is between 241 and 248 inclusive, then the result is 240+256*(A0-241)+A1.
If A0 is 249 then the result is 2288+256*A1+A2.
If A0 is 250 then the result is A1..A3 as a 3-byte big-endian integer.
If A0 is 251 then the result is A1..A4 as a 4-byte big-endian integer.
If A0 is 252 then the result is A1..A5 as a 5-byte big-endian integer.
If A0 is 253 then the result is A1..A6 as a 6-byte big-endian integer.
If A0 is 254 then the result is A1..A7 as a 7-byte big-endian integer.
If A0 is 255 then the result is A1..A8 as a 8-byte big-endian integer.

Types

This section is empty.

Jump to

Keyboard shortcuts

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