Refactorized convert(byte[], int, ...), now uses the pointer version.
[mono.git] / mcs / class / corlib / System.Text / Decoder.cs
index 74062465718f4c05303dfe4d855bbcbf831dd165..bc8e6241cb7413b9c2c8a49a7517ec7661345e7e 100644 (file)
@@ -29,16 +29,13 @@ using System;
 using System.Runtime.InteropServices;
 
 [Serializable]
-#if NET_2_0
 [ComVisible (true)]
-#endif
 public abstract class Decoder
 {
 
        // Constructor.
        protected Decoder () {}
 
-#if NET_2_0
        DecoderFallback fallback = new DecoderReplacementFallback ();
        DecoderFallbackBuffer fallback_buffer;
 
@@ -61,7 +58,6 @@ public abstract class Decoder
                        return fallback_buffer;
                }
        }
-#endif
 
        // Get the number of characters needed to decode a buffer.
        public abstract int GetCharCount (byte[] bytes, int index, int count);
@@ -70,7 +66,6 @@ public abstract class Decoder
        public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
                                                                 char[] chars, int charIndex);
 
-#if NET_2_0
        [ComVisible (false)]
        public virtual int GetCharCount (byte [] bytes, int index, int count, bool flush)
        {
@@ -148,7 +143,7 @@ public abstract class Decoder
        }
 
        [ComVisible (false)]
-       public virtual void Convert (
+       public unsafe virtual void Convert (
                byte [] bytes, int byteIndex, int byteCount,
                char [] chars, int charIndex, int charCount, bool flush,
                out int bytesUsed, out int charsUsed, out bool completed)
@@ -158,23 +153,23 @@ public abstract class Decoder
                if (charCount < 0 || chars.Length < charIndex + charCount)
                        throw new ArgumentOutOfRangeException ("charCount");
 
-               bytesUsed = byteCount;
-               while (true) {
-                       charsUsed = GetCharCount (bytes, byteIndex, bytesUsed, flush);
-                       if (charsUsed <= charCount)
-                               break;
-                       flush = false;
-                       bytesUsed >>= 1;
+               // refactorize passing control to byte* version
+               fixed (char* cptr = chars) {
+                       fixed (byte* bptr = bytes) {
+                               Convert(bptr + byteIndex, byteCount,
+                                       cptr + charIndex, charCount,
+                                       flush,
+                                       out bytesUsed, out charsUsed,
+                                       out completed);
+                       }
                }
-               completed = bytesUsed == byteCount;
-               charsUsed = GetChars (bytes, byteIndex, bytesUsed, chars, charIndex, flush);
        }
 
        void CheckArguments (char [] chars, int charIndex)
        {
                if (chars == null)
                        throw new ArgumentNullException ("chars");
-               if (charIndex < 0 || chars.Length <= charIndex)
+               if (charIndex < 0 || chars.Length < charIndex)
                        throw new ArgumentOutOfRangeException ("charIndex");
        }
 
@@ -182,7 +177,7 @@ public abstract class Decoder
        {
                if (bytes == null)
                        throw new ArgumentNullException ("bytes");
-               if (byteIndex < 0 || bytes.Length <= byteIndex)
+               if (byteIndex < 0)
                        throw new ArgumentOutOfRangeException ("byteIndex");
                if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
                        throw new ArgumentOutOfRangeException ("byteCount");
@@ -199,8 +194,6 @@ public abstract class Decoder
                if (byteCount < 0)
                        throw new ArgumentOutOfRangeException ("byteCount");
        }
-#endif
-
 }; // class Decoder
 
 }; // namespace System.Text