Use EncodingForwarder for GetByteCount(string)
authorJames Ko <jamesqko@gmail.com>
Mon, 4 Jul 2016 15:57:58 +0000 (11:57 -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/e61479088e1ba1f1d8756b8da1a352d518c015c8

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 cc9f6aa..2249d91 100644 (file)
@@ -67,15 +67,11 @@ namespace System.Text
         // parent method is safe
 
         [System.Security.SecuritySafeCritical]  // auto-generated
-        public override unsafe int GetByteCount(String chars)
+        public override int GetByteCount(String chars)
         {
-            // Validate input
-            if (chars==null)
-                throw new ArgumentNullException("chars");
-            Contract.EndContractBlock();
-
-            fixed (char* pChars = chars)
-                return GetByteCount(pChars, chars.Length, null);
+            // NOTE: If chars is null, this will throw an ArgumentNullException
+            // with the parameter name "s" rather than "chars"
+            return EncodingForwarder.GetByteCount(this, chars);
         }
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
index 75f8343..93be85d 100644 (file)
@@ -45,9 +45,29 @@ namespace System.Text
             if (count == 0)
                 return 0;
 
-            // Just call the pointer version
+            // Just call the (internal) pointer version
             fixed (char* pChars = chars)
-                return encoding.GetByteCount(pChars + index, count, null);
+                return encoding.GetByteCount(pChars + index, count, encoder: null);
+        }
+
+        public unsafe static int GetByteCount(Encoding encoding, string s)
+        {
+            if (s == null)
+            {
+                throw new ArgumentNullException("s");
+            }
+            Contract.EndContractBlock();
+
+            // NOTE: The behavior of fixed *is* defined by
+            // the spec for empty strings, although not for
+            // null strings/empty char arrays. See
+            // http://stackoverflow.com/q/37757751/4077294
+            // Regardless, we may still want to check
+            // for if (chars.Length == 0) in the future
+            // and short-circuit as an optimization (TODO).
+
+            fixed (char* pChars = s)
+                return encoding.GetByteCount(pChars, s.Length, encoder: null);
         }
     }
 }
index 91625ae..33ad250 100644 (file)
@@ -52,15 +52,9 @@ namespace System.Text
         // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding
         // parent method is safe
         [System.Security.SecuritySafeCritical] // overrides public transparent member
-        public override unsafe int GetByteCount(String s)
+        public override int GetByteCount(String s)
         {
-            // Validate input
-            if (s==null)
-                throw new ArgumentNullException("s");
-            Contract.EndContractBlock();
-
-            fixed (char* pChars = s)
-                return GetByteCount(pChars, s.Length, null);
+            return EncodingForwarder.GetByteCount(this, s);
         }       
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
index 7a4cb58..110cff8 100644 (file)
@@ -106,15 +106,9 @@ namespace System.Text
         // parent method is safe
 
         [System.Security.SecuritySafeCritical]  // auto-generated
-        public override unsafe int GetByteCount(String s)
+        public override int GetByteCount(String s)
         {
-            // Validate input
-            if (s==null)
-                throw new ArgumentNullException("s");
-            Contract.EndContractBlock();
-
-            fixed (char* pChars = s)
-                return GetByteCount(pChars, s.Length, null);
+            return EncodingForwarder.GetByteCount(this, s);
         }
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
index edf5c95..c04a01e 100644 (file)
@@ -173,15 +173,9 @@ namespace System.Text
 
         [System.Security.SecuritySafeCritical]  // auto-generated
         [System.Runtime.InteropServices.ComVisible(false)]
-        public override unsafe int GetByteCount(String s)
+        public override int GetByteCount(String s)
         {
-            // Validate input
-            if (s==null)
-                throw new ArgumentNullException("s");
-            Contract.EndContractBlock();
-
-            fixed (char* pChars = s)
-                return GetByteCount(pChars, s.Length, null);
+            return EncodingForwarder.GetByteCount(this, s);
         }
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
index 84ca87b..a7cfaba 100644 (file)
@@ -130,15 +130,11 @@ namespace System.Text
         // parent method is safe
 
         [System.Security.SecuritySafeCritical]  // auto-generated
-        public override unsafe int GetByteCount(String chars)
+        public override int GetByteCount(String chars)
         {
-            // Validate input
-            if (chars==null)
-                throw new ArgumentNullException("s");
-            Contract.EndContractBlock();
-
-            fixed (char* pChars = chars)
-                return GetByteCount(pChars, chars.Length, null);
+            // NOTE: If chars is null, this will throw an ArgumentNullException
+            // with the parameter name "s" rather than "chars"
+            return EncodingForwarder.GetByteCount(this, chars);
         }
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
index 5fb2ad6..fb670b6 100644 (file)
@@ -104,15 +104,9 @@ namespace System.Text
         // parent method is safe
 
         [System.Security.SecuritySafeCritical]  // auto-generated
-        public override unsafe int GetByteCount(String s)
+        public override int GetByteCount(String s)
         {
-            // Validate input
-            if (s==null)
-                throw new ArgumentNullException("s");
-            Contract.EndContractBlock();
-
-            fixed (char* pChars = s)
-                return GetByteCount(pChars, s.Length, null);
+            return EncodingForwarder.GetByteCount(this, s);
         }
 
         // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)