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;
48 public override bool IsSingleByte {
53 // Get the number of bytes needed to encode a character buffer.
54 public override int GetByteCount (char[] chars, int index, int count)
57 throw new ArgumentNullException ("chars");
59 if (index < 0 || index > chars.Length) {
60 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
62 if (count < 0 || count > (chars.Length - index)) {
63 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
68 // Convenience wrappers for "GetByteCount".
69 public override int GetByteCount (String s)
72 throw new ArgumentNullException ("s");
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)
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);
89 int GetBytes (char[] chars, int charIndex, int charCount,
90 byte[] bytes, int byteIndex,
91 ref EncoderFallbackBuffer buffer,
92 ref char [] fallback_chars)
96 throw new ArgumentNullException ("chars");
99 throw new ArgumentNullException ("bytes");
101 if (charIndex < 0 || charIndex > chars.Length) {
102 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
104 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
105 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
107 if (byteIndex < 0 || byteIndex > bytes.Length) {
108 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
110 if ((bytes.Length - byteIndex) < charCount) {
111 throw new ArgumentException (_("Arg_InsufficientSpace"));
113 int count = charCount;
115 while (count-- > 0) {
116 ch = chars [charIndex++];
117 if (ch < (char)0x80) {
118 bytes [byteIndex++] = (byte)ch;
122 buffer = EncoderFallback.CreateFallbackBuffer ();
123 if (Char.IsSurrogate (ch) && count > 1 &&
124 Char.IsSurrogate (chars [charIndex]))
125 buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
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);
136 bytes [byteIndex++] = (byte)'?';
143 // Convenience wrappers for "GetBytes".
144 public override int GetBytes (String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
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);
154 int GetBytes (String s, int charIndex, int charCount,
155 byte[] bytes, int byteIndex,
156 ref EncoderFallbackBuffer buffer,
157 ref char [] fallback_chars)
161 throw new ArgumentNullException ("s");
164 throw new ArgumentNullException ("bytes");
166 if (charIndex < 0 || charIndex > s.Length) {
167 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
169 if (charCount < 0 || charCount > (s.Length - charIndex)) {
170 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
172 if (byteIndex < 0 || byteIndex > bytes.Length) {
173 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
175 if ((bytes.Length - byteIndex) < charCount) {
176 throw new ArgumentException (_("Arg_InsufficientSpace"));
178 int count = charCount;
180 while (count-- > 0) {
181 ch = s [charIndex++];
182 if (ch < (char)0x80) {
183 bytes [byteIndex++] = (byte)ch;
187 buffer = EncoderFallback.CreateFallbackBuffer ();
188 if (Char.IsSurrogate (ch) && count > 1 &&
189 Char.IsSurrogate (s [charIndex]))
190 buffer.Fallback (ch, s [charIndex], charIndex++ - 1);
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);
201 bytes [byteIndex++] = (byte)'?';
208 // Get the number of characters needed to decode a byte buffer.
209 public override int GetCharCount (byte[] bytes, int index, int count)
212 throw new ArgumentNullException ("bytes");
214 if (index < 0 || index > bytes.Length) {
215 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
217 if (count < 0 || count > (bytes.Length - index)) {
218 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
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)
227 throw new ArgumentNullException ("bytes");
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"));
237 if ((chars.Length - charIndex) < byteCount)
238 throw new ArgumentException (_("Arg_InsufficientSpace"));
240 int count = byteCount;
241 while (count-- > 0) {
242 char c = (char)(bytes [byteIndex++]);
245 chars [charIndex++] = c;
250 // Get the maximum number of bytes needed to encode a
251 // specified number of characters.
252 public override int GetMaxByteCount (int charCount)
255 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
260 // Get the maximum number of characters needed to decode a
261 // specified number of bytes.
262 public override int GetMaxCharCount (int byteCount)
265 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
270 // Decode a buffer of bytes into a string.
271 public override String GetString (byte[] bytes, int index, int count)
274 throw new ArgumentNullException ("bytes");
276 if (index < 0 || index > bytes.Length) {
277 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
279 if (count < 0 || count > (bytes.Length - index)) {
280 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
286 fixed (byte *ss = &bytes [0]) {
287 return new String ((sbyte*)ss, index, count);
293 [CLSCompliantAttribute (false)]
294 public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
297 throw new ArgumentNullException ("chars");
299 throw new ArgumentNullException ("bytes");
301 throw new ArgumentOutOfRangeException ("charCount");
303 throw new ArgumentOutOfRangeException ("byteCount");
305 if (byteCount < charCount)
306 throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
308 for (int i = 0; i < charCount; i++){
310 bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
315 [CLSCompliantAttribute(false)]
316 public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
319 throw new ArgumentNullException ("bytes");
321 throw new ArgumentNullException ("chars");
323 throw new ArgumentOutOfRangeException ("charCount");
325 throw new ArgumentOutOfRangeException ("byteCount");
326 if (charCount < byteCount)
327 throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
329 for (int i = 0; i < byteCount; i++){
331 chars [i] = b > 127 ? '?' : (char) b;
337 [CLSCompliantAttribute(false)]
338 public unsafe override int GetCharCount (byte *bytes, int count)
343 [CLSCompliantAttribute(false)]
344 public unsafe override int GetByteCount (char *chars, int count)
349 // This routine is gone in 2.x
350 public override String GetString (byte[] bytes)
353 throw new ArgumentNullException ("bytes");
355 int count = bytes.Length;
359 fixed (byte *ss = &bytes [0]) {
360 return new String ((sbyte*)ss, 0, count);
366 }; // class ASCIIEncoding
368 }; // namespace System.Text