merge -r 53370:58178
[mono.git] / mcs / class / corlib / System.Text / ASCIIEncoding.cs
1 /*
2  * ASCIIEncoding.cs - Implementation of the "System.Text.ASCIIEncoding" class.
3  *
4  * Copyright (c) 2001  Southern Storm Software, Pty Ltd
5  * Copyright (C) 2003 Novell, Inc.
6  * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 namespace System.Text
28 {
29
30 using System;
31
32 [Serializable]
33 [MonoTODO ("Fix serialization compatibility with MS.NET")]
34 public class ASCIIEncoding : Encoding
35 {
36         // Magic number used by Windows for "ASCII".
37         internal const int ASCII_CODE_PAGE = 20127;
38
39         // Constructor.
40         public ASCIIEncoding () : base(ASCII_CODE_PAGE) {
41                 body_name = header_name = web_name= "us-ascii";
42                 encoding_name = "US-ASCII";
43                 is_mail_news_display = true;
44                 is_mail_news_save = true;
45         }
46
47         // Get the number of bytes needed to encode a character buffer.
48         public override int GetByteCount (char[] chars, int index, int count)
49         {
50                 if (chars == null) {
51                         throw new ArgumentNullException ("chars");
52                 }
53                 if (index < 0 || index > chars.Length) {
54                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
55                 }
56                 if (count < 0 || count > (chars.Length - index)) {
57                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
58                 }
59                 return count;
60         }
61
62         // Convenience wrappers for "GetByteCount".
63         public override int GetByteCount (String s)
64         {
65                 if (s == null) {
66                         throw new ArgumentNullException ("s");
67                 }
68                 return s.Length;
69         }
70
71         // Get the bytes that result from encoding a character buffer.
72         public override int GetBytes (char[] chars, int charIndex, int charCount,
73                                                                  byte[] bytes, int byteIndex)
74         {
75 #if NET_2_0
76 // well, yes, I know this #if is ugly, but I think it is the simplest switch.
77                 EncoderFallbackBuffer buffer = null;
78                 char [] fallback_chars = null;
79                 return GetBytes (chars, charIndex, charCount, bytes,
80                         byteIndex, ref buffer, ref fallback_chars);
81         }
82
83         int GetBytes (char[] chars, int charIndex, int charCount,
84                       byte[] bytes, int byteIndex,
85                       ref EncoderFallbackBuffer buffer,
86                       ref char [] fallback_chars)
87         {
88 #endif
89                 if (chars == null) {
90                         throw new ArgumentNullException ("chars");
91                 }
92                 if (bytes == null) {
93                         throw new ArgumentNullException ("bytes");
94                 }
95                 if (charIndex < 0 || charIndex > chars.Length) {
96                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
97                 }
98                 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
99                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
100                 }
101                 if (byteIndex < 0 || byteIndex > bytes.Length) {
102                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
103                 }
104                 if ((bytes.Length - byteIndex) < charCount) {
105                         throw new ArgumentException (_("Arg_InsufficientSpace"));
106                 }
107                 int count = charCount;
108                 char ch;
109                 while (count-- > 0) {
110                         ch = chars [charIndex++];
111                         if (ch < (char)0x80) {
112                                 bytes [byteIndex++] = (byte)ch;
113                         } else {
114 #if NET_2_0
115                                 if (buffer == null)
116                                         buffer = EncoderFallback.CreateFallbackBuffer ();
117                                 if (Char.IsSurrogate (ch) && count > 1 &&
118                                     Char.IsSurrogate (chars [charIndex]))
119                                         buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
120                                 else
121                                         buffer.Fallback (ch, charIndex - 1);
122                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
123                                         fallback_chars = new char [buffer.Remaining];
124                                 for (int i = 0; i < fallback_chars.Length; i++)
125                                         fallback_chars [i] = buffer.GetNextChar ();
126                                 byteIndex += GetBytes (fallback_chars, 0, 
127                                         fallback_chars.Length, bytes, byteIndex,
128                                         ref buffer, ref fallback_chars);
129 #else
130                                 bytes [byteIndex++] = (byte)'?';
131 #endif
132                         }
133                 }
134                 return charCount;
135         }
136
137         // Convenience wrappers for "GetBytes".
138         public override int GetBytes (String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
139         {
140 #if NET_2_0
141 // I know this #if is ugly, but I think it is the simplest switch.
142                 EncoderFallbackBuffer buffer = null;
143                 char [] fallback_chars = null;
144                 return GetBytes (s, charIndex, charCount, bytes, byteIndex,
145                         ref buffer, ref fallback_chars);
146         }
147
148         int GetBytes (String s, int charIndex, int charCount,
149                       byte[] bytes, int byteIndex,
150                       ref EncoderFallbackBuffer buffer,
151                       ref char [] fallback_chars)
152         {
153 #endif
154                 if (s == null) {
155                         throw new ArgumentNullException ("s");
156                 }
157                 if (bytes == null) {
158                         throw new ArgumentNullException ("bytes");
159                 }
160                 if (charIndex < 0 || charIndex > s.Length) {
161                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
162                 }
163                 if (charCount < 0 || charCount > (s.Length - charIndex)) {
164                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
165                 }
166                 if (byteIndex < 0 || byteIndex > bytes.Length) {
167                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
168                 }
169                 if ((bytes.Length - byteIndex) < charCount) {
170                         throw new ArgumentException (_("Arg_InsufficientSpace"));
171                 }
172                 int count = charCount;
173                 char ch;
174                 while (count-- > 0) {
175                         ch = s [charIndex++];
176                         if (ch < (char)0x80) {
177                                 bytes [byteIndex++] = (byte)ch;
178                         } else {
179 #if NET_2_0
180                                 if (buffer == null)
181                                         buffer = EncoderFallback.CreateFallbackBuffer ();
182                                 if (Char.IsSurrogate (ch) && count > 1 &&
183                                     Char.IsSurrogate (s [charIndex]))
184                                         buffer.Fallback (ch, s [charIndex], charIndex++ - 1);
185                                 else
186                                         buffer.Fallback (ch, charIndex - 1);
187                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
188                                         fallback_chars = new char [buffer.Remaining];
189                                 for (int i = 0; i < fallback_chars.Length; i++)
190                                         fallback_chars [i] = buffer.GetNextChar ();
191                                 byteIndex += GetBytes (fallback_chars, 0, 
192                                         fallback_chars.Length, bytes, byteIndex,
193                                         ref buffer, ref fallback_chars);
194 #else
195                                 bytes [byteIndex++] = (byte)'?';
196 #endif
197                         }
198                 }
199                 return charCount;
200         }
201
202         // Get the number of characters needed to decode a byte buffer.
203         public override int GetCharCount (byte[] bytes, int index, int count)
204         {
205                 if (bytes == null) {
206                         throw new ArgumentNullException ("bytes");
207                 }
208                 if (index < 0 || index > bytes.Length) {
209                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
210                 }
211                 if (count < 0 || count > (bytes.Length - index)) {
212                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
213                 }
214                 return count;
215         }
216
217         // Get the characters that result from decoding a byte buffer.
218         public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
219         {
220                 if (bytes == null)
221                         throw new ArgumentNullException ("bytes");
222                 if (chars == null) 
223                         throw new ArgumentNullException ("chars");
224                 if (byteIndex < 0 || byteIndex > bytes.Length) 
225                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
226                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) 
227                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
228                 if (charIndex < 0 || charIndex > chars.Length) 
229                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
230
231                 if ((chars.Length - charIndex) < byteCount) 
232                         throw new ArgumentException (_("Arg_InsufficientSpace"));
233
234                 int count = byteCount;
235                 while (count-- > 0) {
236                         char c = (char)(bytes [byteIndex++]);
237                         if (c > 127)
238                                 c = '?';
239                         chars [charIndex++] = c;
240                 }
241                 return byteCount;
242         }
243
244         // Get the maximum number of bytes needed to encode a
245         // specified number of characters.
246         public override int GetMaxByteCount (int charCount)
247         {
248                 if (charCount < 0) {
249                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
250                 }
251                 return charCount;
252         }
253
254         // Get the maximum number of characters needed to decode a
255         // specified number of bytes.
256         public override int GetMaxCharCount (int byteCount)
257         {
258                 if (byteCount < 0) {
259                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
260                 }
261                 return byteCount;
262         }
263
264         // Decode a buffer of bytes into a string.
265         public override String GetString (byte[] bytes, int index, int count)
266         {
267                 if (bytes == null) {
268                         throw new ArgumentNullException ("bytes");
269                 }
270                 if (index < 0 || index > bytes.Length) {
271                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
272                 }
273                 if (count < 0 || count > (bytes.Length - index)) {
274                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
275                 }
276                 if (count == 0)
277                         return String.Empty;
278                 
279                 unsafe {
280                         fixed (byte *ss = &bytes [0]) {
281                                 return new String ((sbyte*)ss, index, count);
282                         }
283                 }
284         }
285
286 #if NET_2_0
287         [CLSCompliantAttribute (false)]
288         public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
289         {
290                 if (chars == null)
291                         throw new ArgumentNullException ("chars");
292                 if (bytes == null)
293                         throw new ArgumentNullException ("bytes");
294                 if (charCount < 0)
295                         throw new ArgumentOutOfRangeException ("charCount");
296                 if (byteCount < 0)
297                         throw new ArgumentOutOfRangeException ("byteCount");
298
299                 if (byteCount < charCount)
300                         throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
301
302                 for (int i = 0; i < charCount; i++){
303                         char c = chars [i];
304                         bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
305                 }
306                 return charCount;
307         }
308
309         [CLSCompliantAttribute(false)]
310         public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
311         {
312                 if (bytes == null)
313                         throw new ArgumentNullException ("bytes");
314                 if (chars == null) 
315                         throw new ArgumentNullException ("chars");
316                 if (charCount < 0)
317                         throw new ArgumentOutOfRangeException ("charCount");
318                 if (byteCount < 0)
319                         throw new ArgumentOutOfRangeException ("byteCount");
320                 if (charCount < byteCount)
321                         throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
322
323                 for (int i = 0; i < byteCount; i++){
324                         byte b = bytes [i];
325                         chars [i] = b > 127 ? '?' : (char) b;
326                 }
327                 return byteCount;
328                 
329         }
330
331         [CLSCompliantAttribute(false)]
332         public unsafe override int GetCharCount (byte *bytes, int count)
333         {
334                 return count;
335         }
336
337         [CLSCompliantAttribute(false)]
338         public unsafe override int GetByteCount (char *chars, int count)
339         {
340                 return count;
341         }
342 #else
343         // This routine is gone in 2.x
344         public override String GetString (byte[] bytes)
345         {
346                 if (bytes == null) {
347                         throw new ArgumentNullException ("bytes");
348                 }
349                 int count = bytes.Length;
350                 if (count == 0)
351                     return String.Empty;
352                 unsafe {
353                         fixed (byte *ss = &bytes [0]) {
354                                 return new String ((sbyte*)ss, 0, count);
355                         }
356                 }
357         }
358 #endif
359         
360 }; // class ASCIIEncoding
361
362 }; // namespace System.Text