merge -r 53370:58178
[mono.git] / mcs / class / corlib / System.Text / UTF8Encoding.cs
index 3cb174823a64704112f4bbd74a8e0fa90995e268..2c00c72eeaf43682588adfc19c1a40b9b3c86417 100644 (file)
@@ -72,7 +72,7 @@ public class UTF8Encoding : Encoding
 
        // 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, ref uint leftOver, bool flush)
+       private static int InternalGetByteCount (char[] chars, int index, int count, uint leftOver, bool flush)
        {
                // Validate the parameters.
                if (chars == null) {
@@ -85,27 +85,6 @@ 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 uint leftOver, bool flush)
-       {
-               int index = 0;
-
                // Determine the lengths of all characters.
                char ch;
                int length = 0;
@@ -145,8 +124,6 @@ public class UTF8Encoding : Encoding
                        length += 3;
                }
 
-               leftOver = pair;
-
                // Return the final length to the caller.
                return length;
        }
@@ -154,8 +131,7 @@ 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)
        {
-               uint dummy = 0;
-               return InternalGetByteCount (chars, index, count, ref dummy, true);
+               return InternalGetByteCount (chars, index, count, 0, true);
        }
 
        // Convenience wrappers for "GetByteCount".
@@ -222,40 +198,9 @@ 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 uint leftOver, bool flush)
-       {
-               int charIndex = 0;
-               int byteIndex = 0;
-
                // Convert the characters into bytes.
                char ch;
-               int length = byteCount;
+               int length = bytes.Length;
                uint pair;
                uint left = leftOver;
                int posn = byteIndex;
@@ -430,8 +375,7 @@ public class UTF8Encoding : Encoding
        // state between multiple calls to this method.
        private static int InternalGetCharCount (
                byte[] bytes, int index, int count, uint leftOverBits,
-               uint leftOverCount, object provider,
-               ref DecoderFallbackBuffer fallbackBuffer, bool flush)
+               uint leftOverCount, DecoderFallbackBuffer fallbackBuffer, bool flush)
 #else
        private static int InternalGetCharCount (
                byte[] bytes, int index, int count, uint leftOverBits,
@@ -449,20 +393,9 @@ 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);
@@ -502,7 +435,7 @@ public class UTF8Encoding : Encoding
                                } else {
                                        // Invalid UTF-8 start character.
 #if NET_2_0
-                                       length += Fallback (provider, ref fallbackBuffer, bytes, index - 1);
+                                       length += Fallback (fallbackBuffer, bytes, index - 1);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -536,7 +469,7 @@ public class UTF8Encoding : Encoding
                                                        }
                                                        if (overlong) {
 #if NET_2_0
-                                                               length += Fallback (provider, ref fallbackBuffer, bytes, index - 1);
+                                                               length += Fallback (fallbackBuffer, bytes, index - 1);
 #else
                                                                if (throwOnInvalid)
                                                                        throw new ArgumentException (_("Overlong"), leftBits.ToString ());
@@ -548,7 +481,7 @@ public class UTF8Encoding : Encoding
                                                        length += 2;
                                                } else {
 #if NET_2_0
-                                                       length += Fallback (provider, ref fallbackBuffer, bytes, index - 1);
+                                                       length += Fallback (fallbackBuffer, bytes, index - 1);
 #else
                                                        if (throwOnInvalid)
                                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -559,7 +492,7 @@ public class UTF8Encoding : Encoding
                                } else {
                                        // Invalid UTF-8 sequence: clear and restart.
 #if NET_2_0
-                                       length += Fallback (provider, ref fallbackBuffer, bytes, index - 1);
+                                       length += Fallback (fallbackBuffer, bytes, index - 1);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -574,7 +507,7 @@ public class UTF8Encoding : Encoding
                        // We had left-over bytes that didn't make up
                        // a complete UTF-8 character sequence.
 #if NET_2_0
-                       length += Fallback (provider, ref fallbackBuffer, bytes, index - 1);
+                       length += Fallback (fallbackBuffer, bytes, index - 1);
 #else
                        if (throwOnInvalid)
                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -587,30 +520,16 @@ public class UTF8Encoding : Encoding
 
 #if NET_2_0
        // for GetCharCount()
-       static int Fallback (object provider, ref DecoderFallbackBuffer buffer, byte [] bytes, int index)
+       static int Fallback (DecoderFallbackBuffer buffer, byte [] bytes, int index)
        {
-               if (buffer == null) {
-                       DecoderFallback fb = provider as DecoderFallback;
-                       if (fb != null)
-                               buffer = fb.CreateFallbackBuffer ();
-                       else
-                               buffer = ((Decoder) provider).FallbackBuffer;
-               }
                buffer.Fallback (bytes, index - 1);
                return buffer.Remaining;
        }
 
        // for GetChars()
-       static void Fallback (object provider, ref DecoderFallbackBuffer buffer, byte [] bytes, int byteIndex,
+       static void Fallback (DecoderFallbackBuffer buffer, byte [] bytes, int byteIndex,
                char [] chars, ref int charIndex)
        {
-               if (buffer == null) {
-                       DecoderFallback fb = provider as DecoderFallback;
-                       if (fb != null)
-                               buffer = fb.CreateFallbackBuffer ();
-                       else
-                               buffer = ((Decoder) provider).FallbackBuffer;
-               }
                buffer.Fallback (bytes, byteIndex - 1);
                while (buffer.Remaining > 0)
                        chars [charIndex++] = buffer.GetNextChar ();
@@ -621,8 +540,7 @@ public class UTF8Encoding : Encoding
        public override int GetCharCount (byte[] bytes, int index, int count)
        {
 #if NET_2_0
-               DecoderFallbackBuffer buf = null;
-               return InternalGetCharCount (bytes, index, count, 0, 0, DecoderFallback, ref buf, true);
+               return InternalGetCharCount (bytes, index, count, 0, 0, DecoderFallback.CreateFallbackBuffer (), true);
 #else
                return InternalGetCharCount (bytes, index, count, 0, 0, throwOnInvalid, true);
 #endif
@@ -633,8 +551,7 @@ public class UTF8Encoding : Encoding
        private static int InternalGetChars (
                byte[] bytes, int byteIndex, int byteCount, char[] chars,
                int charIndex, ref uint leftOverBits, ref uint leftOverCount,
-               object provider,
-               ref DecoderFallbackBuffer fallbackBuffer, bool flush)
+               DecoderFallbackBuffer fallbackBuffer, bool flush)
 #else
        private static int InternalGetChars (
                byte[] bytes, int byteIndex, int byteCount, char[] chars,
@@ -662,32 +579,17 @@ 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);
-
-               int byteEnd = byteIndex + byteCount;
-               if (byteEnd < 0 || byteEnd > bytes.Length)
-                       throw new SystemException (String.Format ("INTERNAL ERROR: should not happen: {0} {1} {2}", byteIndex, byteCount, byteEnd));
-
-               for(; byteIndex < byteEnd; byteIndex++) {
+               while (byteCount > 0) {
                        // Fetch the next character from the byte buffer.
-                       ch = (uint)(bytes[byteIndex]);
+                       ch = (uint)(bytes[byteIndex++]);
+                       --byteCount;
                        if (leftSize == 0) {
                                // Process a UTF-8 start character.
                                if (ch < (uint)0x0080) {
@@ -724,7 +626,7 @@ public class UTF8Encoding : Encoding
                                } else {
                                        // Invalid UTF-8 start character.
 #if NET_2_0
-                                       Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                       Fallback (fallbackBuffer, bytes, byteIndex, chars, ref posn);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -758,19 +660,10 @@ public class UTF8Encoding : Encoding
                                                        }
                                                        if (overlong) {
 #if NET_2_0
-                                                               Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                                               Fallback (fallbackBuffer, bytes, byteIndex, chars, ref posn);
 #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 {
@@ -792,7 +685,7 @@ public class UTF8Encoding : Encoding
                                                                (char)((leftBits & (uint)0x3FF) + (uint)0xDC00);
                                                } else {
 #if NET_2_0
-                                                       Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                                       Fallback (fallbackBuffer, bytes, byteIndex, chars, ref posn);
 #else
                                                        if (throwOnInvalid)
                                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -803,13 +696,14 @@ public class UTF8Encoding : Encoding
                                } else {
                                        // Invalid UTF-8 sequence: clear and restart.
 #if NET_2_0
-                                       Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                       Fallback (fallbackBuffer, bytes, byteIndex, chars, ref posn);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
 #endif
                                        leftSize = 0;
                                        --byteIndex;
+                                       ++byteCount;
                                }
                        }
                }
@@ -817,7 +711,7 @@ public class UTF8Encoding : Encoding
                        // We had left-over bytes that didn't make up
                        // a complete UTF-8 character sequence.
 #if NET_2_0
-                       Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                       Fallback (fallbackBuffer, bytes, byteIndex, chars, ref posn);
 #else
                        if (throwOnInvalid)
                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -837,9 +731,8 @@ public class UTF8Encoding : Encoding
                uint leftOverBits = 0;
                uint leftOverCount = 0;
 #if NET_2_0
-               DecoderFallbackBuffer buf = null;
                return InternalGetChars (bytes, byteIndex, byteCount, chars, 
-                               charIndex, ref leftOverBits, ref leftOverCount, DecoderFallback, ref buf, true);
+                               charIndex, ref leftOverBits, ref leftOverCount, DecoderFallback.CreateFallbackBuffer (), true);
 #else
                return InternalGetChars (bytes, byteIndex, byteCount, chars, 
                                charIndex, ref leftOverBits, ref leftOverCount, throwOnInvalid, true);
@@ -963,9 +856,8 @@ public class UTF8Encoding : Encoding
                public override int GetCharCount (byte[] bytes, int index, int count)
                {
 #if NET_2_0
-                       DecoderFallbackBuffer buf = null;
                        return InternalGetCharCount (bytes, index, count,
-                               leftOverBits, leftOverCount, this, ref buf, false);
+                               leftOverBits, leftOverCount, FallbackBuffer, false);
 #else
                        return InternalGetCharCount (bytes, index, count,
                                        leftOverBits, leftOverCount, throwOnInvalid, false);
@@ -975,9 +867,8 @@ public class UTF8Encoding : Encoding
                                                 int byteCount, char[] chars, int charIndex)
                {
 #if NET_2_0
-                       DecoderFallbackBuffer buf = null;
                        return InternalGetChars (bytes, byteIndex, byteCount,
-                               chars, charIndex, ref leftOverBits, ref leftOverCount, this, ref buf, false);
+                               chars, charIndex, ref leftOverBits, ref leftOverCount, FallbackBuffer, false);
 #else
                        return InternalGetChars (bytes, byteIndex, byteCount,
                                chars, charIndex, ref leftOverBits, ref leftOverCount, throwOnInvalid, false);
@@ -991,47 +882,29 @@ public class UTF8Encoding : Encoding
        private class UTF8Encoder : Encoder
        {
                private bool emitIdentifier;
-               private uint leftOverForCount;
-               private uint leftOverForConv;
+               private uint leftOver;
 
                // Constructor.
                public UTF8Encoder (bool emitIdentifier)
                {
                        this.emitIdentifier = emitIdentifier;
-                       leftOverForCount = 0;
-                       leftOverForConv = 0;
+                       leftOver = 0;
                }
 
                // Override inherited methods.
                public override int GetByteCount (char[] chars, int index,
                                         int count, bool flush)
                {
-                       return InternalGetByteCount (chars, index, count, ref leftOverForCount, flush);
+                       return InternalGetByteCount (chars, index, count, leftOver, flush);
                }
                public override int GetBytes (char[] chars, int charIndex,
-                                        int charCount, byte[] bytes, int byteIndex, bool flush)
-               {
-                       int result;
-                       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 charCount, byte[] bytes, int byteCount, bool flush)
                {
                        int result;
-                       result = InternalGetBytes (chars, charCount, bytes, byteCount, ref leftOverForConv, flush);
+                       result = InternalGetBytes (chars, charIndex, charCount, bytes, byteCount, ref leftOver, flush);
                        emitIdentifier = false;
                        return result;
                }
-#endif
 
        } // class UTF8Encoder