base62

package module
v0.0.0-...-204cf11 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2018 License: Apache-2.0 Imports: 1 Imported by: 2

README

go-base62

Base62 encoder/decoder with golang. Uses the variant [A-Za-z0-9], but can be initialized with custom variant. The test cases covered the variant mentioned previously.

Installation

$ go get github.com/alextanhongpin/base62

Usage

Encode/Decode:

import base62 "github.com/alextanhongpin/base62"

func main() {
  fmt.Println(base62.Decode("golang")) // Outputs: 30847375997
  fmt.Println(base62.Encode(30847375997)) // Outputs: "golang"
}

Factory:

import base62 "github.com/alextanhongpin/base62"

func main() {
  b62 := base62.New(base62.DEFAULT_CHARS) // Or use your own variant
  fmt.Println(b62.Decode("golang")) // Outputs: 30847375997
  fmt.Println(b62.Encode(30847375997)) // Outputs: "golang"
}

Test Table

To test the encoding/decoding, we use the NATO Phonetic Alphabets. If the encoded values matches the decoded values, the test passes. There's a quickcheck test too to capture unexpected input.

Text Decoded
Alfa 386411
Bravo 40145909
Charlie 201958818817
Delta 66642463
Echo 1305265
Foxtrot 379112811228
Golf 1828288
Hotel 128160920
India 142637661
Juliett 611630563254
Kilo 2758545
Lima 2996921
Mike 3235129
November 51675855992598
Oscar 232482994
Papa 3919667
Quebec 16276243687
Romeo 275897375
Sierra 17931255627
Tango 302117423
Uniform 1229975219171
Victor 20679184986
Whiskey 1338081975441
Xray 5890733
Yankee 23311959173
Zulu 6379599

Performance test

It's fast. Period.

$ go test -bench=. -benchmem -memprofile mem.out -cpuprofile cpu.out

Output:

goos: darwin
goarch: amd64
pkg: github.com/alextanhongpin/base62
BenchmarkEncode-4   	10000000	       200 ns/op	      64 B/op	       2 allocs/op
PASS
ok  	github.com/alextanhongpin/base62	2.378s

Documentation

Index

Constants

View Source
const Base62 uint64 = 62

Base62 represents the base62 const.

Variables

View Source
var (
	// Base62 character set, [A-Za-z0-9]. https://tools.ietf.org/html/rfc4648
	// We follow the orders similar to base64.
	// Note that there are several different variants in the web, such as
	// [0-9a-zA-Z] or [a-zA-Z0-9] and so on.
	DEFAULT_CHARS = [Base62]rune{
		'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
		'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
	}
)

Functions

func Decode

func Decode(s string) uint64

Decode attempts to convert a base 62 string back into an integer.

func Encode

func Encode(in uint64) string

Encode converts the given integer into a base 62 string.

Types

type Factory

type Factory struct {
	// contains filtered or unexported fields
}

Factory allows the creation of new base62 encoder/decoder with different variant of characters.

func New

func New(chars [Base62]rune) *Factory

New returns a new Factory with the given variant of characters.

func (*Factory) Decode

func (f *Factory) Decode(s string) uint64

Decode decodes the string back into integer.

func (*Factory) Encode

func (f *Factory) Encode(in uint64) string

Encode encodes the given integer into a base62 string.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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