2 * Latin1Encoding.cs - Implementation of the
3 * "System.Text.Latin1Encoding" class.
5 * Copyright (c) 2002 Southern Storm Software, Pty Ltd
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
32 internal class Latin1Encoding : Encoding
34 // Magic number used by Windows for the ISO Latin1 code page.
35 internal const int ISOLATIN_CODE_PAGE = 28591;
38 public Latin1Encoding () : base (ISOLATIN_CODE_PAGE)
40 // Nothing to do here.
43 // Get the number of bytes needed to encode a character buffer.
44 public override int GetByteCount (char[] chars, int index, int count)
47 throw new ArgumentNullException ("chars");
49 if (index < 0 || index > chars.Length) {
50 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
52 if (count < 0 || count > (chars.Length - index)) {
53 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
58 // Convenience wrappers for "GetByteCount".
59 public override int GetByteCount (String s)
62 throw new ArgumentNullException ("s");
67 // Get the bytes that result from encoding a character buffer.
68 public override int GetBytes (char[] chars, int charIndex, int charCount,
69 byte[] bytes, int byteIndex)
72 throw new ArgumentNullException ("chars");
75 throw new ArgumentNullException ("bytes");
77 if (charIndex < 0 || charIndex > chars.Length) {
78 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
80 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
81 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
83 if (byteIndex < 0 || byteIndex > bytes.Length) {
84 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
86 if ((bytes.Length - byteIndex) < charCount) {
87 throw new ArgumentException (_("Arg_InsufficientSpace"));
89 int count = charCount;
92 ch = chars [charIndex++];
93 if (ch < (char)0x0100) {
94 bytes [byteIndex++] = (byte)ch;
95 } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
96 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
98 bytes [byteIndex++] = (byte)'?';
104 // Convenience wrappers for "GetBytes".
105 public override int GetBytes (String s, int charIndex, int charCount,
106 byte[] bytes, int byteIndex)
109 throw new ArgumentNullException ("s");
112 throw new ArgumentNullException ("bytes");
114 if (charIndex < 0 || charIndex > s.Length) {
115 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
117 if (charCount < 0 || charCount > (s.Length - charIndex)) {
118 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
120 if (byteIndex < 0 || byteIndex > bytes.Length) {
121 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
123 if ((bytes.Length - byteIndex) < charCount) {
124 throw new ArgumentException (_("Arg_InsufficientSpace"));
126 int count = charCount;
128 while (count-- > 0) {
129 ch = s [charIndex++];
130 if (ch < (char)0x0100) {
131 bytes [byteIndex++] = (byte)ch;
132 } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
133 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
135 bytes [byteIndex++] = (byte)'?';
141 // Get the number of characters needed to decode a byte buffer.
142 public override int GetCharCount (byte[] bytes, int index, int count)
145 throw new ArgumentNullException ("bytes");
147 if (index < 0 || index > bytes.Length) {
148 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
150 if (count < 0 || count > (bytes.Length - index)) {
151 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
156 // Get the characters that result from decoding a byte buffer.
157 public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
158 char[] chars, int charIndex)
161 throw new ArgumentNullException ("bytes");
164 throw new ArgumentNullException ("chars");
166 if (byteIndex < 0 || byteIndex > bytes.Length) {
167 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
169 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
170 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
172 if (charIndex < 0 || charIndex > chars.Length) {
173 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
175 if ((chars.Length - charIndex) < byteCount) {
176 throw new ArgumentException (_("Arg_InsufficientSpace"));
178 int count = byteCount;
179 while (count-- > 0) {
180 chars [charIndex++] = (char)(bytes [byteIndex++]);
185 // Get the maximum number of bytes needed to encode a
186 // specified number of characters.
187 public override int GetMaxByteCount (int charCount)
190 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
195 // Get the maximum number of characters needed to decode a
196 // specified number of bytes.
197 public override int GetMaxCharCount (int byteCount)
200 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
205 // Decode a buffer of bytes into a string.
206 public override String GetString (byte[] bytes, int index, int count)
209 throw new ArgumentNullException ("bytes");
211 if (index < 0 || index > bytes.Length) {
212 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
214 if (count < 0 || count > (bytes.Length - index)) {
215 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
220 fixed (byte *ss = &bytes [0]) {
221 return new String ((sbyte*)ss, index, count);
225 public override String GetString (byte[] bytes)
228 throw new ArgumentNullException ("bytes");
230 int count = bytes.Length;
234 fixed (byte *ss = &bytes [0]) {
235 return new String ((sbyte*)ss, 0, count);
242 // Get the mail body name for this encoding.
243 public override String BodyName
250 // Get the human-readable name for this encoding.
251 public override String EncodingName
254 return "Western European (ISO)";
258 // Get the mail agent header name for this encoding.
259 public override String HeaderName
266 // Determine if this encoding can be displayed in a Web browser.
267 public override bool IsBrowserDisplay
274 // Determine if this encoding can be saved from a Web browser.
275 public override bool IsBrowserSave
282 // Determine if this encoding can be displayed in a mail/news agent.
283 public override bool IsMailNewsDisplay
290 // Determine if this encoding can be saved from a mail/news agent.
291 public override bool IsMailNewsSave
298 // Get the IANA-preferred Web name for this encoding.
299 public override String WebName
306 #endif // !ECMA_COMPAT
308 }; // class Latin1Encoding
310 }; // namespace System.Text