ASCIIEncoding.cs 8.7 KB

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