2006-02-02 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / corlib / System.Text / UTF8Encoding.cs
index 6e165e5ba22d846547ce00498e03daa9f374f299..80fc8808b7b80106a3c9bf930dd49bcf394f8a3e 100644 (file)
@@ -70,9 +70,11 @@ public class UTF8Encoding : Encoding
                windows_code_page = UnicodeEncoding.UNICODE_CODE_PAGE;
        }
 
+       #region GetByteCount()
+
        // Internal version of "GetByteCount" which can handle a rolling
        // state between multiple calls to this method.
-       private static int InternalGetByteCount (char[] chars, int index, int count, uint leftOver, bool flush)
+       private static int InternalGetByteCount (char[] chars, int index, int count, ref char leftOver, bool flush)
        {
                // Validate the parameters.
                if (chars == null) {
@@ -85,10 +87,31 @@ public class UTF8Encoding : Encoding
                        throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
                }
 
+               if (index == chars.Length) {
+                       if (flush && leftOver != '\0') {
+                               // Flush the left-over surrogate pair start.
+                               leftOver = '\0';
+                               return 3;
+                       }
+                       return 0;
+               }
+
+               unsafe {
+                       fixed (char* cptr = chars) {
+                               return InternalGetByteCount (cptr + index, count, ref leftOver, flush);
+                       }
+               }
+       }
+
+
+       private unsafe static int InternalGetByteCount (char* chars, int count, ref char leftOver, bool flush)
+       {
+               int index = 0;
+
                // Determine the lengths of all characters.
                char ch;
                int length = 0;
-               uint pair = leftOver;
+               char pair = leftOver;
                while (count > 0) {
                        ch = chars[index];
                        if (pair == 0) {
@@ -98,14 +121,23 @@ public class UTF8Encoding : Encoding
                                        length += 2;
                                } else if (ch >= '\uD800' && ch <= '\uDBFF') {
                                        // This is the start of a surrogate pair.
-                                       pair = (uint)ch;
+                                       pair = ch;
                                } else {
                                        length += 3;
                                }
                        } else if (ch >= '\uDC00' && ch <= '\uDFFF') {
-                               // We have a surrogate pair.
-                               length += 4;
-                               pair = 0;
+                               if (pair != 0) {
+                                       // We have a surrogate pair.
+                                       length += 4;
+                                       pair = '\0';
+                               } else {
+                                       // We have a surrogate tail without 
+                                       // leading surrogate. In NET_2_0 it
+                                       // uses fallback. In NET_1_1 we output
+                                       // wrong surrogate.
+                                       length += 3;
+                                       pair = '\0';
+                               }
                        } else {
                                // We have a surrogate start followed by a
                                // regular character.  Technically, this is
@@ -113,16 +145,20 @@ public class UTF8Encoding : Encoding
                                // We write out the surrogate start and then
                                // re-visit the current character again.
                                length += 3;
-                               pair = 0;
+                               pair = '\0';
                                continue;
                        }
                        ++index;
                        --count;
                }
-               if (flush && pair != 0) {
-                       // Flush the left-over surrogate pair start.
-                       length += 3;
+               if (flush) {
+                       if (pair != '\0')
+                               // Flush the left-over surrogate pair start.
+                               length += 3;
+                       leftOver = '\0';
                }
+               else
+                       leftOver = pair;
 
                // Return the final length to the caller.
                return length;
@@ -131,7 +167,8 @@ public class UTF8Encoding : Encoding
        // Get the number of bytes needed to encode a character buffer.
        public override int GetByteCount (char[] chars, int index, int count)
        {
-               return InternalGetByteCount (chars, index, count, 0, true);
+               char dummy = '\0';
+               return InternalGetByteCount (chars, index, count, ref dummy, true);
        }
 
        // Convenience wrappers for "GetByteCount".
@@ -142,43 +179,23 @@ public class UTF8Encoding : Encoding
                        throw new ArgumentNullException ("s");
                }
 
-               // Determine the lengths of all characters.
-               char ch;
-               int index = 0;
-               int count = s.Length;
-               int length = 0;
-               uint pair;
-               while (count > 0) {
-                       ch = s[index++];
-                       if (ch < '\u0080') {
-                               ++length;
-                       } else if (ch < '\u0800') {
-                               length += 2;
-                       } else if (ch >= '\uD800' && ch <= '\uDBFF' && count > 1) {
-                               // This may be the start of a surrogate pair.
-                               pair = (uint)(s[index]);
-                               if (pair >= (uint)0xDC00 && pair <= (uint)0xDFFF) {
-                                       length += 4;
-                                       ++index;
-                                       --count;
-                               } else {
-                                       length += 3;
-                               }
-                       } else {
-                               length += 3;
+               unsafe {
+                       fixed (char* cptr = s) {
+                               char dummy = '\0';
+                               return InternalGetByteCount (cptr, s.Length, ref dummy, true);
                        }
-                       --count;
                }
-
-               // Return the final length to the caller.
-               return length;
        }
 
+       #endregion
+
+       #region GetBytes()
+
        // Internal version of "GetBytes" which can handle a rolling
        // state between multiple calls to this method.
        private static int InternalGetBytes (char[] chars, int charIndex,
                                             int charCount, byte[] bytes,
-                                            int byteIndex, ref uint leftOver,
+                                            int byteIndex, ref char leftOver,
                                             bool flush)
        {
                // Validate the parameters.
@@ -198,93 +215,148 @@ public class UTF8Encoding : Encoding
                        throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
                }
 
+               if (charIndex == chars.Length) {
+                       if (flush && leftOver != 0) {
+                               // Flush the left-over surrogate pair start.
+                               bytes [byteIndex++] = 0xEF;
+                               bytes [byteIndex++] = 0xBB;
+                               bytes [byteIndex++] = 0xBF;
+                               leftOver = '\0';
+                               return 3;
+                       }
+                       return 0;
+               }
+
+               unsafe {
+                       fixed (char* cptr = chars) {
+                               fixed (byte *bptr = bytes) {
+                                       return InternalGetBytes (
+                                               cptr + charIndex, charCount,
+                                               bptr + byteIndex, bytes.Length - byteIndex,
+                                               ref leftOver, flush);
+                               }
+                       }
+               }
+       }
+
+       private unsafe static int InternalGetBytes (char* chars, int charCount,
+                                            byte* bytes, int byteCount,
+                                            ref char leftOver, bool flush)
+       {
+               int charIndex = 0;
+               int byteIndex = 0;
+
+               // Convert the characters into bytes.
                // Convert the characters into bytes.
                char ch;
-               int length = bytes.Length;
-               uint pair;
-               uint left = leftOver;
+               int length = byteCount;
+               char pair = leftOver;
                int posn = byteIndex;
+               int code = 0;
+
                while (charCount > 0) {
                        // Fetch the next UTF-16 character pair value.
-                       ch = chars[charIndex++];
-                       --charCount;
-                       if (left == 0) {
-                               if (ch >= '\uD800' && ch <= '\uDBFF') {
-                                       // This is the start of a surrogate pair.
-                                       left = (uint)ch;
+                       ch = chars [charIndex];
+                       if (pair == '\0') {
+                               if (ch < '\uD800' || ch >= '\uE000')
+                                       code = ch;
+                               else if (ch < '\uDC00') {
+                                       // surrogate start
+                                       pair = ch;
+                                       ++charIndex;
+                                       --charCount;
+                                       continue;
+                               } else { // ch <= '\uDFFF'
+                                       // We have a surrogate tail without leading 
+                                       // surrogate. In NET_2_0 it uses fallback.
+                                       // In NET_1_1 we output wrong surrogate.
+                                       if ((posn + 3) > length) {
+                                               throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
+                                       }
+                                       bytes [posn++] = (byte) (0xE0 | (ch >> 12));
+                                       bytes [posn++] = (byte) (0x80 | ((ch >> 6) & 0x3F));
+                                       bytes [posn++] = (byte) (0x80 | (ch & 0x3F));
+                                       ++charIndex;
+                                       --charCount;
                                        continue;
-                               } else {
-                                       // This is a regular character.
-                                       pair = (uint)ch;
                                }
-                       } else if (ch >= '\uDC00' && ch <= '\uDFFF') {
-                               // We have a surrogate pair.
-                               pair = ((left - (uint)0xD800) << 10) +
-                                          (((uint)ch) - (uint)0xDC00) +
-                                          (uint)0x10000;
-                               left = 0;
                        } else {
-                               // We have a surrogate start followed by a
-                               // regular character.  Technically, this is
-                               // invalid, but we have to do something.
-                               // We write out the surrogate start and then
-                               // re-visit the current character again.
-                               pair = (uint)left;
-                               left = 0;
-                               --charIndex;
-                               ++charCount;
+                               if ('\uDC00' <= ch && ch <= '\uDFFF')
+                                       code =  0x10000 + (int) ch - 0xDC00 +
+                                               (((int) pair - 0xD800) << 10);
+                               else {
+                                       // We have a surrogate start followed by a
+                                       // regular character.  Technically, this is
+                                       // invalid, but we have to do something.
+                                       // We write out the surrogate start and then
+                                       // re-visit the current character again.
+                                       if ((posn + 3) > length) {
+                                               throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
+                                       }
+                                       bytes [posn++] = (byte) (0xE0 | (pair >> 12));
+                                       bytes [posn++] = (byte) (0x80 | ((pair >> 6) & 0x3F));
+                                       bytes [posn++] = (byte) (0x80 | (pair & 0x3F));
+                                       pair = '\0';
+                                       continue;
+                               }
+                               pair = '\0';
                        }
+                       ++charIndex;
+                       --charCount;
 
                        // Encode the character pair value.
-                       if (pair < (uint)0x0080) {
-                               if (posn >= length) {
+                       if (code < 0x0080) {
+                               if (posn >= length)
                                        throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               }
-                               bytes[posn++] = (byte)pair;
-                       } else if (pair < (uint)0x0800) {
-                               if ((posn + 2) > length) {
+                               bytes [posn++] = (byte)code;
+                       } else if (code < 0x0800) {
+                               if ((posn + 2) > length)
                                        throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               }
-                               bytes[posn++] = (byte)(0xC0 | (pair >> 6));
-                               bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
-                       } else if (pair < (uint)0x10000) {
-                               if ((posn + 3) > length) {
+                               bytes [posn++] = (byte) (0xC0 | (code >> 6));
+                               bytes [posn++] = (byte) (0x80 | (code & 0x3F));
+                       } else if (code < 0x10000) {
+                               if ((posn + 3) > length)
                                        throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               }
-                               bytes[posn++] = (byte)(0xE0 | (pair >> 12));
-                               bytes[posn++] = (byte)(0x80 | ((pair >> 6) & 0x3F));
-                               bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
+                               bytes [posn++] = (byte) (0xE0 | (code >> 12));
+                               bytes [posn++] = (byte) (0x80 | ((code >> 6) & 0x3F));
+                               bytes [posn++] = (byte) (0x80 | (code & 0x3F));
                        } else {
-                               if ((posn + 4) > length) {
+                               if ((posn + 4) > length)
                                        throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               }
-                               bytes[posn++] = (byte)(0xF0 | (pair >> 18));
-                               bytes[posn++] = (byte)(0x80 | ((pair >> 12) & 0x3F));
-                               bytes[posn++] = (byte)(0x80 | ((pair >> 6) & 0x3F));
-                               bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
+                               bytes [posn++] = (byte) (0xF0 | (code >> 18));
+                               bytes [posn++] = (byte) (0x80 | ((code >> 12) & 0x3F));
+                               bytes [posn++] = (byte) (0x80 | ((code >> 6) & 0x3F));
+                               bytes [posn++] = (byte) (0x80 | (code & 0x3F));
                        }
                }
-               if (flush && left != 0) {
-                       // Flush the left-over surrogate pair start.
-                       if ((posn + 3) > length) {
-                               throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
+
+               if (flush) {
+                       if (pair != '\0') {
+                               // Flush the left-over incomplete surrogate.
+                               if ((posn + 3) > length) {
+                                       throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
+                               }
+                               bytes [posn++] = (byte) (0xE0 | (pair >> 12));
+                               bytes [posn++] = (byte) (0x80 | ((pair >> 6) & 0x3F));
+                               bytes [posn++] = (byte) (0x80 | (pair & 0x3F));
                        }
-                       bytes[posn++] = (byte)(0xE0 | (left >> 12));
-                       bytes[posn++] = (byte)(0x80 | ((left >> 6) & 0x3F));
-                       bytes[posn++] = (byte)(0x80 | (left & 0x3F));
-                       left = 0;
+                       leftOver = '\0';
                }
-               leftOver = left;
 
                // Return the final count to the caller.
                return posn - byteIndex;
        }
 
+       private unsafe int Fallback (byte* bytes, int byteCount, char lead, char tail)
+       {
+               throw new NotImplementedException ();
+       }
+
        // Get the bytes that result from encoding a character buffer.
        public override int GetBytes (char[] chars, int charIndex, int charCount,
                                                                 byte[] bytes, int byteIndex)
        {
-               uint leftOver = 0;
+               char leftOver = '\0';
                return InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, ref leftOver, true);
        }
 
@@ -309,65 +381,24 @@ public class UTF8Encoding : Encoding
                        throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
                }
 
-               // Convert the characters into bytes.
-               char ch;
-               int length = bytes.Length;
-               uint pair;
-               int posn = byteIndex;
-               while (charCount > 0) {
-                       // Fetch the next UTF-16 character pair value.
-                       ch = s[charIndex++];
-                       if (ch >= '\uD800' && ch <= '\uDBFF' && charCount > 1) {
-                               // This may be the start of a surrogate pair.
-                               pair = (uint)(s[charIndex]);
-                               if (pair >= (uint)0xDC00 && pair <= (uint)0xDFFF) {
-                                       pair = (pair - (uint)0xDC00) +
-                                                  ((((uint)ch) - (uint)0xD800) << 10) +
-                                                  (uint)0x10000;
-                                       ++charIndex;
-                                       --charCount;
-                               } else {
-                                       pair = (uint)ch;
-                               }
-                       } else {
-                               pair = (uint)ch;
-                       }
-                       --charCount;
+               if (charIndex == s.Length)
+                       return 0;
 
-                       // Encode the character pair value.
-                       if (pair < (uint)0x0080) {
-                               if (posn >= length) {
-                                       throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               }
-                               bytes[posn++] = (byte)pair;
-                       } else if (pair < (uint)0x0800) {
-                               if ((posn + 2) > length) {
-                                       throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               }
-                               bytes[posn++] = (byte)(0xC0 | (pair >> 6));
-                               bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
-                       } else if (pair < (uint)0x10000) {
-                               if ((posn + 3) > length) {
-                                       throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               }
-                               bytes[posn++] = (byte)(0xE0 | (pair >> 12));
-                               bytes[posn++] = (byte)(0x80 | ((pair >> 6) & 0x3F));
-                               bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
-                       } else {
-                               if ((posn + 4) > length) {
-                                       throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
+               unsafe {
+                       fixed (char* cptr = s) {
+                               fixed (byte *bptr = bytes) {
+                                       char dummy = '\0';
+                                       return InternalGetBytes (
+                                               cptr + charIndex, charCount,
+                                               bptr + byteIndex, bytes.Length - byteIndex,
+                                               ref dummy, true);
                                }
-                               bytes[posn++] = (byte)(0xF0 | (pair >> 18));
-                               bytes[posn++] = (byte)(0x80 | ((pair >> 12) & 0x3F));
-                               bytes[posn++] = (byte)(0x80 | ((pair >> 6) & 0x3F));
-                               bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
                        }
                }
-
-               // Return the final count to the caller.
-               return posn - byteIndex;
        }
 
+       #endregion
+
        // Internal version of "GetCharCount" which can handle a rolling
        // state between multiple calls to this method.
 #if NET_2_0
@@ -394,9 +425,20 @@ public class UTF8Encoding : Encoding
                        throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
                }
 
+               int length = 0;
+
+               if (leftOverCount == 0) {
+                       int end = index + count;
+                       for (; index < end; index++, count--) {
+                               if (bytes [index] < 0x80)
+                                       length++;
+                               else
+                                       break;
+                       }
+               }
+
                // Determine the number of characters that we have.
                uint ch;
-               int length = 0;
                uint leftBits = leftOverBits;
                uint leftSoFar = (leftOverCount & (uint)0x0F);
                uint leftSize = ((leftOverCount >> 4) & (uint)0x0F);
@@ -596,10 +638,21 @@ public class UTF8Encoding : Encoding
                if (charIndex == chars.Length)
                        return 0;
 
+               int posn = charIndex;
+
+               if (leftOverCount == 0) {
+                       int end = byteIndex + byteCount;
+                       for (; byteIndex < end; posn++, byteIndex++, byteCount--) {
+                               if (bytes [byteIndex] < 0x80)
+                                       chars [posn] = (char) bytes [byteIndex];
+                               else
+                                       break;
+                       }
+               }
+
                // Convert the bytes into the output buffer.
                uint ch;
                int length = chars.Length;
-               int posn = charIndex;
                uint leftBits = leftOverBits;
                uint leftSoFar = (leftOverCount & (uint)0x0F);
                uint leftSize = ((leftOverCount >> 4) & (uint)0x0F);
@@ -685,6 +738,15 @@ public class UTF8Encoding : Encoding
 #else
                                                                if (throwOnInvalid)
                                                                        throw new ArgumentException (_("Overlong"), leftBits.ToString ());
+#endif
+                                                       }
+                                                       else if ((leftBits & 0xF800) == 0xD800) {
+                                                               // UTF-8 doesn't use surrogate characters
+#if NET_2_0
+                                                               Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+#else
+                                                               if (throwOnInvalid)
+                                                                       throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
 #endif
                                                        }
                                                        else {
@@ -905,30 +967,48 @@ public class UTF8Encoding : Encoding
        private class UTF8Encoder : Encoder
        {
                private bool emitIdentifier;
-               private uint leftOver;
+               private char leftOverForCount;
+               private char leftOverForConv;
 
                // Constructor.
                public UTF8Encoder (bool emitIdentifier)
                {
                        this.emitIdentifier = emitIdentifier;
-                       leftOver = 0;
+                       leftOverForCount = '\0';
+                       leftOverForConv = '\0';
                }
 
                // Override inherited methods.
                public override int GetByteCount (char[] chars, int index,
                                         int count, bool flush)
                {
-                       return InternalGetByteCount (chars, index, count, leftOver, flush);
+                       return InternalGetByteCount (chars, index, count, ref leftOverForCount, flush);
                }
                public override int GetBytes (char[] chars, int charIndex,
-                                        int charCount, byte[] bytes, int byteCount, bool flush)
+                                        int charCount, byte[] bytes, int byteIndex, bool flush)
                {
                        int result;
-                       result = InternalGetBytes (chars, charIndex, charCount, bytes, byteCount, ref leftOver, flush);
+                       result = InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, ref leftOverForConv, flush);
                        emitIdentifier = false;
                        return result;
                }
 
+#if NET_2_0
+               public unsafe override int GetByteCount (char* chars, int count, bool flush)
+               {
+                       return InternalGetByteCount (chars, count, ref leftOverForCount, flush);
+               }
+
+               public unsafe override int GetBytes (char* chars, int charCount,
+                       byte* bytes, int byteCount, bool flush)
+               {
+                       int result;
+                       result = InternalGetBytes (chars, charCount, bytes, byteCount, ref leftOverForConv, flush);
+                       emitIdentifier = false;
+                       return result;
+               }
+#endif
+
        } // class UTF8Encoder
 
 }; // class UTF8Encoding