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