From fa55fd74ae3e9374468d32db7371c82e6ae13593 Mon Sep 17 00:00:00 2001 From: James Ko Date: Mon, 4 Jul 2016 12:25:21 -0400 Subject: [PATCH] Use EncodingForwarder for GetBytes(string, int, int, byte[], int) --- src/mscorlib/src/System/Text/ASCIIEncoding.cs | 31 ++-------------- .../src/System/Text/EncodingForwarder.cs | 36 +++++++++++++++++++ src/mscorlib/src/System/Text/EncodingNLS.cs | 30 ++-------------- src/mscorlib/src/System/Text/UTF32Encoding.cs | 30 ++-------------- src/mscorlib/src/System/Text/UTF7Encoding.cs | 30 ++-------------- src/mscorlib/src/System/Text/UTF8Encoding.cs | 30 ++-------------- .../src/System/Text/UnicodeEncoding.cs | 30 ++-------------- 7 files changed, 49 insertions(+), 168 deletions(-) diff --git a/src/mscorlib/src/System/Text/ASCIIEncoding.cs b/src/mscorlib/src/System/Text/ASCIIEncoding.cs index 595fa45380f1..dfe3bcca795d 100644 --- a/src/mscorlib/src/System/Text/ASCIIEncoding.cs +++ b/src/mscorlib/src/System/Text/ASCIIEncoding.cs @@ -92,36 +92,11 @@ public override unsafe int GetByteCount(char* chars, int count) // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding [System.Security.SecuritySafeCritical] // auto-generated - public override unsafe int GetBytes(String chars, int charIndex, int charCount, + public override int GetBytes(String chars, int charIndex, int charCount, byte[] bytes, int byteIndex) { - if (chars == null || bytes == null) - throw new ArgumentNullException((chars == null ? "chars" : "bytes"), - Environment.GetResourceString("ArgumentNull_Array")); - - if (charIndex < 0 || charCount < 0) - throw new ArgumentOutOfRangeException((charIndex<0 ? "charIndex" : "charCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if (chars.Length - charIndex < charCount) - throw new ArgumentOutOfRangeException("chars", - Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); - - if (byteIndex < 0 || byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException("byteIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - int byteCount = bytes.Length - byteIndex; - - // Fixed doesn't like empty byte arrays - if (bytes.Length == 0) - bytes = new byte[1]; - - fixed (char* pChars = chars) - fixed ( byte* pBytes = bytes) - return GetBytes(pChars + charIndex, charCount, - pBytes + byteIndex, byteCount, null); + // NOTE: Will throw ArgumentNull of "s" is chars is null. + return EncodingForwarder.GetBytes(this, chars, charIndex, charCount, bytes, byteIndex); } // Encodes a range of characters in a character array into a range of bytes diff --git a/src/mscorlib/src/System/Text/EncodingForwarder.cs b/src/mscorlib/src/System/Text/EncodingForwarder.cs index 5820c4a22b40..1b701dc49241 100644 --- a/src/mscorlib/src/System/Text/EncodingForwarder.cs +++ b/src/mscorlib/src/System/Text/EncodingForwarder.cs @@ -87,5 +87,41 @@ public unsafe static int GetByteCount(Encoding encoding, char* chars, int count) // Call the internal version, with an empty encoder return encoding.GetByteCount(chars, count, encoder: null); } + + public unsafe static int GetBytes(Encoding encoding, string s, int charIndex, int charCount, byte[] bytes, int byteIndex) + { + Contract.Assert(encoding != null); + if (s == null || bytes == null) + { + throw new ArgumentNullException(s == null ? "s" : "bytes", Environment.GetResourceString("ArgumentNull_Array")); + } + if (charIndex < 0 || charCount < 0) + { + throw new ArgumentOutOfRangeException(charIndex < 0 ? "charIndex" : "charCount", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + } + if (s.Length - charIndex < charCount) + { + throw new ArgumentOutOfRangeException("s", Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); + } + if (byteIndex < 0 || byteIndex > bytes.Length) + { + throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index")); + } + Contract.EndContractBlock(); + + int byteCount = bytes.Length - byteIndex; + + // Fixed doesn't like empty arrays + // TODO: Consider just throwing an + // exception here instead of allocating + // a new array, if (byteCount == 0) + if (bytes.Length == 0) + bytes = new byte[1]; + + fixed (char* pChars = s) fixed (byte* pBytes = bytes) + { + return encoding.GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, encoder: null); + } + } } } diff --git a/src/mscorlib/src/System/Text/EncodingNLS.cs b/src/mscorlib/src/System/Text/EncodingNLS.cs index 438db1fa4dc8..84dd90acf0f7 100644 --- a/src/mscorlib/src/System/Text/EncodingNLS.cs +++ b/src/mscorlib/src/System/Text/EncodingNLS.cs @@ -71,36 +71,10 @@ public override unsafe int GetByteCount(char* chars, int count) // So if you fix this, fix the others. Currently those include: // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding [System.Security.SecuritySafeCritical] // overrides public transparent member - public override unsafe int GetBytes(String s, int charIndex, int charCount, + public override int GetBytes(String s, int charIndex, int charCount, byte[] bytes, int byteIndex) { - if (s == null || bytes == null) - throw new ArgumentNullException((s == null ? "s" : "bytes"), - Environment.GetResourceString("ArgumentNull_Array")); - - if (charIndex < 0 || charCount < 0) - throw new ArgumentOutOfRangeException((charIndex<0 ? "charIndex" : "charCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if (s.Length - charIndex < charCount) - throw new ArgumentOutOfRangeException("s", - Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); - - if (byteIndex < 0 || byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException("byteIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - int byteCount = bytes.Length - byteIndex; - - // Fixed doesn't like empty arrays - if (bytes.Length == 0) - bytes = new byte[1]; - - fixed (char* pChars = s) - fixed ( byte* pBytes = bytes) - return GetBytes(pChars + charIndex, charCount, - pBytes + byteIndex, byteCount, null); + return EncodingForwarder.GetBytes(this, s, charIndex, charCount, bytes, byteIndex); } // Encodes a range of characters in a character array into a range of bytes diff --git a/src/mscorlib/src/System/Text/UTF32Encoding.cs b/src/mscorlib/src/System/Text/UTF32Encoding.cs index 44e764dcee7c..5407ea7565fb 100644 --- a/src/mscorlib/src/System/Text/UTF32Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF32Encoding.cs @@ -128,36 +128,10 @@ public override unsafe int GetByteCount(char* chars, int count) // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding [System.Security.SecuritySafeCritical] // auto-generated - public override unsafe int GetBytes(String s, int charIndex, int charCount, + public override int GetBytes(String s, int charIndex, int charCount, byte[] bytes, int byteIndex) { - if (s == null || bytes == null) - throw new ArgumentNullException((s == null ? "s" : "bytes"), - Environment.GetResourceString("ArgumentNull_Array")); - - if (charIndex < 0 || charCount < 0) - throw new ArgumentOutOfRangeException((charIndex<0 ? "charIndex" : "charCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if (s.Length - charIndex < charCount) - throw new ArgumentOutOfRangeException("s", - Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); - - if (byteIndex < 0 || byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException("byteIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - int byteCount = bytes.Length - byteIndex; - - // Fix our input array if 0 length because fixed doesn't like 0 length arrays - if (bytes.Length == 0) - bytes = new byte[1]; - - fixed (char* pChars = s) - fixed ( byte* pBytes = bytes) - return GetBytes(pChars + charIndex, charCount, - pBytes + byteIndex, byteCount, null); + return EncodingForwarder.GetBytes(this, s, charIndex, charCount, bytes, byteIndex); } // Encodes a range of characters in a character array into a range of bytes diff --git a/src/mscorlib/src/System/Text/UTF7Encoding.cs b/src/mscorlib/src/System/Text/UTF7Encoding.cs index 6f9f35df9e73..20d9b80a408b 100644 --- a/src/mscorlib/src/System/Text/UTF7Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF7Encoding.cs @@ -197,36 +197,10 @@ public override unsafe int GetByteCount(char* chars, int count) [System.Security.SecuritySafeCritical] // auto-generated [System.Runtime.InteropServices.ComVisible(false)] - public override unsafe int GetBytes(String s, int charIndex, int charCount, + public override int GetBytes(String s, int charIndex, int charCount, byte[] bytes, int byteIndex) { - if (s == null || bytes == null) - throw new ArgumentNullException((s == null ? "s" : "bytes"), - Environment.GetResourceString("ArgumentNull_Array")); - - if (charIndex < 0 || charCount < 0) - throw new ArgumentOutOfRangeException((charIndex<0 ? "charIndex" : "charCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if (s.Length - charIndex < charCount) - throw new ArgumentOutOfRangeException("s", - Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); - - if (byteIndex < 0 || byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException("byteIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - int byteCount = bytes.Length - byteIndex; - - // Fixed doesn't like empty arrays - if (bytes.Length == 0) - bytes = new byte[1]; - - fixed (char* pChars = s) - fixed ( byte* pBytes = bytes) - return GetBytes(pChars + charIndex, charCount, - pBytes + byteIndex, byteCount, null); + return EncodingForwarder.GetBytes(this, s, charIndex, charCount, bytes, byteIndex); } // Encodes a range of characters in a character array into a range of bytes diff --git a/src/mscorlib/src/System/Text/UTF8Encoding.cs b/src/mscorlib/src/System/Text/UTF8Encoding.cs index 32b219e0ec67..9d0e17103447 100644 --- a/src/mscorlib/src/System/Text/UTF8Encoding.cs +++ b/src/mscorlib/src/System/Text/UTF8Encoding.cs @@ -155,36 +155,10 @@ public override unsafe int GetByteCount(char* chars, int count) // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding [System.Security.SecuritySafeCritical] // auto-generated - public override unsafe int GetBytes(String s, int charIndex, int charCount, + public override int GetBytes(String s, int charIndex, int charCount, byte[] bytes, int byteIndex) { - if (s == null || bytes == null) - throw new ArgumentNullException((s == null ? "s" : "bytes"), - Environment.GetResourceString("ArgumentNull_Array")); - - if (charIndex < 0 || charCount < 0) - throw new ArgumentOutOfRangeException((charIndex<0 ? "charIndex" : "charCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if (s.Length - charIndex < charCount) - throw new ArgumentOutOfRangeException("s", - Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); - - if (byteIndex < 0 || byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException("byteIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - int byteCount = bytes.Length - byteIndex; - - // Fixed doesn't like 0 length arrays. - if (bytes.Length == 0) - bytes = new byte[1]; - - fixed (char* pChars = s) - fixed ( byte* pBytes = bytes) - return GetBytes(pChars + charIndex, charCount, - pBytes + byteIndex, byteCount, null); + return EncodingForwarder.GetBytes(this, s, charIndex, charCount, bytes, byteIndex); } // Encodes a range of characters in a character array into a range of bytes diff --git a/src/mscorlib/src/System/Text/UnicodeEncoding.cs b/src/mscorlib/src/System/Text/UnicodeEncoding.cs index dba764358b0a..84782a95bec0 100644 --- a/src/mscorlib/src/System/Text/UnicodeEncoding.cs +++ b/src/mscorlib/src/System/Text/UnicodeEncoding.cs @@ -127,36 +127,10 @@ public override unsafe int GetByteCount(char* chars, int count) // EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding [System.Security.SecuritySafeCritical] // auto-generated - public override unsafe int GetBytes(String s, int charIndex, int charCount, + public override int GetBytes(String s, int charIndex, int charCount, byte[] bytes, int byteIndex) { - if (s == null || bytes == null) - throw new ArgumentNullException((s == null ? "s" : "bytes"), - Environment.GetResourceString("ArgumentNull_Array")); - - if (charIndex < 0 || charCount < 0) - throw new ArgumentOutOfRangeException((charIndex<0 ? "charIndex" : "charCount"), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); - - if (s.Length - charIndex < charCount) - throw new ArgumentOutOfRangeException("s", - Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); - - if (byteIndex < 0 || byteIndex > bytes.Length) - throw new ArgumentOutOfRangeException("byteIndex", - Environment.GetResourceString("ArgumentOutOfRange_Index")); - Contract.EndContractBlock(); - - int byteCount = bytes.Length - byteIndex; - - // Fixed doesn't like 0 length arrays. - if (bytes.Length == 0) - bytes = new byte[1]; - - fixed (char* pChars = s) - fixed ( byte* pBytes = bytes) - return GetBytes(pChars + charIndex, charCount, - pBytes + byteIndex, byteCount, null); + return EncodingForwarder.GetBytes(this, s, charIndex, charCount, bytes, byteIndex); } // Encodes a range of characters in a character array into a range of bytes