GenericParameter.cs: override Module properly
[mcs.git] / class / corlib / System.Text / ASCIIEncoding.cs
blob5532e0b9352b63299815cae3ae7046d77d371246
1 /*
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.
27 namespace System.Text
30 using System;
31 using System.Runtime.InteropServices;
33 [Serializable]
34 [ComVisible (true)]
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;
41 // Constructor.
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;
49 [ComVisible (false)]
50 public override bool IsSingleByte {
51 get { return true; }
54 // Get the number of bytes needed to encode a character buffer.
55 public override int GetByteCount (char[] chars, int index, int count)
57 if (chars == null) {
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"));
66 return count;
69 // Convenience wrappers for "GetByteCount".
70 public override int GetByteCount (String chars)
72 if (chars == null) {
73 throw new ArgumentNullException ("chars");
75 return chars.Length;
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 EncoderFallbackBuffer buffer = null;
83 char [] fallback_chars = null;
85 return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
86 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)
94 if (chars == null)
95 throw new ArgumentNullException ("chars");
97 unsafe {
98 fixed (char *cptr = chars) {
99 return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
104 // Convenience wrappers for "GetBytes".
105 public override int GetBytes (String chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
107 EncoderFallbackBuffer buffer = null;
108 char [] fallback_chars = null;
109 return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
110 ref buffer, ref fallback_chars);
113 int GetBytes (String chars, int charIndex, int charCount,
114 byte[] bytes, int byteIndex,
115 ref EncoderFallbackBuffer buffer,
116 ref char [] fallback_chars)
118 if (chars == null)
119 throw new ArgumentNullException ("chars");
121 unsafe {
122 fixed (char *cptr = chars) {
123 return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
128 unsafe int InternalGetBytes (char *chars, int charLength, int charIndex, int charCount,
129 byte[] bytes, int byteIndex,
130 ref EncoderFallbackBuffer buffer,
131 ref char [] fallback_chars)
133 if (bytes == null)
134 throw new ArgumentNullException ("bytes");
135 if (charIndex < 0 || charIndex > charLength)
136 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
137 if (charCount < 0 || charCount > (charLength - charIndex))
138 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
139 if (byteIndex < 0 || byteIndex > bytes.Length)
140 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
141 if ((bytes.Length - byteIndex) < charCount)
142 throw new ArgumentException (_("Arg_InsufficientSpace"));
144 int count = charCount;
145 char ch;
146 while (count-- > 0) {
147 ch = chars [charIndex++];
148 if (ch < (char)0x80) {
149 bytes [byteIndex++] = (byte)ch;
150 } else {
151 if (buffer == null)
152 buffer = EncoderFallback.CreateFallbackBuffer ();
153 if (Char.IsSurrogate (ch) && count > 1 &&
154 Char.IsSurrogate (chars [charIndex]))
155 buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
156 else
157 buffer.Fallback (ch, charIndex - 1);
158 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
159 fallback_chars = new char [buffer.Remaining];
160 for (int i = 0; i < fallback_chars.Length; i++)
161 fallback_chars [i] = buffer.GetNextChar ();
162 byteIndex += GetBytes (fallback_chars, 0,
163 fallback_chars.Length, bytes, byteIndex,
164 ref buffer, ref fallback_chars);
167 return charCount;
170 // Get the number of characters needed to decode a byte buffer.
171 public override int GetCharCount (byte[] bytes, int index, int count)
173 if (bytes == null) {
174 throw new ArgumentNullException ("bytes");
176 if (index < 0 || index > bytes.Length) {
177 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
179 if (count < 0 || count > (bytes.Length - index)) {
180 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
182 return count;
185 // Get the characters that result from decoding a byte buffer.
186 public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
188 DecoderFallbackBuffer buffer = null;
189 return GetChars (bytes, byteIndex, byteCount, chars,
190 charIndex, ref buffer);
193 int GetChars (byte[] bytes, int byteIndex, int byteCount,
194 char[] chars, int charIndex,
195 ref DecoderFallbackBuffer buffer)
197 if (bytes == null)
198 throw new ArgumentNullException ("bytes");
199 if (chars == null)
200 throw new ArgumentNullException ("chars");
201 if (byteIndex < 0 || byteIndex > bytes.Length)
202 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
203 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
204 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
205 if (charIndex < 0 || charIndex > chars.Length)
206 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
208 if ((chars.Length - charIndex) < byteCount)
209 throw new ArgumentException (_("Arg_InsufficientSpace"));
211 int count = byteCount;
212 while (count-- > 0) {
213 char c = (char) bytes [byteIndex++];
214 if (c < '\x80')
215 chars [charIndex++] = c;
216 else {
217 if (buffer == null)
218 buffer = DecoderFallback.CreateFallbackBuffer ();
219 buffer.Fallback (bytes, byteIndex);
220 while (buffer.Remaining > 0)
221 chars [charIndex++] = buffer.GetNextChar ();
224 return byteCount;
227 // Get the maximum number of bytes needed to encode a
228 // specified number of characters.
229 public override int GetMaxByteCount (int charCount)
231 if (charCount < 0) {
232 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
234 return charCount;
237 // Get the maximum number of characters needed to decode a
238 // specified number of bytes.
239 public override int GetMaxCharCount (int byteCount)
241 if (byteCount < 0) {
242 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
244 return byteCount;
247 // Decode a buffer of bytes into a string.
248 public override String GetString (byte[] bytes, int byteIndex, int byteCount)
250 if (bytes == null) {
251 throw new ArgumentNullException ("bytes");
253 if (byteIndex < 0 || byteIndex > bytes.Length) {
254 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
256 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
257 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
259 if (byteCount == 0)
260 return String.Empty;
262 unsafe {
263 fixed (byte* bytePtr = bytes) {
264 string s = string.InternalAllocateStr (byteCount);
266 fixed (char* charPtr = s) {
267 byte* currByte = bytePtr + byteIndex;
268 byte* lastByte = currByte + byteCount;
269 char* currChar = charPtr;
271 while (currByte < lastByte) {
272 byte b = currByte++ [0];
273 currChar++ [0] = b <= 0x7F ? (char) b : (char) '?';
277 return s;
282 [CLSCompliantAttribute (false)]
283 [ComVisible (false)]
284 public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
286 if (chars == null)
287 throw new ArgumentNullException ("chars");
288 if (bytes == null)
289 throw new ArgumentNullException ("bytes");
290 if (charCount < 0)
291 throw new ArgumentOutOfRangeException ("charCount");
292 if (byteCount < 0)
293 throw new ArgumentOutOfRangeException ("byteCount");
295 if (byteCount < charCount)
296 throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
298 for (int i = 0; i < charCount; i++){
299 char c = chars [i];
300 bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
302 return charCount;
305 [CLSCompliantAttribute(false)]
306 [ComVisible (false)]
307 public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
309 if (bytes == null)
310 throw new ArgumentNullException ("bytes");
311 if (chars == null)
312 throw new ArgumentNullException ("chars");
313 if (charCount < 0)
314 throw new ArgumentOutOfRangeException ("charCount");
315 if (byteCount < 0)
316 throw new ArgumentOutOfRangeException ("byteCount");
317 if (charCount < byteCount)
318 throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
320 for (int i = 0; i < byteCount; i++){
321 byte b = bytes [i];
322 chars [i] = b > 127 ? '?' : (char) b;
324 return byteCount;
328 [CLSCompliantAttribute(false)]
329 [ComVisible (false)]
330 public unsafe override int GetCharCount (byte *bytes, int count)
332 return count;
335 [CLSCompliantAttribute(false)]
336 [ComVisible (false)]
337 public unsafe override int GetByteCount (char *chars, int count)
339 return count;
342 [ComVisible (false)]
343 public override Decoder GetDecoder ()
345 return base.GetDecoder ();
348 [ComVisible (false)]
349 public override Encoder GetEncoder ()
351 return base.GetEncoder ();
353 }; // class ASCIIEncoding
355 }; // namespace System.Text