Base1024Encoding
Namespace: SnowBank.Text · class
Helper type for using the Base-1024 encoding
Remarks
The Base-1024 encoding represents a span of bytes into a Unicode string that packs 10 input bits into each character, and that will preserve the ordering (when using ordinal sorting rules)
String literals generated by this encoding will use a range of code points from 48 (\u0030, or the Digit Zero '0') to 1071 (\u042f or the Cyrillic Capital Letter Ya 'Я').
When converting values (integers, uuids, ...) with this encoding, please ensure that you are using big-endian encoding, as well as identically sized intermediate byte representation! Comparing differently-size buffers may not preserve the initial ordering.
For example: (int) 0x1234 and (long) 0x1234 will be represented as 00001234 and 0000000000001234 that encodes to "00ŤȰ!" and "00000}°".
If unsure, always use the largest possible byte size for the intermediate representation.
Methods
Decode
static Slice Decode(string source)
static Slice Decode(ReadOnlySpan<char> source)
static TResult Decode<TResult>(ReadOnlySpan<char> source, Func<ReadOnlySpan<byte>, TResult> decoder)
DecodeGuidValue
static Guid DecodeGuidValue(ReadOnlySpan<char> source)
DecodeInt32Value
static int DecodeInt32Value(ReadOnlySpan<char> source)
DecodeInt64Value
static long DecodeInt64Value(ReadOnlySpan<char> source)
DecodeUInt32Value
static uint DecodeUInt32Value(ReadOnlySpan<char> source)
DecodeUInt64Value
static ulong DecodeUInt64Value(ReadOnlySpan<char> source)
DecodeUuid128Value
static Uuid128 DecodeUuid128Value(ReadOnlySpan<char> source)
DecodeUuid64Value
static Uuid64 DecodeUuid64Value(ReadOnlySpan<char> source)
DecodeUuid80Value
static Uuid80 DecodeUuid80Value(ReadOnlySpan<char> source)
DecodeUuid96Value
static Uuid96 DecodeUuid96Value(ReadOnlySpan<char> source)
Encode
static string Encode(byte[] source)
Encodes an array of bytes into a Base-1024 string literal
source— Bytes to encode
Returns: Encoded string literal that preserves the ordering of the input value
static string Encode(Slice source)
Encodes a Slice into a Base-1024 string literal
source— Bytes to encode
Returns: Encoded string literal that preserves the ordering of the input value
static string Encode(ReadOnlySpan<byte> source)
Encodes a span of bytes into a Base-1024 string literal
source— Bytes to encode
Returns: Encoded string literal that preserves the ordering of the input value
EncodeGuidValue
static string EncodeGuidValue(Guid value)
Encodes a Guid into a Base-1024 string literal
value— Value to encode
Returns: Encoded string literal that preserves the ordering of the input value
Strings generated by this method can be compared to other strings generated from inputs of 16 bytes using big-endian encoding. Comparisons with differently sized inputs will produce an invalid ordering!
EncodeInt32Value
static string EncodeInt32Value(int value)
Encodes a Int32 into a Base-1024 string literal
value— Value to encode
Returns: String literal that preserves the ordering the of input value
Strings generated by this method can be compared to other strings generated from inputs of 4 bytes using big-endian encoding. Comparisons with differently sized inputs will produce an invalid ordering!
EncodeInt64Value
static string EncodeInt64Value(long value)
Encodes a Int64 into a Base-1024 string literal
value— Value to encode
Returns: Encoded string literal that preserves the ordering of the input value
Strings generated by this method can be compared to other strings generated from inputs of 8 bytes using big-endian encoding. Comparisons with differently sized inputs will produce an invalid ordering!
EncodeUInt32Value
static string EncodeUInt32Value(uint value)
Encodes a UInt32 into a Base-1024 string literal
value— Value to encode
Returns: Encoded string literal that preserves the ordering of the input value
Strings generated by this method can be compared to other strings generated from inputs of 4 bytes using big-endian encoding. Comparisons with differently sized inputs will produce an invalid ordering!
EncodeUInt64Value
static string EncodeUInt64Value(ulong value)
Encodes a UInt64 into a Base-1024 string literal
value— Value to encode
Returns: Encoded string literal that preserves the ordering of the input value
Strings generated by this method can be compared to other strings generated from inputs of 8 bytes using big-endian encoding. Comparisons with differently sized inputs will produce an invalid ordering!
EncodeUtf8StringValue
static string EncodeUtf8StringValue(string value)
Encodes a Uuid128 into a Base-1024 string literal
value— Value to encode
Returns: Encoded string literal that preserves the ordering of the input value
The input string will be encoded into UTF-8 bytes first, and then these bytes will be encoded by calling Encode
Strings generated by this method can be compared to other strings if they also represented ASCII or UTF-8 encoded "text"
static string EncodeUtf8StringValue(ReadOnlySpan<char> value)
Encodes a Uuid128 into a Base-1024 string literal
value— Value to encode
Returns: Encoded string literal that preserves the ordering of the input value
The input string will be encoded into UTF-8 bytes first, and then these bytes will be encoded by calling Encode
Strings generated by this method can be compared to other strings if they also represented ASCII or UTF-8 encoded "text"
EncodeUuid128Value
static string EncodeUuid128Value(Uuid128 value)
Encodes a Uuid128 into a Base-1024 string literal
value— Value to encode
Returns: Encoded string literal that preserves the ordering of the input value
Strings generated by this method can be compared to other strings generated from inputs of 16 bytes using big-endian encoding. Comparisons with differently sized inputs will produce an invalid ordering!
GetByteCount
static int GetByteCount(ReadOnlySpan<char> source)
Computes the minimum capacity (in bytes) to decode a given Base-1024 string literal
source— String literal that needs to be decoded
Returns: Minimum buffer size (in bytes) required to decode the entire string.
Decoding requires, on average, 5 bytes per 4 input characters.
GetCharCount
static int GetCharCount(int byteCount)
Computes the number of characters required to encode a given number of bytes into Base-1024
byteCount— Size in bytes of the input
Returns: Size in characters of the encoded output
Encoding requires, on average, 4 characters per 5 input bytes.
TryDecodeTo
static bool TryDecodeTo(ReadOnlySpan<char> source, Span<byte> output, out int bytesWritten)
TryEncodeTo
static bool TryEncodeTo(Slice source, Span<char> destination, out int charsWritten)
Encodes a span of bytes into a Base-1024 string literal into a destination buffer
source— Bytes to encodedestination— Destination buffer, which should be large enoughcharsWritten— If successful, receives the number of bytes written todestination
Returns: false if destination was not large enough; otherwise, true.
The caller can determine the minimum buffer capacity by calling .
static bool TryEncodeTo(ReadOnlySpan<byte> source, Span<char> destination, out int charsWritten)