merge -r 61110:61111
[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 NET_2_0
227 // well, yes, I know this #if is ugly, but I think it is the simplest switch.
228                 DecoderFallbackBuffer buffer = null;
229                 return GetChars (bytes, byteIndex, byteCount, chars,
230                         charIndex, ref buffer);
231         }
232
233         int GetChars (byte[] bytes, int byteIndex, int byteCount,
234                       char[] chars, int charIndex,
235                       ref DecoderFallbackBuffer buffer)
236         {
237 #endif
238                 if (bytes == null)
239                         throw new ArgumentNullException ("bytes");
240                 if (chars == null) 
241                         throw new ArgumentNullException ("chars");
242                 if (byteIndex < 0 || byteIndex > bytes.Length) 
243                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
244                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) 
245                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
246                 if (charIndex < 0 || charIndex > chars.Length) 
247                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
248
249                 if ((chars.Length - charIndex) < byteCount) 
250                         throw new ArgumentException (_("Arg_InsufficientSpace"));
251
252                 int count = byteCount;
253                 while (count-- > 0) {
254                         char c = (char) bytes [byteIndex++];
255                         if (c < '\x80')
256                                 chars [charIndex++] = c;
257                         else {
258 #if NET_2_0
259                                 if (buffer == null)
260                                         buffer = DecoderFallback.CreateFallbackBuffer ();
261                                 buffer.Fallback (bytes, byteIndex);
262                                 while (buffer.Remaining > 0)
263                                         chars [charIndex++] = buffer.GetNextChar ();
264 #else
265                                 chars [charIndex++] = '?';
266 #endif
267                         }
268                 }
269                 return byteCount;
270         }
271
272         // Get the maximum number of bytes needed to encode a
273         // specified number of characters.
274         public override int GetMaxByteCount (int charCount)
275         {
276                 if (charCount < 0) {
277                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
278                 }
279                 return charCount;
280         }
281
282         // Get the maximum number of characters needed to decode a
283         // specified number of bytes.
284         public override int GetMaxCharCount (int byteCount)
285         {
286                 if (byteCount < 0) {
287                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
288                 }
289                 return byteCount;
290         }
291
292         // Decode a buffer of bytes into a string.
293         public override String GetString (byte[] bytes, int index, int count)
294         {
295                 if (bytes == null) {
296                         throw new ArgumentNullException ("bytes");
297                 }
298                 if (index < 0 || index > bytes.Length) {
299                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
300                 }
301                 if (count < 0 || count > (bytes.Length - index)) {
302                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
303                 }
304                 if (count == 0)
305                         return String.Empty;
306                 
307                 unsafe {
308                         fixed (byte *ss = &bytes [0]) {
309                                 return new String ((sbyte*)ss, index, count);
310                         }
311                 }
312         }
313
314 #if NET_2_0
315         [CLSCompliantAttribute (false)]
316         public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
317         {
318                 if (chars == null)
319                         throw new ArgumentNullException ("chars");
320                 if (bytes == null)
321                         throw new ArgumentNullException ("bytes");
322                 if (charCount < 0)
323                         throw new ArgumentOutOfRangeException ("charCount");
324                 if (byteCount < 0)
325                         throw new ArgumentOutOfRangeException ("byteCount");
326
327                 if (byteCount < charCount)
328                         throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
329
330                 for (int i = 0; i < charCount; i++){
331                         char c = chars [i];
332                         bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
333                 }
334                 return charCount;
335         }
336
337         [CLSCompliantAttribute(false)]
338         public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
339         {
340                 if (bytes == null)
341                         throw new ArgumentNullException ("bytes");
342                 if (chars == null) 
343                         throw new ArgumentNullException ("chars");
344                 if (charCount < 0)
345                         throw new ArgumentOutOfRangeException ("charCount");
346                 if (byteCount < 0)
347                         throw new ArgumentOutOfRangeException ("byteCount");
348                 if (charCount < byteCount)
349                         throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
350
351                 for (int i = 0; i < byteCount; i++){
352                         byte b = bytes [i];
353                         chars [i] = b > 127 ? '?' : (char) b;
354                 }
355                 return byteCount;
356                 
357         }
358
359         [CLSCompliantAttribute(false)]
360         public unsafe override int GetCharCount (byte *bytes, int count)
361         {
362                 return count;
363         }
364
365         [CLSCompliantAttribute(false)]
366         public unsafe override int GetByteCount (char *chars, int count)
367         {
368                 return count;
369         }
370 #else
371         // This routine is gone in 2.x
372         public override String GetString (byte[] bytes)
373         {
374                 if (bytes == null) {
375                         throw new ArgumentNullException ("bytes");
376                 }
377                 int count = bytes.Length;
378                 if (count == 0)
379                     return String.Empty;
380                 unsafe {
381                         fixed (byte *ss = &bytes [0]) {
382                                 return new String ((sbyte*)ss, 0, count);
383                         }
384                 }
385         }
386 #endif
387
388 #if NET_2_0
389         [MonoTODO ("we have simple override to match method signature.")]
390         public override Decoder GetDecoder ()
391         {
392                 return base.GetDecoder ();
393         }
394
395         [MonoTODO ("we have simple override to match method signature.")]
396         public override Encoder GetEncoder ()
397         {
398                 return base.GetEncoder ();
399         }
400 #endif
401 }; // class ASCIIEncoding
402
403 }; // namespace System.Text