anybase

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: MIT Imports: 1 Imported by: 0

README

anybase

Go Reference Go Report Card CI codecov

Create an arbitrary base-n encoder and decoder with any set digits, with the limit that n is in [2, 256) and that each digit can be represented as a single byte.

This does not truly allow any base-n, for the sake of API simplicity and familiarity based on the encoding/hex standard library. Base-1 would be absurd and implemented as tallies (1 = 1, 2 = 11, etc.), and Base-257+ would not be able to be stored in a byte.

View the examples for more details.

Documentation

Overview

Package anybase provides utilities for converting bytes to base-n

Example
package main

import (
	"fmt"

	"github.com/spenserblack/anybase"
)

func main() {
	encoder := anybase.Encoder("0123456789abcdef")
	// It will often be useful to create an encoder-decoder pair
	decoder := encoder.Decoder()
	src1 := []byte{0xAB, 0xCD}
	dst := make([]byte, encoder.EncodedLen(len(src1)))
	encoder.Encode(src1, dst)
	fmt.Printf("%s\n", dst)

	src2 := make([]byte, decoder.DecodedLen(len(dst)))
	decoder.Decode(dst, src2)
	fmt.Printf("[%X, %X]\n", src2[0], src2[1])
}
Output:

abcd
[AB, CD]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder map[byte]byte

Decoder decodes from the source bytes to a destination byte slice.

Example
package main

import (
	"fmt"

	"github.com/spenserblack/anybase"
)

func main() {
	// Base-3, where the digits are 1, 2, and 3.
	decoder := anybase.Decoder{
		'1': 0,
		'2': 1,
		'3': 2,
	}
	dst := []byte("111123")
	src := make([]byte, decoder.DecodedLen(len(dst)))
	decoder.Decode(dst, src)
	fmt.Printf("%v", src)
}
Output:

[5]

func (Decoder) Decode

func (decoder Decoder) Decode(dst, src []byte) (int, error)

Decode converts the destination bytes back into the byte slice.

func (Decoder) DecodedLen

func (decoder Decoder) DecodedLen(dstLen int) int

DecodedLen returns the number of bytes that the decoded destination would be.

func (Decoder) Encoder

func (decoder Decoder) Encoder() Encoder

Encoder returns an encoder that is compatible with the decoder.

type Encoder

type Encoder []byte

Encoder encodes source bytes to a destination byte slice.

Example
package main

import (
	"fmt"

	"github.com/spenserblack/anybase"
)

func main() {
	// Base-2, where the digits are "a" and "b"
	encoder := anybase.Encoder("ab")
	src := []byte{0b01011001}
	dst := make([]byte, encoder.EncodedLen(len(src)))
	encoder.Encode(src, dst)
	fmt.Printf("%s", dst)
}
Output:

ababbaab

func (Encoder) Decoder

func (encoder Encoder) Decoder() Decoder

Decoder creates a decoder that is compatible with the encoder.

func (Encoder) Encode

func (encoder Encoder) Encode(src, dst []byte) int

Encode converts the source bytes to a destination base-n byte slice.

func (Encoder) EncodedLen

func (encoder Encoder) EncodedLen(srcLen int) int

EncodedLen returns the length of an encoded dst.

type ErrBadEncodedByte

type ErrBadEncodedByte byte

ErrBadEncodedByte is an error type for an invalid encoded byte.

func (ErrBadEncodedByte) Error

func (err ErrBadEncodedByte) Error() string

Jump to

Keyboard shortcuts

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