words

package module
v0.0.0-...-727f76d Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2023 License: BSD-2-Clause Imports: 3 Imported by: 0

README

words

Go Reference

Encode an integer into a short list of memorable words and decode it back again.

	encoded, _ := Encode(5000)
	decoded, _ := Decode(encoded)
	fmt.Printf("5000 -> %v -> %d", encoded, decoded)
	// Output:
	// 5000 -> [rub fog] -> 5000

For the simple case the list of words will be of variable length - single words for small integers, longer sentences as they get larger. For some use cases having a fixed length of sentence for all valid input integers can be convenient. Including a range in the parameters to Encode will do that.

	encoded, _ := Encode(5000, 0, 10_000_000_000)
	decoded, _ := Decode(encoded, 0)
	fmt.Printf("5000 -> %v -> %d", encoded, decoded)
	// Output:
	// 5000 -> [cannon cannon rub fog] -> 5000

It's slightly more convenient, and slightly more efficient, to create and reuse a Coder to do the same thing.

	coder, _ := New(0, 10_000_000)
	encoded, _ := coder.Encode(42)
	decoded, _ := coder.Decode(encoded)
	fmt.Printf("42 -> %v -> %d", encoded, decoded)
	// Output:
	// 42 -> [cannon cannon tank] -> 42

words uses the word list from https://github.com/wcodes-org/wordlist

Documentation

Overview

Example
encoded, _ := Encode(5000)
decoded, _ := Decode(encoded)
fmt.Printf("5000 -> %v -> %d", encoded, decoded)
Output:

5000 -> [rub fog] -> 5000
Example (Fixed_range)
encoded, _ := Encode(5000, 0, 10_000_000_000)
decoded, _ := Decode(encoded, 0)
fmt.Printf("5000 -> %v -> %d", encoded, decoded)
Output:

5000 -> [cannon cannon rub fog] -> 5000

Index

Examples

Constants

This section is empty.

Variables

View Source
var List = []string{}/* 1024 elements not displayed */

List is the list of words used to encode values

Functions

func Decode

func Decode(words []string, ranges ...int64) (int64, error)

Decode a list of words back into an integer

func Encode

func Encode(value int64, ranges ...int64) ([]string, error)

Encode a value in the range (minVal..maxVal) into a list of words

func Length

func Length(minVal, maxVal int64) (int, error)

Length returns the number of words required to encode integers in the given range

Types

type Coder

type Coder struct {
	// contains filtered or unexported fields
}
Example
coder, _ := New(0, 10_000_000)
encoded, _ := coder.Encode(42)
decoded, _ := coder.Decode(encoded)
fmt.Printf("42 -> %v -> %d", encoded, decoded)
Output:

42 -> [cannon cannon tank] -> 42

func New

func New(valueRange ...int64) (Coder, error)

New creates a new Coder. If passed two values, minimum and maximum, it will create slices of strings of a fixed length, adequate to encode all integers in that range. If passed a single value, minimum, it will create slices of strings of variable length, able to encode any number greater than or equal to minimum. If called with no parameters it creates a Coder that encodes any non-negative integer into a variable length slice of strings.

func (Coder) Decode

func (c Coder) Decode(words []string) (int64, error)

Decode converts a list of words back into an integer

func (Coder) Encode

func (c Coder) Encode(value int64) ([]string, error)

Encode converts an integer into a list of words

func (Coder) Length

func (c Coder) Length() int

Length returns the fixed length of slices this Coder generates, or -1 if it generates variable lengths

Jump to

Keyboard shortcuts

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