Use EncodingForwarder for GetByteCount(char*, int)
authorJames Ko <jamesqko@gmail.com>
Mon, 4 Jul 2016 16:05:22 +0000 (12:05 -0400)
committerJames Ko <jamesqko@gmail.com>
Mon, 4 Jul 2016 19:23:51 +0000 (15:23 -0400)
Commit migrated from https://github.com/dotnet/coreclr/commit/56c687e0afefc6c223dc0d296a4c64ab5465ebdd

src/coreclr/src/mscorlib/src/System/Text/ASCIIEncoding.cs
src/coreclr/src/mscorlib/src/System/Text/EncodingForwarder.cs
src/coreclr/src/mscorlib/src/System/Text/EncodingNLS.cs
src/coreclr/src/mscorlib/src/System/Text/UTF32Encoding.cs
src/coreclr/src/mscorlib/src/System/Text/UTF7Encoding.cs
src/coreclr/src/mscorlib/src/System/Text/UTF8Encoding.cs
src/coreclr/src/mscorlib/src/System/Text/UnicodeEncoding.cs

index 2249d91..595fa45 100644 (file)
@@ -83,18 +83,7 @@ namespace System.Text
         [System.Runtime.InteropServices.ComVisible(false)]
         public override unsafe int GetByteCount(char* chars, int count)
         {
-            // Validate Parameters
-            if (chars == null)
-                throw new ArgumentNullException("chars",
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count",
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-
-            // Call it with empty encoder
-            return GetByteCount(chars, count, null);
+            return EncodingForwarder.GetByteCount(this, chars, count);
         }
 
         // Parent method is safe.
index 13d7bac..5820c4a 100644 (file)
@@ -52,6 +52,7 @@ namespace System.Text
 
         public unsafe static int GetByteCount(Encoding encoding, string s)
         {
+            Contract.Assert(encoding != null);
             if (s == null)
             {
                 throw new ArgumentNullException("s");
@@ -69,5 +70,22 @@ namespace System.Text
             fixed (char* pChars = s)
                 return encoding.GetByteCount(pChars, s.Length, encoder: null);
         }
+
+        public unsafe static int GetByteCount(Encoding encoding, char* chars, int count)
+        {
+            Contract.Assert(encoding != null);
+            if (chars == null)
+            {
+                throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
+            }
+            if (count < 0)
+            {
+                throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
+            }
+            Contract.EndContractBlock();
+
+            // Call the internal version, with an empty encoder
+            return encoding.GetByteCount(chars, count, encoder: null);
+        }
     }
 }
index 33ad250..438db1f 100644 (file)
@@ -63,18 +63,7 @@ namespace System.Text
         [System.Security.SecurityCritical]  // auto-generated
         public override unsafe int GetByteCount(char* chars, int count)
         {
-            // Validate Parameters
-            if (chars == null)
-                throw new ArgumentNullException("chars", 
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count", 
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-            
-            // Call it with empty encoder
-            return GetByteCount(chars, count, null);
+            return EncodingForwarder.GetByteCount(this, chars, count);
         }
 
         // Parent method is safe.
index 110cff8..44e764d 100644 (file)
@@ -119,18 +119,7 @@ namespace System.Text
         [CLSCompliant(false)]
         public override unsafe int GetByteCount(char* chars, int count)
         {
-            // Validate Parameters
-            if (chars == null)
-                throw new ArgumentNullException("chars",
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count",
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-
-            // Call it with empty encoder
-            return GetByteCount(chars, count, null);
+            return EncodingForwarder.GetByteCount(this, chars, count);
         }
 
         // Parent method is safe.
index c04a01e..6f9f35d 100644 (file)
@@ -187,18 +187,7 @@ namespace System.Text
         [System.Runtime.InteropServices.ComVisible(false)]
         public override unsafe int GetByteCount(char* chars, int count)
         {
-            // Validate Parameters
-            if (chars == null)
-                throw new ArgumentNullException("chars",
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count",
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-
-            // Call it with empty encoder
-            return GetByteCount(chars, count, null);
+            return EncodingForwarder.GetByteCount(this, chars, count);
         }
 
         // Parent method is safe.
index a7cfaba..32b219e 100644 (file)
@@ -146,18 +146,7 @@ namespace System.Text
         [System.Runtime.InteropServices.ComVisible(false)]
         public override unsafe int GetByteCount(char* chars, int count)
         {
-            // Validate Parameters
-            if (chars == null)
-                throw new ArgumentNullException("chars",
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count",
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-
-            // Call it with empty encoder
-            return GetByteCount(chars, count, null);
+            return EncodingForwarder.GetByteCount(this, chars, count);
         }
 
         // Parent method is safe.
index fb670b6..dba7643 100644 (file)
@@ -118,18 +118,7 @@ namespace System.Text
         [System.Runtime.InteropServices.ComVisible(false)]
         public override unsafe int GetByteCount(char* chars, int count)
         {
-            // Validate Parameters
-            if (chars == null)
-                throw new ArgumentNullException("chars",
-                    Environment.GetResourceString("ArgumentNull_Array"));
-
-            if (count < 0)
-                throw new ArgumentOutOfRangeException("count",
-                    Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
-            Contract.EndContractBlock();
-
-            // Call it with empty encoder
-            return GetByteCount(chars, count, null);
+            return EncodingForwarder.GetByteCount(this, chars, count);
         }
 
         // Parent method is safe.