Skip to main content

Class CrockfordBase32

Namespace: AdCodicem.ValueObjects.Identifiers
Assembly: AdCodicem.ValueObjects.Identifiers.dll

The Crockford Base32 alphabet, and the decoding that makes an entity identifier canonical.

public static class CrockfordBase32

Inheritance

objectCrockfordBase32

Inherited Members

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks

The alphabet is 0123456789abcdefghjkmnpqrstvwxyz: the digits, then the letters with i, l, o and u removed. Two properties are load bearing here.

It is strictly increasing in ASCII, so an ordinal comparison of two encoded values reproduces the comparison of the numbers they encode. That is what lets the time bucket at the head of an identifier order the database index chronologically without any decoding, under the ordinal comparison this library already uses by default.

It is case insensitive on input and folds the confusable characters, so normalization produces a single canonical spelling and a case-insensitive column collation can no longer collapse two distinct identifiers into one.

Fields

Alphabet

The encoding alphabet, indexed by the value it encodes.

public const string Alphabet = "0123456789abcdefghjkmnpqrstvwxyz"

Field Value

string

Remarks

Lower case is the canonical spelling, so an identifier is one unbroken lowercase token wherever it travels — a URL, a JSON body, a log line. Upper case still decodes; normalization folds it down.

BitsPerSymbol

The number of bits one symbol carries.

public const int BitsPerSymbol = 5

Field Value

int

Methods

Canonicalize(char)

Rewrites a character into its canonical spelling.

public static char Canonicalize(char symbol)

Parameters

symbol char

Character to canonicalize.

Returns

char

The canonical symbol, or symbol itself when it decodes to nothing — normalization must never reject, so an unusable character is carried through for validation to refuse.

Decode(char)

Decodes a symbol, accepting either case and the Crockford aliases.

public static int Decode(char symbol)

Parameters

symbol char

Character to decode.

Returns

int

The value from 0 to 31, or -1 when symbol is not a symbol.

Encode(int)

Encodes a five-bit value.

public static char Encode(int value)

Parameters

value int

Value to encode, from 0 to 31.

Returns

char

The symbol carrying value.

Exceptions

ArgumentOutOfRangeException

value does not fit in five bits.

IsSymbol(char)

Determines whether a character decodes to a value.

public static bool IsSymbol(char symbol)

Parameters

symbol char

Character to test.

Returns

bool

true when symbol is a symbol or one of its aliases.