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.
31 using System.Runtime.InteropServices;
35 [MonoLimitation ("Serialization format not compatible with .NET")]
36 public class ASCIIEncoding : Encoding
38 // Magic number used by Windows for "ASCII".
39 internal const int ASCII_CODE_PAGE = 20127;
42 public ASCIIEncoding () : base(ASCII_CODE_PAGE) {
43 body_name = header_name = web_name= "us-ascii";
44 encoding_name = "US-ASCII";
45 is_mail_news_display = true;
46 is_mail_news_save = true;
50 public override bool IsSingleByte {
54 // Get the number of bytes needed to encode a character buffer.
55 public override int GetByteCount (char[] chars, int index, int count)
58 throw new ArgumentNullException ("chars");
60 if (index < 0 || index > chars.Length) {
61 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
63 if (count < 0 || count > (chars.Length - index)) {
64 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
69 // Convenience wrappers for "GetByteCount".
70 public override int GetByteCount (String chars)
73 throw new ArgumentNullException ("chars");
78 // Get the bytes that result from encoding a character buffer.
79 public override int GetBytes (char[] chars, int charIndex, int charCount,
80 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;
86 return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
87 ref buffer, ref fallback_chars);
90 int GetBytes (char[] chars, int charIndex, int charCount,
91 byte[] bytes, int byteIndex,
92 ref EncoderFallbackBuffer buffer,
93 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;
121 buffer = EncoderFallback.CreateFallbackBuffer ();
122 if (Char.IsSurrogate (ch) && count > 1 &&
123 Char.IsSurrogate (chars [charIndex]))
124 buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
126 buffer.Fallback (ch, charIndex - 1);
127 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
128 fallback_chars = new char [buffer.Remaining];
129 for (int i = 0; i < fallback_chars.Length; i++)
130 fallback_chars [i] = buffer.GetNextChar ();
131 byteIndex += GetBytes (fallback_chars, 0,
132 fallback_chars.Length, bytes, byteIndex,
133 ref buffer, ref fallback_chars);
139 // Convenience wrappers for "GetBytes".
140 public override int GetBytes (String chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
142 // I know this #if is ugly, but I think it is the simplest switch.
143 EncoderFallbackBuffer buffer = null;
144 char [] fallback_chars = null;
145 return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
146 ref buffer, ref fallback_chars);
149 int GetBytes (String chars, int charIndex, int charCount,
150 byte[] bytes, int byteIndex,
151 ref EncoderFallbackBuffer buffer,
152 ref char [] fallback_chars)
155 throw new ArgumentNullException ("chars");
158 throw new ArgumentNullException ("bytes");
160 if (charIndex < 0 || charIndex > chars.Length) {
161 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
163 if (charCount < 0 || charCount > (chars.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 = chars [charIndex++];
176 if (ch < (char)0x80) {
177 bytes [byteIndex++] = (byte)ch;
180 buffer = EncoderFallback.CreateFallbackBuffer ();
181 if (Char.IsSurrogate (ch) && count > 1 &&
182 Char.IsSurrogate (chars [charIndex]))
183 buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
185 buffer.Fallback (ch, charIndex - 1);
186 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
187 fallback_chars = new char [buffer.Remaining];
188 for (int i = 0; i < fallback_chars.Length; i++)
189 fallback_chars [i] = buffer.GetNextChar ();
190 byteIndex += GetBytes (fallback_chars, 0,
191 fallback_chars.Length, bytes, byteIndex,
192 ref buffer, ref fallback_chars);
198 // Get the number of characters needed to decode a byte buffer.
199 public override int GetCharCount (byte[] bytes, int index, int count)
202 throw new ArgumentNullException ("bytes");
204 if (index < 0 || index > bytes.Length) {
205 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
207 if (count < 0 || count > (bytes.Length - index)) {
208 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
213 // Get the characters that result from decoding a byte buffer.
214 public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
216 // well, yes, I know this #if is ugly, but I think it is the simplest switch.
217 DecoderFallbackBuffer buffer = null;
218 return GetChars (bytes, byteIndex, byteCount, chars,
219 charIndex, ref buffer);
222 int GetChars (byte[] bytes, int byteIndex, int byteCount,
223 char[] chars, int charIndex,
224 ref DecoderFallbackBuffer buffer)
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++];
244 chars [charIndex++] = c;
247 buffer = DecoderFallback.CreateFallbackBuffer ();
248 buffer.Fallback (bytes, byteIndex);
249 while (buffer.Remaining > 0)
250 chars [charIndex++] = buffer.GetNextChar ();
256 // Get the maximum number of bytes needed to encode a
257 // specified number of characters.
258 public override int GetMaxByteCount (int charCount)
261 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
266 // Get the maximum number of characters needed to decode a
267 // specified number of bytes.
268 public override int GetMaxCharCount (int byteCount)
271 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
276 // Decode a buffer of bytes into a string.
277 public override String GetString (byte[] bytes, int byteIndex, int byteCount)
280 throw new ArgumentNullException ("bytes");
282 if (byteIndex < 0 || byteIndex > bytes.Length) {
283 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
285 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
286 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
292 fixed (byte* bytePtr = bytes) {
293 string s = string.InternalAllocateStr (byteCount);
295 fixed (char* charPtr = s) {
296 byte* currByte = bytePtr + byteIndex;
297 byte* lastByte = currByte + byteCount;
298 char* currChar = charPtr;
300 while (currByte < lastByte) {
301 byte b = currByte++ [0];
302 currChar++ [0] = b <= 0x7F ? (char) b : (char) '?';
311 [CLSCompliantAttribute (false)]
313 public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
316 throw new ArgumentNullException ("chars");
318 throw new ArgumentNullException ("bytes");
320 throw new ArgumentOutOfRangeException ("charCount");
322 throw new ArgumentOutOfRangeException ("byteCount");
324 if (byteCount < charCount)
325 throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
327 for (int i = 0; i < charCount; i++){
329 bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
334 [CLSCompliantAttribute(false)]
336 public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
339 throw new ArgumentNullException ("bytes");
341 throw new ArgumentNullException ("chars");
343 throw new ArgumentOutOfRangeException ("charCount");
345 throw new ArgumentOutOfRangeException ("byteCount");
346 if (charCount < byteCount)
347 throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
349 for (int i = 0; i < byteCount; i++){
351 chars [i] = b > 127 ? '?' : (char) b;
357 [CLSCompliantAttribute(false)]
359 public unsafe override int GetCharCount (byte *bytes, int count)
364 [CLSCompliantAttribute(false)]
366 public unsafe override int GetByteCount (char *chars, int count)
372 public override Decoder GetDecoder ()
374 return base.GetDecoder ();
378 public override Encoder GetEncoder ()
380 return base.GetEncoder ();
382 }; // class ASCIIEncoding
384 }; // namespace System.Text