UTF8Encoding.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // System.Text.UTF8Encoding.cs
  3. //
  4. // Authors:
  5. // Sean MacIsaac ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. //
  9. namespace System.Text
  10. {
  11. public class UTF8Encoding : Encoding
  12. {
  13. public override int GetByteCount(char[] chars, int index, int count)
  14. {
  15. // FIXME
  16. return count*6;
  17. }
  18. public override int GetBytes(char[] chars, int charIndex, int charCount,
  19. byte[] bytes, int byteIndex)
  20. {
  21. if (chars == null || bytes == null)
  22. throw new ArgumentNullException();
  23. if (charIndex < 0 || charCount < 0 || byteIndex < 0 ||
  24. charIndex + charCount > chars.Length ||
  25. byteIndex + GetByteCount(chars, charIndex, charCount) > bytes.Length)
  26. throw new ArgumentOutOfRangeException();
  27. // this is slow implementation just to get the things going
  28. int outputIndex = byteIndex;
  29. for (int i = 0; i < charCount; i++)
  30. {
  31. int charCode = (int)chars[charIndex + i];
  32. if (charCode < 0) // negative chars are invalid
  33. throw new ArgumentOutOfRangeException();
  34. if (charCode < 0x80)
  35. bytes [outputIndex++] = (byte)charCode;
  36. else
  37. if (charCode < 0x800)
  38. {
  39. bytes [outputIndex++] = (byte)((charCode >> 6) | 0xC0);
  40. bytes [outputIndex++] = (byte)((charCode & 0x3F) | 0x80);
  41. }
  42. else
  43. {
  44. // LAME: if chars[] come as UTF-16 - here we have to decode the surrogate pair, before proceeding
  45. // charCode = some magic with charCode and (int)chars[++i + charIndex] if needed
  46. if (charCode < 0x10000)
  47. {
  48. bytes [outputIndex++] = (byte)((charCode >> 12) | 0xE0);
  49. bytes [outputIndex++] = (byte)(((charCode >> 6) & 0x3F) | 0x80);
  50. bytes [outputIndex++] = (byte)((charCode & 0x3F) | 0x80);
  51. }
  52. else
  53. if (charCode < 0x200000)
  54. {
  55. bytes [outputIndex++] = (byte)((charCode >> 18) | 0xF0);
  56. bytes [outputIndex++] = (byte)(((charCode >> 12) & 0x3F) | 0x80);
  57. bytes [outputIndex++] = (byte)(((charCode >> 6) & 0x3F) | 0x80);
  58. bytes [outputIndex++] = (byte)((charCode & 0x3F) | 0x80);
  59. }
  60. else
  61. if (charCode < 0x4000000)
  62. {
  63. bytes [outputIndex++] = (byte)((charCode >> 24) | 0xF8);
  64. bytes [outputIndex++] = (byte)(((charCode >> 18) & 0x3F) | 0x80);
  65. bytes [outputIndex++] = (byte)(((charCode >> 12) & 0x3F) | 0x80);
  66. bytes [outputIndex++] = (byte)(((charCode >> 6) & 0x3F) | 0x80);
  67. bytes [outputIndex++] = (byte)((charCode & 0x3F) | 0x80);
  68. }
  69. else
  70. {
  71. bytes [outputIndex++] = (byte)((charCode >> 30) | 0xFC);
  72. bytes [outputIndex++] = (byte)(((charCode >> 24) & 0x3F) | 0x80);
  73. bytes [outputIndex++] = (byte)(((charCode >> 18) & 0x3F) | 0x80);
  74. bytes [outputIndex++] = (byte)(((charCode >> 12) & 0x3F) | 0x80);
  75. bytes [outputIndex++] = (byte)(((charCode >> 6) & 0x3F) | 0x80);
  76. bytes [outputIndex++] = (byte)((charCode & 0x3F) | 0x80);
  77. }
  78. }
  79. }
  80. return (outputIndex - byteIndex);
  81. }
  82. public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
  83. {
  84. char[] chars = s.ToCharArray(charIndex, charCount);
  85. return GetBytes(chars, 0, charCount, bytes, byteIndex);
  86. }
  87. public override byte[] GetBytes(string s)
  88. {
  89. char[] chars = s.ToCharArray();
  90. byte[] bytes = new byte[GetByteCount(chars, 0, chars.Length)];
  91. GetBytes(chars, 0, chars.Length, bytes, bytes.Length);
  92. return bytes;
  93. }
  94. public override int GetCharCount(byte[] bytes, int byteIndex, int
  95. byteCount)
  96. {
  97. int count = 0;
  98. for (int i = byteIndex; i < byteIndex + byteCount; i++)
  99. if ((bytes[i] & 0xC0) != 0x80)
  100. count++;
  101. return count;
  102. }
  103. public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
  104. char[] chars, int charIndex)
  105. {
  106. if (chars == null || bytes == null)
  107. throw new ArgumentNullException();
  108. if (charIndex < 0 || byteCount < 0 || byteIndex < 0 ||
  109. charIndex + GetCharCount(bytes, byteIndex, byteCount) > chars.Length
  110. ||
  111. byteIndex + byteCount > bytes.Length)
  112. throw new ArgumentOutOfRangeException();
  113. // FIXME
  114. return 0;
  115. }
  116. public override int GetMaxByteCount(int charCount)
  117. {
  118. return charCount*6;
  119. }
  120. public override int GetMaxCharCount(int byteCount)
  121. {
  122. return byteCount;
  123. }
  124. }
  125. }