Author: atsushi
Date: 2005-12-08 06:59:32 -0500 (Thu, 08 Dec 2005)
New Revision: 54109

Modified:
   trunk/mcs/class/corlib/System.Text/ChangeLog
   trunk/mcs/class/corlib/System.Text/Encoding.cs
   trunk/mcs/class/corlib/System.Text/UTF8Encoding.cs
Log:
2005-12-08  Atsushi Enomoto  <[EMAIL PROTECTED]>

        * Encoding.cs : now under 2.0 GetBytes(string,...) dispatches to
          byte*-based GetBytes().
        * UTF8Encoding.cs : avoid extraneous DecoderFallbackBuffer creation
          which came to happen after introducing fallback buffer.



Modified: trunk/mcs/class/corlib/System.Text/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.Text/ChangeLog        2005-12-08 10:45:12 UTC 
(rev 54108)
+++ trunk/mcs/class/corlib/System.Text/ChangeLog        2005-12-08 11:59:32 UTC 
(rev 54109)
@@ -1,3 +1,10 @@
+2005-12-08  Atsushi Enomoto  <[EMAIL PROTECTED]>
+
+       * Encoding.cs : now under 2.0 GetBytes(string,...) dispatches to
+         byte*-based GetBytes().
+       * UTF8Encoding.cs : avoid extraneous DecoderFallbackBuffer creation
+         which came to happen after introducing fallback buffer.
+
 2005-11-28  Atsushi Enomoto  <[EMAIL PROTECTED]>
 
        * Encoding.cs, ASCIIEncoding.cs, Latin1Encoding.cs :

Modified: trunk/mcs/class/corlib/System.Text/Encoding.cs
===================================================================
--- trunk/mcs/class/corlib/System.Text/Encoding.cs      2005-12-08 10:45:12 UTC 
(rev 54108)
+++ trunk/mcs/class/corlib/System.Text/Encoding.cs      2005-12-08 11:59:32 UTC 
(rev 54109)
@@ -225,21 +225,60 @@
        public virtual int GetBytes (String s, int charIndex, int charCount,
                                                                byte[] bytes, 
int byteIndex)
        {
-               if (s == null) {
+               if (s == null)
                        throw new ArgumentNullException ("s");
+#if NET_2_0
+               if (charIndex < 0 || charIndex > s.Length)
+                       throw new ArgumentOutOfRangeException ("charIndex", 
_("ArgRange_Array"));
+               if (charCount < 0 || charIndex + charCount > s.Length)
+                       throw new ArgumentOutOfRangeException ("charCount", 
_("ArgRange_Array"));
+               if (byteIndex < 0 || byteIndex > bytes.Length)
+                       throw new ArgumentOutOfRangeException ("byteIndex", 
_("ArgRange_Array"));
+
+               if (charCount == 0 || bytes.Length == byteIndex)
+                       return 0;
+               unsafe {
+                       fixed (char* cptr = s) {
+                               fixed (byte* bptr = bytes) {
+                                       return GetBytes (cptr + charIndex,
+                                               charCount,
+                                               bptr + byteIndex,
+                                               bytes.Length - byteIndex);
+                               }
+                       }
                }
+#else
                return GetBytes (s.ToCharArray(), charIndex, charCount, bytes, 
byteIndex);
+#endif
        }
        public virtual byte[] GetBytes (String s)
        {
-               if (s == null) {
+               if (s == null)
                        throw new ArgumentNullException ("s");
+
+#if NET_2_0
+               if (s.Length == 0)
+                       return new byte [0];
+               int byteCount = GetByteCount (s);
+               if (byteCount == 0)
+                       return new byte [0];
+               unsafe {
+                       fixed (char* cptr = s) {
+                               byte [] bytes = new byte [byteCount];
+                               fixed (byte* bptr = bytes) {
+                                       GetBytes (cptr, s.Length,
+                                               bptr, byteCount);
+                                       return bytes;
+                               }
+                       }
                }
+#else
                char[] chars = s.ToCharArray ();
                int numBytes = GetByteCount (chars, 0, chars.Length);
                byte[] bytes = new byte [numBytes];
                GetBytes (chars, 0, chars.Length, bytes, 0);
                return bytes;
+#endif
        }
        public virtual byte[] GetBytes (char[] chars, int index, int count)
        {

Modified: trunk/mcs/class/corlib/System.Text/UTF8Encoding.cs
===================================================================
--- trunk/mcs/class/corlib/System.Text/UTF8Encoding.cs  2005-12-08 10:45:12 UTC 
(rev 54108)
+++ trunk/mcs/class/corlib/System.Text/UTF8Encoding.cs  2005-12-08 11:59:32 UTC 
(rev 54109)
@@ -375,7 +375,8 @@
        // state between multiple calls to this method.
        private static int InternalGetCharCount (
                byte[] bytes, int index, int count, uint leftOverBits,
-               uint leftOverCount, DecoderFallbackBuffer fallbackBuffer, bool 
flush)
+               uint leftOverCount, object provider,
+               ref DecoderFallbackBuffer fallbackBuffer, bool flush)
 #else
        private static int InternalGetCharCount (
                byte[] bytes, int index, int count, uint leftOverBits,
@@ -435,7 +436,7 @@
                                } else {
                                        // Invalid UTF-8 start character.
 #if NET_2_0
-                                       length += Fallback (fallbackBuffer, 
bytes, index - 1);
+                                       length += Fallback (provider, ref 
fallbackBuffer, bytes, index - 1);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException 
(_("Arg_InvalidUTF8"), "bytes");
@@ -469,7 +470,7 @@
                                                        }
                                                        if (overlong) {
 #if NET_2_0
-                                                               length += 
Fallback (fallbackBuffer, bytes, index - 1);
+                                                               length += 
Fallback (provider, ref fallbackBuffer, bytes, index - 1);
 #else
                                                                if 
(throwOnInvalid)
                                                                        throw 
new ArgumentException (_("Overlong"), leftBits.ToString ());
@@ -481,7 +482,7 @@
                                                        length += 2;
                                                } else {
 #if NET_2_0
-                                                       length += Fallback 
(fallbackBuffer, bytes, index - 1);
+                                                       length += Fallback 
(provider, ref fallbackBuffer, bytes, index - 1);
 #else
                                                        if (throwOnInvalid)
                                                                throw new 
ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -492,7 +493,7 @@
                                } else {
                                        // Invalid UTF-8 sequence: clear and 
restart.
 #if NET_2_0
-                                       length += Fallback (fallbackBuffer, 
bytes, index - 1);
+                                       length += Fallback (provider, ref 
fallbackBuffer, bytes, index - 1);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException 
(_("Arg_InvalidUTF8"), "bytes");
@@ -507,7 +508,7 @@
                        // We had left-over bytes that didn't make up
                        // a complete UTF-8 character sequence.
 #if NET_2_0
-                       length += Fallback (fallbackBuffer, bytes, index - 1);
+                       length += Fallback (provider, ref fallbackBuffer, 
bytes, index - 1);
 #else
                        if (throwOnInvalid)
                                throw new ArgumentException 
(_("Arg_InvalidUTF8"), "bytes");
@@ -520,16 +521,30 @@
 
 #if NET_2_0
        // for GetCharCount()
-       static int Fallback (DecoderFallbackBuffer buffer, byte [] bytes, int 
index)
+       static int Fallback (object provider, ref 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 (DecoderFallbackBuffer buffer, byte [] bytes, int 
byteIndex,
+       static void Fallback (object provider, ref 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 ();
@@ -540,7 +555,8 @@
        public override int GetCharCount (byte[] bytes, int index, int count)
        {
 #if NET_2_0
-               return InternalGetCharCount (bytes, index, count, 0, 0, 
DecoderFallback.CreateFallbackBuffer (), true);
+               DecoderFallbackBuffer buf = null;
+               return InternalGetCharCount (bytes, index, count, 0, 0, 
DecoderFallback, ref buf, true);
 #else
                return InternalGetCharCount (bytes, index, count, 0, 0, 
throwOnInvalid, true);
 #endif
@@ -551,7 +567,8 @@
        private static int InternalGetChars (
                byte[] bytes, int byteIndex, int byteCount, char[] chars,
                int charIndex, ref uint leftOverBits, ref uint leftOverCount,
-               DecoderFallbackBuffer fallbackBuffer, bool flush)
+               object provider,
+               ref DecoderFallbackBuffer fallbackBuffer, bool flush)
 #else
        private static int InternalGetChars (
                byte[] bytes, int byteIndex, int byteCount, char[] chars,
@@ -626,7 +643,7 @@
                                } else {
                                        // Invalid UTF-8 start character.
 #if NET_2_0
-                                       Fallback (fallbackBuffer, bytes, 
byteIndex, chars, ref posn);
+                                       Fallback (provider, ref fallbackBuffer, 
bytes, byteIndex, chars, ref posn);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException 
(_("Arg_InvalidUTF8"), "bytes");
@@ -660,7 +677,7 @@
                                                        }
                                                        if (overlong) {
 #if NET_2_0
-                                                               Fallback 
(fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                                               Fallback 
(provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
 #else
                                                                if 
(throwOnInvalid)
                                                                        throw 
new ArgumentException (_("Overlong"), leftBits.ToString ());
@@ -685,7 +702,7 @@
                                                                
(char)((leftBits & (uint)0x3FF) + (uint)0xDC00);
                                                } else {
 #if NET_2_0
-                                                       Fallback 
(fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                                       Fallback (provider, ref 
fallbackBuffer, bytes, byteIndex, chars, ref posn);
 #else
                                                        if (throwOnInvalid)
                                                                throw new 
ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -696,7 +713,7 @@
                                } else {
                                        // Invalid UTF-8 sequence: clear and 
restart.
 #if NET_2_0
-                                       Fallback (fallbackBuffer, bytes, 
byteIndex, chars, ref posn);
+                                       Fallback (provider, ref fallbackBuffer, 
bytes, byteIndex, chars, ref posn);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException 
(_("Arg_InvalidUTF8"), "bytes");
@@ -711,7 +728,7 @@
                        // We had left-over bytes that didn't make up
                        // a complete UTF-8 character sequence.
 #if NET_2_0
-                       Fallback (fallbackBuffer, bytes, byteIndex, chars, ref 
posn);
+                       Fallback (provider, ref fallbackBuffer, bytes, 
byteIndex, chars, ref posn);
 #else
                        if (throwOnInvalid)
                                throw new ArgumentException 
(_("Arg_InvalidUTF8"), "bytes");
@@ -731,8 +748,9 @@
                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.CreateFallbackBuffer (), true);
+                               charIndex, ref leftOverBits, ref leftOverCount, 
DecoderFallback, ref buf, true);
 #else
                return InternalGetChars (bytes, byteIndex, byteCount, chars, 
                                charIndex, ref leftOverBits, ref leftOverCount, 
throwOnInvalid, true);
@@ -856,8 +874,9 @@
                public override int GetCharCount (byte[] bytes, int index, int 
count)
                {
 #if NET_2_0
+                       DecoderFallbackBuffer buf = null;
                        return InternalGetCharCount (bytes, index, count,
-                               leftOverBits, leftOverCount, FallbackBuffer, 
false);
+                               leftOverBits, leftOverCount, this, ref buf, 
false);
 #else
                        return InternalGetCharCount (bytes, index, count,
                                        leftOverBits, leftOverCount, 
throwOnInvalid, false);
@@ -867,8 +886,9 @@
                                                 int byteCount, char[] chars, 
int charIndex)
                {
 #if NET_2_0
+                       DecoderFallbackBuffer buf = null;
                        return InternalGetChars (bytes, byteIndex, byteCount,
-                               chars, charIndex, ref leftOverBits, ref 
leftOverCount, FallbackBuffer, false);
+                               chars, charIndex, ref leftOverBits, ref 
leftOverCount, this, ref buf, false);
 #else
                        return InternalGetChars (bytes, byteIndex, byteCount,
                                chars, charIndex, ref leftOverBits, ref 
leftOverCount, throwOnInvalid, false);

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to