2 * ASCIIEncoding.cs - Implementation of the "System.Text.ASCIIEncoding" class.
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)
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:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
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.
33 [MonoTODO ("Fix serialization compatibility with MS.NET")]
34 public class ASCIIEncoding : Encoding
36 // Magic number used by Windows for "ASCII".
37 internal const int ASCII_CODE_PAGE = 20127;
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;
47 // Get the number of bytes needed to encode a character buffer.
48 public override int GetByteCount (char[] chars, int index, int count)
51 throw new ArgumentNullException ("chars");
53 if (index < 0 || index > chars.Length) {
54 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
56 if (count < 0 || count > (chars.Length - index)) {
57 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
62 // Convenience wrappers for "GetByteCount".
63 public override int GetByteCount (String s)
66 throw new ArgumentNullException ("s");
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)
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);
83 int GetBytes (char[] chars, int charIndex, int charCount,
84 byte[] bytes, int byteIndex,
85 ref EncoderFallbackBuffer buffer,
86 ref char [] fallback_chars)
90 throw new ArgumentNullException ("chars");
93 throw new ArgumentNullException ("bytes");
95 if (charIndex < 0 || charIndex > chars.Length) {
96 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
98 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
99 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
101 if (byteIndex < 0 || byteIndex > bytes.Length) {
102 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
104 if ((bytes.Length - byteIndex) < charCount) {
105 throw new ArgumentException (_("Arg_InsufficientSpace"));
107 int count = charCount;
109 while (count-- > 0) {
110 ch = chars [charIndex++];
111 if (ch < (char)0x80) {
112 bytes [byteIndex++] = (byte)ch;
116 buffer = EncoderFallback.CreateFallbackBuffer ();
117 if (Char.IsSurrogate (ch) && count > 1 &&
118 Char.IsSurrogate (chars [charIndex]))
119 buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
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);
130 bytes [byteIndex++] = (byte)'?';
137 // Convenience wrappers for "GetBytes".
138 public override int GetBytes (String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
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);
148 int GetBytes (String s, int charIndex, int charCount,
149 byte[] bytes, int byteIndex,
150 ref EncoderFallbackBuffer buffer,
151 ref char [] fallback_chars)
155 throw new ArgumentNullException ("s");
158 throw new ArgumentNullException ("bytes");
160 if (charIndex < 0 || charIndex > s.Length) {
161 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
163 if (charCount < 0 || charCount > (s.Length - charIndex)) {
164 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
166 if (byteIndex < 0 || byteIndex > bytes.Length) {
167 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
169 if ((bytes.Length - byteIndex) < charCount) {
170 throw new ArgumentException (_("Arg_InsufficientSpace"));
172 int count = charCount;
174 while (count-- > 0) {
175 ch = s [charIndex++];
176 if (ch < (char)0x80) {
177 bytes [byteIndex++] = (byte)ch;
181 buffer = EncoderFallback.CreateFallbackBuffer ();
182 if (Char.IsSurrogate (ch) && count > 1 &&
183 Char.IsSurrogate (s [charIndex]))
184 buffer.Fallback (ch, s [charIndex], charIndex++ - 1);
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);
195 bytes [byteIndex++] = (byte)'?';
202 // Get the number of characters needed to decode a byte buffer.
203 public override int GetCharCount (byte[] bytes, int index, int count)
206 throw new ArgumentNullException ("bytes");
208 if (index < 0 || index > bytes.Length) {
209 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
211 if (count < 0 || count > (bytes.Length - index)) {
212 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
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)
221 throw new ArgumentNullException ("bytes");
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"));
231 if ((chars.Length - charIndex) < byteCount)
232 throw new ArgumentException (_("Arg_InsufficientSpace"));
234 int count = byteCount;
235 while (count-- > 0) {
236 char c = (char)(bytes [byteIndex++]);
239 chars [charIndex++] = c;
244 // Get the maximum number of bytes needed to encode a
245 // specified number of characters.
246 public override int GetMaxByteCount (int charCount)
249 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
254 // Get the maximum number of characters needed to decode a
255 // specified number of bytes.
256 public override int GetMaxCharCount (int byteCount)
259 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
264 // Decode a buffer of bytes into a string.
265 public override String GetString (byte[] bytes, int index, int count)
268 throw new ArgumentNullException ("bytes");
270 if (index < 0 || index > bytes.Length) {
271 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
273 if (count < 0 || count > (bytes.Length - index)) {
274 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
280 fixed (byte *ss = &bytes [0]) {
281 return new String ((sbyte*)ss, index, count);
287 [CLSCompliantAttribute (false)]
288 public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
291 throw new ArgumentNullException ("chars");
293 throw new ArgumentNullException ("bytes");
295 throw new ArgumentOutOfRangeException ("charCount");
297 throw new ArgumentOutOfRangeException ("byteCount");
299 if (byteCount < charCount)
300 throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
302 for (int i = 0; i < charCount; i++){
304 bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
309 [CLSCompliantAttribute(false)]
310 public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
313 throw new ArgumentNullException ("bytes");
315 throw new ArgumentNullException ("chars");
317 throw new ArgumentOutOfRangeException ("charCount");
319 throw new ArgumentOutOfRangeException ("byteCount");
320 if (charCount < byteCount)
321 throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
323 for (int i = 0; i < byteCount; i++){
325 chars [i] = b > 127 ? '?' : (char) b;
331 [CLSCompliantAttribute(false)]
332 public unsafe override int GetCharCount (byte *bytes, int count)
337 [CLSCompliantAttribute(false)]
338 public unsafe override int GetByteCount (char *chars, int count)
343 // This routine is gone in 2.x
344 public override String GetString (byte[] bytes)
347 throw new ArgumentNullException ("bytes");
349 int count = bytes.Length;
353 fixed (byte *ss = &bytes [0]) {
354 return new String ((sbyte*)ss, 0, count);
360 }; // class ASCIIEncoding
362 }; // namespace System.Text