Class JoinCodeFormat

Namespace
Unity.Multiplayer.Core

The join-code format: data characters from a 32-symbol alphabet with no look-alike glyphs, followed by one check character.

public static class JoinCodeFormat
Inheritance
JoinCodeFormat
Inherited Members

Remarks

Alphabet. Alphabet is the digits 2–9 and the letters A–Z without I and O: exactly 32 symbols, so each character carries 5 bits. A code never contains 0, O, 1 or I, so a reader never has to tell them apart.

Check character. The last character is a Damm check over GF(32) (the quasigroup x ∘ y = αx + y with α a root of x⁵ + x² + 1). It catches every single-character substitution, every swap of two neighbouring characters and every swap of two characters one apart, wherever they fall, the check character included.

Typo tolerance. Parsing ignores case, whitespace and the separators - _ . (and Unicode dashes). Because a correct code never contains 0, O, 1 or I, such a character is a misread; if exactly one appears, the parser tries the glyphs it is usually mistaken for (0/O → D or Q; 1/I → L, J, T or 7) and accepts the one candidate the check character confirms. The check catches every single substitution, so at most one candidate can pass.

Fields

Alphabet

The 32 symbols a code is written in, in value order: 2–9, then A–Z without I and O.

public const string Alphabet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ"

Field Value

string

BitsPerCharacter

Bits each data character carries (log2 of the alphabet size).

public const int BitsPerCharacter = 5

Field Value

int

DefaultDataLength

Data characters in a freshly generated code: 8, i.e. 40 bits of entropy; with the check character a code is 9 characters, shown as ABC-EFG-HJK.

public const int DefaultDataLength = 8

Field Value

int

DefaultGroupSize

Characters per group when a code is formatted for display.

public const int DefaultGroupSize = 3

Field Value

int

DefaultSeparator

The separator Format(string) puts between groups.

public const char DefaultSeparator = '-'

Field Value

char

MaxDataLength

Most data characters a code may have (the check character not counted).

public const int MaxDataLength = 16

Field Value

int

MinDataLength

Fewest data characters a code may have (the check character not counted).

public const int MinDataLength = 6

Field Value

int

Methods

ComputeCheckCharacter(string)

The check character for data, which must be upper-case alphabet symbols.

public static char ComputeCheckCharacter(string data)

Parameters

data string

Returns

char

Exceptions

ArgumentNullException

data is null.

ArgumentException

data holds a character outside Alphabet.

EntropyBits(int)

Bits of entropy in a random code with dataLength data characters.

public static int EntropyBits(int dataLength)

Parameters

dataLength int

Returns

int

Format(string)

Splits canonical into groups of DefaultGroupSize joined by DefaultSeparator, for display: ABC-EFG-HJK.

public static string Format(string canonical)

Parameters

canonical string

Returns

string

Format(string, int, char)

Splits canonical into groups of groupSize joined by separator.

public static string Format(string canonical, int groupSize, char separator)

Parameters

canonical string
groupSize int
separator char

Returns

string

Exceptions

ArgumentOutOfRangeException

groupSize is less than 1.

HasValidCheck(string)

True when canonical (upper-case alphabet symbols, check character last) passes the check. Length is not checked here.

public static bool HasValidCheck(string canonical)

Parameters

canonical string

Returns

bool

IsAlphabetCharacter(char)

True when c is one of the 32 alphabet symbols (upper case only).

public static bool IsAlphabetCharacter(char c)

Parameters

c char

Returns

bool

LookAlikeCandidates(char)

The alphabet symbols c is usually a misreading of, or the empty string when c is not a known look-alike (0, O, 1, I, upper case).

public static string LookAlikeCandidates(char c)

Parameters

c char

Returns

string

Normalize(string)

Upper-cases input and strips whitespace and separators (- _ . and Unicode dashes). It does not correct look-alikes or validate; TryParse(string, out JoinCodeParseResult) does. Null becomes the empty string.

public static string Normalize(string input)

Parameters

input string

Returns

string