diff options
Diffstat (limited to 'src/mscorlib/src/System/Text/Encoding.cs')
-rw-r--r-- | src/mscorlib/src/System/Text/Encoding.cs | 60 |
1 files changed, 34 insertions, 26 deletions
diff --git a/src/mscorlib/src/System/Text/Encoding.cs b/src/mscorlib/src/System/Text/Encoding.cs index ee4b7ec64c..8cb01e41fa 100644 --- a/src/mscorlib/src/System/Text/Encoding.cs +++ b/src/mscorlib/src/System/Text/Encoding.cs @@ -745,14 +745,20 @@ namespace System.Text [Pure] public int GetByteCount(string s, int index, int count) { - if ((s == null) || - (index < 0) || - (count < 0) || - (index > s.Length - count)) - { - EncodingForwarder.ThrowValidationFailed(s, index, count); - } + if (s == null) + throw new ArgumentNullException(nameof(s), + Environment.GetResourceString("ArgumentNull_String")); + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index), + Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count), + Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + if (index > s.Length - count) + throw new ArgumentOutOfRangeException(nameof(index), + Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); Contract.EndContractBlock(); + unsafe { fixed (char* pChar = s) @@ -859,37 +865,39 @@ namespace System.Text // string range. // [Pure] - public unsafe byte[] GetBytes(string s, int index, int count) + public byte[] GetBytes(string s, int index, int count) { - if ((s == null) || - (index < 0) || - (count < 0) || - (index > s.Length - count)) - { - EncodingForwarder.ThrowValidationFailed(s, index, count); - } + if (s == null) + throw new ArgumentNullException(nameof(s), + Environment.GetResourceString("ArgumentNull_String")); + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index), + Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + if (count < 0) + throw new ArgumentOutOfRangeException(nameof(count), + Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + if (index > s.Length - count) + throw new ArgumentOutOfRangeException(nameof(index), + Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); Contract.EndContractBlock(); - byte[] bytes; - fixed (char* pChar = s) + unsafe { - int byteCount = GetByteCount(pChar + index, count); - if (byteCount == 0) - { - bytes = Array.Empty<byte>(); - } - else + fixed (char* pChar = s) { - bytes = new byte[byteCount]; + int byteCount = GetByteCount(pChar + index, count); + if (byteCount == 0) + return Array.Empty<byte>(); + + byte[] bytes = new byte[byteCount]; fixed (byte* pBytes = &bytes[0]) { int bytesReceived = GetBytes(pChar + index, count, pBytes, byteCount); Debug.Assert(byteCount == bytesReceived); } + return bytes; } } - - return bytes; } public virtual int GetBytes(String s, int charIndex, int charCount, |