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
BitsPerCharacter
Bits each data character carries (log2 of the alphabet size).
public const int BitsPerCharacter = 5
Field Value
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
DefaultGroupSize
Characters per group when a code is formatted for display.
public const int DefaultGroupSize = 3
Field Value
DefaultSeparator
The separator Format(string) puts between groups.
public const char DefaultSeparator = '-'
Field Value
MaxDataLength
Most data characters a code may have (the check character not counted).
public const int MaxDataLength = 16
Field Value
MinDataLength
Fewest data characters a code may have (the check character not counted).
public const int MinDataLength = 6
Field Value
Methods
ComputeCheckCharacter(string)
The check character for data, which must be upper-case alphabet symbols.
public static char ComputeCheckCharacter(string data)
Parameters
datastring
Returns
Exceptions
- ArgumentNullException
datais null.- ArgumentException
dataholds a character outside Alphabet.
EntropyBits(int)
Bits of entropy in a random code with dataLength data characters.
public static int EntropyBits(int dataLength)
Parameters
dataLengthint
Returns
Format(string)
Splits canonical into groups of DefaultGroupSize joined by
DefaultSeparator, for display: ABC-EFG-HJK.
public static string Format(string canonical)
Parameters
canonicalstring
Returns
Format(string, int, char)
Splits canonical into groups of groupSize joined by separator.
public static string Format(string canonical, int groupSize, char separator)
Parameters
Returns
Exceptions
- ArgumentOutOfRangeException
groupSizeis 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
canonicalstring
Returns
IsAlphabetCharacter(char)
True when c is one of the 32 alphabet symbols (upper case only).
public static bool IsAlphabetCharacter(char c)
Parameters
cchar
Returns
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
cchar
Returns
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
inputstring