Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Use EncodingForwarder for GetChars(byte[], int, int, char[], int)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesqo committed Jul 4, 2016
1 parent f83ecbe commit 860434d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 210 deletions.
37 changes: 2 additions & 35 deletions src/mscorlib/src/System/Text/ASCIIEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
38 changes: 38 additions & 0 deletions src/mscorlib/src/System/Text/EncodingForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
37 changes: 2 additions & 35 deletions src/mscorlib/src/System/Text/EncodingNLS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 2 additions & 35 deletions src/mscorlib/src/System/Text/UTF32Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 2 additions & 35 deletions src/mscorlib/src/System/Text/UTF7Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 2 additions & 35 deletions src/mscorlib/src/System/Text/UTF8Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 2 additions & 35 deletions src/mscorlib/src/System/Text/UnicodeEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 860434d

Please sign in to comment.