2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / I18N / Other / CP57002.cs
blobb449c6d34dc68ef71a3b45b0f13e0c338bca8181
1 /*
2 * CP57002.cs - ISCII code pages 57002-57011.
4 * Atsushi Enomoto <atsushi@ximian.com> (C) 2005 Novell, Inc.
6 * original copyright:
8 * Copyright (c) 2002 Southern Storm Software, Pty Ltd
10 * Permission is hereby granted, free of charge, to any person obtaining
11 * a copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 * OTHER DEALINGS IN THE SOFTWARE.
29 namespace I18N.Other
32 using System;
33 using System.Text;
34 using I18N.Common;
36 // This class provides an abstract base for the ISCII encodings,
37 // which all have a similar pattern. Code points 0x00-0x7F are
38 // the standard ASCII character set, and code points 0x80-0xFF
39 // are a shifted version of the Unicode character set, starting
40 // at a fixed offset.
42 [Serializable]
43 public abstract class ISCIIEncoding : MonoEncoding
45 // Internal state.
46 int shift;
47 string encodingName;
48 string webName;
50 // Constructor.
51 protected ISCIIEncoding(int codePage, int shift,
52 String encodingName, String webName)
53 : base(codePage)
55 this.shift = shift;
56 this.encodingName = encodingName;
57 this.webName = webName;
60 // Get the number of bytes needed to encode a character buffer.
61 public override int GetByteCount(char[] chars, int index, int count)
63 if(chars == null)
65 throw new ArgumentNullException("chars");
67 if(index < 0 || index > chars.Length)
69 throw new ArgumentOutOfRangeException
70 ("index", Strings.GetString("ArgRange_Array"));
72 if(count < 0 || count > (chars.Length - index))
74 throw new ArgumentOutOfRangeException
75 ("count", Strings.GetString("ArgRange_Array"));
77 return count;
80 // Convenience wrappers for "GetByteCount".
81 public override int GetByteCount(String s)
83 if(s == null)
85 throw new ArgumentNullException("s");
87 return s.Length;
90 public unsafe override int GetByteCountImpl (char* chars, int count)
92 #if NET_2_0
93 int index = 0;
94 int length = 0;
95 char ch;
96 char first = (char)shift;
97 char last = (char)(shift + 0x7F);
98 while(count-- > 0)
100 ch = chars[index++];
101 if(ch < (char)0x0080)
103 // Regular ASCII subset.
104 length++;
106 else if(ch >= first && ch <= last)
108 // ISCII range that we need to shift.
109 length++;
111 else if(ch >= '\uFF01' && ch <= '\uFF5E')
113 // ASCII full-width characters.
114 length++;
116 else
118 // FIXME: implement fallback support for GetByteCountImpl().
119 length++;
121 count--;
124 // Return the final length of the output.
125 return length;
126 #else
127 return count;
128 #endif
131 public unsafe override int GetBytesImpl (char* chars, int charCount, byte* bytes, int byteCount)
133 #if NET_2_0
134 EncoderFallbackBuffer buffer = null;
135 #endif
136 int charIndex = 0;
137 int byteIndex = 0;
139 if(chars == null)
141 throw new ArgumentNullException("chars");
143 if(bytes == null)
145 throw new ArgumentNullException("bytes");
148 // Convert the characters into bytes.
149 char ch;
150 int posn = byteIndex;
151 char first = (char)shift;
152 char last = (char)(shift + 0x7F);
153 while(charCount-- > 0)
155 ch = chars[charIndex++];
156 if(ch < (char)0x0080)
158 // Regular ASCII subset.
159 bytes[posn++] = (byte)ch;
161 else if(ch >= first && ch <= last)
163 // ISCII range that we need to shift.
164 bytes[posn++] = (byte)(ch - first + 0x80);
166 else if(ch >= '\uFF01' && ch <= '\uFF5E')
168 // ASCII full-width characters.
169 bytes[posn++] = (byte)(ch - 0xFEE0);
171 else
173 #if NET_2_0
174 HandleFallback (ref buffer, chars, ref charIndex, ref charCount, bytes, ref posn, ref byteCount);
175 continue;
176 #else
177 bytes[posn++] = (byte)'?';
178 #endif
180 byteCount--;
183 // Return the final length of the output.
184 return posn - byteIndex;
188 // Convenience wrappers for "GetBytes".
189 public override int GetBytes(String s, int charIndex, int charCount,
190 byte[] bytes, int byteIndex)
192 // Validate the parameters.
193 if(s == null)
195 throw new ArgumentNullException("s");
197 if(bytes == null)
199 throw new ArgumentNullException("bytes");
201 if(charIndex < 0 || charIndex > s.Length)
203 throw new ArgumentOutOfRangeException
204 ("charIndex",
205 Strings.GetString("ArgRange_StringIndex"));
207 if(charCount < 0 || charCount > (s.Length - charIndex))
209 throw new ArgumentOutOfRangeException
210 ("charCount",
211 Strings.GetString("ArgRange_StringRange"));
213 if(byteIndex < 0 || byteIndex > bytes.Length)
215 throw new ArgumentOutOfRangeException
216 ("byteIndex",
217 Strings.GetString("ArgRange_Array"));
219 if((bytes.Length - byteIndex) < charCount)
221 throw new ArgumentException
222 (Strings.GetString("Arg_InsufficientSpace"), "bytes");
225 // Convert the characters into bytes.
226 char ch;
227 int posn = byteIndex;
228 char first = (char)shift;
229 char last = (char)(shift + 0x7F);
230 while(charCount-- > 0)
232 ch = s[charIndex++];
233 if(ch < (char)0x0080)
235 // Regular ASCII subset.
236 bytes[posn++] = (byte)ch;
238 else if(ch >= first && ch <= last)
240 // ISCII range that we need to shift.
241 bytes[posn++] = (byte)(ch - first + 0x80);
243 else if(ch >= '\uFF01' && ch <= '\uFF5E')
245 // ASCII full-width characters.
246 bytes[posn++] = (byte)(ch - 0xFEE0);
248 else
250 bytes[posn++] = (byte)'?';
254 // Return the final length of the output.
255 return posn - byteIndex;
259 // Get the number of characters needed to decode a byte buffer.
260 public override int GetCharCount(byte[] bytes, int index, int count)
262 if(bytes == null)
264 throw new ArgumentNullException("bytes");
266 if(index < 0 || index > bytes.Length)
268 throw new ArgumentOutOfRangeException
269 ("index", Strings.GetString("ArgRange_Array"));
271 if(count < 0 || count > (bytes.Length - index))
273 throw new ArgumentOutOfRangeException
274 ("count", Strings.GetString("ArgRange_Array"));
276 return count;
279 // Get the characters that result from decoding a byte buffer.
280 public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
281 char[] chars, int charIndex)
283 // Validate the parameters.
284 if(bytes == null)
286 throw new ArgumentNullException("bytes");
288 if(chars == null)
290 throw new ArgumentNullException("chars");
292 if(byteIndex < 0 || byteIndex > bytes.Length)
294 throw new ArgumentOutOfRangeException
295 ("byteIndex", Strings.GetString("ArgRange_Array"));
297 if(byteCount < 0 || byteCount > (bytes.Length - byteIndex))
299 throw new ArgumentOutOfRangeException
300 ("byteCount", Strings.GetString("ArgRange_Array"));
302 if(charIndex < 0 || charIndex > chars.Length)
304 throw new ArgumentOutOfRangeException
305 ("charIndex", Strings.GetString("ArgRange_Array"));
307 if((chars.Length - charIndex) < byteCount)
309 throw new ArgumentException
310 (Strings.GetString("Arg_InsufficientSpace"), "chars");
313 // Convert the bytes into characters.
314 int count = byteCount;
315 int byteval;
316 int shift = this.shift - 0x80;
317 while(count-- > 0)
319 byteval = (int)(bytes[byteIndex++]);
320 if(byteval < 0x80)
322 // Ordinary ASCII character.
323 chars[charIndex++] = (char)byteval;
325 else
327 // Shift the ISCII character into the Unicode page.
328 chars[charIndex++] = (char)(byteval + shift);
331 return byteCount;
334 // Get the maximum number of bytes needed to encode a
335 // specified number of characters.
336 public override int GetMaxByteCount(int charCount)
338 if(charCount < 0)
340 throw new ArgumentOutOfRangeException
341 ("charCount",
342 Strings.GetString("ArgRange_NonNegative"));
344 return charCount;
347 // Get the maximum number of characters needed to decode a
348 // specified number of bytes.
349 public override int GetMaxCharCount(int byteCount)
351 if(byteCount < 0)
353 throw new ArgumentOutOfRangeException
354 ("byteCount",
355 Strings.GetString("ArgRange_NonNegative"));
357 return byteCount;
360 #if !ECMA_COMPAT
362 // Get the mail body name for this encoding.
363 public override String BodyName
367 return webName;
371 // Get the human-readable name for this encoding.
372 public override String EncodingName
376 return encodingName;
380 // Get the mail agent header name for this encoding.
381 public override String HeaderName
385 return webName;
389 // Get the IANA-preferred Web name for this encoding.
390 public override String WebName
394 return webName;
398 #endif // !ECMA_COMPAT
400 }; // class ISCIIEncoding
402 // Define the ISCII code pages as subclasses of "ISCIIEncoding".
404 [Serializable]
405 public class CP57002 : ISCIIEncoding
407 public CP57002() : base(57002, 0x0900, "ISCII Devanagari", "x-iscii-de") {}
409 }; // class CP57002
411 [Serializable]
412 public class CP57003 : ISCIIEncoding
414 public CP57003() : base(57003, 0x0980, "ISCII Bengali", "x-iscii-be") {}
416 }; // class CP57003
418 [Serializable]
419 public class CP57004 : ISCIIEncoding
421 public CP57004() : base(57004, 0x0B80, "ISCII Tamil", "x-iscii-ta") {}
423 }; // class CP57004
425 [Serializable]
426 public class CP57005 : ISCIIEncoding
428 public CP57005() : base(57005, 0x0B80, "ISCII Telugu", "x-iscii-te") {}
430 }; // class CP57005
432 [Serializable]
433 public class CP57006 : ISCIIEncoding
435 // Note: Unicode has a "Sinhala" page, but no "Assamese" page.
436 // Until I hear otherwise, I will assume that they are the same
437 // thing with different names - Rhys Weatherley, 16 April 2002.
438 public CP57006() : base(57006, 0x0D80, "ISCII Assamese", "x-iscii-as") {}
440 }; // class CP57006
442 [Serializable]
443 public class CP57007 : ISCIIEncoding
445 public CP57007() : base(57007, 0x0B00, "ISCII Oriya", "x-iscii-or") {}
447 }; // class CP57007
449 [Serializable]
450 public class CP57008 : ISCIIEncoding
452 public CP57008() : base(57008, 0x0C80, "ISCII Kannada", "x-iscii-ka") {}
454 }; // class CP57008
456 [Serializable]
457 public class CP57009 : ISCIIEncoding
459 public CP57009() : base(57009, 0x0D00, "ISCII Malayalam", "x-iscii-ma") {}
461 }; // class CP57009
463 [Serializable]
464 public class CP57010 : ISCIIEncoding
466 public CP57010() : base(57010, 0x0A80, "ISCII Gujarati", "x-iscii-gu") {}
468 }; // class CP57010
470 [Serializable]
471 public class CP57011 : ISCIIEncoding
473 // Note: Unicode has a "Gurmukhi" page, but no "Punjabi" page.
474 // Other ISCII-related information on the Internet seems to
475 // indicate that they are the same. Until I hear otherwise,
476 // I will assume that they are the same thing with different
477 // names - Rhys Weatherley, 16 April 2002.
478 public CP57011() : base(57011, 0x0A00, "ISCII Punjabi", "x-iscii-pa") {}
480 }; // class CP57011
482 // Define the web encoding name aliases for the above code pages.
484 [Serializable]
485 public class ENCx_iscii_de : CP57002
487 public ENCx_iscii_de() : base() {}
489 }; // class ENCx_iscii_de
491 [Serializable]
492 public class ENCx_iscii_be : CP57003
494 public ENCx_iscii_be() : base() {}
496 }; // class ENCx_iscii_be
498 [Serializable]
499 public class ENCx_iscii_ta : CP57004
501 public ENCx_iscii_ta() : base() {}
503 }; // class ENCx_iscii_ta
505 [Serializable]
506 public class ENCx_iscii_te : CP57005
508 public ENCx_iscii_te() : base() {}
510 }; // class ENCx_iscii_te
512 [Serializable]
513 public class ENCx_iscii_as : CP57006
515 public ENCx_iscii_as() : base() {}
517 }; // class ENCx_iscii_as
519 [Serializable]
520 public class ENCx_iscii_or : CP57007
522 public ENCx_iscii_or() : base() {}
524 }; // class ENCx_iscii_or
526 [Serializable]
527 public class ENCx_iscii_ka : CP57008
529 public ENCx_iscii_ka() : base() {}
531 }; // class ENCx_iscii_ka
533 [Serializable]
534 public class ENCx_iscii_ma : CP57009
536 public ENCx_iscii_ma() : base() {}
538 }; // class ENCx_iscii_ma
540 [Serializable]
541 public class ENCx_iscii_gu : CP57010
543 public ENCx_iscii_gu() : base() {}
545 }; // class ENCx_iscii_gu
547 [Serializable]
548 public class ENCx_iscii_pa : CP57011
550 public ENCx_iscii_pa() : base() {}
552 }; // class ENCx_iscii_pa
554 }; // namespace I18N.Other