From 01df1f48e118f619533ac8697912765be33c36eb Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 29 Apr 2021 18:42:12 +0100 Subject: [PATCH] Use abstract class to implement encoding methods. --- Claunia.Encoding.sln.DotSettings | 3 + Claunia.Encoding/ATASCII.cs | 358 ++---------------------- Claunia.Encoding/Apple2.cs | 358 ++---------------------- Claunia.Encoding/Apple2c.cs | 358 ++---------------------- Claunia.Encoding/Apple2e.cs | 358 ++---------------------- Claunia.Encoding/Apple2gs.cs | 358 ++---------------------- Claunia.Encoding/AtariST.cs | 358 ++---------------------- Claunia.Encoding/Encoding.cs | 20 +- Claunia.Encoding/Gem.cs | 358 ++---------------------- Claunia.Encoding/Geos.cs | 358 ++---------------------- Claunia.Encoding/LisaRoman.cs | 358 ++---------------------- Claunia.Encoding/MacArabic.cs | 358 ++---------------------- Claunia.Encoding/MacCentralEuropean.cs | 358 ++---------------------- Claunia.Encoding/MacCroatian.cs | 358 ++---------------------- Claunia.Encoding/MacCyrillic.cs | 358 ++---------------------- Claunia.Encoding/MacFarsi.cs | 358 ++---------------------- Claunia.Encoding/MacGreek.cs | 358 ++---------------------- Claunia.Encoding/MacHebrew.cs | 358 ++---------------------- Claunia.Encoding/MacRoman.cs | 358 ++---------------------- Claunia.Encoding/MacRomanian.cs | 358 ++---------------------- Claunia.Encoding/MacTurkish.cs | 358 ++---------------------- Claunia.Encoding/MacUkrainian.cs | 358 ++---------------------- Claunia.Encoding/PETSCII.cs | 359 ++----------------------- Claunia.Encoding/Radix50.cs | 212 ++------------- Claunia.Encoding/SingleByteEncoding.cs | 337 +++++++++++++++++++++++ Claunia.Encoding/ZX80.cs | 358 ++---------------------- Claunia.Encoding/ZX81.cs | 358 ++---------------------- Claunia.Encoding/ZXSpectrum.cs | 358 ++---------------------- 28 files changed, 728 insertions(+), 8437 deletions(-) create mode 100644 Claunia.Encoding.sln.DotSettings create mode 100644 Claunia.Encoding/SingleByteEncoding.cs diff --git a/Claunia.Encoding.sln.DotSettings b/Claunia.Encoding.sln.DotSettings new file mode 100644 index 0000000..d4fff26 --- /dev/null +++ b/Claunia.Encoding.sln.DotSettings @@ -0,0 +1,3 @@ + + True + True \ No newline at end of file diff --git a/Claunia.Encoding/ATASCII.cs b/Claunia.Encoding/ATASCII.cs index a41f80b..5e284d4 100644 --- a/Claunia.Encoding/ATASCII.cs +++ b/Claunia.Encoding/ATASCII.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an ATARI Standard Code for Information Interchange character encoding of Unicode characters. - public class Atascii : Encoding + public class Atascii : SingleByteEncoding { - const string BODY_NAME = "atascii"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Atari Standard Code for Information Interchange"; - const string HEADER_NAME = "atascii"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "atascii"; + public override int CodePage => 0; + public override string EncodingName => "Atari Standard Code for Information Interchange"; + public override string HeaderName => "atascii"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => true; /// The ATASCII to Unicode character map. - static readonly char[] _atasciiTable = + protected override char[] CharTable => new[] { // 0x00 '\u2665', '\u251C', '\uFFFD', '\u2518', '\u2524', '\u2510', '\u2571', '\u2572', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0007', '\u0000', '\u0000' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a ATASCII character to an Unicode character - /// Unicode character. - /// ATASCII character. - static char GetChar(byte character) => _atasciiTable[character]; - /// Converts a Unicode character to an ATASCII character /// ATASCII character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/Apple2.cs b/Claunia.Encoding/Apple2.cs index 29bf10c..7ea4986 100644 --- a/Claunia.Encoding/Apple2.cs +++ b/Claunia.Encoding/Apple2.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Apple II character encoding of Unicode characters. - public class Apple2 : Encoding + public sealed class Apple2 : SingleByteEncoding { - const string BODY_NAME = "apple2"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Western European (Apple II)"; - const string HEADER_NAME = "apple2"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "apple2"; + public override int CodePage => 0; + public override string EncodingName => "Western European (Apple II)"; + public override string HeaderName => "apple2"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => true; /// The Apple II to Unicode character map. - static readonly char[] _apple2Table = + protected override char[] CharTable => new[] { // 0x00 '\u0040', '\u0041', '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Apple II character to an Unicode character - /// Unicode character. - /// Apple II character. - static char GetChar(byte character) => _apple2Table[character]; - /// Converts a Unicode character to an Apple II character /// Apple II character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/Apple2c.cs b/Claunia.Encoding/Apple2c.cs index 1134dcd..b04c70d 100644 --- a/Claunia.Encoding/Apple2c.cs +++ b/Claunia.Encoding/Apple2c.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Apple IIc character encoding of Unicode characters. - public class Apple2C : Encoding + public sealed class Apple2C : SingleByteEncoding { - const string BODY_NAME = "apple2c"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Western European (Apple IIc)"; - const string HEADER_NAME = "apple2c"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "apple2c"; + public override int CodePage => 0; + public override string EncodingName => "Western European (Apple IIc)"; + public override string HeaderName => "apple2c"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Apple IIc to Unicode character map. Inverted screen characters are mapped to normal characters. - static readonly char[] _apple2CTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', '\u007E', '\u007F' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Apple IIc character to an Unicode character - /// Unicode character. - /// Apple IIc character. - static char GetChar(byte character) => _apple2CTable[character]; - /// Converts a Unicode character to an Apple IIc character /// Apple IIc character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/Apple2e.cs b/Claunia.Encoding/Apple2e.cs index b64ca1a..f8a8c24 100644 --- a/Claunia.Encoding/Apple2e.cs +++ b/Claunia.Encoding/Apple2e.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Apple IIe character encoding of Unicode characters. - public class Apple2E : Encoding + public class Apple2E : SingleByteEncoding { - const string BODY_NAME = "apple2e"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Western European (Apple IIe)"; - const string HEADER_NAME = "apple2e"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "apple2e"; + public override int CodePage => 0; + public override string EncodingName => "Western European (Apple IIe)"; + public override string HeaderName => "apple2e"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Apple IIe to Unicode character map. - static readonly char[] _apple2ETable = + protected override char[] CharTable => new[] { // 0x00 '\u0020', '\u0021', '\u0022', '\u0023', '\u0024', '\u0025', '\u0026', '\u0027', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Apple IIe character to an Unicode character - /// Unicode character. - /// Apple IIe character. - static char GetChar(byte character) => _apple2ETable[character]; - /// Converts a Unicode character to an Apple IIe character /// Apple IIe character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/Apple2gs.cs b/Claunia.Encoding/Apple2gs.cs index cff2c41..cf636cc 100644 --- a/Claunia.Encoding/Apple2gs.cs +++ b/Claunia.Encoding/Apple2gs.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Apple IIgs character encoding of Unicode characters. - public class Apple2Gs : Encoding + public class Apple2Gs : SingleByteEncoding { - const string BODY_NAME = "apple2gs"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Western European (Apple IIgs)"; - const string HEADER_NAME = "apple2gs"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "apple2gs"; + public override int CodePage => 0; + public override string EncodingName => "Western European (Apple IIgs)"; + public override string HeaderName => "apple2gs"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Apple IIgs to Unicode character map. Inverted screen characters are mapped to normal characters. - static readonly char[] _apple2GsTable = + protected override char[] CharTable => new[] { // 0x00 '\u0040', '\u0041', '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', '\u007E', '\u0000' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Apple IIgs character to an Unicode character - /// Unicode character. - /// Apple IIgs character. - static char GetChar(byte character) => _apple2GsTable[character]; - /// Converts a Unicode character to an Apple IIgs character /// Apple IIgs character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/AtariST.cs b/Claunia.Encoding/AtariST.cs index 9b1fd88..f0e725e 100644 --- a/Claunia.Encoding/AtariST.cs +++ b/Claunia.Encoding/AtariST.cs @@ -24,31 +24,29 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Atari ST character encoding of Unicode characters. // TODO: 0x09 => U+1F552, 0x0A => U+1F514 - public class AtariSt : Encoding + public class AtariSt : SingleByteEncoding { - const string BODY_NAME = "atarist"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Western European (Atari ST)"; - const string HEADER_NAME = "atarist"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "atarist"; + public override int CodePage => 0; + public override string EncodingName => "Western European (Atari ST)"; + public override string HeaderName => "atarist"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => true; /// The Atari ST to Unicode character map. - static readonly char[] _atariStTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u21E7', '\u21E9', '\u21E8', '\u21E6', '\u274E', '\uFFFD', '\uFFFD', @@ -147,336 +145,10 @@ namespace Claunia.Encoding '\u00B0', '\u2219', '\u00B7', '\u221A', '\u207F', '\u00B2', '\u00B3', '\u00AF' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Atari ST character to an Unicode character - /// Unicode character. - /// Atari ST character. - static char GetChar(byte character) => _atariStTable[character]; - /// Converts a Unicode character to an Atari ST character /// Atari ST character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/Encoding.cs b/Claunia.Encoding/Encoding.cs index 569c57c..a5de3a4 100644 --- a/Claunia.Encoding/Encoding.cs +++ b/Claunia.Encoding/Encoding.cs @@ -92,11 +92,12 @@ namespace Claunia.Encoding /// Returns an array that contains all encodings. /// An array that contains all encodings. public new static IEnumerable GetEncodings() => - (from type in Assembly.GetExecutingAssembly().GetTypes() where type.IsSubclassOf(typeof(Encoding)) - let encoding = (Encoding)type.GetConstructor(new Type[] - {}).Invoke(new object[] - {}) select new - EncodingInfo(encoding.CodePage, encoding.BodyName, encoding.EncodingName, false, type)).ToArray(); + from type in Assembly.GetExecutingAssembly().GetTypes() + where type.IsSubclassOf(typeof(Encoding)) && !type.IsAbstract let encoding = (Encoding)type. + GetConstructor(new Type[] + {})?.Invoke(new object[] + {}) where encoding is {} + select new EncodingInfo(encoding.CodePage, encoding.BodyName, encoding.EncodingName, false, type); /// Returns the encoding associated with the specified code page name. /// The encoding associated with the specified code page. @@ -107,13 +108,14 @@ namespace Claunia.Encoding public new static System.Text.Encoding GetEncoding(string name) { foreach(Type type in Assembly.GetExecutingAssembly().GetTypes()) - if(type.IsSubclassOf(typeof(Encoding))) + if(type.IsSubclassOf(typeof(Encoding)) && + !type.IsAbstract) { var encoding = (Encoding)type.GetConstructor(new Type[] - {}).Invoke(new object[] - {}); + {})?.Invoke(new object[] + {}); - if(encoding.BodyName == name.ToLowerInvariant()) + if(encoding?.BodyName == name.ToLowerInvariant()) return encoding; } diff --git a/Claunia.Encoding/Gem.cs b/Claunia.Encoding/Gem.cs index 57968cd..23eb218 100644 --- a/Claunia.Encoding/Gem.cs +++ b/Claunia.Encoding/Gem.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents a GEM character encoding of Unicode characters. - public class Gem : Encoding + public class Gem : SingleByteEncoding { - const string BODY_NAME = "gem"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Western European (GEM)"; - const string HEADER_NAME = "gem"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "gem"; + public override int CodePage => 0; + public override string EncodingName => "Western European (GEM)"; + public override string HeaderName => "gem"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => true; /// The GEM to Unicode character map. Pending: 0x09 => 0x0001F552, 0x0A => 0x0001F514 - static readonly char[] _gemTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u21E7', '\u21E9', '\u21E8', '\u21E6', '\u25FC', '\uFFFD', '\u25C6', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u00B0', '\u2219', '\u00B7', '\u221A', '\u207F', '\u00B2', '\u25A0', '\u2205' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a GEM character to an Unicode character - /// Unicode character. - /// GEM character. - static char GetChar(byte character) => _gemTable[character]; - /// Converts a Unicode character to an GEM character /// GEM character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/Geos.cs b/Claunia.Encoding/Geos.cs index 2d42a20..7e4863e 100644 --- a/Claunia.Encoding/Geos.cs +++ b/Claunia.Encoding/Geos.cs @@ -24,32 +24,30 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an GEOS character encoding of Unicode characters. - public class Geos : Encoding + public class Geos : SingleByteEncoding { - const string BODY_NAME = "geos"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Western European (GEOS)"; - const string HEADER_NAME = "geos"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "geos"; + public override int CodePage => 0; + public override string EncodingName => "Western European (GEOS)"; + public override string HeaderName => "geos"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => true; /// /// The GEOS to Unicode character map. In the GEOS character map application lots of positions appears as '\u002E' /// ('.' period) in normal (non-symbol) fonts. /// - static readonly char[] _geosTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -148,336 +146,10 @@ namespace Claunia.Encoding '\u00B7', '\u00B0', '\u00B8', '\u2032', '\u02DB', '\u02D8', '\u00A0', '\u002E' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a GEOS character to an Unicode character - /// Unicode character. - /// GEOS character. - static char GetChar(byte character) => _geosTable[character]; - /// Converts a Unicode character to an GEOS character /// GEOS character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/LisaRoman.cs b/Claunia.Encoding/LisaRoman.cs index 897c85a..58cee67 100644 --- a/Claunia.Encoding/LisaRoman.cs +++ b/Claunia.Encoding/LisaRoman.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Apple Lisa character encoding of Unicode characters. - public class LisaRoman : Encoding + public class LisaRoman : SingleByteEncoding { - const string BODY_NAME = "lisa"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Western European (Apple Lisa)"; - const string HEADER_NAME = "lisa"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 10000; + public override string BodyName => "lisa"; + public override int CodePage => 0; + public override string EncodingName => "Western European (Apple Lisa)"; + public override string HeaderName => "lisa"; + public override string WebName => ""; + public override int WindowsCodePage => 10000; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => true; /// The Lisa to Unicode character map. MacRoman is a superset of LisaRoman. - static readonly char[] _lisaRomanTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a LisaRoman character to an Unicode character - /// Unicode character. - /// LisaRoman character. - static char GetChar(byte character) => _lisaRomanTable[character]; - /// Converts a Unicode character to an LisaRoman character /// LisaRoman character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacArabic.cs b/Claunia.Encoding/MacArabic.cs index a60ac95..b8cce47 100644 --- a/Claunia.Encoding/MacArabic.cs +++ b/Claunia.Encoding/MacArabic.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Mac Arabic character encoding of Unicode characters. - public class MacArabic : Encoding + public class MacArabic : SingleByteEncoding { - const string BODY_NAME = "x-mac-arabic"; - const int CODEPAGE = 10004; - const string ENCODING_NAME = "Arabic (Mac)"; - const string HEADER_NAME = "x-mac-arabic"; - const string WEB_NAME = "x-mac-arabic"; - const int WINDOWS_CODEPAGE = 10004; + public override string BodyName => "x-mac-arabic"; + public override int CodePage => 10004; + public override string EncodingName => "Arabic (Mac)"; + public override string HeaderName => "x-mac-arabic"; + public override string WebName => "x-mac-arabic"; + public override int WindowsCodePage => 10004; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Macintosh Arabic to Unicode character map. - static readonly char[] _macArabicTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u06AF', '\u0688', '\u0691', '\u007B', '\u007C', '\u007D', '\u0698', '\u06D2' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Mac Arabic character to an Unicode character - /// Unicode character. - /// Mac Arabic character. - static char GetChar(byte character) => _macArabicTable[character]; - /// Converts a Unicode character to an Mac Arabic character /// Mac Arabic character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacCentralEuropean.cs b/Claunia.Encoding/MacCentralEuropean.cs index e7689e4..6353316 100644 --- a/Claunia.Encoding/MacCentralEuropean.cs +++ b/Claunia.Encoding/MacCentralEuropean.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Mac CentralEuropean character encoding of Unicode characters. - public class MacCentralEuropean : Encoding + public class MacCentralEuropean : SingleByteEncoding { - const string BODY_NAME = "x-mac-ce"; - const int CODEPAGE = 10029; - const string ENCODING_NAME = "Central European (Mac)"; - const string HEADER_NAME = "x-mac-ce"; - const string WEB_NAME = "x-mac-ce"; - const int WINDOWS_CODEPAGE = 10029; + public override string BodyName => "x-mac-ce"; + public override int CodePage => 10029; + public override string EncodingName => "Central European (Mac)"; + public override string HeaderName => "x-mac-ce"; + public override string WebName => "x-mac-ce"; + public override int WindowsCodePage => 10029; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Macintosh CentralEuropean to Unicode character map. - static readonly char[] _macCentralEuropeanTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u00DD', '\u00FD', '\u0137', '\u017B', '\u0141', '\u017C', '\u0122', '\u02C7' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Mac CentralEuropean character to an Unicode character - /// Unicode character. - /// Mac CentralEuropean character. - static char GetChar(byte character) => _macCentralEuropeanTable[character]; - /// Converts a Unicode character to an Mac CentralEuropean character /// Mac CentralEuropean character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacCroatian.cs b/Claunia.Encoding/MacCroatian.cs index 096ab77..03e4b9d 100644 --- a/Claunia.Encoding/MacCroatian.cs +++ b/Claunia.Encoding/MacCroatian.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Mac Croatian character encoding of Unicode characters. - public class MacCroatian : Encoding + public class MacCroatian : SingleByteEncoding { - const string BODY_NAME = "x-mac-croatian"; - const int CODEPAGE = 10082; - const string ENCODING_NAME = "Croatian (Mac)"; - const string HEADER_NAME = "x-mac-croatian"; - const string WEB_NAME = "x-mac-croatian"; - const int WINDOWS_CODEPAGE = 10082; + public override string BodyName => "x-mac-croatian"; + public override int CodePage => 10082; + public override string EncodingName => "Croatian (Mac)"; + public override string HeaderName => "x-mac-croatian"; + public override string WebName => "x-mac-croatian"; + public override int WindowsCodePage => 10082; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Macintosh Croatian to Unicode character map. - static readonly char[] _macCroatianTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u00AF', '\u03C0', '\u00CB', '\u02DA', '\u00B8', '\u00CA', '\u00E6', '\u02C7' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Mac Croatian character to an Unicode character - /// Unicode character. - /// Mac Croatian character. - static char GetChar(byte character) => _macCroatianTable[character]; - /// Converts a Unicode character to an Mac Croatian character /// Mac Croatian character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacCyrillic.cs b/Claunia.Encoding/MacCyrillic.cs index 47c5e8a..21f1c86 100644 --- a/Claunia.Encoding/MacCyrillic.cs +++ b/Claunia.Encoding/MacCyrillic.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Mac Cyrillic character encoding of Unicode characters. - public class MacCyrillic : Encoding + public class MacCyrillic : SingleByteEncoding { - const string BODY_NAME = "x-mac-cyrillic"; - const int CODEPAGE = 10007; - const string ENCODING_NAME = "Cyrillic (Mac)"; - const string HEADER_NAME = "x-mac-cyrillic"; - const string WEB_NAME = "x-mac-cyrillic"; - const int WINDOWS_CODEPAGE = 10007; + public override string BodyName => "x-mac-cyrillic"; + public override int CodePage => 10007; + public override string EncodingName => "Cyrillic (Mac)"; + public override string HeaderName => "x-mac-cyrillic"; + public override string WebName => "x-mac-cyrillic"; + public override int WindowsCodePage => 10007; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Macintosh Cyrillic to Unicode character map. - static readonly char[] _macCyrillicTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u0448', '\u0449', '\u044A', '\u044B', '\u044C', '\u044D', '\u044E', '\u20AC' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Mac Cyrillic character to an Unicode character - /// Unicode character. - /// Mac Cyrillic character. - static char GetChar(byte character) => _macCyrillicTable[character]; - /// Converts a Unicode character to an Mac Cyrillic character /// Mac Cyrillic character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacFarsi.cs b/Claunia.Encoding/MacFarsi.cs index 32a4d59..937d441 100644 --- a/Claunia.Encoding/MacFarsi.cs +++ b/Claunia.Encoding/MacFarsi.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Mac Farsi character encoding of Unicode characters. - public class MacFarsi : Encoding + public class MacFarsi : SingleByteEncoding { - const string BODY_NAME = "x-mac-farsi"; - const int CODEPAGE = 10014; - const string ENCODING_NAME = "Farsi (Mac)"; - const string HEADER_NAME = "x-mac-farsi"; - const string WEB_NAME = "x-mac-farsi"; - const int WINDOWS_CODEPAGE = 10014; + public override string BodyName => "x-mac-farsi"; + public override int CodePage => 10014; + public override string EncodingName => "Farsi (Mac)"; + public override string HeaderName => "x-mac-farsi"; + public override string WebName => "x-mac-farsi"; + public override int WindowsCodePage => 10014; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Macintosh Farsi to Unicode character map. - static readonly char[] _macFarsiTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u06AF', '\u0688', '\u0691', '\u007B', '\u007C', '\u007D', '\u0698', '\u06D2' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Mac Farsi character to an Unicode character - /// Unicode character. - /// Mac Farsi character. - static char GetChar(byte character) => _macFarsiTable[character]; - /// Converts a Unicode character to an Mac Farsi character /// Mac Farsi character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacGreek.cs b/Claunia.Encoding/MacGreek.cs index 289e8f7..fcb5da6 100644 --- a/Claunia.Encoding/MacGreek.cs +++ b/Claunia.Encoding/MacGreek.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Mac Greek character encoding of Unicode characters. - public class MacGreek : Encoding + public class MacGreek : SingleByteEncoding { - const string BODY_NAME = "x-mac-greek"; - const int CODEPAGE = 10006; - const string ENCODING_NAME = "Greek (Mac)"; - const string HEADER_NAME = "x-mac-greek"; - const string WEB_NAME = "x-mac-greek"; - const int WINDOWS_CODEPAGE = 10006; + public override string BodyName => "x-mac-greek"; + public override int CodePage => 10006; + public override string EncodingName => "Greek (Mac)"; + public override string HeaderName => "x-mac-greek"; + public override string WebName => "x-mac-greek"; + public override int WindowsCodePage => 10006; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Macintosh Greek to Unicode character map. - static readonly char[] _macGreekTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u03C7', '\u03C5', '\u03B6', '\u03CA', '\u03CB', '\u0390', '\u03B0', '\u00AD' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Mac Greek character to an Unicode character - /// Unicode character. - /// Mac Greek character. - static char GetChar(byte character) => _macGreekTable[character]; - /// Converts a Unicode character to an Mac Greek character /// Mac Greek character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacHebrew.cs b/Claunia.Encoding/MacHebrew.cs index bdd6446..e0c5d48 100644 --- a/Claunia.Encoding/MacHebrew.cs +++ b/Claunia.Encoding/MacHebrew.cs @@ -24,33 +24,31 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Mac Hebrew character encoding of Unicode characters. - public class MacHebrew : Encoding + public class MacHebrew : SingleByteEncoding { - const string BODY_NAME = "x-mac-hebrew"; - const int CODEPAGE = 10005; - const string ENCODING_NAME = "Hebrew (Mac)"; - const string HEADER_NAME = "x-mac-hebrew"; - const string WEB_NAME = "x-mac-hebrew"; - const int WINDOWS_CODEPAGE = 10005; + public override string BodyName => "x-mac-hebrew"; + public override int CodePage => 10005; + public override string EncodingName => "Hebrew (Mac)"; + public override string HeaderName => "x-mac-hebrew"; + public override string WebName => "x-mac-hebrew"; + public override int WindowsCodePage => 10005; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Macintosh Hebrew to Unicode character map. // TODO: 0x81 => 0x05F2+0x05B7 // TODO: 0xC0 => 0xF86A+0x05DC+0x05B9 // TODO: 0xDE => 0x05B8+0xF87F - static readonly char[] _macHebrewTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -149,336 +147,10 @@ namespace Claunia.Encoding '\u05E8', '\u05E9', '\u05EA', '\u007D', '\u005D', '\u007B', '\u005B', '\u007C' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Mac Hebrew character to an Unicode character - /// Unicode character. - /// Mac Hebrew character. - static char GetChar(byte character) => _macHebrewTable[character]; - /// Converts a Unicode character to an Mac Hebrew character /// Mac Hebrew character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacRoman.cs b/Claunia.Encoding/MacRoman.cs index a7c76e5..0870c3b 100644 --- a/Claunia.Encoding/MacRoman.cs +++ b/Claunia.Encoding/MacRoman.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Apple Mac character encoding of Unicode characters. - public class MacRoman : Encoding + public class MacRoman : SingleByteEncoding { - const string BODY_NAME = "macintosh"; - const int CODEPAGE = 10000; - const string ENCODING_NAME = "Western European (Mac)"; - const string HEADER_NAME = "macintosh"; - const string WEB_NAME = "macintosh"; - const int WINDOWS_CODEPAGE = 10000; + public override string BodyName => "macintosh"; + public override int CodePage => 10000; + public override string EncodingName => "Western European (Mac)"; + public override string HeaderName => "macintosh"; + public override string WebName => "macintosh"; + public override int WindowsCodePage => 10000; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Mac to Unicode character map. MacRoman is a superset of LisaRoman. - static readonly char[] _macRomanTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u00AF', '\u02D8', '\u02D9', '\u02DA', '\u00B8', '\u02DD', '\u02DB', '\u02C7' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a MacRoman character to an Unicode character - /// Unicode character. - /// MacRoman character. - static char GetChar(byte character) => _macRomanTable[character]; - /// Converts a Unicode character to an MacRoman character /// MacRoman character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacRomanian.cs b/Claunia.Encoding/MacRomanian.cs index c2f8706..af4bf96 100644 --- a/Claunia.Encoding/MacRomanian.cs +++ b/Claunia.Encoding/MacRomanian.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Apple Mac character encoding of Unicode characters. - public class MacRomanian : Encoding + public class MacRomanian : SingleByteEncoding { - const string BODY_NAME = "x-mac-romanian"; - const int CODEPAGE = 10010; - const string ENCODING_NAME = "Romanianian (Mac)"; - const string HEADER_NAME = "x-mac-romanian"; - const string WEB_NAME = "x-mac-romanian"; - const int WINDOWS_CODEPAGE = 10010; + public override string BodyName => "x-mac-romanian"; + public override int CodePage => 10010; + public override string EncodingName => "Romanianian (Mac)"; + public override string HeaderName => "x-mac-romanian"; + public override string WebName => "x-mac-romanian"; + public override int WindowsCodePage => 10010; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Mac to Unicode character map. MacRomanian is a superset of MacRomanian. - static readonly char[] _macRomanianTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u00AF', '\u02D8', '\u02D9', '\u02DA', '\u00B8', '\u02DD', '\u02DB', '\u02C7' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a MacRomanian character to an Unicode character - /// Unicode character. - /// MacRomanian character. - static char GetChar(byte character) => _macRomanianTable[character]; - /// Converts a Unicode character to an MacRomanian character /// MacRomanian character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacTurkish.cs b/Claunia.Encoding/MacTurkish.cs index 0669bdd..a4f1966 100644 --- a/Claunia.Encoding/MacTurkish.cs +++ b/Claunia.Encoding/MacTurkish.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Mac Turkish character encoding of Unicode characters. - public class MacTurkish : Encoding + public class MacTurkish : SingleByteEncoding { - const string BODY_NAME = "x-mac-turkish"; - const int CODEPAGE = 10081; - const string ENCODING_NAME = "Turkish (Mac)"; - const string HEADER_NAME = "x-mac-turkish"; - const string WEB_NAME = "x-mac-turkish"; - const int WINDOWS_CODEPAGE = 10081; + public override string BodyName => "x-mac-turkish"; + public override int CodePage => 10081; + public override string EncodingName => "Turkish (Mac)"; + public override string HeaderName => "x-mac-turkish"; + public override string WebName => "x-mac-turkish"; + public override int WindowsCodePage => 10081; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Macintosh Turkish to Unicode character map. - static readonly char[] _macTurkishTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u00AF', '\u02D8', '\u02D9', '\u02DA', '\u00B8', '\u02DD', '\u02DB', '\u02C7' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Mac Turkish character to an Unicode character - /// Unicode character. - /// Mac Turkish character. - static char GetChar(byte character) => _macTurkishTable[character]; - /// Converts a Unicode character to an Mac Turkish character /// Mac Turkish character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/MacUkrainian.cs b/Claunia.Encoding/MacUkrainian.cs index b442a5d..f0f3d6f 100644 --- a/Claunia.Encoding/MacUkrainian.cs +++ b/Claunia.Encoding/MacUkrainian.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an Mac Ukrainian character encoding of Unicode characters. - public class MacUkrainian : Encoding + public class MacUkrainian : SingleByteEncoding { - const string BODY_NAME = "x-mac-ukrainian"; - const int CODEPAGE = 10017; - const string ENCODING_NAME = "Ukrainian (Mac)"; - const string HEADER_NAME = "x-mac-ukrainian"; - const string WEB_NAME = "x-mac-ukrainian"; - const int WINDOWS_CODEPAGE = 10017; + public override string BodyName => "x-mac-ukrainian"; + public override int CodePage => 10017; + public override string EncodingName => "Ukrainian (Mac)"; + public override string HeaderName => "x-mac-ukrainian"; + public override string WebName => "x-mac-ukrainian"; + public override int WindowsCodePage => 10017; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = true; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => true; + public override bool IsSingleByte => true; /// The Macintosh Ukrainian to Unicode character map. - static readonly char[] _macUkrainianTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\u0448', '\u0449', '\u044A', '\u044B', '\u044C', '\u044D', '\u044E', '\u20AC' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a Mac Ukrainian character to an Unicode character - /// Unicode character. - /// Mac Ukrainian character. - static char GetChar(byte character) => _macUkrainianTable[character]; - /// Converts a Unicode character to an Mac Ukrainian character /// Mac Ukrainian character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/PETSCII.cs b/Claunia.Encoding/PETSCII.cs index ee3087c..9343c4f 100644 --- a/Claunia.Encoding/PETSCII.cs +++ b/Claunia.Encoding/PETSCII.cs @@ -24,33 +24,31 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// /// Represents an Commodore PET Standard Code for Information Interchange (aka CBM ASCII) character encoding of /// Unicode characters. /// - public class Petscii : Encoding + public class Petscii : SingleByteEncoding { - const string BODY_NAME = "petscii"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Commodore PET Standard Code for Information Interchange"; - const string HEADER_NAME = "petscii"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "petscii"; + public override int CodePage => 0; + public override string EncodingName => "Commodore PET Standard Code for Information Interchange"; + public override string HeaderName => "petscii"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => true; /// The PETSCII to Unicode character map, unshifted (default) variant. /// Reference used: http://style64.org/petscii/ and others. - static readonly char[] _petsciiTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0000', '\u0000', '\u0003', '\u0000', '\u0000', '\u0000', '\u0000', @@ -149,335 +147,10 @@ namespace Claunia.Encoding '\u0000', '\u2583', '\u0000', '\u2596', '\u259D', '\u2518', '\u2598', '\u259A' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a PETSCII character to an Unicode character - /// Unicode character. - /// PETSCII character. - static char GetChar(byte character) => _petsciiTable[character]; - /// Converts a Unicode character to an PETSCII character /// PETSCII character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { if(character == 0x0000) return 0x3F; @@ -485,7 +158,7 @@ namespace Claunia.Encoding for(int i = 0; i < 256; i++) // TODO: convert this to a gigantic switch statement too? - if(_petsciiTable[i] == character) + if(CharTable[i] == character) return (byte)i; // Fallback to '?' diff --git a/Claunia.Encoding/Radix50.cs b/Claunia.Encoding/Radix50.cs index 6b67712..9a9f064 100644 --- a/Claunia.Encoding/Radix50.cs +++ b/Claunia.Encoding/Radix50.cs @@ -29,24 +29,24 @@ using System; namespace Claunia.Encoding { /// Represents an Radix-50 (PDP-11) character encoding of Unicode characters. - public class Radix50 : Encoding + public class Radix50 : SingleByteEncoding { - const string BODY_NAME = "radix50"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Western European (Radix-50)"; - const string HEADER_NAME = "radix50"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "radix50"; + public override int CodePage => 0; + public override string EncodingName => "Western European (Radix-50)"; + public override string HeaderName => "radix50"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = false; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => false; /// The Radix-50 to Unicode character map, when bits are shifted right - static readonly char[] _radix50Table = + protected override char[] CharTable => new[] { // 0x00 '\u0020', '\u0041', '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', @@ -73,46 +73,6 @@ namespace Claunia.Encoding '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - /// Calculates the number of bytes produced by encoding the characters in the specified . /// The number of bytes produced by encoding the specified characters. /// The containing the set of characters to encode. @@ -156,71 +116,6 @@ namespace Claunia.Encoding return chars.Length * 6 % 8 > 0 ? (chars.Length * 6 / 8) + 1 : chars.Length * 6 / 8; } - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - /// Encodes a set of characters from the specified character array into a sequence of bytes. /// A byte array containing the results of encoding the specified set of characters. /// The character array containing the set of characters to encode. @@ -286,16 +181,6 @@ namespace Claunia.Encoding return bytes; } - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. /// The number of characters produced by decoding the specified sequence of bytes. /// The byte array containing the sequence of bytes to decode. @@ -318,55 +203,6 @@ namespace Claunia.Encoding return count * 8 / 6; } - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - /// Decodes a sequence of bytes from the specified byte array into a set of characters. /// The chars. /// The byte array containing the sequence of bytes to decode. @@ -445,31 +281,15 @@ namespace Claunia.Encoding return byteCount * 8 / 6; } - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - /// Converts a Apple II character to an Unicode character /// Unicode character. /// Apple II character. - static char GetChar(byte character) => _radix50Table[character & 0x3F]; + char GetChar(byte character) => CharTable[character & 0x3F]; /// Converts a Unicode character to an Apple II character /// Apple II character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/SingleByteEncoding.cs b/Claunia.Encoding/SingleByteEncoding.cs new file mode 100644 index 0000000..de5b5a9 --- /dev/null +++ b/Claunia.Encoding/SingleByteEncoding.cs @@ -0,0 +1,337 @@ +using System; + +namespace Claunia.Encoding +{ + public abstract class SingleByteEncoding : Encoding + { + protected abstract char[] CharTable { get; } + + /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. + public abstract override bool IsBrowserDisplay { get; } + + /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. + public abstract override bool IsBrowserSave { get; } + + /// + /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying + /// content. + /// + public abstract override bool IsMailNewsDisplay { get; } + + /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. + public abstract override bool IsMailNewsSave { get; } + + /// Gets a value indicating whether the current encoding is read-only. + /// The is single byte. + public abstract override bool IsReadOnly { get; } + + /// Gets a value indicating whether the current encoding uses single-byte code points. + public abstract override bool IsSingleByte { get; } + + /// Gets the code page identifier of the current Encoding. + public abstract override int CodePage { get; } + + /// Gets a name for the current encoding that can be used with mail agent body tags + public abstract override string BodyName { get; } + + /// Gets a name for the current encoding that can be used with mail agent header tags + public abstract override string HeaderName { get; } + + /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. + public abstract override string WebName { get; } + + /// Gets the human-readable description of the current encoding. + public abstract override string EncodingName { get; } + + /// Gets the Windows operating system code page that most closely corresponds to the current encoding. + public abstract override int WindowsCodePage { get; } + + /// Calculates the number of bytes produced by encoding the characters in the specified . + /// The number of bytes produced by encoding the specified characters. + /// The containing the set of characters to encode. + public override int GetByteCount(string s) + { + if(s == null) + throw new ArgumentNullException(nameof(s)); + + return s.Length; + } + + /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. + /// The number of bytes produced by encoding the specified characters. + /// The character array containing the set of characters to encode. + /// The index of the first character to encode. + /// The number of characters to encode. + public override int GetByteCount(char[] chars, int index, int count) + { + if(chars == null) + throw new ArgumentNullException(nameof(chars)); + + if(index < 0 || + index >= chars.Length) + throw new ArgumentOutOfRangeException(nameof(index)); + + if(count < 0 || + index + count > chars.Length) + throw new ArgumentOutOfRangeException(nameof(index)); + + return count; + } + + /// Calculates the number of bytes produced by encoding all the characters in the specified character array. + /// The number of bytes produced by encoding all the characters in the specified character array. + /// The character array containing the characters to encode. + public override int GetByteCount(char[] chars) + { + if(chars == null) + throw new ArgumentNullException(nameof(chars)); + + return chars.Length; + } + + /// Encodes a set of characters from the specified into the specified byte array. + /// The actual number of bytes written into bytes. + /// The containing the set of characters to encode. + /// The index of the first character to encode. + /// The number of characters to encode. + /// The byte array to contain the resulting sequence of bytes. + /// The index at which to start writing the resulting sequence of bytes. + public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => + GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); + + /// Encodes all the characters in the specified string into a sequence of bytes. + /// A byte array containing the results of encoding the specified set of characters. + /// The string containing the characters to encode. + public override byte[] GetBytes(string s) + { + if(s == null) + throw new ArgumentNullException(nameof(s)); + + return GetBytes(s.ToCharArray(), 0, s.Length); + } + + /// Encodes a set of characters from the specified character array into the specified byte array. + /// The actual number of bytes written into bytes. + /// The character array containing the set of characters to encode. + /// The index of the first character to encode. + /// The number of characters to encode. + /// The byte array to contain the resulting sequence of bytes. + /// The index at which to start writing the resulting sequence of bytes. + public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) + { + if(chars == null) + throw new ArgumentNullException(nameof(chars)); + + if(bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + if(charIndex < 0) + throw new ArgumentOutOfRangeException(nameof(charIndex)); + + if(charCount < 0) + throw new ArgumentOutOfRangeException(nameof(charCount)); + + if(byteIndex < 0) + throw new ArgumentOutOfRangeException(nameof(byteIndex)); + + if(charIndex >= chars.Length) + throw new ArgumentOutOfRangeException(nameof(charIndex)); + + if(charCount + charIndex > chars.Length) + throw new ArgumentOutOfRangeException(nameof(charCount)); + + if(byteIndex >= bytes.Length) + throw new ArgumentOutOfRangeException(nameof(byteIndex)); + + if(byteIndex + charCount > bytes.Length) + throw new ArgumentException(nameof(bytes)); + + byte[] temp = GetBytes(chars, charIndex, charCount); + + for(int i = 0; i < temp.Length; i++) + bytes[i + byteIndex] = temp[i]; + + return charCount; + } + + /// Encodes a set of characters from the specified character array into a sequence of bytes. + /// A byte array containing the results of encoding the specified set of characters. + /// The character array containing the set of characters to encode. + /// The index of the first character to encode. + /// The number of characters to encode. + public override byte[] GetBytes(char[] chars, int index, int count) + { + if(chars == null) + throw new ArgumentNullException(nameof(chars)); + + if(index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); + + if(count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + if(count + index > chars.Length) + throw new ArgumentOutOfRangeException(nameof(count)); + + byte[] bytes = new byte[count]; + + for(int i = 0; i < count; i++) + bytes[i] = GetByte(chars[index + i]); + + return bytes; + } + + /// Encodes all the characters in the specified character array into a sequence of bytes. + /// A byte array containing the results of encoding the specified set of characters. + /// The character array containing the characters to encode. + public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); + + /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. + /// The number of characters produced by decoding the specified sequence of bytes. + /// The byte array containing the sequence of bytes to decode. + public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); + + /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. + /// The number of characters produced by decoding the specified sequence of bytes. + /// The byte array containing the sequence of bytes to decode. + /// The index of the first byte to decode. + /// The number of bytes to decode. + public override int GetCharCount(byte[] bytes, int index, int count) + { + if(bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + if(index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); + + if(count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + if(count + index > bytes.Length) + throw new ArgumentOutOfRangeException(nameof(count)); + + return count; + } + + /// Decodes a sequence of bytes from the specified byte array into the specified character array. + /// The actual number of characters written into chars. + /// The byte array containing the sequence of bytes to decode. + /// The index of the first byte to decode. + /// The number of bytes to decode. + /// The character array to contain the resulting set of characters. + /// The index at which to start writing the resulting set of characters. + public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) + { + if(bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + if(chars == null) + throw new ArgumentNullException(nameof(chars)); + + if(byteIndex < 0) + throw new ArgumentOutOfRangeException(nameof(byteIndex)); + + if(byteCount < 0) + throw new ArgumentOutOfRangeException(nameof(byteCount)); + + if(charIndex < 0) + throw new ArgumentOutOfRangeException(nameof(charIndex)); + + if(byteIndex >= bytes.Length) + throw new ArgumentOutOfRangeException(nameof(byteIndex)); + + if(byteCount + byteIndex > bytes.Length) + throw new ArgumentOutOfRangeException(nameof(byteCount)); + + if(charIndex >= chars.Length) + throw new ArgumentOutOfRangeException(nameof(charIndex)); + + if(charIndex + byteCount > chars.Length) + throw new ArgumentException(nameof(chars)); + + char[] temp = GetChars(bytes, byteIndex, byteCount); + + for(int i = 0; i < temp.Length; i++) + chars[i + charIndex] = temp[i]; + + return byteCount; + } + + /// Decodes all the bytes in the specified byte array into a set of characters. + /// A character array containing the results of decoding the specified sequence of bytes. + /// The byte array containing the sequence of bytes to decode. + public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); + + /// Decodes a sequence of bytes from the specified byte array into a set of characters. + /// The chars. + /// The byte array containing the sequence of bytes to decode. + /// The index of the first byte to decode. + /// The number of bytes to decode. + public override char[] GetChars(byte[] bytes, int index, int count) + { + if(bytes == null) + throw new ArgumentNullException(nameof(bytes)); + + if(index < 0) + throw new ArgumentOutOfRangeException(nameof(index)); + + if(count < 0) + throw new ArgumentOutOfRangeException(nameof(count)); + + if(count + index > bytes.Length) + throw new ArgumentOutOfRangeException(nameof(count)); + + char[] chars = new char[count]; + + for(int i = 0; i < count; i++) + chars[i] = GetChar(bytes[index + i]); + + return chars; + } + + /// Calculates the maximum number of bytes produced by encoding the specified number of characters. + /// The maximum number of bytes produced by encoding the specified number of characters. + /// The number of characters to encode. + public override int GetMaxByteCount(int charCount) + { + if(charCount < 0) + throw new ArgumentOutOfRangeException(nameof(charCount)); + + return charCount; + } + + /// Calculates the maximum number of characters produced by decoding the specified number of bytes. + /// The maximum number of characters produced by decoding the specified number of bytes. + /// The number of bytes to decode. + public override int GetMaxCharCount(int byteCount) + { + if(byteCount < 0) + throw new ArgumentOutOfRangeException(nameof(byteCount)); + + return byteCount; + } + + /// Returns a sequence of bytes that specifies the encoding used. + /// A byte array of length zero, as a preamble is not required. + public override byte[] GetPreamble() => new byte[0]; + + /// Decodes all the bytes in the specified byte array into a string. + /// A string that contains the results of decoding the specified sequence of bytes. + /// The byte array containing the sequence of bytes to decode. + public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); + + /// Decodes a sequence of bytes from the specified byte array into a string. + /// A string that contains the results of decoding the specified sequence of bytes. + /// The byte array containing the sequence of bytes to decode. + /// The index of the first byte to decode. + /// The number of bytes to decode. + public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); + + /// Converts a codepage character to an Unicode character + /// Unicode character. + /// Codepage character. + char GetChar(byte character) => CharTable[character]; + + private protected abstract byte GetByte(char character); + } +} \ No newline at end of file diff --git a/Claunia.Encoding/ZX80.cs b/Claunia.Encoding/ZX80.cs index 1b6a8f9..c6cd4e1 100644 --- a/Claunia.Encoding/ZX80.cs +++ b/Claunia.Encoding/ZX80.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents a ZX80 character encoding of Unicode characters. - public class Zx80 : Encoding + public class Zx80 : SingleByteEncoding { - const string BODY_NAME = "zx80"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Sinclair ZX80 character set"; - const string HEADER_NAME = "zx80"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "zx80"; + public override int CodePage => 0; + public override string EncodingName => "Sinclair ZX80 character set"; + public override string HeaderName => "zx80"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => true; /// The ZX80 to Unicode character map. - static readonly char[] _zx80Table = + protected override char[] CharTable => new[] { // 0x00 '\u0020', '\u0022', '\u258C', '\u2584', '\u2598', '\u259D', '\u2596', '\u2597', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\u0000' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a ZX80 character to an Unicode character - /// Unicode character. - /// ZX80 character. - static char GetChar(byte character) => _zx80Table[character]; - /// Converts a Unicode character to an ZX80 character /// ZX80 character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/ZX81.cs b/Claunia.Encoding/ZX81.cs index 5bb4978..ce89f6a 100644 --- a/Claunia.Encoding/ZX81.cs +++ b/Claunia.Encoding/ZX81.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents a ZX81 character encoding of Unicode characters. - public class Zx81 : Encoding + public class Zx81 : SingleByteEncoding { - const string BODY_NAME = "zx81"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Sinclair ZX81 character set"; - const string HEADER_NAME = "zx81"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "zx81"; + public override int CodePage => 0; + public override string EncodingName => "Sinclair ZX81 character set"; + public override string HeaderName => "zx81"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => true; /// The ZX81 to Unicode character map. - static readonly char[] _zx81Table = + protected override char[] CharTable => new[] { // 0x00 '\u0020', '\u2598', '\u259D', '\u2580', '\u2596', '\u258C', '\u259E', '\u259B', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a ZX81 character to an Unicode character - /// Unicode character. - /// ZX81 character. - static char GetChar(byte character) => _zx81Table[character]; - /// Converts a Unicode character to an ZX81 character /// ZX81 character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) { diff --git a/Claunia.Encoding/ZXSpectrum.cs b/Claunia.Encoding/ZXSpectrum.cs index f36a967..605f104 100644 --- a/Claunia.Encoding/ZXSpectrum.cs +++ b/Claunia.Encoding/ZXSpectrum.cs @@ -24,29 +24,27 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. -using System; - namespace Claunia.Encoding { /// Represents an ZX Spectrum character encoding of Unicode characters. - public class ZxSpectrum : Encoding + public class ZxSpectrum : SingleByteEncoding { - const string BODY_NAME = "spectrum"; - const int CODEPAGE = 0; - const string ENCODING_NAME = "Sinclair ZX Spectrum character set"; - const string HEADER_NAME = "spectrum"; - const string WEB_NAME = ""; - const int WINDOWS_CODEPAGE = 0; + public override string BodyName => "spectrum"; + public override int CodePage => 0; + public override string EncodingName => "Sinclair ZX Spectrum character set"; + public override string HeaderName => "spectrum"; + public override string WebName => ""; + public override int WindowsCodePage => 0; - const bool BROWSER_DISPLAY = false; - const bool BROWSER_SAVE = false; - const bool MAIL_NEWS_DISPLAY = false; - const bool MAIL_NEWS_SAVE = false; - const bool READ_ONLY = false; - const bool SINGLE_BYTE = true; + public override bool IsBrowserDisplay => false; + public override bool IsBrowserSave => false; + public override bool IsMailNewsDisplay => false; + public override bool IsMailNewsSave => false; + public override bool IsReadOnly => false; + public override bool IsSingleByte => true; /// The ZX Spectrum to Unicode character map. - static readonly char[] _zxSpectrumTable = + protected override char[] CharTable => new[] { // 0x00 '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0000', '\u0009', '\u0000', @@ -145,336 +143,10 @@ namespace Claunia.Encoding '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF', '\uFFFF' }; - /// Gets a value indicating whether the current encoding can be used by browser clients for displaying content. - public override bool IsBrowserDisplay => BROWSER_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by browser clients for saving content. - public override bool IsBrowserSave => BROWSER_SAVE; - - /// - /// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying - /// content. - /// - public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY; - - /// Gets a value indicating whether the current encoding can be used by mail and news clients for saving content. - public override bool IsMailNewsSave => MAIL_NEWS_SAVE; - - /// Gets a value indicating whether the current encoding is read-only. - /// The is single byte. - public override bool IsReadOnly => READ_ONLY; - - /// Gets a value indicating whether the current encoding uses single-byte code points. - public override bool IsSingleByte => SINGLE_BYTE; - - /// Gets the code page identifier of the current Encoding. - public override int CodePage => CODEPAGE; - - /// Gets a name for the current encoding that can be used with mail agent body tags - public override string BodyName => BODY_NAME; - - /// Gets a name for the current encoding that can be used with mail agent header tags - public override string HeaderName => HEADER_NAME; - - /// Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. - public override string WebName => WEB_NAME; - - /// Gets the human-readable description of the current encoding. - public override string EncodingName => ENCODING_NAME; - - /// Gets the Windows operating system code page that most closely corresponds to the current encoding. - public override int WindowsCodePage => WINDOWS_CODEPAGE; - - /// Calculates the number of bytes produced by encoding the characters in the specified . - /// The number of bytes produced by encoding the specified characters. - /// The containing the set of characters to encode. - public override int GetByteCount(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return s.Length; - } - - /// Calculates the number of bytes produced by encoding a set of characters from the specified character array. - /// The number of bytes produced by encoding the specified characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override int GetByteCount(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0 || - index >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0 || - index + count > chars.Length) - throw new ArgumentOutOfRangeException(nameof(index)); - - return count; - } - - /// Calculates the number of bytes produced by encoding all the characters in the specified character array. - /// The number of bytes produced by encoding all the characters in the specified character array. - /// The character array containing the characters to encode. - public override int GetByteCount(char[] chars) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - return chars.Length; - } - - /// Encodes a set of characters from the specified into the specified byte array. - /// The actual number of bytes written into bytes. - /// The containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) => - GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex); - - /// Encodes all the characters in the specified string into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The string containing the characters to encode. - public override byte[] GetBytes(string s) - { - if(s == null) - throw new ArgumentNullException(nameof(s)); - - return GetBytes(s.ToCharArray(), 0, s.Length); - } - - /// Encodes a set of characters from the specified character array into the specified byte array. - /// The actual number of bytes written into bytes. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - /// The byte array to contain the resulting sequence of bytes. - /// The index at which to start writing the resulting sequence of bytes. - public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charCount + charIndex > chars.Length) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteIndex + charCount > bytes.Length) - throw new ArgumentException(nameof(bytes)); - - byte[] temp = GetBytes(chars, charIndex, charCount); - - for(int i = 0; i < temp.Length; i++) - bytes[i + byteIndex] = temp[i]; - - return charCount; - } - - /// Encodes a set of characters from the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the set of characters to encode. - /// The index of the first character to encode. - /// The number of characters to encode. - public override byte[] GetBytes(char[] chars, int index, int count) - { - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > chars.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - byte[] bytes = new byte[count]; - - for(int i = 0; i < count; i++) - bytes[i] = GetByte(chars[index + i]); - - return bytes; - } - - /// Encodes all the characters in the specified character array into a sequence of bytes. - /// A byte array containing the results of encoding the specified set of characters. - /// The character array containing the characters to encode. - public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length); - - /// Calculates the number of characters produced by decoding all the bytes in the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length); - - /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. - /// The number of characters produced by decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override int GetCharCount(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - return count; - } - - /// Decodes a sequence of bytes from the specified byte array into the specified character array. - /// The actual number of characters written into chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - /// The character array to contain the resulting set of characters. - /// The index at which to start writing the resulting set of characters. - public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(chars == null) - throw new ArgumentNullException(nameof(chars)); - - if(byteIndex < 0) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex < 0) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(byteIndex >= bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteIndex)); - - if(byteCount + byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - if(charIndex >= chars.Length) - throw new ArgumentOutOfRangeException(nameof(charIndex)); - - if(charIndex + byteCount > chars.Length) - throw new ArgumentException(nameof(chars)); - - char[] temp = GetChars(bytes, byteIndex, byteCount); - - for(int i = 0; i < temp.Length; i++) - chars[i + charIndex] = temp[i]; - - return byteCount; - } - - /// Decodes all the bytes in the specified byte array into a set of characters. - /// A character array containing the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a set of characters. - /// The chars. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override char[] GetChars(byte[] bytes, int index, int count) - { - if(bytes == null) - throw new ArgumentNullException(nameof(bytes)); - - if(index < 0) - throw new ArgumentOutOfRangeException(nameof(index)); - - if(count < 0) - throw new ArgumentOutOfRangeException(nameof(count)); - - if(count + index > bytes.Length) - throw new ArgumentOutOfRangeException(nameof(count)); - - char[] chars = new char[count]; - - for(int i = 0; i < count; i++) - chars[i] = GetChar(bytes[index + i]); - - return chars; - } - - /// Calculates the maximum number of bytes produced by encoding the specified number of characters. - /// The maximum number of bytes produced by encoding the specified number of characters. - /// The number of characters to encode. - public override int GetMaxByteCount(int charCount) - { - if(charCount < 0) - throw new ArgumentOutOfRangeException(nameof(charCount)); - - return charCount; - } - - /// Calculates the maximum number of characters produced by decoding the specified number of bytes. - /// The maximum number of characters produced by decoding the specified number of bytes. - /// The number of bytes to decode. - public override int GetMaxCharCount(int byteCount) - { - if(byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount)); - - return byteCount; - } - - /// Returns a sequence of bytes that specifies the encoding used. - /// A byte array of length zero, as a preamble is not required. - public override byte[] GetPreamble() => new byte[0]; - - /// Decodes all the bytes in the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length); - - /// Decodes a sequence of bytes from the specified byte array into a string. - /// A string that contains the results of decoding the specified sequence of bytes. - /// The byte array containing the sequence of bytes to decode. - /// The index of the first byte to decode. - /// The number of bytes to decode. - public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count)); - - /// Converts a ZX Spectrum character to an Unicode character - /// Unicode character. - /// ZX Spectrum character. - static char GetChar(byte character) => _zxSpectrumTable[character]; - /// Converts a Unicode character to an ZX Spectrum character /// ZX Spectrum character. /// Unicode character. - static byte GetByte(char character) + private protected override byte GetByte(char character) { switch(character) {