**** Merged from MCS ****
[mono-project.git] / mcs / class / corlib / System.Text / ASCIIEncoding.cs
blob89201ae2cc31b4fe4ac1e3e4a34032f679a66d03
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;
32 [Serializable]
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;
39 // Constructor.
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)
50 if (chars == null) {
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"));
59 return count;
62 // Convenience wrappers for "GetByteCount".
63 public override int GetByteCount (String s)
65 if (s == null) {
66 throw new ArgumentNullException ("s");
68 return s.Length;
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)
75 if (chars == null) {
76 throw new ArgumentNullException ("chars");
78 if (bytes == null) {
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;
94 char ch;
95 while (count-- > 0) {
96 ch = chars [charIndex++];
97 if (ch < (char)0x80) {
98 bytes [byteIndex++] = (byte)ch;
99 } else {
100 bytes [byteIndex++] = (byte)'?';
103 return charCount;
106 // Convenience wrappers for "GetBytes".
107 public override int GetBytes (String s, int charIndex, int charCount,
108 byte[] bytes, int byteIndex)
110 if (s == null) {
111 throw new ArgumentNullException ("s");
113 if (bytes == null) {
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;
129 char ch;
130 while (count-- > 0) {
131 ch = s [charIndex++];
132 if (ch < (char)0x80) {
133 bytes [byteIndex++] = (byte)ch;
134 } else {
135 bytes [byteIndex++] = (byte)'?';
138 return charCount;
141 // Get the number of characters needed to decode a byte buffer.
142 public override int GetCharCount (byte[] bytes, int index, int count)
144 if (bytes == null) {
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"));
153 return count;
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)
160 if (bytes == null) {
161 throw new ArgumentNullException ("bytes");
163 if (chars == null) {
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++]);
181 if (c > 127)
182 c = '?';
183 chars [charIndex++] = c;
185 return byteCount;
188 // Get the maximum number of bytes needed to encode a
189 // specified number of characters.
190 public override int GetMaxByteCount (int charCount)
192 if (charCount < 0) {
193 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
195 return charCount;
198 // Get the maximum number of characters needed to decode a
199 // specified number of bytes.
200 public override int GetMaxCharCount (int byteCount)
202 if (byteCount < 0) {
203 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
205 return byteCount;
208 // Decode a buffer of bytes into a string.
209 public override String GetString (byte[] bytes, int index, int count)
211 if (bytes == null) {
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"));
220 if (count == 0)
221 return String.Empty;
222 unsafe {
223 fixed (byte *ss = &bytes [0]) {
224 return new String ((sbyte*)ss, index, count);
228 public override String GetString (byte[] bytes)
230 if (bytes == null) {
231 throw new ArgumentNullException ("bytes");
233 int count = bytes.Length;
234 if (count == 0)
235 return String.Empty;
236 unsafe {
237 fixed (byte *ss = &bytes [0]) {
238 return new String ((sbyte*)ss, 0, count);
243 }; // class ASCIIEncoding
245 }; // namespace System.Text