Merge pull request #3528 from BrzVlad/fix-sgen-check-before-collections
[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         // until we change the callers:
35         internal static string _ (string arg) {
36                 return arg;
37         }
38
39         // Magic number used by Windows for the ISO Latin1 code page.
40         internal const int ISOLATIN_CODE_PAGE = 28591;
41
42         // Constructor.
43         public Latin1Encoding () : base (ISOLATIN_CODE_PAGE)
44         {
45                 // Nothing to do here.
46         }
47
48         public override bool IsSingleByte {
49                 get { return true; }
50         }
51
52         public override bool IsAlwaysNormalized (NormalizationForm form)
53         {
54                 return form == NormalizationForm.FormC;
55         }
56
57         // Get the number of bytes needed to encode a character buffer.
58         public override int GetByteCount (char[] chars, int index, int count)
59         {
60                 if (chars == null) {
61                         throw new ArgumentNullException ("chars");
62                 }
63                 if (index < 0 || index > chars.Length) {
64                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
65                 }
66                 if (count < 0 || count > (chars.Length - index)) {
67                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
68                 }
69                 return count;
70         }
71
72         // Convenience wrappers for "GetByteCount".
73         public override int GetByteCount (String s)
74         {
75                 if (s == null) {
76                         throw new ArgumentNullException ("s");
77                 }
78                 return s.Length;
79         }
80
81         // Get the bytes that result from encoding a character buffer.
82         public override int GetBytes (char[] chars, int charIndex, int charCount,
83                                                                  byte[] bytes, int byteIndex)
84         {
85                 EncoderFallbackBuffer buffer = null;
86                 char [] fallback_chars = null;
87                 return GetBytes (chars, charIndex, charCount, bytes,
88                         byteIndex, ref buffer, ref fallback_chars);
89         }
90
91         int GetBytes (char[] chars, int charIndex, int charCount,
92                       byte[] bytes, int byteIndex,
93                       ref EncoderFallbackBuffer buffer,
94                       ref char [] fallback_chars)
95         {
96                 if (chars == null)
97                         throw new ArgumentNullException ("chars");
98
99                 unsafe {
100                         fixed (char *cptr = chars) {
101                                 return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
102                         }
103                 }
104         }
105
106         // Convenience wrappers for "GetBytes".
107         public override int GetBytes (String s, int charIndex, int charCount,
108                                                                  byte[] bytes, int byteIndex)
109         {
110                 EncoderFallbackBuffer buffer = null;
111                 char [] fallback_chars = null;
112                 return GetBytes (s, charIndex, charCount, bytes, byteIndex,
113                         ref buffer, ref fallback_chars);
114         }
115
116         int GetBytes (String s, int charIndex, int charCount,
117                       byte[] bytes, int byteIndex,
118                       ref EncoderFallbackBuffer buffer,
119                       ref char [] fallback_chars)
120         {
121                 if (s == null)
122                         throw new ArgumentNullException ("s");
123
124                 unsafe {
125                         fixed (char *chars = s) {
126                                 return InternalGetBytes (chars, s.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
127                         }
128                 }
129         }
130
131         unsafe int InternalGetBytes (char *chars, int charLength, int charIndex, int charCount,
132                       byte[] bytes, int byteIndex,
133                       ref EncoderFallbackBuffer buffer,
134                       ref char [] fallback_chars)
135         {
136                 if (bytes == null)
137                         throw new ArgumentNullException ("bytes");
138                 if (charIndex < 0 || charIndex > charLength)
139                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
140                 if (charCount < 0 || charCount > (charLength - charIndex))
141                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
142                 if (byteIndex < 0 || byteIndex > bytes.Length)
143                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
144                 if ((bytes.Length - byteIndex) < charCount)
145                         throw new ArgumentException (_("Arg_InsufficientSpace"));
146
147                 int count = charCount;
148                 char ch;
149                 while (count-- > 0) {
150                         ch = chars [charIndex++];
151                         if (ch < (char)0x0100) {
152                                 bytes [byteIndex++] = (byte)ch;
153                         } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
154                                 bytes [byteIndex++] = (byte)(ch - 0xFEE0);
155                         } else {
156                                 if (buffer == null)
157                                         buffer = EncoderFallback.CreateFallbackBuffer ();
158                                 if (Char.IsSurrogate (ch) && count > 1 &&
159                                     Char.IsSurrogate (chars [charIndex]))
160                                 {
161                                         buffer.Fallback (ch, chars [charIndex], charIndex - 1);
162                                         charIndex++;
163                                         count--;
164                                 }
165                                 else
166                                         buffer.Fallback (ch, charIndex - 1);
167                                 if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
168                                         fallback_chars = new char [buffer.Remaining];
169                                 for (int i = 0; i < fallback_chars.Length; i++)
170                                         fallback_chars [i] = buffer.GetNextChar ();
171                                 byteIndex += GetBytes (fallback_chars, 0, 
172                                         fallback_chars.Length, bytes, byteIndex,
173                                         ref buffer, ref fallback_chars);
174                         }
175                 }
176                 return charCount;
177         }
178
179         // Get the number of characters needed to decode a byte buffer.
180         public override int GetCharCount (byte[] bytes, int index, int count)
181         {
182                 if (bytes == null) {
183                         throw new ArgumentNullException ("bytes");
184                 }
185                 if (index < 0 || index > bytes.Length) {
186                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
187                 }
188                 if (count < 0 || count > (bytes.Length - index)) {
189                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
190                 }
191                 return count;
192         }
193
194         // Get the characters that result from decoding a byte buffer.
195         public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
196                                                                  char[] chars, int charIndex)
197         {
198                 if (bytes == null) {
199                         throw new ArgumentNullException ("bytes");
200                 }
201                 if (chars == null) {
202                         throw new ArgumentNullException ("chars");
203                 }
204                 if (byteIndex < 0 || byteIndex > bytes.Length) {
205                         throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
206                 }
207                 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
208                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
209                 }
210                 if (charIndex < 0 || charIndex > chars.Length) {
211                         throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
212                 }
213                 if ((chars.Length - charIndex) < byteCount) {
214                         throw new ArgumentException (_("Arg_InsufficientSpace"));
215                 }
216                 int count = byteCount;
217                 while (count-- > 0) {
218                         chars [charIndex++] = (char)(bytes [byteIndex++]);
219                 }
220                 return byteCount;
221         }
222
223         // Get the maximum number of bytes needed to encode a
224         // specified number of characters.
225         public override int GetMaxByteCount (int charCount)
226         {
227                 if (charCount < 0) {
228                         throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
229                 }
230                 return charCount;
231         }
232
233         // Get the maximum number of characters needed to decode a
234         // specified number of bytes.
235         public override int GetMaxCharCount (int byteCount)
236         {
237                 if (byteCount < 0) {
238                         throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
239                 }
240                 return byteCount;
241         }
242
243         // Decode a buffer of bytes into a string.
244         public override String GetString (byte[] bytes, int index, int count)
245         {
246                 if (bytes == null) {
247                         throw new ArgumentNullException ("bytes");
248                 }
249                 if (index < 0 || index > bytes.Length) {
250                         throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
251                 }
252                 if (count < 0 || count > (bytes.Length - index)) {
253                         throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
254                 }
255                 if (count == 0)
256                     return String.Empty;
257                 unsafe {
258                         fixed (byte* bytePtr = bytes) {
259                                 string s = string.FastAllocateString (count);
260
261                                 fixed (char* charPtr = s) {
262                                         byte* currByte = bytePtr + index;
263                                         byte* lastByte = currByte + count;
264                                         char* currChar = charPtr;
265
266                                         while (currByte < lastByte)
267                                                 currChar++ [0] = (char) currByte++ [0];
268                                 }
269
270                                 return s;
271                         }
272                 }
273         }
274         public override String GetString (byte[] bytes)
275         {
276                 if (bytes == null) {
277                         throw new ArgumentNullException ("bytes");
278                 }
279
280                 return GetString (bytes, 0, bytes.Length);
281         }
282
283 #if !ECMA_COMPAT
284
285         // Get the mail body name for this encoding.
286         public override String BodyName
287         {
288                 get {
289                         return "iso-8859-1";
290                 }
291         }
292
293         // Get the human-readable name for this encoding.
294         public override String EncodingName
295         {
296                 get {
297                         return "Western European (ISO)";
298                 }
299         }
300
301         // Get the mail agent header name for this encoding.
302         public override String HeaderName
303         {
304                 get {
305                         return "iso-8859-1";
306                 }
307         }
308
309         // Determine if this encoding can be displayed in a Web browser.
310         public override bool IsBrowserDisplay
311         {
312                 get {
313                         return true;
314                 }
315         }
316
317         // Determine if this encoding can be saved from a Web browser.
318         public override bool IsBrowserSave
319         {
320                 get {
321                         return true;
322                 }
323         }
324
325         // Determine if this encoding can be displayed in a mail/news agent.
326         public override bool IsMailNewsDisplay
327         {
328                 get {
329                         return true;
330                 }
331         }
332
333         // Determine if this encoding can be saved from a mail/news agent.
334         public override bool IsMailNewsSave
335         {
336                 get {
337                         return true;
338                 }
339         }
340
341         // Get the IANA-preferred Web name for this encoding.
342         public override String WebName
343         {
344                 get {
345                         return "iso-8859-1";
346                 }
347         }
348
349 #endif // !ECMA_COMPAT
350
351 }; // class Latin1Encoding
352
353 }; // namespace System.Text