From 72a39525abd027e4f1daa1a208f99ea4b92e8ab1 Mon Sep 17 00:00:00 2001 From: James Ko Date: Mon, 4 Jul 2016 11:57:58 -0400 Subject: [PATCH] Use EncodingForwarder for GetByteCount(string) Commit migrated from https://github.com/dotnet/coreclr/commit/e61479088e1ba1f1d8756b8da1a352d518c015c8 --- .../src/mscorlib/src/System/Text/ASCIIEncoding.cs | 12 ++++------- .../mscorlib/src/System/Text/EncodingForwarder.cs | 24 ++++++++++++++++++++-- .../src/mscorlib/src/System/Text/EncodingNLS.cs | 10 ++------- .../src/mscorlib/src/System/Text/UTF32Encoding.cs | 10 ++------- .../src/mscorlib/src/System/Text/UTF7Encoding.cs | 10 ++------- .../src/mscorlib/src/System/Text/UTF8Encoding.cs | 12 ++++------- .../mscorlib/src/System/Text/UnicodeEncoding.cs | 10 ++------- 7 files changed, 38 insertions(+), 50 deletions(-) diff --git a/src/coreclr/src/mscorlib/src/System/Text/ASCIIEncoding.cs b/src/coreclr/src/mscorlib/src/System/Text/ASCIIEncoding.cs index cc9f6aa..2249d91 100644 --- a/src/coreclr/src/mscorlib/src/System/Text/ASCIIEncoding.cs +++ b/src/coreclr/src/mscorlib/src/System/Text/ASCIIEncoding.cs @@ -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) diff --git a/src/coreclr/src/mscorlib/src/System/Text/EncodingForwarder.cs b/src/coreclr/src/mscorlib/src/System/Text/EncodingForwarder.cs index 75f8343..93be85d 100644 --- a/src/coreclr/src/mscorlib/src/System/Text/EncodingForwarder.cs +++ b/src/coreclr/src/mscorlib/src/System/Text/EncodingForwarder.cs @@ -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); } } } diff --git a/src/coreclr/src/mscorlib/src/System/Text/EncodingNLS.cs b/src/coreclr/src/mscorlib/src/System/Text/EncodingNLS.cs index 91625ae..33ad250 100644 --- a/src/coreclr/src/mscorlib/src/System/Text/EncodingNLS.cs +++ b/src/coreclr/src/mscorlib/src/System/Text/EncodingNLS.cs @@ -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) diff --git a/src/coreclr/src/mscorlib/src/System/Text/UTF32Encoding.cs b/src/coreclr/src/mscorlib/src/System/Text/UTF32Encoding.cs index 7a4cb58..110cff8 100644 --- a/src/coreclr/src/mscorlib/src/System/Text/UTF32Encoding.cs +++ b/src/coreclr/src/mscorlib/src/System/Text/UTF32Encoding.cs @@ -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) diff --git a/src/coreclr/src/mscorlib/src/System/Text/UTF7Encoding.cs b/src/coreclr/src/mscorlib/src/System/Text/UTF7Encoding.cs index edf5c95..c04a01e 100644 --- a/src/coreclr/src/mscorlib/src/System/Text/UTF7Encoding.cs +++ b/src/coreclr/src/mscorlib/src/System/Text/UTF7Encoding.cs @@ -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) diff --git a/src/coreclr/src/mscorlib/src/System/Text/UTF8Encoding.cs b/src/coreclr/src/mscorlib/src/System/Text/UTF8Encoding.cs index 84ca87b..a7cfaba 100644 --- a/src/coreclr/src/mscorlib/src/System/Text/UTF8Encoding.cs +++ b/src/coreclr/src/mscorlib/src/System/Text/UTF8Encoding.cs @@ -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) diff --git a/src/coreclr/src/mscorlib/src/System/Text/UnicodeEncoding.cs b/src/coreclr/src/mscorlib/src/System/Text/UnicodeEncoding.cs index 5fb2ad6..fb670b6 100644 --- a/src/coreclr/src/mscorlib/src/System/Text/UnicodeEncoding.cs +++ b/src/coreclr/src/mscorlib/src/System/Text/UnicodeEncoding.cs @@ -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) -- 2.7.4