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 throw new ArgumentNullException ("chars");
79 throw new ArgumentNullException ("bytes");
81 if (charIndex
< 0 || charIndex
> chars
.Length
) {
82 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
84 if (charCount
< 0 || charCount
> (chars
.Length
- charIndex
)) {
85 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
87 if (byteIndex
< 0 || byteIndex
> bytes
.Length
) {
88 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
90 if ((bytes
.Length
- byteIndex
) < charCount
) {
91 throw new ArgumentException (_("Arg_InsufficientSpace"));
93 int count
= charCount
;
96 ch
= chars
[charIndex
++];
97 if (ch
< (char)0x80) {
98 bytes
[byteIndex
++] = (byte)ch
;
100 bytes
[byteIndex
++] = (byte)'?';
106 // Convenience wrappers for "GetBytes".
107 public override int GetBytes (String s
, int charIndex
, int charCount
,
108 byte[] bytes
, int byteIndex
)
111 throw new ArgumentNullException ("s");
114 throw new ArgumentNullException ("bytes");
116 if (charIndex
< 0 || charIndex
> s
.Length
) {
117 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
119 if (charCount
< 0 || charCount
> (s
.Length
- charIndex
)) {
120 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
122 if (byteIndex
< 0 || byteIndex
> bytes
.Length
) {
123 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
125 if ((bytes
.Length
- byteIndex
) < charCount
) {
126 throw new ArgumentException (_("Arg_InsufficientSpace"));
128 int count
= charCount
;
130 while (count
-- > 0) {
131 ch
= s
[charIndex
++];
132 if (ch
< (char)0x80) {
133 bytes
[byteIndex
++] = (byte)ch
;
135 bytes
[byteIndex
++] = (byte)'?';
141 // Get the number of characters needed to decode a byte buffer.
142 public override int GetCharCount (byte[] bytes
, int index
, int count
)
145 throw new ArgumentNullException ("bytes");
147 if (index
< 0 || index
> bytes
.Length
) {
148 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
150 if (count
< 0 || count
> (bytes
.Length
- index
)) {
151 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
156 // Get the characters that result from decoding a byte buffer.
157 public override int GetChars (byte[] bytes
, int byteIndex
, int byteCount
,
158 char[] chars
, int charIndex
)
161 throw new ArgumentNullException ("bytes");
164 throw new ArgumentNullException ("chars");
166 if (byteIndex
< 0 || byteIndex
> bytes
.Length
) {
167 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
169 if (byteCount
< 0 || byteCount
> (bytes
.Length
- byteIndex
)) {
170 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
172 if (charIndex
< 0 || charIndex
> chars
.Length
) {
173 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
175 if ((chars
.Length
- charIndex
) < byteCount
) {
176 throw new ArgumentException (_("Arg_InsufficientSpace"));
178 int count
= byteCount
;
179 while (count
-- > 0) {
180 char c
= (char)(bytes
[byteIndex
++]);
183 chars
[charIndex
++] = c
;
188 // Get the maximum number of bytes needed to encode a
189 // specified number of characters.
190 public override int GetMaxByteCount (int charCount
)
193 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
198 // Get the maximum number of characters needed to decode a
199 // specified number of bytes.
200 public override int GetMaxCharCount (int byteCount
)
203 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
208 // Decode a buffer of bytes into a string.
209 public override String
GetString (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 fixed (byte *ss
= &bytes
[0]) {
224 return new String ((sbyte*)ss
, index
, count
);
228 public override String
GetString (byte[] bytes
)
231 throw new ArgumentNullException ("bytes");
233 int count
= bytes
.Length
;
237 fixed (byte *ss
= &bytes
[0]) {
238 return new String ((sbyte*)ss
, 0, count
);
243 }; // class ASCIIEncoding
245 }; // namespace System.Text