2 * Latin1Encoding.cs - Implementation of the
3 * "System.Text.Latin1Encoding" class.
5 * Copyright (C) 2002 Southern Storm Software, Pty Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 internal class Latin1Encoding : Encoding
29 // Magic number used by Windows for the ISO Latin1 code page.
30 internal const int ISOLATIN_CODE_PAGE = 28591;
33 public Latin1Encoding()
34 : base(ISOLATIN_CODE_PAGE)
36 // Nothing to do here.
39 // Get the number of bytes needed to encode a character buffer.
40 public override int GetByteCount(char[] chars, int index, int count)
44 throw new ArgumentNullException("chars");
46 if(index < 0 || index > chars.Length)
48 throw new ArgumentOutOfRangeException
49 ("index", _("ArgRange_Array"));
51 if(count < 0 || count > (chars.Length - index))
53 throw new ArgumentOutOfRangeException
54 ("count", _("ArgRange_Array"));
59 // Convenience wrappers for "GetByteCount".
60 public override int GetByteCount(String s)
64 throw new ArgumentNullException("s");
69 // Get the bytes that result from encoding a character buffer.
70 public override int GetBytes(char[] chars, int charIndex, int charCount,
71 byte[] bytes, int byteIndex)
75 throw new ArgumentNullException("chars");
79 throw new ArgumentNullException("bytes");
81 if(charIndex < 0 || charIndex > chars.Length)
83 throw new ArgumentOutOfRangeException
84 ("charIndex", _("ArgRange_Array"));
86 if(charCount < 0 || charCount > (chars.Length - charIndex))
88 throw new ArgumentOutOfRangeException
89 ("charCount", _("ArgRange_Array"));
91 if(byteIndex < 0 || byteIndex > bytes.Length)
93 throw new ArgumentOutOfRangeException
94 ("byteIndex", _("ArgRange_Array"));
96 if((bytes.Length - byteIndex) < charCount)
98 throw new ArgumentException
99 (_("Arg_InsufficientSpace"));
101 int count = charCount;
105 ch = chars[charIndex++];
106 if(ch < (char)0x0100)
108 bytes[byteIndex++] = (byte)ch;
110 else if(ch >= '\uFF01' && ch <= '\uFF5E')
112 bytes[byteIndex++] = (byte)(ch - 0xFEE0);
116 bytes[byteIndex++] = (byte)'?';
122 // Convenience wrappers for "GetBytes".
123 public override int GetBytes(String s, int charIndex, int charCount,
124 byte[] bytes, int byteIndex)
128 throw new ArgumentNullException("s");
132 throw new ArgumentNullException("bytes");
134 if(charIndex < 0 || charIndex > s.Length)
136 throw new ArgumentOutOfRangeException
137 ("charIndex", _("ArgRange_StringIndex"));
139 if(charCount < 0 || charCount > (s.Length - charIndex))
141 throw new ArgumentOutOfRangeException
142 ("charCount", _("ArgRange_StringRange"));
144 if(byteIndex < 0 || byteIndex > bytes.Length)
146 throw new ArgumentOutOfRangeException
147 ("byteIndex", _("ArgRange_Array"));
149 if((bytes.Length - byteIndex) < charCount)
151 throw new ArgumentException(_("Arg_InsufficientSpace"));
153 int count = charCount;
158 if(ch < (char)0x0100)
160 bytes[byteIndex++] = (byte)ch;
162 else if(ch >= '\uFF01' && ch <= '\uFF5E')
164 bytes[byteIndex++] = (byte)(ch - 0xFEE0);
168 bytes[byteIndex++] = (byte)'?';
174 // Get the number of characters needed to decode a byte buffer.
175 public override int GetCharCount(byte[] bytes, int index, int count)
179 throw new ArgumentNullException("bytes");
181 if(index < 0 || index > bytes.Length)
183 throw new ArgumentOutOfRangeException
184 ("index", _("ArgRange_Array"));
186 if(count < 0 || count > (bytes.Length - index))
188 throw new ArgumentOutOfRangeException
189 ("count", _("ArgRange_Array"));
194 // Get the characters that result from decoding a byte buffer.
195 public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
196 char[] chars, int charIndex)
200 throw new ArgumentNullException("bytes");
204 throw new ArgumentNullException("chars");
206 if(byteIndex < 0 || byteIndex > bytes.Length)
208 throw new ArgumentOutOfRangeException
209 ("byteIndex", _("ArgRange_Array"));
211 if(byteCount < 0 || byteCount > (bytes.Length - byteIndex))
213 throw new ArgumentOutOfRangeException
214 ("byteCount", _("ArgRange_Array"));
216 if(charIndex < 0 || charIndex > chars.Length)
218 throw new ArgumentOutOfRangeException
219 ("charIndex", _("ArgRange_Array"));
221 if((chars.Length - charIndex) < byteCount)
223 throw new ArgumentException(_("Arg_InsufficientSpace"));
225 int count = byteCount;
228 chars[charIndex++] = (char)(bytes[byteIndex++]);
233 // Get the maximum number of bytes needed to encode a
234 // specified number of characters.
235 public override int GetMaxByteCount(int charCount)
239 throw new ArgumentOutOfRangeException
240 ("charCount", _("ArgRange_NonNegative"));
245 // Get the maximum number of characters needed to decode a
246 // specified number of bytes.
247 public override int GetMaxCharCount(int byteCount)
251 throw new ArgumentOutOfRangeException
252 ("byteCount", _("ArgRange_NonNegative"));
257 // Decode a buffer of bytes into a string.
258 public override String GetString(byte[] bytes, int index, int count)
262 throw new ArgumentNullException("bytes");
264 if(index < 0 || index > bytes.Length)
266 throw new ArgumentOutOfRangeException
267 ("index", _("ArgRange_Array"));
269 if(count < 0 || count > (bytes.Length - index))
271 throw new ArgumentOutOfRangeException
272 ("count", _("ArgRange_Array"));
274 String s = String.NewString(count);
278 s.SetChar(posn++, (char)(bytes[index++]));
282 public override String GetString(byte[] bytes)
286 throw new ArgumentNullException("bytes");
288 int count = bytes.Length;
290 String s = String.NewString(count);
293 s.SetChar(posn, (char)(bytes[posn]));
301 // Get the mail body name for this encoding.
302 public override String BodyName
310 // Get the human-readable name for this encoding.
311 public override String EncodingName
315 return "Western European (ISO)";
319 // Get the mail agent header name for this encoding.
320 public override String HeaderName
328 // Determine if this encoding can be displayed in a Web browser.
329 public override bool IsBrowserDisplay
337 // Determine if this encoding can be saved from a Web browser.
338 public override bool IsBrowserSave
346 // Determine if this encoding can be displayed in a mail/news agent.
347 public override bool IsMailNewsDisplay
355 // Determine if this encoding can be saved from a mail/news agent.
356 public override bool IsMailNewsSave
364 // Get the IANA-preferred Web name for this encoding.
365 public override String WebName
373 #endif // !ECMA_COMPAT
375 }; // class Latin1Encoding
377 }; // namespace System.Text