ASCIIEncoding.cs 7.3 KB

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