ASCIIEncoding.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * ASCIIEncoding.cs - Implementation of the "System.Text.ASCIIEncoding" class.
  3. *
  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)
  7. *
  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:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  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.
  25. */
  26. namespace System.Text
  27. {
  28. using System;
  29. [Serializable]
  30. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  31. public class ASCIIEncoding : Encoding
  32. {
  33. // Magic number used by Windows for "ASCII".
  34. internal const int ASCII_CODE_PAGE = 20127;
  35. // Constructor.
  36. public ASCIIEncoding () : base(ASCII_CODE_PAGE) {
  37. body_name = header_name = web_name= "us-ascii";
  38. encoding_name = "US-ASCII";
  39. is_mail_news_display = true;
  40. is_mail_news_save = true;
  41. }
  42. // Get the number of bytes needed to encode a character buffer.
  43. public override int GetByteCount (char[] chars, int index, int count)
  44. {
  45. if (chars == null) {
  46. throw new ArgumentNullException ("chars");
  47. }
  48. if (index < 0 || index > chars.Length) {
  49. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  50. }
  51. if (count < 0 || count > (chars.Length - index)) {
  52. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  53. }
  54. return count;
  55. }
  56. // Convenience wrappers for "GetByteCount".
  57. public override int GetByteCount (String s)
  58. {
  59. if (s == null) {
  60. throw new ArgumentNullException ("s");
  61. }
  62. return s.Length;
  63. }
  64. // Get the bytes that result from encoding a character buffer.
  65. public override int GetBytes (char[] chars, int charIndex, int charCount,
  66. byte[] bytes, int byteIndex)
  67. {
  68. if (chars == null) {
  69. throw new ArgumentNullException ("chars");
  70. }
  71. if (bytes == null) {
  72. throw new ArgumentNullException ("bytes");
  73. }
  74. if (charIndex < 0 || charIndex > chars.Length) {
  75. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  76. }
  77. if (charCount < 0 || charCount > (chars.Length - charIndex)) {
  78. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  79. }
  80. if (byteIndex < 0 || byteIndex > bytes.Length) {
  81. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  82. }
  83. if ((bytes.Length - byteIndex) < charCount) {
  84. throw new ArgumentException (_("Arg_InsufficientSpace"));
  85. }
  86. int count = charCount;
  87. char ch;
  88. while (count-- > 0) {
  89. ch = chars [charIndex++];
  90. if (ch < (char)0x80) {
  91. bytes [byteIndex++] = (byte)ch;
  92. } else {
  93. bytes [byteIndex++] = (byte)'?';
  94. }
  95. }
  96. return charCount;
  97. }
  98. // Convenience wrappers for "GetBytes".
  99. public override int GetBytes (String s, int charIndex, int charCount,
  100. byte[] bytes, int byteIndex)
  101. {
  102. if (s == null) {
  103. throw new ArgumentNullException ("s");
  104. }
  105. if (bytes == null) {
  106. throw new ArgumentNullException ("bytes");
  107. }
  108. if (charIndex < 0 || charIndex > s.Length) {
  109. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
  110. }
  111. if (charCount < 0 || charCount > (s.Length - charIndex)) {
  112. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
  113. }
  114. if (byteIndex < 0 || byteIndex > bytes.Length) {
  115. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  116. }
  117. if ((bytes.Length - byteIndex) < charCount) {
  118. throw new ArgumentException (_("Arg_InsufficientSpace"));
  119. }
  120. int count = charCount;
  121. char ch;
  122. while (count-- > 0) {
  123. ch = s [charIndex++];
  124. if (ch < (char)0x80) {
  125. bytes [byteIndex++] = (byte)ch;
  126. } else {
  127. bytes [byteIndex++] = (byte)'?';
  128. }
  129. }
  130. return charCount;
  131. }
  132. // Get the number of characters needed to decode a byte buffer.
  133. public override int GetCharCount (byte[] bytes, int index, int count)
  134. {
  135. if (bytes == null) {
  136. throw new ArgumentNullException ("bytes");
  137. }
  138. if (index < 0 || index > bytes.Length) {
  139. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  140. }
  141. if (count < 0 || count > (bytes.Length - index)) {
  142. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  143. }
  144. return count;
  145. }
  146. // Get the characters that result from decoding a byte buffer.
  147. public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
  148. char[] chars, int charIndex)
  149. {
  150. if (bytes == null) {
  151. throw new ArgumentNullException ("bytes");
  152. }
  153. if (chars == null) {
  154. throw new ArgumentNullException ("chars");
  155. }
  156. if (byteIndex < 0 || byteIndex > bytes.Length) {
  157. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  158. }
  159. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  160. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  161. }
  162. if (charIndex < 0 || charIndex > chars.Length) {
  163. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  164. }
  165. if ((chars.Length - charIndex) < byteCount) {
  166. throw new ArgumentException (_("Arg_InsufficientSpace"));
  167. }
  168. int count = byteCount;
  169. while (count-- > 0) {
  170. char c = (char)(bytes [byteIndex++]);
  171. if (c > 127)
  172. c = '?';
  173. chars [charIndex++] = c;
  174. }
  175. return byteCount;
  176. }
  177. // Get the maximum number of bytes needed to encode a
  178. // specified number of characters.
  179. public override int GetMaxByteCount (int charCount)
  180. {
  181. if (charCount < 0) {
  182. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  183. }
  184. return charCount;
  185. }
  186. // Get the maximum number of characters needed to decode a
  187. // specified number of bytes.
  188. public override int GetMaxCharCount (int byteCount)
  189. {
  190. if (byteCount < 0) {
  191. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
  192. }
  193. return byteCount;
  194. }
  195. // Decode a buffer of bytes into a string.
  196. public override String GetString (byte[] bytes, int index, int count)
  197. {
  198. if (bytes == null) {
  199. throw new ArgumentNullException ("bytes");
  200. }
  201. if (index < 0 || index > bytes.Length) {
  202. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  203. }
  204. if (count < 0 || count > (bytes.Length - index)) {
  205. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  206. }
  207. if (count == 0)
  208. return String.Empty;
  209. unsafe {
  210. fixed (byte *ss = &bytes [0]) {
  211. return new String ((sbyte*)ss, index, count);
  212. }
  213. }
  214. }
  215. public override String GetString (byte[] bytes)
  216. {
  217. if (bytes == null) {
  218. throw new ArgumentNullException ("bytes");
  219. }
  220. int count = bytes.Length;
  221. if (count == 0)
  222. return String.Empty;
  223. unsafe {
  224. fixed (byte *ss = &bytes [0]) {
  225. return new String ((sbyte*)ss, 0, count);
  226. }
  227. }
  228. }
  229. }; // class ASCIIEncoding
  230. }; // namespace System.Text