Use abstract class to implement encoding methods.

This commit is contained in:
2021-04-29 18:42:12 +01:00
parent 852dd674a3
commit 01df1f48e1
28 changed files with 728 additions and 8437 deletions

View File

@@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Claunia/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=NONINFRINGEMENT/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@@ -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
{
/// <summary>Represents an ATARI Standard Code for Information Interchange character encoding of Unicode characters.</summary>
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;
/// <summary>The ATASCII to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a ATASCII character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">ATASCII character.</param>
static char GetChar(byte character) => _atasciiTable[character];
/// <summary>Converts a Unicode character to an ATASCII character</summary>
/// <returns>ATASCII character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Apple II character encoding of Unicode characters.</summary>
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;
/// <summary>The Apple II to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Apple II character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Apple II character.</param>
static char GetChar(byte character) => _apple2Table[character];
/// <summary>Converts a Unicode character to an Apple II character</summary>
/// <returns>Apple II character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Apple IIc character encoding of Unicode characters.</summary>
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;
/// <summary>The Apple IIc to Unicode character map. Inverted screen characters are mapped to normal characters.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Apple IIc character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Apple IIc character.</param>
static char GetChar(byte character) => _apple2CTable[character];
/// <summary>Converts a Unicode character to an Apple IIc character</summary>
/// <returns>Apple IIc character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Apple IIe character encoding of Unicode characters.</summary>
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;
/// <summary>The Apple IIe to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Apple IIe character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Apple IIe character.</param>
static char GetChar(byte character) => _apple2ETable[character];
/// <summary>Converts a Unicode character to an Apple IIe character</summary>
/// <returns>Apple IIe character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Apple IIgs character encoding of Unicode characters.</summary>
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;
/// <summary>The Apple IIgs to Unicode character map. Inverted screen characters are mapped to normal characters.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Apple IIgs character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Apple IIgs character.</param>
static char GetChar(byte character) => _apple2GsTable[character];
/// <summary>Converts a Unicode character to an Apple IIgs character</summary>
/// <returns>Apple IIgs character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Atari ST character encoding of Unicode characters.</summary>
// 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;
/// <summary>The Atari ST to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Atari ST character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Atari ST character.</param>
static char GetChar(byte character) => _atariStTable[character];
/// <summary>Converts a Unicode character to an Atari ST character</summary>
/// <returns>Atari ST character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -92,11 +92,12 @@ namespace Claunia.Encoding
/// <summary>Returns an array that contains all encodings.</summary>
/// <returns>An array that contains all encodings.</returns>
public new static IEnumerable<EncodingInfo> 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);
/// <summary>Returns the encoding associated with the specified code page name.</summary>
/// <returns>The encoding associated with the specified code page.</returns>
@@ -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;
}

View File

@@ -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
{
/// <summary>Represents a GEM character encoding of Unicode characters.</summary>
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;
/// <summary>The GEM to Unicode character map. Pending: 0x09 => 0x0001F552, 0x0A => 0x0001F514</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a GEM character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">GEM character.</param>
static char GetChar(byte character) => _gemTable[character];
/// <summary>Converts a Unicode character to an GEM character</summary>
/// <returns>GEM character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an GEOS character encoding of Unicode characters.</summary>
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;
/// <summary>
/// The GEOS to Unicode character map. In the GEOS character map application lots of positions appears as '\u002E'
/// ('.' period) in normal (non-symbol) fonts.
/// </summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a GEOS character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">GEOS character.</param>
static char GetChar(byte character) => _geosTable[character];
/// <summary>Converts a Unicode character to an GEOS character</summary>
/// <returns>GEOS character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Apple Lisa character encoding of Unicode characters.</summary>
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;
/// <summary>The Lisa to Unicode character map. MacRoman is a superset of LisaRoman.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a LisaRoman character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">LisaRoman character.</param>
static char GetChar(byte character) => _lisaRomanTable[character];
/// <summary>Converts a Unicode character to an LisaRoman character</summary>
/// <returns>LisaRoman character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Mac Arabic character encoding of Unicode characters.</summary>
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;
/// <summary>The Macintosh Arabic to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Mac Arabic character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Mac Arabic character.</param>
static char GetChar(byte character) => _macArabicTable[character];
/// <summary>Converts a Unicode character to an Mac Arabic character</summary>
/// <returns>Mac Arabic character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Mac CentralEuropean character encoding of Unicode characters.</summary>
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;
/// <summary>The Macintosh CentralEuropean to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Mac CentralEuropean character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Mac CentralEuropean character.</param>
static char GetChar(byte character) => _macCentralEuropeanTable[character];
/// <summary>Converts a Unicode character to an Mac CentralEuropean character</summary>
/// <returns>Mac CentralEuropean character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Mac Croatian character encoding of Unicode characters.</summary>
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;
/// <summary>The Macintosh Croatian to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Mac Croatian character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Mac Croatian character.</param>
static char GetChar(byte character) => _macCroatianTable[character];
/// <summary>Converts a Unicode character to an Mac Croatian character</summary>
/// <returns>Mac Croatian character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Mac Cyrillic character encoding of Unicode characters.</summary>
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;
/// <summary>The Macintosh Cyrillic to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Mac Cyrillic character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Mac Cyrillic character.</param>
static char GetChar(byte character) => _macCyrillicTable[character];
/// <summary>Converts a Unicode character to an Mac Cyrillic character</summary>
/// <returns>Mac Cyrillic character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Mac Farsi character encoding of Unicode characters.</summary>
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;
/// <summary>The Macintosh Farsi to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Mac Farsi character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Mac Farsi character.</param>
static char GetChar(byte character) => _macFarsiTable[character];
/// <summary>Converts a Unicode character to an Mac Farsi character</summary>
/// <returns>Mac Farsi character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Mac Greek character encoding of Unicode characters.</summary>
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;
/// <summary>The Macintosh Greek to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Mac Greek character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Mac Greek character.</param>
static char GetChar(byte character) => _macGreekTable[character];
/// <summary>Converts a Unicode character to an Mac Greek character</summary>
/// <returns>Mac Greek character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Mac Hebrew character encoding of Unicode characters.</summary>
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;
/// <summary>The Macintosh Hebrew to Unicode character map.</summary>
// 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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Mac Hebrew character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Mac Hebrew character.</param>
static char GetChar(byte character) => _macHebrewTable[character];
/// <summary>Converts a Unicode character to an Mac Hebrew character</summary>
/// <returns>Mac Hebrew character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Apple Mac character encoding of Unicode characters.</summary>
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;
/// <summary>The Mac to Unicode character map. MacRoman is a superset of LisaRoman.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a MacRoman character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">MacRoman character.</param>
static char GetChar(byte character) => _macRomanTable[character];
/// <summary>Converts a Unicode character to an MacRoman character</summary>
/// <returns>MacRoman character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Apple Mac character encoding of Unicode characters.</summary>
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;
/// <summary>The Mac to Unicode character map. MacRomanian is a superset of MacRomanian.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a MacRomanian character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">MacRomanian character.</param>
static char GetChar(byte character) => _macRomanianTable[character];
/// <summary>Converts a Unicode character to an MacRomanian character</summary>
/// <returns>MacRomanian character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Mac Turkish character encoding of Unicode characters.</summary>
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;
/// <summary>The Macintosh Turkish to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Mac Turkish character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Mac Turkish character.</param>
static char GetChar(byte character) => _macTurkishTable[character];
/// <summary>Converts a Unicode character to an Mac Turkish character</summary>
/// <returns>Mac Turkish character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an Mac Ukrainian character encoding of Unicode characters.</summary>
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;
/// <summary>The Macintosh Ukrainian to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Mac Ukrainian character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Mac Ukrainian character.</param>
static char GetChar(byte character) => _macUkrainianTable[character];
/// <summary>Converts a Unicode character to an Mac Ukrainian character</summary>
/// <returns>Mac Ukrainian character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>
/// Represents an Commodore PET Standard Code for Information Interchange (aka CBM ASCII) character encoding of
/// Unicode characters.
/// </summary>
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;
/// <summary>The PETSCII to Unicode character map, unshifted (default) variant.</summary>
/// <remarks>Reference used: http://style64.org/petscii/ and others.</remarks>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a PETSCII character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">PETSCII character.</param>
static char GetChar(byte character) => _petsciiTable[character];
/// <summary>Converts a Unicode character to an PETSCII character</summary>
/// <returns>PETSCII character.</returns>
/// <param name="character">Unicode character.</param>
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 '?'

View File

@@ -29,24 +29,24 @@ using System;
namespace Claunia.Encoding
{
/// <summary>Represents an Radix-50 (PDP-11) character encoding of Unicode characters.</summary>
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;
/// <summary>The Radix-50 to Unicode character map, when bits are shifted right</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
@@ -156,71 +116,6 @@ namespace Claunia.Encoding
return chars.Length * 6 % 8 > 0 ? (chars.Length * 6 / 8) + 1 : chars.Length * 6 / 8;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
@@ -286,16 +181,6 @@ namespace Claunia.Encoding
return bytes;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
@@ -318,55 +203,6 @@ namespace Claunia.Encoding
return count * 8 / 6;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
@@ -445,31 +281,15 @@ namespace Claunia.Encoding
return byteCount * 8 / 6;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a Apple II character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Apple II character.</param>
static char GetChar(byte character) => _radix50Table[character & 0x3F];
char GetChar(byte character) => CharTable[character & 0x3F];
/// <summary>Converts a Unicode character to an Apple II character</summary>
/// <returns>Apple II character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -0,0 +1,337 @@
using System;
namespace Claunia.Encoding
{
public abstract class SingleByteEncoding : Encoding
{
protected abstract char[] CharTable { get; }
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public abstract override bool IsBrowserDisplay { get; }
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public abstract override bool IsBrowserSave { get; }
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public abstract override bool IsMailNewsDisplay { get; }
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public abstract override bool IsMailNewsSave { get; }
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public abstract override bool IsReadOnly { get; }
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public abstract override bool IsSingleByte { get; }
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public abstract override int CodePage { get; }
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public abstract override string BodyName { get; }
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public abstract override string HeaderName { get; }
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public abstract override string WebName { get; }
/// <summary>Gets the human-readable description of the current encoding.</summary>
public abstract override string EncodingName { get; }
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public abstract override int WindowsCodePage { get; }
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a codepage character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">Codepage character.</param>
char GetChar(byte character) => CharTable[character];
private protected abstract byte GetByte(char character);
}
}

View File

@@ -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
{
/// <summary>Represents a ZX80 character encoding of Unicode characters.</summary>
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;
/// <summary>The ZX80 to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a ZX80 character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">ZX80 character.</param>
static char GetChar(byte character) => _zx80Table[character];
/// <summary>Converts a Unicode character to an ZX80 character</summary>
/// <returns>ZX80 character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents a ZX81 character encoding of Unicode characters.</summary>
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;
/// <summary>The ZX81 to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a ZX81 character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">ZX81 character.</param>
static char GetChar(byte character) => _zx81Table[character];
/// <summary>Converts a Unicode character to an ZX81 character</summary>
/// <returns>ZX81 character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{

View File

@@ -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
{
/// <summary>Represents an ZX Spectrum character encoding of Unicode characters.</summary>
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;
/// <summary>The ZX Spectrum to Unicode character map.</summary>
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'
};
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for displaying content.</summary>
public override bool IsBrowserDisplay => BROWSER_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by browser clients for saving content.</summary>
public override bool IsBrowserSave => BROWSER_SAVE;
/// <summary>
/// Gets a value indicating whether the current encoding can be used by mail and news clients for displaying
/// content.
/// </summary>
public override bool IsMailNewsDisplay => MAIL_NEWS_DISPLAY;
/// <summary>Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.</summary>
public override bool IsMailNewsSave => MAIL_NEWS_SAVE;
/// <summary>Gets a value indicating whether the current encoding is read-only.</summary>
/// <value>The is single byte.</value>
public override bool IsReadOnly => READ_ONLY;
/// <summary>Gets a value indicating whether the current encoding uses single-byte code points.</summary>
public override bool IsSingleByte => SINGLE_BYTE;
/// <summary>Gets the code page identifier of the current Encoding.</summary>
public override int CodePage => CODEPAGE;
/// <summary>Gets a name for the current encoding that can be used with mail agent body tags</summary>
public override string BodyName => BODY_NAME;
/// <summary>Gets a name for the current encoding that can be used with mail agent header tags</summary>
public override string HeaderName => HEADER_NAME;
/// <summary>Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding.</summary>
public override string WebName => WEB_NAME;
/// <summary>Gets the human-readable description of the current encoding.</summary>
public override string EncodingName => ENCODING_NAME;
/// <summary>Gets the Windows operating system code page that most closely corresponds to the current encoding.</summary>
public override int WindowsCodePage => WINDOWS_CODEPAGE;
/// <summary>Calculates the number of bytes produced by encoding the characters in the specified <see cref="string" />.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
public override int GetByteCount(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return s.Length;
}
/// <summary>Calculates the number of bytes produced by encoding a set of characters from the specified character array.</summary>
/// <returns>The number of bytes produced by encoding the specified characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Calculates the number of bytes produced by encoding all the characters in the specified character array.</summary>
/// <returns>The number of bytes produced by encoding all the characters in the specified character array.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override int GetByteCount(char[] chars)
{
if(chars == null)
throw new ArgumentNullException(nameof(chars));
return chars.Length;
}
/// <summary>Encodes a set of characters from the specified <see cref="string" /> into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="s">The <see cref="string" /> containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex) =>
GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
/// <summary>Encodes all the characters in the specified string into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="s">The string containing the characters to encode.</param>
public override byte[] GetBytes(string s)
{
if(s == null)
throw new ArgumentNullException(nameof(s));
return GetBytes(s.ToCharArray(), 0, s.Length);
}
/// <summary>Encodes a set of characters from the specified character array into the specified byte array.</summary>
/// <returns>The actual number of bytes written into bytes.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="charIndex">The index of the first character to encode.</param>
/// <param name="charCount">The number of characters to encode.</param>
/// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
/// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
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;
}
/// <summary>Encodes a set of characters from the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the set of characters to encode.</param>
/// <param name="index">The index of the first character to encode.</param>
/// <param name="count">The number of characters to encode.</param>
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;
}
/// <summary>Encodes all the characters in the specified character array into a sequence of bytes.</summary>
/// <returns>A byte array containing the results of encoding the specified set of characters.</returns>
/// <param name="chars">The character array containing the characters to encode.</param>
public override byte[] GetBytes(char[] chars) => GetBytes(chars, 0, chars.Length);
/// <summary>Calculates the number of characters produced by decoding all the bytes in the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override int GetCharCount(byte[] bytes) => GetCharCount(bytes, 0, bytes.Length);
/// <summary>Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.</summary>
/// <returns>The number of characters produced by decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Decodes a sequence of bytes from the specified byte array into the specified character array.</summary>
/// <returns>The actual number of characters written into chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="byteIndex">The index of the first byte to decode.</param>
/// <param name="byteCount">The number of bytes to decode.</param>
/// <param name="chars">The character array to contain the resulting set of characters.</param>
/// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
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;
}
/// <summary>Decodes all the bytes in the specified byte array into a set of characters.</summary>
/// <returns>A character array containing the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override char[] GetChars(byte[] bytes) => GetChars(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a set of characters.</summary>
/// <returns>The chars.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
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;
}
/// <summary>Calculates the maximum number of bytes produced by encoding the specified number of characters.</summary>
/// <returns>The maximum number of bytes produced by encoding the specified number of characters.</returns>
/// <param name="charCount">The number of characters to encode.</param>
public override int GetMaxByteCount(int charCount)
{
if(charCount < 0)
throw new ArgumentOutOfRangeException(nameof(charCount));
return charCount;
}
/// <summary>Calculates the maximum number of characters produced by decoding the specified number of bytes.</summary>
/// <returns>The maximum number of characters produced by decoding the specified number of bytes.</returns>
/// <param name="byteCount">The number of bytes to decode.</param>
public override int GetMaxCharCount(int byteCount)
{
if(byteCount < 0)
throw new ArgumentOutOfRangeException(nameof(byteCount));
return byteCount;
}
/// <summary>Returns a sequence of bytes that specifies the encoding used.</summary>
/// <returns>A byte array of length zero, as a preamble is not required.</returns>
public override byte[] GetPreamble() => new byte[0];
/// <summary>Decodes all the bytes in the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
public override string GetString(byte[] bytes) => GetString(bytes, 0, bytes.Length);
/// <summary>Decodes a sequence of bytes from the specified byte array into a string.</summary>
/// <returns>A string that contains the results of decoding the specified sequence of bytes.</returns>
/// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
/// <param name="index">The index of the first byte to decode.</param>
/// <param name="count">The number of bytes to decode.</param>
public override string GetString(byte[] bytes, int index, int count) => new(GetChars(bytes, index, count));
/// <summary>Converts a ZX Spectrum character to an Unicode character</summary>
/// <returns>Unicode character.</returns>
/// <param name="character">ZX Spectrum character.</param>
static char GetChar(byte character) => _zxSpectrumTable[character];
/// <summary>Converts a Unicode character to an ZX Spectrum character</summary>
/// <returns>ZX Spectrum character.</returns>
/// <param name="character">Unicode character.</param>
static byte GetByte(char character)
private protected override byte GetByte(char character)
{
switch(character)
{