2003-02-19 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / corlib / System.Text / Latin1Encoding.cs
1 /*
2  * Latin1Encoding.cs - Implementation of the
3  *                      "System.Text.Latin1Encoding" class.
4  *
5  * Copyright (c) 2002  Southern Storm Software, Pty Ltd
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 namespace System.Text
27 {
28
29 using System;
30
31 [Serializable]
32 internal class Latin1Encoding : Encoding
33 {
34         // Magic number used by Windows for the ISO Latin1 code page.
35         internal const int ISOLATIN_CODE_PAGE = 28591;
36
37         // Constructor.
38         public Latin1Encoding () : base (ISOLATIN_CODE_PAGE)
39         {
40                 // Nothing to do here.
41         }
42
43         // Get the number of bytes needed to encode a character buffer.
44         public override int GetByteCount (char[] chars, int index, int count)
45         {
46                 if (chars == null) {
47                         throw new ArgumentNullException ("chars");
48                 }
49                 if (index < 0 || index > chars.Length) {
50                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
51                 }
52                 if (count < 0 || count > (chars.Length - index)) {
53                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
54                 }
55                 return count;
56         }
57
58         // Convenience wrappers for "GetByteCount".
59         public override int GetByteCount (String s)
60         {
61                 if (s == null) {
62                         throw new ArgumentNullException ("s");
63                 }
64                 return s.Length;
65         }
66
67         // Get the bytes that result from encoding a character buffer.
68         public override int GetBytes (char[] chars, int charIndex, int charCount,
69                                                                  byte[] bytes, int byteIndex)
70         {
71                 if (chars == null) {
72                         throw new ArgumentNullException ("chars");
73                 }
74                 if (bytes == null) {
75                         throw new ArgumentNullException ("bytes");
76                 }
77                 if (charIndex < 0 || charIndex > chars.Length) {
78                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
79                 }
80                 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
81                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
82                 }
83                 if (byteIndex < 0 || byteIndex > bytes.Length) {
84                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
85                 }
86                 if ((bytes.Length - byteIndex) < charCount) {
87                         throw new ArgumentException (_("Arg_InsufficientSpace"));
88                 }
89                 int count = charCount;
90                 char ch;
91                 while (count-- > 0) {
92                         ch = chars [charIndex++];
93                         if (ch < (char)0x0100) {
94                                 bytes [byteIndex++] = (byte)ch;
95                         } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
96                                 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
97                         } else {
98                                 bytes [byteIndex++] = (byte)'?';
99                         }
100                 }
101                 return charCount;
102         }
103
104         // Convenience wrappers for "GetBytes".
105         public override int GetBytes (String s, int charIndex, int charCount,
106                                                                  byte[] bytes, int byteIndex)
107         {
108                 if (s == null) {
109                         throw new ArgumentNullException ("s");
110                 }
111                 if (bytes == null) {
112                         throw new ArgumentNullException ("bytes");
113                 }
114                 if (charIndex < 0 || charIndex > s.Length) {
115                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
116                 }
117                 if (charCount < 0 || charCount > (s.Length - charIndex)) {
118                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
119                 }
120                 if (byteIndex < 0 || byteIndex > bytes.Length) {
121                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
122                 }
123                 if ((bytes.Length - byteIndex) < charCount) {
124                         throw new ArgumentException (_("Arg_InsufficientSpace"));
125                 }
126                 int count = charCount;
127                 char ch;
128                 while (count-- > 0) {
129                         ch = s [charIndex++];
130                         if (ch < (char)0x0100) {
131                                 bytes [byteIndex++] = (byte)ch;
132                         } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
133                                 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
134                         } else {
135                                 bytes [byteIndex++] = (byte)'?';
136                         }
137                 }
138                 return charCount;
139         }
140
141         // Get the number of characters needed to decode a byte buffer.
142         public override int GetCharCount (byte[] bytes, int index, int count)
143         {
144                 if (bytes == null) {
145                         throw new ArgumentNullException ("bytes");
146                 }
147                 if (index < 0 || index > bytes.Length) {
148                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
149                 }
150                 if (count < 0 || count > (bytes.Length - index)) {
151                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
152                 }
153                 return count;
154         }
155
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)
159         {
160                 if (bytes == null) {
161                         throw new ArgumentNullException ("bytes");
162                 }
163                 if (chars == null) {
164                         throw new ArgumentNullException ("chars");
165                 }
166                 if (byteIndex < 0 || byteIndex > bytes.Length) {
167                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
168                 }
169                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
170                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
171                 }
172                 if (charIndex < 0 || charIndex > chars.Length) {
173                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
174                 }
175                 if ((chars.Length - charIndex) < byteCount) {
176                         throw new ArgumentException (_("Arg_InsufficientSpace"));
177                 }
178                 int count = byteCount;
179                 while (count-- > 0) {
180                         chars [charIndex++] = (char)(bytes [byteIndex++]);
181                 }
182                 return byteCount;
183         }
184
185         // Get the maximum number of bytes needed to encode a
186         // specified number of characters.
187         public override int GetMaxByteCount (int charCount)
188         {
189                 if (charCount < 0) {
190                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
191                 }
192                 return charCount;
193         }
194
195         // Get the maximum number of characters needed to decode a
196         // specified number of bytes.
197         public override int GetMaxCharCount (int byteCount)
198         {
199                 if (byteCount < 0) {
200                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
201                 }
202                 return byteCount;
203         }
204
205         // Decode a buffer of bytes into a string.
206         public override String GetString (byte[] bytes, int index, int count)
207         {
208                 if (bytes == null) {
209                         throw new ArgumentNullException ("bytes");
210                 }
211                 if (index < 0 || index > bytes.Length) {
212                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
213                 }
214                 if (count < 0 || count > (bytes.Length - index)) {
215                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
216                 }
217                 unsafe {
218                         fixed (byte *ss = &bytes [0]) {
219                                 return new String ((sbyte*)ss, index, count);
220                         }
221                 }
222         }
223         public override String GetString (byte[] bytes)
224         {
225                 if (bytes == null) {
226                         throw new ArgumentNullException ("bytes");
227                 }
228                 int count = bytes.Length;
229                 unsafe {
230                         fixed (byte *ss = &bytes [0]) {
231                                 return new String ((sbyte*)ss, 0, count);
232                         }
233                 }
234         }
235
236 #if !ECMA_COMPAT
237
238         // Get the mail body name for this encoding.
239         public override String BodyName
240         {
241                 get {
242                         return "iso-8859-1";
243                 }
244         }
245
246         // Get the human-readable name for this encoding.
247         public override String EncodingName
248         {
249                 get {
250                         return "Western European (ISO)";
251                 }
252         }
253
254         // Get the mail agent header name for this encoding.
255         public override String HeaderName
256         {
257                 get {
258                         return "iso-8859-1";
259                 }
260         }
261
262         // Determine if this encoding can be displayed in a Web browser.
263         public override bool IsBrowserDisplay
264         {
265                 get {
266                         return true;
267                 }
268         }
269
270         // Determine if this encoding can be saved from a Web browser.
271         public override bool IsBrowserSave
272         {
273                 get {
274                         return true;
275                 }
276         }
277
278         // Determine if this encoding can be displayed in a mail/news agent.
279         public override bool IsMailNewsDisplay
280         {
281                 get {
282                         return true;
283                 }
284         }
285
286         // Determine if this encoding can be saved from a mail/news agent.
287         public override bool IsMailNewsSave
288         {
289                 get {
290                         return true;
291                 }
292         }
293
294         // Get the IANA-preferred Web name for this encoding.
295         public override String WebName
296         {
297                 get {
298                         return "iso-8859-1";
299                 }
300         }
301
302 #endif // !ECMA_COMPAT
303
304 }; // class Latin1Encoding
305
306 }; // namespace System.Text