diff --git a/src/mscorlib/src/System/Text/ASCIIEncoding.cs b/src/mscorlib/src/System/Text/ASCIIEncoding.cs index f94b70f70c70..144dc9cf66d8 100644 --- a/src/mscorlib/src/System/Text/ASCIIEncoding.cs +++ b/src/mscorlib/src/System/Text/ASCIIEncoding.cs @@ -155,17 +155,7 @@ public override int GetCharCount(byte[] bytes, int index, int count) [System.Runtime.InteropServices.ComVisible(false)] public override unsafe int GetCharCount(byte* bytes, int count) { - // Validate Parameters - if (bytes == null) - throw new ArgumentNullException("bytes", - Environment.GetResourceString("ArgumentNull_Array")); - - if (count < 0) - throw new ArgumentOutOfRangeException("count", - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - Contract.EndContractBlock(); - - return GetCharCount(bytes, count, null); + return EncodingForwarder.GetCharCount(this, bytes, count); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) diff --git a/src/mscorlib/src/System/Text/EncodingForwarder.cs b/src/mscorlib/src/System/Text/EncodingForwarder.cs index 3d807004ddce..5cd897c288aa 100644 --- a/src/mscorlib/src/System/Text/EncodingForwarder.cs +++ b/src/mscorlib/src/System/Text/EncodingForwarder.cs @@ -22,6 +22,14 @@ internal static class EncodingForwarder // These set of methods exist so instead of duplicating code, we can // simply have those overriden methods call here to do the actual work. + // NOTE: This class should ONLY be called from Encodings that override + // the internal methods which accept an Encoder/DecoderNLS. The reason + // for this is that by default, those methods just call the same overload + // except without the encoder/decoder parameter. If an overriden method + // without that parameter calls this class, which calls the overload with + // the parameter, it will call the same method again, which will eventually + // lead to a StackOverflowException. + public unsafe static int GetByteCount(Encoding encoding, char[] chars, int index, int count) { // Validate parameters @@ -205,5 +213,21 @@ public unsafe static int GetCharCount(Encoding encoding, byte[] bytes, int index fixed (byte* pBytes = bytes) return encoding.GetCharCount(pBytes + index, count, decoder: null); } + + public unsafe static int GetCharCount(Encoding encoding, byte* bytes, int count) + { + Contract.Assert(encoding != null); + if (bytes == null) + { + throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array")); + } + if (count < 0) + { + throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + } + Contract.EndContractBlock(); + + return encoding.GetCharCount(bytes, count, decoder: null); + } } } diff --git a/src/mscorlib/src/System/Text/EncodingNLS.cs b/src/mscorlib/src/System/Text/EncodingNLS.cs index 4d4c8cd3c975..f684407f4292 100644 --- a/src/mscorlib/src/System/Text/EncodingNLS.cs +++ b/src/mscorlib/src/System/Text/EncodingNLS.cs @@ -125,17 +125,7 @@ public override int GetCharCount(byte[] bytes, int index, int count) [System.Security.SecurityCritical] // auto-generated public override unsafe int GetCharCount(byte* bytes, int count) { - // Validate Parameters - if (bytes == null) - throw new ArgumentNullException("bytes", - Environment.GetResourceString("ArgumentNull_Array")); - - if (count < 0) - throw new ArgumentOutOfRangeException("count", - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - Contract.EndContractBlock(); - - return GetCharCount(bytes, count, null); + return EncodingForwarder.GetCharCount(this, bytes, count); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) diff --git a/src/mscorlib/src/System/Text/UTF32Encoding.cs b/src/mscorlib/src/System/Text/UTF32Encoding.cs index 69172fa4479e..3259d9413395 100644 --- a/src/mscorlib/src/System/Text/UTF32Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF32Encoding.cs @@ -188,17 +188,7 @@ public override int GetCharCount(byte[] bytes, int index, int count) [CLSCompliant(false)] public override unsafe int GetCharCount(byte* bytes, int count) { - // Validate Parameters - if (bytes == null) - throw new ArgumentNullException("bytes", - Environment.GetResourceString("ArgumentNull_Array")); - - if (count < 0) - throw new ArgumentOutOfRangeException("count", - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - Contract.EndContractBlock(); - - return GetCharCount(bytes, count, null); + return EncodingForwarder.GetCharCount(this, bytes, count); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) diff --git a/src/mscorlib/src/System/Text/UTF7Encoding.cs b/src/mscorlib/src/System/Text/UTF7Encoding.cs index 42b481ad681d..bac655620162 100644 --- a/src/mscorlib/src/System/Text/UTF7Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF7Encoding.cs @@ -259,17 +259,7 @@ public override int GetCharCount(byte[] bytes, int index, int count) [System.Runtime.InteropServices.ComVisible(false)] public override unsafe int GetCharCount(byte* bytes, int count) { - // Validate Parameters - if (bytes == null) - throw new ArgumentNullException("bytes", - Environment.GetResourceString("ArgumentNull_Array")); - - if (count < 0) - throw new ArgumentOutOfRangeException("count", - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - Contract.EndContractBlock(); - - return GetCharCount(bytes, count, null); + return EncodingForwarder.GetCharCount(this, bytes, count); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) diff --git a/src/mscorlib/src/System/Text/UTF8Encoding.cs b/src/mscorlib/src/System/Text/UTF8Encoding.cs index 96b83ff1c234..3b8114f38116 100644 --- a/src/mscorlib/src/System/Text/UTF8Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF8Encoding.cs @@ -217,17 +217,7 @@ public override int GetCharCount(byte[] bytes, int index, int count) [System.Runtime.InteropServices.ComVisible(false)] public override unsafe int GetCharCount(byte* bytes, int count) { - // Validate Parameters - if (bytes == null) - throw new ArgumentNullException("bytes", - Environment.GetResourceString("ArgumentNull_Array")); - - if (count < 0) - throw new ArgumentOutOfRangeException("count", - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - Contract.EndContractBlock(); - - return GetCharCount(bytes, count, null); + return EncodingForwarder.GetCharCount(this, bytes, count); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS) diff --git a/src/mscorlib/src/System/Text/UnicodeEncoding.cs b/src/mscorlib/src/System/Text/UnicodeEncoding.cs index 4c5dff773f88..b52858a32a64 100644 --- a/src/mscorlib/src/System/Text/UnicodeEncoding.cs +++ b/src/mscorlib/src/System/Text/UnicodeEncoding.cs @@ -189,17 +189,7 @@ public override int GetCharCount(byte[] bytes, int index, int count) [System.Runtime.InteropServices.ComVisible(false)] public override unsafe int GetCharCount(byte* bytes, int count) { - // Validate Parameters - if (bytes == null) - throw new ArgumentNullException("bytes", - Environment.GetResourceString("ArgumentNull_Array")); - - if (count < 0) - throw new ArgumentOutOfRangeException("count", - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - Contract.EndContractBlock(); - - return GetCharCount(bytes, count, null); + return EncodingForwarder.GetCharCount(this, bytes, count); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)