shortening

package module
v0.0.0-...-64267dd Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2019 License: MIT Imports: 2 Imported by: 4

README

Shortening

A bijective base64 encoder for creating short URLs.

Padding significant

Most encoders use decimal bases to convert characters. This can waste space by refusing to use the zero-value in the most significant position.

For example, in base10 the numbers 1, 01, 00001, are all equivalent despite being padded with leading 0's.

This is why non-bijective encoders will skip values such as A and AAA in favor of possibly longer strings.

By using these values we can recover 64(n-1) IDs of length n.

Length Radix64 Shortening difference
1 63 64 1
2 4,032 4,096 64
3 258,048 262,144 4096
4 16,515,072 16,777,216 262,144
5 1,056,964,608 1,073,741,824 16,777,216

Performance

Benchmark ns/op
Encode64 46.2
Decode64 21.6
Encode32 46.4
Decode32 23.7
  • go1.12.4 darwin/amd64
  • Intel Xeon X5675 - 3.06 GHz

References

Wikipedia: Bijective numeration

Documentation

Index

Constants

View Source
const CharSet32 = `ABCDEFGHIJKLMNOPQRSTUVWXYZ234567`
View Source
const CharSet64 = `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_`

Variables

View Source
var (
	InvalidDecodeLen = errors.New("shortening: invalid decode length")
	InvalidCharacter = errors.New("shortening: invalid decode character")
	Overflow         = errors.New("shortening: uint64 overflow")
)

Functions

func Decode

func Decode(b []byte) (n uint64, err error)

Decode turns a slice of characters back into the original unit64.

Errors are returned for invalid characters or input that would cause an overflow.

func Decode32

func Decode32(b []byte) (n uint64, err error)

Decode32 turns a slice of characters back into the original unit64.

Errors are returned for invalid characters or input that would cause an overflow.

func Encode

func Encode(n uint64) []byte

Encode turns an uint64 into a slice of characters from 'CharSet64'

func Encode32

func Encode32(n uint64) []byte

Encode32 turns an uint64 into a slice of characters from 'charSet32'

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳