Code Monkey home page Code Monkey logo

base62's Introduction

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

base62's People

Contributors

alextanhongpin avatar

Watchers

 avatar  avatar  avatar

Forkers

brenol

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.