From 860434db541daf533b43491280774dc4274590b8 Mon Sep 17 00:00:00 2001 From: James Ko Date: Mon, 4 Jul 2016 14:55:39 -0400 Subject: [PATCH] Use EncodingForwarder for GetChars(byte[], int, int, char[], int) --- src/mscorlib/src/System/Text/ASCIIEncoding.cs | 37 +----------------- .../src/System/Text/EncodingForwarder.cs | 38 +++++++++++++++++++ src/mscorlib/src/System/Text/EncodingNLS.cs | 37 +----------------- src/mscorlib/src/System/Text/UTF32Encoding.cs | 37 +----------------- src/mscorlib/src/System/Text/UTF7Encoding.cs | 37 +----------------- src/mscorlib/src/System/Text/UTF8Encoding.cs | 37 +----------------- .../src/System/Text/UnicodeEncoding.cs | 37 +----------------- 7 files changed, 50 insertions(+), 210 deletions(-) diff --git a/src/mscorlib/src/System/Text/ASCIIEncoding.cs b/src/mscorlib/src/System/Text/ASCIIEncoding.cs index 144dc9cf66d8..267ccc7c2b44 100644 --- a/src/mscorlib/src/System/Text/ASCIIEncoding.cs +++ b/src/mscorlib/src/System/Text/ASCIIEncoding.cs @@ -164,43 +164,10 @@ public override unsafe int GetCharCount(byte* bytes, int count) // parent method is safe [System.Security.SecuritySafeCritical] // auto-generated - public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, + public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { - // Validate Parameters - if (bytes == null || chars == null) - throw new ArgumentNullException(bytes == null ? "bytes" : "chars", - Environment.GetResourceString("ArgumentNull_Array")); - - if (byteIndex < 0 || byteCount < 0) - throw new ArgumentOutOfRangeException((byteIndex<0 ? "byteIndex" : "byteCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if ( bytes.Length - byteIndex < byteCount) - throw new ArgumentOutOfRangeException("bytes", - Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); - - if (charIndex < 0 || charIndex > chars.Length) - throw new ArgumentOutOfRangeException("charIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - // If no input, return 0 & avoid fixed problem - if (byteCount == 0) - return 0; - - // Just call pointer version - int charCount = chars.Length - charIndex; - - // Fixed doesn't like empty char arrays - if (chars.Length == 0) - chars = new char[1]; - - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) - // Remember that charCount is # to decode, not size of array - return GetChars(pBytes + byteIndex, byteCount, - pChars + charIndex, charCount, null); + return EncodingForwarder.GetChars(this, bytes, byteIndex, byteCount, chars, charIndex); } // 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 5cd897c288aa..deddad0e6af4 100644 --- a/src/mscorlib/src/System/Text/EncodingForwarder.cs +++ b/src/mscorlib/src/System/Text/EncodingForwarder.cs @@ -229,5 +229,43 @@ public unsafe static int GetCharCount(Encoding encoding, byte* bytes, int count) return encoding.GetCharCount(bytes, count, decoder: null); } + + public unsafe static int GetChars(Encoding encoding, byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) + { + Contract.Assert(encoding != null); + if (bytes == null || chars == null) + { + throw new ArgumentNullException(bytes == null ? "bytes" : "chars", Environment.GetResourceString("ArgumentNull_Array")); + } + if (byteIndex < 0 || byteCount < 0) + { + throw new ArgumentOutOfRangeException(byteIndex < 0 ? "byteIndex" : "byteCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + } + if (bytes.Length - byteIndex < byteCount) + { + throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); + } + if (charIndex < 0 || charIndex > chars.Length) + { + throw new ArgumentOutOfRangeException("charIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); + } + Contract.EndContractBlock(); + + if (byteCount == 0) + return 0; + + // NOTE: This is the # of chars we can decode, + // not the size of the array + int charCount = chars.Length - charIndex; + + // Fixed doesn't like 0 length arrays. + if (chars.Length == 0) + chars = new char[1]; + + fixed (byte* pBytes = bytes) fixed (char* pChars = chars) + { + return encoding.GetChars(pBytes + byteIndex, byteCount, pChars + charIndex, charCount, decoder: null); + } + } } } diff --git a/src/mscorlib/src/System/Text/EncodingNLS.cs b/src/mscorlib/src/System/Text/EncodingNLS.cs index f684407f4292..d24a10984661 100644 --- a/src/mscorlib/src/System/Text/EncodingNLS.cs +++ b/src/mscorlib/src/System/Text/EncodingNLS.cs @@ -133,43 +133,10 @@ public override unsafe int GetCharCount(byte* bytes, int count) // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding // parent method is safe [System.Security.SecuritySafeCritical] // overrides public transparent member - public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, + public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { - // Validate Parameters - if (bytes == null || chars == null) - throw new ArgumentNullException(bytes == null ? "bytes" : "chars", - Environment.GetResourceString("ArgumentNull_Array")); - - if (byteIndex < 0 || byteCount < 0) - throw new ArgumentOutOfRangeException((byteIndex<0 ? "byteIndex" : "byteCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if ( bytes.Length - byteIndex < byteCount) - throw new ArgumentOutOfRangeException("bytes", - Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); - - if (charIndex < 0 || charIndex > chars.Length) - throw new ArgumentOutOfRangeException("charIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - // If no input, return 0 & avoid fixed problem - if (byteCount == 0) - return 0; - - // Just call pointer version - int charCount = chars.Length - charIndex; - - // Fixed doesn't like empty arrays - if (chars.Length == 0) - chars = new char[1]; - - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) - // Remember that charCount is # to decode, not size of array - return GetChars(pBytes + byteIndex, byteCount, - pChars + charIndex, charCount, null); + return EncodingForwarder.GetChars(this, bytes, byteIndex, byteCount, chars, charIndex); } // 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 3259d9413395..6ce6702e71be 100644 --- a/src/mscorlib/src/System/Text/UTF32Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF32Encoding.cs @@ -197,43 +197,10 @@ public override unsafe int GetCharCount(byte* bytes, int count) // parent method is safe [System.Security.SecuritySafeCritical] // auto-generated - public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, + public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { - // Validate Parameters - if (bytes == null || chars == null) - throw new ArgumentNullException(bytes == null ? "bytes" : "chars", - Environment.GetResourceString("ArgumentNull_Array")); - - if (byteIndex < 0 || byteCount < 0) - throw new ArgumentOutOfRangeException((byteIndex<0 ? "byteIndex" : "byteCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if ( bytes.Length - byteIndex < byteCount) - throw new ArgumentOutOfRangeException("bytes", - Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); - - if (charIndex < 0 || charIndex > chars.Length) - throw new ArgumentOutOfRangeException("charIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - // If no input, return 0 & avoid fixed problem - if (byteCount == 0) - return 0; - - // Just call pointer version - int charCount = chars.Length - charIndex; - - // Fix our input array if 0 length because fixed doesn't like 0 length arrays - if (chars.Length == 0) - chars = new char[1]; - - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) - // Remember that charCount is # to decode, not size of array - return GetChars(pBytes + byteIndex, byteCount, - pChars + charIndex, charCount, null); + return EncodingForwarder.GetChars(this, bytes, byteIndex, byteCount, chars, charIndex); } // 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 bac655620162..aca8a0c8e2fc 100644 --- a/src/mscorlib/src/System/Text/UTF7Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF7Encoding.cs @@ -268,43 +268,10 @@ public override unsafe int GetCharCount(byte* bytes, int count) // parent method is safe [System.Security.SecuritySafeCritical] // auto-generated - public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, + public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { - // Validate Parameters - if (bytes == null || chars == null) - throw new ArgumentNullException(bytes == null ? "bytes" : "chars", - Environment.GetResourceString("ArgumentNull_Array")); - - if (byteIndex < 0 || byteCount < 0) - throw new ArgumentOutOfRangeException((byteIndex<0 ? "byteIndex" : "byteCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if ( bytes.Length - byteIndex < byteCount) - throw new ArgumentOutOfRangeException("bytes", - Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); - - if (charIndex < 0 || charIndex > chars.Length) - throw new ArgumentOutOfRangeException("charIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - // If no input, return 0 & avoid fixed problem - if (byteCount == 0) - return 0; - - // Just call pointer version - int charCount = chars.Length - charIndex; - - // Fixed doesn't like empty arrays - if (chars.Length == 0) - chars = new char[1]; - - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) - // Remember that charCount is # to decode, not size of array - return GetChars(pBytes + byteIndex, byteCount, - pChars + charIndex, charCount, null); + return EncodingForwarder.GetChars(this, bytes, byteIndex, byteCount, chars, charIndex); } // 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 3b8114f38116..d4c6f123d29d 100644 --- a/src/mscorlib/src/System/Text/UTF8Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF8Encoding.cs @@ -226,43 +226,10 @@ public override unsafe int GetCharCount(byte* bytes, int count) // parent method is safe [System.Security.SecuritySafeCritical] // auto-generated - public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, + public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { - // Validate Parameters - if (bytes == null || chars == null) - throw new ArgumentNullException(bytes == null ? "bytes" : "chars", - Environment.GetResourceString("ArgumentNull_Array")); - - if (byteIndex < 0 || byteCount < 0) - throw new ArgumentOutOfRangeException((byteIndex<0 ? "byteIndex" : "byteCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if ( bytes.Length - byteIndex < byteCount) - throw new ArgumentOutOfRangeException("bytes", - Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); - - if (charIndex < 0 || charIndex > chars.Length) - throw new ArgumentOutOfRangeException("charIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - // If no input, return 0 & avoid fixed problem - if (byteCount == 0) - return 0; - - // Just call pointer version - int charCount = chars.Length - charIndex; - - // Fixed doesn't like 0 length arrays. - if (chars.Length == 0) - chars = new char[1]; - - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) - // Remember that charCount is # to decode, not size of array - return GetChars(pBytes + byteIndex, byteCount, - pChars + charIndex, charCount, null); + return EncodingForwarder.GetChars(this, bytes, byteIndex, byteCount, chars, charIndex); } // 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 b52858a32a64..0d8a9aa09b43 100644 --- a/src/mscorlib/src/System/Text/UnicodeEncoding.cs +++ b/src/mscorlib/src/System/Text/UnicodeEncoding.cs @@ -198,43 +198,10 @@ public override unsafe int GetCharCount(byte* bytes, int count) // parent method is safe [System.Security.SecuritySafeCritical] // auto-generated - public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, + public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex) { - // Validate Parameters - if (bytes == null || chars == null) - throw new ArgumentNullException(bytes == null ? "bytes" : "chars", - Environment.GetResourceString("ArgumentNull_Array")); - - if (byteIndex < 0 || byteCount < 0) - throw new ArgumentOutOfRangeException((byteIndex<0 ? "byteIndex" : "byteCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if ( bytes.Length - byteIndex < byteCount) - throw new ArgumentOutOfRangeException("bytes", - Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer")); - - if (charIndex < 0 || charIndex > chars.Length) - throw new ArgumentOutOfRangeException("charIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - // If no input, return 0 & avoid fixed problem - if (byteCount == 0) - return 0; - - // Just call pointer version - int charCount = chars.Length - charIndex; - - // Fixed doesn't like 0 length arrays. - if (chars.Length == 0) - chars = new char[1]; - - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) - // Remember that charCount is # to decode, not size of array - return GetChars(pBytes + byteIndex, byteCount, - pChars + charIndex, charCount, null); + return EncodingForwarder.GetChars(this, bytes, byteIndex, byteCount, chars, charIndex); } // All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)