* support-test-*.cs: Rename from test-*-p2.cs.
[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 #if NET_2_0
48         public override bool IsSingleByte {
49                 get { return true; }
50         }
51 #endif
52
53         // Get the number of bytes needed to encode a character buffer.
54         public override int GetByteCount (char[] chars, int index, int count)
55         {
56                 if (chars == null) {
57                         throw new ArgumentNullException ("chars");
58                 }
59                 if (index < 0 || index > chars.Length) {
60                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
61                 }
62                 if (count < 0 || count > (chars.Length - index)) {
63                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
64                 }
65                 return count;
66         }
67
68         // Convenience wrappers for "GetByteCount".
69         public override int GetByteCount (String s)
70         {
71                 if (s == null) {
72                         throw new ArgumentNullException ("s");
73                 }
74                 return s.Length;
75         }
76
77         // Get the bytes that result from encoding a character buffer.
78         public override int GetBytes (char[] chars, int charIndex, int charCount,
79                                                                  byte[] bytes, int byteIndex)
80         {
81 #if NET_2_0
82 // well, yes, I know this #if is ugly, but I think it is the simplest switch.
83                 EncoderFallbackBuffer buffer = null;
84                 char [] fallback_chars = null;
85                 return GetBytes (chars, charIndex, charCount, bytes,
86                         byteIndex, ref buffer, ref fallback_chars);
87         }
88
89         int GetBytes (char[] chars, int charIndex, int charCount,
90                       byte[] bytes, int byteIndex,
91                       ref EncoderFallbackBuffer buffer,
92                       ref char [] fallback_chars)
93         {
94 #endif
95                 if (chars == null) {
96                         throw new ArgumentNullException ("chars");
97                 }
98                 if (bytes == null) {
99                         throw new ArgumentNullException ("bytes");
100                 }
101                 if (charIndex < 0 || charIndex > chars.Length) {
102                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
103                 }
104                 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
105                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
106                 }
107                 if (byteIndex < 0 || byteIndex > bytes.Length) {
108                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
109                 }
110                 if ((bytes.Length - byteIndex) < charCount) {
111                         throw new ArgumentException (_("Arg_InsufficientSpace"));
112                 }
113                 int count = charCount;
114                 char ch;
115                 while (count-- > 0) {
116                         ch = chars [charIndex++];
117                         if (ch < (char)0x80) {
118                                 bytes [byteIndex++] = (byte)ch;
119                         } else {
120 #if NET_2_0
121                                 if (buffer == null)
122                                         buffer = EncoderFallback.CreateFallbackBuffer ();
123                                 if (Char.IsSurrogate (ch) && count > 1 &&
124                                     Char.IsSurrogate (chars [charIndex]))
125                                         buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
126                                 else
127                                         buffer.Fallback (ch, charIndex - 1);
128                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
129                                         fallback_chars = new char [buffer.Remaining];
130                                 for (int i = 0; i < fallback_chars.Length; i++)
131                                         fallback_chars [i] = buffer.GetNextChar ();
132                                 byteIndex += GetBytes (fallback_chars, 0, 
133                                         fallback_chars.Length, bytes, byteIndex,
134                                         ref buffer, ref fallback_chars);
135 #else
136                                 bytes [byteIndex++] = (byte)'?';
137 #endif
138                         }
139                 }
140                 return charCount;
141         }
142
143         // Convenience wrappers for "GetBytes".
144         public override int GetBytes (String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
145         {
146 #if NET_2_0
147 // I know this #if is ugly, but I think it is the simplest switch.
148                 EncoderFallbackBuffer buffer = null;
149                 char [] fallback_chars = null;
150                 return GetBytes (s, charIndex, charCount, bytes, byteIndex,
151                         ref buffer, ref fallback_chars);
152         }
153
154         int GetBytes (String s, int charIndex, int charCount,
155                       byte[] bytes, int byteIndex,
156                       ref EncoderFallbackBuffer buffer,
157                       ref char [] fallback_chars)
158         {
159 #endif
160                 if (s == null) {
161                         throw new ArgumentNullException ("s");
162                 }
163                 if (bytes == null) {
164                         throw new ArgumentNullException ("bytes");
165                 }
166                 if (charIndex < 0 || charIndex > s.Length) {
167                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
168                 }
169                 if (charCount < 0 || charCount > (s.Length - charIndex)) {
170                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
171                 }
172                 if (byteIndex < 0 || byteIndex > bytes.Length) {
173                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
174                 }
175                 if ((bytes.Length - byteIndex) < charCount) {
176                         throw new ArgumentException (_("Arg_InsufficientSpace"));
177                 }
178                 int count = charCount;
179                 char ch;
180                 while (count-- > 0) {
181                         ch = s [charIndex++];
182                         if (ch < (char)0x80) {
183                                 bytes [byteIndex++] = (byte)ch;
184                         } else {
185 #if NET_2_0
186                                 if (buffer == null)
187                                         buffer = EncoderFallback.CreateFallbackBuffer ();
188                                 if (Char.IsSurrogate (ch) && count > 1 &&
189                                     Char.IsSurrogate (s [charIndex]))
190                                         buffer.Fallback (ch, s [charIndex], charIndex++ - 1);
191                                 else
192                                         buffer.Fallback (ch, charIndex - 1);
193                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
194                                         fallback_chars = new char [buffer.Remaining];
195                                 for (int i = 0; i < fallback_chars.Length; i++)
196                                         fallback_chars [i] = buffer.GetNextChar ();
197                                 byteIndex += GetBytes (fallback_chars, 0, 
198                                         fallback_chars.Length, bytes, byteIndex,
199                                         ref buffer, ref fallback_chars);
200 #else
201                                 bytes [byteIndex++] = (byte)'?';
202 #endif
203                         }
204                 }
205                 return charCount;
206         }
207
208         // Get the number of characters needed to decode a byte buffer.
209         public override int GetCharCount (byte[] bytes, int index, int count)
210         {
211                 if (bytes == null) {
212                         throw new ArgumentNullException ("bytes");
213                 }
214                 if (index < 0 || index > bytes.Length) {
215                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
216                 }
217                 if (count < 0 || count > (bytes.Length - index)) {
218                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
219                 }
220                 return count;
221         }
222
223         // Get the characters that result from decoding a byte buffer.
224         public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
225         {
226                 if (bytes == null)
227                         throw new ArgumentNullException ("bytes");
228                 if (chars == null) 
229                         throw new ArgumentNullException ("chars");
230                 if (byteIndex < 0 || byteIndex > bytes.Length) 
231                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
232                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) 
233                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
234                 if (charIndex < 0 || charIndex > chars.Length) 
235                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
236
237                 if ((chars.Length - charIndex) < byteCount) 
238                         throw new ArgumentException (_("Arg_InsufficientSpace"));
239
240                 int count = byteCount;
241                 while (count-- > 0) {
242                         char c = (char)(bytes [byteIndex++]);
243                         if (c > 127)
244                                 c = '?';
245                         chars [charIndex++] = c;
246                 }
247                 return byteCount;
248         }
249
250         // Get the maximum number of bytes needed to encode a
251         // specified number of characters.
252         public override int GetMaxByteCount (int charCount)
253         {
254                 if (charCount < 0) {
255                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
256                 }
257                 return charCount;
258         }
259
260         // Get the maximum number of characters needed to decode a
261         // specified number of bytes.
262         public override int GetMaxCharCount (int byteCount)
263         {
264                 if (byteCount < 0) {
265                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
266                 }
267                 return byteCount;
268         }
269
270         // Decode a buffer of bytes into a string.
271         public override String GetString (byte[] bytes, int index, int count)
272         {
273                 if (bytes == null) {
274                         throw new ArgumentNullException ("bytes");
275                 }
276                 if (index < 0 || index > bytes.Length) {
277                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
278                 }
279                 if (count < 0 || count > (bytes.Length - index)) {
280                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
281                 }
282                 if (count == 0)
283                         return String.Empty;
284                 
285                 unsafe {
286                         fixed (byte *ss = &bytes [0]) {
287                                 return new String ((sbyte*)ss, index, count);
288                         }
289                 }
290         }
291
292 #if NET_2_0
293         [CLSCompliantAttribute (false)]
294         public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
295         {
296                 if (chars == null)
297                         throw new ArgumentNullException ("chars");
298                 if (bytes == null)
299                         throw new ArgumentNullException ("bytes");
300                 if (charCount < 0)
301                         throw new ArgumentOutOfRangeException ("charCount");
302                 if (byteCount < 0)
303                         throw new ArgumentOutOfRangeException ("byteCount");
304
305                 if (byteCount < charCount)
306                         throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
307
308                 for (int i = 0; i < charCount; i++){
309                         char c = chars [i];
310                         bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
311                 }
312                 return charCount;
313         }
314
315         [CLSCompliantAttribute(false)]
316         public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
317         {
318                 if (bytes == null)
319                         throw new ArgumentNullException ("bytes");
320                 if (chars == null) 
321                         throw new ArgumentNullException ("chars");
322                 if (charCount < 0)
323                         throw new ArgumentOutOfRangeException ("charCount");
324                 if (byteCount < 0)
325                         throw new ArgumentOutOfRangeException ("byteCount");
326                 if (charCount < byteCount)
327                         throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
328
329                 for (int i = 0; i < byteCount; i++){
330                         byte b = bytes [i];
331                         chars [i] = b > 127 ? '?' : (char) b;
332                 }
333                 return byteCount;
334                 
335         }
336
337         [CLSCompliantAttribute(false)]
338         public unsafe override int GetCharCount (byte *bytes, int count)
339         {
340                 return count;
341         }
342
343         [CLSCompliantAttribute(false)]
344         public unsafe override int GetByteCount (char *chars, int count)
345         {
346                 return count;
347         }
348 #else
349         // This routine is gone in 2.x
350         public override String GetString (byte[] bytes)
351         {
352                 if (bytes == null) {
353                         throw new ArgumentNullException ("bytes");
354                 }
355                 int count = bytes.Length;
356                 if (count == 0)
357                     return String.Empty;
358                 unsafe {
359                         fixed (byte *ss = &bytes [0]) {
360                                 return new String ((sbyte*)ss, 0, count);
361                         }
362                 }
363         }
364 #endif
365         
366 }; // class ASCIIEncoding
367
368 }; // namespace System.Text