This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[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                 if (count == 0)
218                     return String.Empty;
219                 unsafe {
220                         fixed (byte *ss = &bytes [0]) {
221                                 return new String ((sbyte*)ss, index, count);
222                         }
223                 }
224         }
225         public override String GetString (byte[] bytes)
226         {
227                 if (bytes == null) {
228                         throw new ArgumentNullException ("bytes");
229                 }
230                 int count = bytes.Length;
231                 if (count == 0)
232                     return String.Empty;
233                 unsafe {
234                         fixed (byte *ss = &bytes [0]) {
235                                 return new String ((sbyte*)ss, 0, count);
236                         }
237                 }
238         }
239
240 #if !ECMA_COMPAT
241
242         // Get the mail body name for this encoding.
243         public override String BodyName
244         {
245                 get {
246                         return "iso-8859-1";
247                 }
248         }
249
250         // Get the human-readable name for this encoding.
251         public override String EncodingName
252         {
253                 get {
254                         return "Western European (ISO)";
255                 }
256         }
257
258         // Get the mail agent header name for this encoding.
259         public override String HeaderName
260         {
261                 get {
262                         return "iso-8859-1";
263                 }
264         }
265
266         // Determine if this encoding can be displayed in a Web browser.
267         public override bool IsBrowserDisplay
268         {
269                 get {
270                         return true;
271                 }
272         }
273
274         // Determine if this encoding can be saved from a Web browser.
275         public override bool IsBrowserSave
276         {
277                 get {
278                         return true;
279                 }
280         }
281
282         // Determine if this encoding can be displayed in a mail/news agent.
283         public override bool IsMailNewsDisplay
284         {
285                 get {
286                         return true;
287                 }
288         }
289
290         // Determine if this encoding can be saved from a mail/news agent.
291         public override bool IsMailNewsSave
292         {
293                 get {
294                         return true;
295                 }
296         }
297
298         // Get the IANA-preferred Web name for this encoding.
299         public override String WebName
300         {
301                 get {
302                         return "iso-8859-1";
303                 }
304         }
305
306 #endif // !ECMA_COMPAT
307
308 }; // class Latin1Encoding
309
310 }; // namespace System.Text