Table of Contents

Class SimpleSpanEncoding

Namespace
Yarhl.Media.Text.Encodings
Assembly
Yarhl.Media.Text.dll

Custom text encoding based on Span types simple to implement.

public abstract class SimpleSpanEncoding : Encoding, ICloneable
Inheritance
SimpleSpanEncoding
Implements
Inherited Members

Constructors

SimpleSpanEncoding(int)

Initializes a new instance of the SimpleSpanEncoding class.

protected SimpleSpanEncoding(int codePage)

Parameters

codePage int

The code page of the encoding.

Remarks

The encoder and decoder fallback throw exceptions.

SimpleSpanEncoding(int, EncoderFallback, DecoderFallback)

Initializes a new instance of the SimpleSpanEncoding class.

protected SimpleSpanEncoding(int codePage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)

Parameters

codePage int

The code page of the encoding.

encoderFallback EncoderFallback

The encoder fallback.

decoderFallback DecoderFallback

The decoder fallback.

Properties

BodyName

Gets a name for the current encoding that can be used with mail agent body tags.

public override string BodyName { get; }

Property Value

string

EncodingName

Gets the human-readable description of the current encoding.

public override abstract string EncodingName { get; }

Property Value

string

HeaderName

Gets a name for the current encoding that can be used with mail agent header tags.

public override string HeaderName { get; }

Property Value

string

IsBrowserDisplay

Gets a value indicating whether the current encoding can be used by browser clients for displaying content. As it's a custom encoding and the other side may not have it, by default is false.

public override bool IsBrowserDisplay { get; }

Property Value

bool

IsBrowserSave

Gets a value indicating whether the current encoding can be used by browser clients for saving content. As it's a custom encoding and the other side may not have it, by default is false.

public override bool IsBrowserSave { get; }

Property Value

bool

IsMailNewsDisplay

Gets a value indicating whether the current encoding can be used by mail and news clients for displaying content. As it's a custom encoding and the other side may not have it, by default is false.

public override bool IsMailNewsDisplay { get; }

Property Value

bool

IsMailNewsSave

Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. As it's a custom encoding and the other side may not have it, by default is false.

public override bool IsMailNewsSave { get; }

Property Value

bool

IsSingleByte

Gets a value indicating whether the current encoding uses single-byte code points.

public override bool IsSingleByte { get; }

Property Value

bool

WebName

Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. As it's a custom encoding, no name is registered in IANA, return empty string.

public override string WebName { get; }

Property Value

string

WindowsCodePage

Gets the Windows operating system code page that most closely corresponds to the current encoding. This property is not in used anymore. By default is 0.

public override int WindowsCodePage { get; }

Property Value

int

Methods

Decode(ReadOnlySpan<byte>, SpanStream<char>)

Decodes the bytes into a char stream.

protected abstract void Decode(ReadOnlySpan<byte> bytes, SpanStream<char> buffer)

Parameters

bytes ReadOnlySpan<byte>

The bytes to decode.

buffer SpanStream<char>

The output char stream.

DecodeUnknownBytes(SpanStream<char>, int, params byte[])

Reports invalid encoded bytes and write the fallback text to the stream depending on the fallback strategy.

protected void DecodeUnknownBytes(SpanStream<char> buffer, int index, params byte[] bytesUnknown)

Parameters

buffer SpanStream<char>

The output chars buffer to write the fallback text.

index int

The index where the invalid data was found.

bytesUnknown byte[]

The invalid unknown bytes found.

Encode(ReadOnlySpan<char>, SpanStream<byte>, bool)

Encodes the characters into the stream.

protected abstract void Encode(ReadOnlySpan<char> chars, SpanStream<byte> buffer, bool isFallbackText = false)

Parameters

chars ReadOnlySpan<char>

The characters to encode.

buffer SpanStream<byte>

The output byte stream.

isFallbackText bool

Value indicating if the unknown char comes from fallback text already.

EncodeUnknownChar(SpanStream<byte>, int, int, bool)

Reports an invalid code-point for this encoding and encode the fallback text.

protected void EncodeUnknownChar(SpanStream<byte> buffer, int codePoint, int index, bool isFallbackText)

Parameters

buffer SpanStream<byte>

The current output byte buffer to encode the fallback text.

codePoint int

The UTF-32 code-point that cannot be encoded.

index int

The index where the code-point was found in the buffer.

isFallbackText bool

Value indicating if the unknown char comes from fallback text already.

Exceptions

FormatException

Thrown when the fallback text cannot be encoded.

GetByteCount(char[])

Calculates the number of bytes produced by encoding all the characters in the specified character array.

public override int GetByteCount(char[] chars)

Parameters

chars char[]

The character array containing the characters to encode.

Returns

int

The number of bytes produced by encoding all the characters in the specified character array.

GetByteCount(char[], int, int)

Calculates the number of bytes produced by encoding a set of characters from the specified character array.

public override int GetByteCount(char[] chars, int index, int count)

Parameters

chars char[]

The character array containing the set of characters to encode.

index int

The index of the first character to encode.

count int

The number of characters to encode.

Returns

int

The number of bytes produced by encoding the specified characters.

GetByteCount(ReadOnlySpan<char>)

Calculates the number of bytes produced by encoding the characters in the specified character span.

public override int GetByteCount(ReadOnlySpan<char> chars)

Parameters

chars ReadOnlySpan<char>

The span of characters to encode.

Returns

int

The number of bytes produced by encoding the specified character span.

GetByteCount(string)

Calculates the number of bytes produced by encoding the characters in the specified string.

public override int GetByteCount(string s)

Parameters

s string

The string containing the set of characters to encode.

Returns

int

The number of bytes produced by encoding the specified characters.

GetBytes(char[])

When overridden in a derived class, encodes all the characters in the specified character array into a sequence of bytes.

public override byte[] GetBytes(char[] chars)

Parameters

chars char[]

The character array containing the characters to encode.

Returns

byte[]

A byte array containing the results of encoding the specified set of characters.

Exceptions

ArgumentNullException

chars is null.

EncoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

EncoderFallback is set to EncoderExceptionFallback.

GetBytes(char[], int, int)

When overridden in a derived class, encodes a set of characters from the specified character array into a sequence of bytes.

public override byte[] GetBytes(char[] chars, int index, int count)

Parameters

chars char[]

The character array containing the set of characters to encode.

index int

The index of the first character to encode.

count int

The number of characters to encode.

Returns

byte[]

A byte array containing the results of encoding the specified set of characters.

Exceptions

ArgumentNullException

chars is null.

ArgumentOutOfRangeException

index or count is less than zero.

-or-

index and count do not denote a valid range in chars.

EncoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

EncoderFallback is set to EncoderExceptionFallback.

GetBytes(char[], int, int, byte[], int)

When overridden in a derived class, encodes a set of characters from the specified character array into the specified byte array.

public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)

Parameters

chars char[]

The character array containing the set of characters to encode.

charIndex int

The index of the first character to encode.

charCount int

The number of characters to encode.

bytes byte[]

The byte array to contain the resulting sequence of bytes.

byteIndex int

The index at which to start writing the resulting sequence of bytes.

Returns

int

The actual number of bytes written into bytes.

Exceptions

ArgumentNullException

chars is null.

-or-

bytes is null.

ArgumentOutOfRangeException

charIndex or charCount or byteIndex is less than zero.

-or-

charIndex and charCount do not denote a valid range in chars.

-or-

byteIndex is not a valid index in bytes.

ArgumentException

bytes does not have enough capacity from byteIndex to the end of the array to accommodate the resulting bytes.

EncoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

EncoderFallback is set to EncoderExceptionFallback.

GetBytes(ReadOnlySpan<char>)

Encodes the characters.

public byte[] GetBytes(ReadOnlySpan<char> chars)

Parameters

chars ReadOnlySpan<char>

The characters to encode.

Returns

byte[]

The output encoded data.

GetBytes(ReadOnlySpan<char>, Span<byte>)

When overridden in a derived class, encodes into a span of bytes a set of characters from the specified read-only span.

public override int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes)

Parameters

chars ReadOnlySpan<char>

The span containing the set of characters to encode.

bytes Span<byte>

The byte span to hold the encoded bytes.

Returns

int

The number of encoded bytes.

GetBytes(string)

When overridden in a derived class, encodes all the characters in the specified string into a sequence of bytes.

public override byte[] GetBytes(string s)

Parameters

s string

The string containing the characters to encode.

Returns

byte[]

A byte array containing the results of encoding the specified set of characters.

Exceptions

ArgumentNullException

s is null.

EncoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

EncoderFallback is set to EncoderExceptionFallback.

GetBytes(string, int, int, byte[], int)

When overridden in a derived class, encodes a set of characters from the specified string into the specified byte array.

public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)

Parameters

s string

The string containing the set of characters to encode.

charIndex int

The index of the first character to encode.

charCount int

The number of characters to encode.

bytes byte[]

The byte array to contain the resulting sequence of bytes.

byteIndex int

The index at which to start writing the resulting sequence of bytes.

Returns

int

The actual number of bytes written into bytes.

Exceptions

ArgumentNullException

s is null.

-or-

bytes is null.

ArgumentOutOfRangeException

charIndex or charCount or byteIndex is less than zero.

-or-

charIndex and charCount do not denote a valid range in chars.

-or-

byteIndex is not a valid index in bytes.

ArgumentException

bytes does not have enough capacity from byteIndex to the end of the array to accommodate the resulting bytes.

EncoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

EncoderFallback is set to EncoderExceptionFallback.

GetCharCount(byte[])

When overridden in a derived class, calculates the number of characters produced by decoding all the bytes in the specified byte array.

public override int GetCharCount(byte[] bytes)

Parameters

bytes byte[]

The byte array containing the sequence of bytes to decode.

Returns

int

The number of characters produced by decoding the specified sequence of bytes.

Exceptions

ArgumentNullException

bytes is null.

DecoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

GetCharCount(byte[], int, int)

When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.

public override int GetCharCount(byte[] bytes, int index, int count)

Parameters

bytes byte[]

The byte array containing the sequence of bytes to decode.

index int

The index of the first byte to decode.

count int

The number of bytes to decode.

Returns

int

The number of characters produced by decoding the specified sequence of bytes.

Exceptions

ArgumentNullException

bytes is null.

ArgumentOutOfRangeException

index or count is less than zero.

-or-

index and count do not denote a valid range in bytes.

DecoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

GetCharCount(ReadOnlySpan<byte>)

When overridden in a derived class, calculates the number of characters produced by decoding the provided read-only byte span.

public override int GetCharCount(ReadOnlySpan<byte> bytes)

Parameters

bytes ReadOnlySpan<byte>

A read-only byte span to decode.

Returns

int

The number of characters produced by decoding the byte span.

GetChars(byte[])

When overridden in a derived class, decodes all the bytes in the specified byte array into a set of characters.

public override char[] GetChars(byte[] bytes)

Parameters

bytes byte[]

The byte array containing the sequence of bytes to decode.

Returns

char[]

A character array containing the results of decoding the specified sequence of bytes.

Exceptions

ArgumentNullException

bytes is null.

DecoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

GetChars(byte[], int, int)

When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a set of characters.

public override char[] GetChars(byte[] bytes, int index, int count)

Parameters

bytes byte[]

The byte array containing the sequence of bytes to decode.

index int

The index of the first byte to decode.

count int

The number of bytes to decode.

Returns

char[]

A character array containing the results of decoding the specified sequence of bytes.

Exceptions

ArgumentNullException

bytes is null.

ArgumentOutOfRangeException

index or count is less than zero.

-or-

index and count do not denote a valid range in bytes.

DecoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

GetChars(byte[], int, int, char[], int)

When overridden in a derived class, decodes a sequence of bytes from the specified byte array into the specified character array.

public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)

Parameters

bytes byte[]

The byte array containing the sequence of bytes to decode.

byteIndex int

The index of the first byte to decode.

byteCount int

The number of bytes to decode.

chars char[]

The character array to contain the resulting set of characters.

charIndex int

The index at which to start writing the resulting set of characters.

Returns

int

The actual number of characters written into chars.

Exceptions

ArgumentNullException

bytes is null.

-or-

chars is null.

ArgumentOutOfRangeException

byteIndex or byteCount or charIndex is less than zero.

-or-

byteindex and byteCount do not denote a valid range in bytes.

-or-

charIndex is not a valid index in chars.

ArgumentException

chars does not have enough capacity from charIndex to the end of the array to accommodate the resulting characters.

DecoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

GetChars(ReadOnlySpan<byte>)

Decodes the bytes.

public char[] GetChars(ReadOnlySpan<byte> bytes)

Parameters

bytes ReadOnlySpan<byte>

The data to decode.

Returns

char[]

The decoded characters.

GetChars(ReadOnlySpan<byte>, Span<char>)

When overridden in a derived class, decodes all the bytes in the specified read-only byte span into a character span.

public override int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars)

Parameters

bytes ReadOnlySpan<byte>

A read-only span containing the sequence of bytes to decode.

chars Span<char>

The character span receiving the decoded bytes.

Returns

int

The number of decoded bytes.

GetString(byte[])

When overridden in a derived class, decodes all the bytes in the specified byte array into a string.

public override string GetString(byte[] bytes)

Parameters

bytes byte[]

The byte array containing the sequence of bytes to decode.

Returns

string

A string that contains the results of decoding the specified sequence of bytes.

Exceptions

ArgumentException

The byte array contains invalid Unicode code points.

ArgumentNullException

bytes is null.

DecoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.

GetString(byte[], int, int)

When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a string.

public override string GetString(byte[] bytes, int index, int count)

Parameters

bytes byte[]

The byte array containing the sequence of bytes to decode.

index int

The index of the first byte to decode.

count int

The number of bytes to decode.

Returns

string

A string that contains the results of decoding the specified sequence of bytes.

Exceptions

ArgumentException

The byte array contains invalid Unicode code points.

ArgumentNullException

bytes is null.

ArgumentOutOfRangeException

index or count is less than zero.

-or-

index and count do not denote a valid range in bytes.

DecoderFallbackException

A fallback occurred (for more information, see Character Encoding in .NET)

-and-

DecoderFallback is set to DecoderExceptionFallback.