Add missing file
[mono.git] / mcs / class / corlib / System.Text / Latin1Encoding.cs
1 /*
2  * Latin1Encoding.cs - Implementation of the
3  *                      "System.Text.Latin1Encoding" class.
4  *
5  * Copyright (C) 2002  Southern Storm Software, Pty Ltd.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 namespace System.Text
23 {
24
25 using System;
26
27 internal class Latin1Encoding : Encoding
28 {
29         // Magic number used by Windows for the ISO Latin1 code page.
30         internal const int ISOLATIN_CODE_PAGE = 28591;
31
32         // Constructor.
33         public Latin1Encoding()
34                         : base(ISOLATIN_CODE_PAGE)
35                         {
36                                 // Nothing to do here.
37                         }
38
39         // Get the number of bytes needed to encode a character buffer.
40         public override int GetByteCount(char[] chars, int index, int count)
41                         {
42                                 if(chars == null)
43                                 {
44                                         throw new ArgumentNullException("chars");
45                                 }
46                                 if(index < 0 || index > chars.Length)
47                                 {
48                                         throw new ArgumentOutOfRangeException
49                                                 ("index", _("ArgRange_Array"));
50                                 }
51                                 if(count < 0 || count > (chars.Length - index))
52                                 {
53                                         throw new ArgumentOutOfRangeException
54                                                 ("count", _("ArgRange_Array"));
55                                 }
56                                 return count;
57                         }
58
59         // Convenience wrappers for "GetByteCount".
60         public override int GetByteCount(String s)
61                         {
62                                 if(s == null)
63                                 {
64                                         throw new ArgumentNullException("s");
65                                 }
66                                 return s.Length;
67                         }
68
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)
72                         {
73                                 if(chars == null)
74                                 {
75                                         throw new ArgumentNullException("chars");
76                                 }
77                                 if(bytes == null)
78                                 {
79                                         throw new ArgumentNullException("bytes");
80                                 }
81                                 if(charIndex < 0 || charIndex > chars.Length)
82                                 {
83                                         throw new ArgumentOutOfRangeException
84                                                 ("charIndex", _("ArgRange_Array"));
85                                 }
86                                 if(charCount < 0 || charCount > (chars.Length - charIndex))
87                                 {
88                                         throw new ArgumentOutOfRangeException
89                                                 ("charCount", _("ArgRange_Array"));
90                                 }
91                                 if(byteIndex < 0 || byteIndex > bytes.Length)
92                                 {
93                                         throw new ArgumentOutOfRangeException
94                                                 ("byteIndex", _("ArgRange_Array"));
95                                 }
96                                 if((bytes.Length - byteIndex) < charCount)
97                                 {
98                                         throw new ArgumentException
99                                                 (_("Arg_InsufficientSpace"));
100                                 }
101                                 int count = charCount;
102                                 char ch;
103                                 while(count-- > 0)
104                                 {
105                                         ch = chars[charIndex++];
106                                         if(ch < (char)0x0100)
107                                         {
108                                                 bytes[byteIndex++] = (byte)ch;
109                                         }
110                                         else if(ch >= '\uFF01' && ch <= '\uFF5E')
111                                         {
112                                                 bytes[byteIndex++] = (byte)(ch - 0xFEE0);
113                                         }
114                                         else
115                                         {
116                                                 bytes[byteIndex++] = (byte)'?';
117                                         }
118                                 }
119                                 return charCount;
120                         }
121
122         // Convenience wrappers for "GetBytes".
123         public override int GetBytes(String s, int charIndex, int charCount,
124                                                                  byte[] bytes, int byteIndex)
125                         {
126                                 if(s == null)
127                                 {
128                                         throw new ArgumentNullException("s");
129                                 }
130                                 if(bytes == null)
131                                 {
132                                         throw new ArgumentNullException("bytes");
133                                 }
134                                 if(charIndex < 0 || charIndex > s.Length)
135                                 {
136                                         throw new ArgumentOutOfRangeException
137                                                 ("charIndex", _("ArgRange_StringIndex"));
138                                 }
139                                 if(charCount < 0 || charCount > (s.Length - charIndex))
140                                 {
141                                         throw new ArgumentOutOfRangeException
142                                                 ("charCount", _("ArgRange_StringRange"));
143                                 }
144                                 if(byteIndex < 0 || byteIndex > bytes.Length)
145                                 {
146                                         throw new ArgumentOutOfRangeException
147                                                 ("byteIndex", _("ArgRange_Array"));
148                                 }
149                                 if((bytes.Length - byteIndex) < charCount)
150                                 {
151                                         throw new ArgumentException(_("Arg_InsufficientSpace"));
152                                 }
153                                 int count = charCount;
154                                 char ch;
155                                 while(count-- > 0)
156                                 {
157                                         ch = s[charIndex++];
158                                         if(ch < (char)0x0100)
159                                         {
160                                                 bytes[byteIndex++] = (byte)ch;
161                                         }
162                                         else if(ch >= '\uFF01' && ch <= '\uFF5E')
163                                         {
164                                                 bytes[byteIndex++] = (byte)(ch - 0xFEE0);
165                                         }
166                                         else
167                                         {
168                                                 bytes[byteIndex++] = (byte)'?';
169                                         }
170                                 }
171                                 return charCount;
172                         }
173
174         // Get the number of characters needed to decode a byte buffer.
175         public override int GetCharCount(byte[] bytes, int index, int count)
176                         {
177                                 if(bytes == null)
178                                 {
179                                         throw new ArgumentNullException("bytes");
180                                 }
181                                 if(index < 0 || index > bytes.Length)
182                                 {
183                                         throw new ArgumentOutOfRangeException
184                                                 ("index", _("ArgRange_Array"));
185                                 }
186                                 if(count < 0 || count > (bytes.Length - index))
187                                 {
188                                         throw new ArgumentOutOfRangeException
189                                                 ("count", _("ArgRange_Array"));
190                                 }
191                                 return count;
192                         }
193
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)
197                         {
198                                 if(bytes == null)
199                                 {
200                                         throw new ArgumentNullException("bytes");
201                                 }
202                                 if(chars == null)
203                                 {
204                                         throw new ArgumentNullException("chars");
205                                 }
206                                 if(byteIndex < 0 || byteIndex > bytes.Length)
207                                 {
208                                         throw new ArgumentOutOfRangeException
209                                                 ("byteIndex", _("ArgRange_Array"));
210                                 }
211                                 if(byteCount < 0 || byteCount > (bytes.Length - byteIndex))
212                                 {
213                                         throw new ArgumentOutOfRangeException
214                                                 ("byteCount", _("ArgRange_Array"));
215                                 }
216                                 if(charIndex < 0 || charIndex > chars.Length)
217                                 {
218                                         throw new ArgumentOutOfRangeException
219                                                 ("charIndex", _("ArgRange_Array"));
220                                 }
221                                 if((chars.Length - charIndex) < byteCount)
222                                 {
223                                         throw new ArgumentException(_("Arg_InsufficientSpace"));
224                                 }
225                                 int count = byteCount;
226                                 while(count-- > 0)
227                                 {
228                                         chars[charIndex++] = (char)(bytes[byteIndex++]);
229                                 }
230                                 return byteCount;
231                         }
232
233         // Get the maximum number of bytes needed to encode a
234         // specified number of characters.
235         public override int GetMaxByteCount(int charCount)
236                         {
237                                 if(charCount < 0)
238                                 {
239                                         throw new ArgumentOutOfRangeException
240                                                 ("charCount", _("ArgRange_NonNegative"));
241                                 }
242                                 return charCount;
243                         }
244
245         // Get the maximum number of characters needed to decode a
246         // specified number of bytes.
247         public override int GetMaxCharCount(int byteCount)
248                         {
249                                 if(byteCount < 0)
250                                 {
251                                         throw new ArgumentOutOfRangeException
252                                                 ("byteCount", _("ArgRange_NonNegative"));
253                                 }
254                                 return byteCount;
255                         }
256
257         // Decode a buffer of bytes into a string.
258         public override String GetString(byte[] bytes, int index, int count)
259                         {
260                                 if(bytes == null)
261                                 {
262                                         throw new ArgumentNullException("bytes");
263                                 }
264                                 if(index < 0 || index > bytes.Length)
265                                 {
266                                         throw new ArgumentOutOfRangeException
267                                                 ("index", _("ArgRange_Array"));
268                                 }
269                                 if(count < 0 || count > (bytes.Length - index))
270                                 {
271                                         throw new ArgumentOutOfRangeException
272                                                 ("count", _("ArgRange_Array"));
273                                 }
274                                 String s = String.NewString(count);
275                                 int posn = 0;
276                                 while(count-- > 0)
277                                 {
278                                         s.SetChar(posn++, (char)(bytes[index++]));
279                                 }
280                                 return s;
281                         }
282         public override String GetString(byte[] bytes)
283                         {
284                                 if(bytes == null)
285                                 {
286                                         throw new ArgumentNullException("bytes");
287                                 }
288                                 int count = bytes.Length;
289                                 int posn = 0;
290                                 String s = String.NewString(count);
291                                 while(count-- > 0)
292                                 {
293                                         s.SetChar(posn, (char)(bytes[posn]));
294                                         ++posn;
295                                 }
296                                 return s;
297                         }
298
299 #if !ECMA_COMPAT
300
301         // Get the mail body name for this encoding.
302         public override String BodyName
303                         {
304                                 get
305                                 {
306                                         return "iso-8859-1";
307                                 }
308                         }
309
310         // Get the human-readable name for this encoding.
311         public override String EncodingName
312                         {
313                                 get
314                                 {
315                                         return "Western European (ISO)";
316                                 }
317                         }
318
319         // Get the mail agent header name for this encoding.
320         public override String HeaderName
321                         {
322                                 get
323                                 {
324                                         return "iso-8859-1";
325                                 }
326                         }
327
328         // Determine if this encoding can be displayed in a Web browser.
329         public override bool IsBrowserDisplay
330                         {
331                                 get
332                                 {
333                                         return true;
334                                 }
335                         }
336
337         // Determine if this encoding can be saved from a Web browser.
338         public override bool IsBrowserSave
339                         {
340                                 get
341                                 {
342                                         return true;
343                                 }
344                         }
345
346         // Determine if this encoding can be displayed in a mail/news agent.
347         public override bool IsMailNewsDisplay
348                         {
349                                 get
350                                 {
351                                         return true;
352                                 }
353                         }
354
355         // Determine if this encoding can be saved from a mail/news agent.
356         public override bool IsMailNewsSave
357                         {
358                                 get
359                                 {
360                                         return true;
361                                 }
362                         }
363
364         // Get the IANA-preferred Web name for this encoding.
365         public override String WebName
366                         {
367                                 get
368                                 {
369                                         return "iso-8859-1";
370                                 }
371                         }
372
373 #endif // !ECMA_COMPAT
374
375 }; // class Latin1Encoding
376
377 }; // namespace System.Text