updating to the latest module.
[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 (chars == null) {
76                         throw new ArgumentNullException ("chars");
77                 }
78                 if (bytes == null) {
79                         throw new ArgumentNullException ("bytes");
80                 }
81                 if (charIndex < 0 || charIndex > chars.Length) {
82                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
83                 }
84                 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
85                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
86                 }
87                 if (byteIndex < 0 || byteIndex > bytes.Length) {
88                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
89                 }
90                 if ((bytes.Length - byteIndex) < charCount) {
91                         throw new ArgumentException (_("Arg_InsufficientSpace"));
92                 }
93                 int count = charCount;
94                 char ch;
95                 while (count-- > 0) {
96                         ch = chars [charIndex++];
97                         if (ch < (char)0x80) {
98                                 bytes [byteIndex++] = (byte)ch;
99                         } else {
100                                 bytes [byteIndex++] = (byte)'?';
101                         }
102                 }
103                 return charCount;
104         }
105
106         // Convenience wrappers for "GetBytes".
107         public override int GetBytes (String s, int charIndex, int charCount,
108                                                                  byte[] bytes, int byteIndex)
109         {
110                 if (s == null) {
111                         throw new ArgumentNullException ("s");
112                 }
113                 if (bytes == null) {
114                         throw new ArgumentNullException ("bytes");
115                 }
116                 if (charIndex < 0 || charIndex > s.Length) {
117                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
118                 }
119                 if (charCount < 0 || charCount > (s.Length - charIndex)) {
120                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
121                 }
122                 if (byteIndex < 0 || byteIndex > bytes.Length) {
123                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
124                 }
125                 if ((bytes.Length - byteIndex) < charCount) {
126                         throw new ArgumentException (_("Arg_InsufficientSpace"));
127                 }
128                 int count = charCount;
129                 char ch;
130                 while (count-- > 0) {
131                         ch = s [charIndex++];
132                         if (ch < (char)0x80) {
133                                 bytes [byteIndex++] = (byte)ch;
134                         } else {
135                                 bytes [byteIndex++] = (byte)'?';
136                         }
137                 }
138                 return charCount;
139         }
140
141         // Get the number of characters needed to decode a byte buffer.
142         public override int GetCharCount (byte[] bytes, int index, int count)
143         {
144                 if (bytes == null) {
145                         throw new ArgumentNullException ("bytes");
146                 }
147                 if (index < 0 || index > bytes.Length) {
148                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
149                 }
150                 if (count < 0 || count > (bytes.Length - index)) {
151                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
152                 }
153                 return count;
154         }
155
156         // Get the characters that result from decoding a byte buffer.
157         public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
158                                                                  char[] chars, int charIndex)
159         {
160                 if (bytes == null) {
161                         throw new ArgumentNullException ("bytes");
162                 }
163                 if (chars == null) {
164                         throw new ArgumentNullException ("chars");
165                 }
166                 if (byteIndex < 0 || byteIndex > bytes.Length) {
167                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
168                 }
169                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
170                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
171                 }
172                 if (charIndex < 0 || charIndex > chars.Length) {
173                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
174                 }
175                 if ((chars.Length - charIndex) < byteCount) {
176                         throw new ArgumentException (_("Arg_InsufficientSpace"));
177                 }
178                 int count = byteCount;
179                 while (count-- > 0) {
180                         char c = (char)(bytes [byteIndex++]);
181                         if (c > 127)
182                                 c = '?';
183                         chars [charIndex++] = c;
184                 }
185                 return byteCount;
186         }
187
188         // Get the maximum number of bytes needed to encode a
189         // specified number of characters.
190         public override int GetMaxByteCount (int charCount)
191         {
192                 if (charCount < 0) {
193                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
194                 }
195                 return charCount;
196         }
197
198         // Get the maximum number of characters needed to decode a
199         // specified number of bytes.
200         public override int GetMaxCharCount (int byteCount)
201         {
202                 if (byteCount < 0) {
203                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
204                 }
205                 return byteCount;
206         }
207
208         // Decode a buffer of bytes into a string.
209         public override String GetString (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                 if (count == 0)
221                     return String.Empty;
222                 unsafe {
223                         fixed (byte *ss = &bytes [0]) {
224                                 return new String ((sbyte*)ss, index, count);
225                         }
226                 }
227         }
228         public override String GetString (byte[] bytes)
229         {
230                 if (bytes == null) {
231                         throw new ArgumentNullException ("bytes");
232                 }
233                 int count = bytes.Length;
234                 if (count == 0)
235                     return String.Empty;
236                 unsafe {
237                         fixed (byte *ss = &bytes [0]) {
238                                 return new String ((sbyte*)ss, 0, count);
239                         }
240                 }
241         }
242
243 }; // class ASCIIEncoding
244
245 }; // namespace System.Text