shortcode

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2023 License: MIT Imports: 3 Imported by: 2

README

Short URL Generator

Go implementation for generating Tiny URL- and bit.ly-like URLs, based on Python's short_url.

The intended use is to obfuscate integer IDs into short strings which can be used in your URLs. A bit-shuffling approach is used to avoid generating consecutive, predictable strings. Furthermore, the algorithm is deterministic and guarantees that no collisions will occur.

The URL alphabet is fully customizable and may contain any number of characters. By default, digits and lower-case letters are used, with some removed to avoid confusion between characters like o, O and 0. The default alphabet is shuffled and has a prime number of characters to further improve the results of the algorithm.

The block size specifies how many bits will be shuffled. The lower blockSize bits are reversed. Any bits higher than blockSize will remain as is. blockSize of 0 will leave all bits unaffected and the algorithm will simply be converting your integer to a different base. The minLength parameter allows you to pad the string if you want it to be a specific length.

Sample Usage:

import "shortcode"
code := shortcode.EncodeID(12) // "LhKA"
id := shortcode.DecodeID(code) // 12

Use the top-level functions of the module to use the default encoder settings. Otherwise, you may create your own custom Encoder and use its EncodeID and DecodeID methods.

Documentation

Index

Constants

View Source
const (
	DefaultAlphabet  = "mn6j2c4rv8bpygw95z7hsdaetxuk3fq"
	DefaultBlockSize = 24
	MinLength        = 5
)

Variables

This section is empty.

Functions

func Debase

func Debase(s string) int

func Decode

func Decode(n int) int

func DecodeID

func DecodeID(s string) int

func Enbase

func Enbase(n int) string

func Encode

func Encode(n int) int

func EncodeID

func EncodeID(n int) string

Types

type Encoder

type Encoder struct {
	Alphabet  string
	BlockSize int
	Mask      int
}

func NewEncoder

func NewEncoder(alphabet string, blockSize int) (*Encoder, error)

func (*Encoder) Debase

func (e *Encoder) Debase(s string) int

func (*Encoder) Decode

func (e *Encoder) Decode(n int) int

func (*Encoder) DecodeID

func (e *Encoder) DecodeID(s string) int

func (*Encoder) Enbase

func (e *Encoder) Enbase(x, minLength int) string

func (*Encoder) Encode

func (e *Encoder) Encode(n int) int

func (*Encoder) EncodeID

func (e *Encoder) EncodeID(n, minLength int) string

Jump to

Keyboard shortcuts

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