ASCIIEncoding.cs 7.8 KB

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