Documentation
¶
Overview ¶
Package baseconv provides customizable base encoding/decoding functionality for arbitrary alphabets. It supports both bitwise and mathematical encoding algorithms with optional padding, making it suitable for creating URL-safe identifiers, compact data representations, and custom encoding schemes.
Index ¶
Constants ¶
const ( // AlphabetBase32 defines the character set for base32 encoding using Crockford's variant. // It excludes ambiguous characters (I, L, O, U) to prevent confusion in human-readable contexts. AlphabetBase32 = "0123456789ABCDEFGHJKLMNPQRSTVWXYZ" // AlphabetBase62 defines the character set for base62 encoding. // It includes digits, uppercase letters, and lowercase letters for maximum character density. AlphabetBase62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" )
Variables ¶
var ( // Std32Encoding is the standard base32 encoder using Crockford's alphabet without padding. Std32Encoding = must(NewBaseEncoding(AlphabetBase32)) // StdRaw32Encoding is the standard base32 encoder with padding using '=' character. StdRaw32Encoding = must(NewBaseEncodingWithPadding(AlphabetBase32, '=')) )
var ( // Std62Encoding is the standard base62 encoder without padding. Std62Encoding = must(NewBaseEncoding(AlphabetBase62)) // StdRaw62Encoding is the standard base62 encoder with padding using '=' character. StdRaw62Encoding = must(NewBaseEncodingWithPadding(AlphabetBase62, '=')) )
Functions ¶
This section is empty.
Types ¶
type BaseEncoding ¶
type BaseEncoding struct {
// contains filtered or unexported fields
}
BaseEncoding provides customizable base encoding/decoding functionality. It supports arbitrary alphabets and optional padding characters for flexible encoding schemes.
func NewBaseEncoding ¶
func NewBaseEncoding(alphabet string) (*BaseEncoding, error)
NewBaseEncoding creates a new base encoding instance with the specified alphabet. The alphabet defines the character set used for encoding and must contain at least 2 unique characters.
func NewBaseEncodingWithPadding ¶
func NewBaseEncodingWithPadding(alphabet string, padChar byte) (*BaseEncoding, error)
NewBaseEncodingWithPadding creates a new base encoding instance with alphabet and padding character. The padding character is used to align encoded output and must not conflict with alphabet characters.
func (*BaseEncoding) DecodeString ¶
func (e *BaseEncoding) DecodeString(encoded string) ([]byte, error)
DecodeString decodes a base-encoded string back to binary data. It automatically handles padding removal and selects the appropriate decoding method based on the alphabet size. Returns an error if the input contains invalid characters.
func (*BaseEncoding) EncodeToString ¶
func (e *BaseEncoding) EncodeToString(data []byte) string
EncodeToString encodes binary data to a string using the configured base encoding. It automatically selects the most efficient encoding method based on the alphabet size.