pax_global_header00006660000000000000000000000064134562005710014515gustar00rootroot0000000000000052 comment=7c6542dfbb41505111dde1f321eb5d5002f5fb88 base36-1.0.0/000077500000000000000000000000001345620057100125765ustar00rootroot00000000000000base36-1.0.0/.travis.yml000066400000000000000000000001551345620057100147100ustar00rootroot00000000000000language: go go: - 1.11.x - 1.12.x - tip before_install: - go get -t ./... script: - go test -v base36-1.0.0/LICENSE000066400000000000000000000020751345620057100136070ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015-2019 Martin Lindhe Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. base36-1.0.0/Makefile000066400000000000000000000000661345620057100142400ustar00rootroot00000000000000bench: go test -benchmem -bench=. test: go test -v base36-1.0.0/README.md000066400000000000000000000013551345620057100140610ustar00rootroot00000000000000# About [![Travis-CI](https://api.travis-ci.org/martinlindhe/base36.svg)](https://travis-ci.org/martinlindhe/base36) [![GoDoc](https://godoc.org/github.com/martinlindhe/base36?status.svg)](https://godoc.org/github.com/martinlindhe/base36) Implements Base36 encoding and decoding, which is useful to represent large integers in a case-insensitive alphanumeric way. ## Examples ```go import "github.com/martinlindhe/base36" fmt.Println(base36.Encode(5481594952936519619)) // Output: 15N9Z8L3AU4EB fmt.Println(base36.Decode("15N9Z8L3AU4EB")) // Output: 5481594952936519619 fmt.Println(base36.EncodeBytes([]byte{1, 2, 3, 4})) // Output: A2F44 fmt.Println(base36.DecodeToBytes("A2F44")) // Output: [1 2 3 4] ``` ## License Under [MIT](LICENSE) base36-1.0.0/base36.go000066400000000000000000000053251345620057100142150ustar00rootroot00000000000000package base36 import ( "math" "math/big" "strings" ) var ( base36 = []byte{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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'} index = map[byte]int{ '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22, 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33, 'Y': 34, 'Z': 35, 'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15, 'g': 16, 'h': 17, 'i': 18, 'j': 19, 'k': 20, 'l': 21, 'm': 22, 'n': 23, 'o': 24, 'p': 25, 'q': 26, 'r': 27, 's': 28, 't': 29, 'u': 30, 'v': 31, 'w': 32, 'x': 33, 'y': 34, 'z': 35, } ) // Encode encodes a number to base36. func Encode(value uint64) string { var res [16]byte var i int for i = len(res) - 1; value != 0; i-- { res[i] = base36[value%36] value /= 36 } return string(res[i+1:]) } // Decode decodes a base36-encoded string. func Decode(s string) uint64 { res := uint64(0) l := len(s) - 1 for idx := range s { c := s[l-idx] res += uint64(index[c]) * uint64(math.Pow(36, float64(idx))) } return res } var bigRadix = big.NewInt(36) var bigZero = big.NewInt(0) // EncodeBytesAsBytes encodes a byte slice to base36. func EncodeBytesAsBytes(b []byte) []byte { x := new(big.Int) x.SetBytes(b) answer := make([]byte, 0, len(b)*136/100) for x.Cmp(bigZero) > 0 { mod := new(big.Int) x.DivMod(x, bigRadix, mod) answer = append(answer, base36[mod.Int64()]) } // leading zero bytes for _, i := range b { if i != 0 { break } answer = append(answer, base36[0]) } // reverse alen := len(answer) for i := 0; i < alen/2; i++ { answer[i], answer[alen-1-i] = answer[alen-1-i], answer[i] } return answer } // EncodeBytes encodes a byte slice to base36 string. func EncodeBytes(b []byte) string { return string(EncodeBytesAsBytes(b)) } // DecodeToBytes decodes a base36 string to a byte slice, using alphabet. func DecodeToBytes(b string) []byte { alphabet := string(base36) answer := big.NewInt(0) j := big.NewInt(1) for i := len(b) - 1; i >= 0; i-- { tmp := strings.IndexAny(alphabet, string(b[i])) if tmp == -1 { return []byte("") } idx := big.NewInt(int64(tmp)) tmp1 := big.NewInt(0) tmp1.Mul(j, idx) answer.Add(answer, tmp1) j.Mul(j, bigRadix) } tmpval := answer.Bytes() var numZeros int for numZeros = 0; numZeros < len(b); numZeros++ { if b[numZeros] != alphabet[0] { break } } flen := numZeros + len(tmpval) val := make([]byte, flen, flen) copy(val[numZeros:], tmpval) return val } base36-1.0.0/base36_test.go000066400000000000000000000021241345620057100152460ustar00rootroot00000000000000package base36 import ( "math" "strings" "testing" "github.com/stretchr/testify/assert" ) var raw = []uint64{0, 50, 100, 999, 1000, 1111, 5959, 99999, 123456789, 5481594952936519619, math.MaxInt64 / 2048, math.MaxInt64 / 512, math.MaxInt64, math.MaxUint64} var encoded = []string{"", "1E", "2S", "RR", "RS", "UV", "4LJ", "255R", "21I3V9", "15N9Z8L3AU4EB", "18CE53UN18F", "4XDKKFEK4XR", "1Y2P0IJ32E8E7", "3W5E11264SGSF"} func TestEncode(t *testing.T) { for i, v := range raw { assert.Equal(t, encoded[i], Encode(v)) } } func TestDecode(t *testing.T) { for i, v := range encoded { assert.Equal(t, raw[i], Decode(v)) assert.Equal(t, raw[i], Decode(strings.ToLower(v))) } } func BenchmarkEncode(b *testing.B) { for i := 0; i < b.N; i++ { Encode(5481594952936519619) } } func BenchmarkEncodeBytesAsBytes(b *testing.B) { data := []byte{ 0x86, 0x4F, 0xD2, 0x6F, 0xB5, 0x59, 0xF7, 0x5B, 0x48, 0x4F, 0x2A, 0x48, 0x4F, 0x2A, } for i := 0; i < b.N; i++ { EncodeBytesAsBytes(data) } } func BenchmarkDecode(b *testing.B) { for i := 0; i < b.N; i++ { Decode("1Y2P0IJ32E8E7") } } base36-1.0.0/example_test.go000066400000000000000000000007371345620057100156260ustar00rootroot00000000000000package base36_test import ( "fmt" "github.com/martinlindhe/base36" ) func ExampleEncode() { fmt.Println(base36.Encode(5481594952936519619)) // Output: 15N9Z8L3AU4EB } func ExampleDecode() { fmt.Println(base36.Decode("15N9Z8L3AU4EB")) // Output: 5481594952936519619 } func ExampleEncodeBytes() { fmt.Println(base36.EncodeBytes([]byte{1, 2, 3, 4})) // Output: A2F44 } func ExampleDecodeToBytes() { fmt.Println(base36.DecodeToBytes("A2F44")) // Output: [1 2 3 4] }