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 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
)
95 throw new ArgumentNullException ("chars");
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
)
119 throw new ArgumentNullException ("chars");
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
)
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
;
146 while (count
-- > 0) {
147 ch
= chars
[charIndex
++];
148 if (ch
< (char)0x80) {
149 bytes
[byteIndex
++] = (byte)ch
;
152 buffer
= EncoderFallback
.CreateFallbackBuffer ();
153 if (Char
.IsSurrogate (ch
) && count
> 1 &&
154 Char
.IsSurrogate (chars
[charIndex
]))
155 buffer
.Fallback (ch
, chars
[charIndex
], charIndex
++ - 1);
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
);
170 // Get the number of characters needed to decode a byte buffer.
171 public override int GetCharCount (byte[] bytes
, int index
, int count
)
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"));
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
)
198 throw new ArgumentNullException ("bytes");
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
++];
215 chars
[charIndex
++] = c
;
218 buffer
= DecoderFallback
.CreateFallbackBuffer ();
219 buffer
.Fallback (bytes
, byteIndex
);
220 while (buffer
.Remaining
> 0)
221 chars
[charIndex
++] = buffer
.GetNextChar ();
227 // Get the maximum number of bytes needed to encode a
228 // specified number of characters.
229 public override int GetMaxByteCount (int charCount
)
232 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
237 // Get the maximum number of characters needed to decode a
238 // specified number of bytes.
239 public override int GetMaxCharCount (int byteCount
)
242 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
247 // Decode a buffer of bytes into a string.
248 public override String
GetString (byte[] bytes
, int byteIndex
, int byteCount
)
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"));
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) '?';
282 [CLSCompliantAttribute (false)]
284 public unsafe override int GetBytes (char *chars
, int charCount
, byte *bytes
, int byteCount
)
287 throw new ArgumentNullException ("chars");
289 throw new ArgumentNullException ("bytes");
291 throw new ArgumentOutOfRangeException ("charCount");
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
++){
300 bytes
[i
] = (byte) ((c
< (char) 0x80) ? c
: '?');
305 [CLSCompliantAttribute(false)]
307 public unsafe override int GetChars (byte *bytes
, int byteCount
, char *chars
, int charCount
)
310 throw new ArgumentNullException ("bytes");
312 throw new ArgumentNullException ("chars");
314 throw new ArgumentOutOfRangeException ("charCount");
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
++){
322 chars
[i
] = b
> 127 ? '?' : (char) b
;
328 [CLSCompliantAttribute(false)]
330 public unsafe override int GetCharCount (byte *bytes
, int count
)
335 [CLSCompliantAttribute(false)]
337 public unsafe override int GetByteCount (char *chars
, int count
)
343 public override Decoder
GetDecoder ()
345 return base.GetDecoder ();
349 public override Encoder
GetEncoder ()
351 return base.GetEncoder ();
353 }; // class ASCIIEncoding
355 }; // namespace System.Text