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 GetBytes(char[], int, int, byte[], int)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesqo committed Jul 4, 2016
1 parent fa55fd7 commit 0889431
Show file tree
Hide file tree
Showing 7 changed files with 52 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 @@ -114,43 +114,10 @@ public override int GetBytes(String chars, int charIndex, int charCount,
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
public override int GetBytes(char[] chars, int charIndex, int charCount,
byte[] bytes, int byteIndex)
{
// Validate parameters
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_IndexCountBuffer"));

if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException("byteIndex",
Environment.GetResourceString("ArgumentOutOfRange_Index"));
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (charCount == 0)
return 0;

// Just call pointer version
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)
// Remember that byteCount is # to decode, not size of array.
return GetBytes(pChars + charIndex, charCount,
pBytes + byteIndex, byteCount, null);
return EncodingForwarder.GetBytes(this, chars, charIndex, charCount, bytes, byteIndex);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
40 changes: 40 additions & 0 deletions src/mscorlib/src/System/Text/EncodingForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,45 @@ public unsafe static int GetBytes(Encoding encoding, string s, int charIndex, in
return encoding.GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, encoder: null);
}
}

public unsafe static int GetBytes(Encoding encoding, char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
Contract.Assert(encoding != null);
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_IndexCountBuffer"));
}
if (byteIndex < 0 || byteIndex > bytes.Length)
{
throw new ArgumentOutOfRangeException("byteIndex", Environment.GetResourceString("ArgumentOutOfRange_Index"));
}
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (charCount == 0)
return 0;

// Note that this is the # of bytes to decode,
// not the size of the array
int byteCount = bytes.Length - byteIndex;

// Fixed doesn't like 0 length arrays.
if (bytes.Length == 0)
bytes = new byte[1];

// Just call the (internal) pointer version
fixed (char* pChars = chars) fixed (byte* pBytes = bytes)
{
return encoding.GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, encoder: 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 @@ -91,43 +91,10 @@ public override int GetBytes(String s, int charIndex, int charCount,
// EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding
// parent method is safe
[System.Security.SecuritySafeCritical] // overrides public transparent member
public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
public override int GetBytes(char[] chars, int charIndex, int charCount,
byte[] bytes, int byteIndex)
{
// Validate parameters
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_IndexCountBuffer"));

if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException("byteIndex",
Environment.GetResourceString("ArgumentOutOfRange_Index"));
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (charCount == 0)
return 0;

// Just call pointer version
int byteCount = bytes.Length - byteIndex;

// Fixed doesn't like empty arrays
if (bytes.Length == 0)
bytes = new byte[1];

fixed (char* pChars = chars)
fixed (byte* pBytes = bytes)
// Remember that byteCount is # to decode, not size of array.
return GetBytes(pChars + charIndex, charCount,
pBytes + byteIndex, byteCount, null);
return EncodingForwarder.GetBytes(this, chars, charIndex, charCount, bytes, byteIndex);
}

// 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 @@ -149,43 +149,10 @@ public override int GetBytes(String s, int charIndex, int charCount,
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
public override int GetBytes(char[] chars, int charIndex, int charCount,
byte[] bytes, int byteIndex)
{
// Validate parameters
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_IndexCountBuffer"));

if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException("byteIndex",
Environment.GetResourceString("ArgumentOutOfRange_Index"));
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (charCount == 0)
return 0;

// Just call pointer version
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 = chars)
fixed (byte* pBytes = bytes)
// Remember that byteCount is # to decode, not size of array.
return GetBytes(pChars + charIndex, charCount,
pBytes + byteIndex, byteCount, null);
return EncodingForwarder.GetBytes(this, chars, charIndex, charCount, bytes, byteIndex);
}

// 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 @@ -218,43 +218,10 @@ public override int GetBytes(String s, int charIndex, int charCount,
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
public override int GetBytes(char[] chars, int charIndex, int charCount,
byte[] bytes, int byteIndex)
{
// Validate parameters
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_IndexCountBuffer"));

if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException("byteIndex",
Environment.GetResourceString("ArgumentOutOfRange_Index"));
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (charCount == 0)
return 0;

// Just call pointer version
int byteCount = bytes.Length - byteIndex;

// Fixed doesn't like empty arrays
if (bytes.Length == 0)
bytes = new byte[1];

fixed (char* pChars = chars)
fixed (byte* pBytes = bytes)
// Remember that byteCount is # to decode, not size of array.
return GetBytes(pChars + charIndex, charCount,
pBytes + byteIndex, byteCount, null);
return EncodingForwarder.GetBytes(this, chars, charIndex, charCount, bytes, byteIndex);
}

// 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 @@ -176,43 +176,10 @@ public override int GetBytes(String s, int charIndex, int charCount,
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
public override int GetBytes(char[] chars, int charIndex, int charCount,
byte[] bytes, int byteIndex)
{
// Validate parameters
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_IndexCountBuffer"));

if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException("byteIndex",
Environment.GetResourceString("ArgumentOutOfRange_Index"));
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (charCount == 0)
return 0;

// Just call pointer version
int byteCount = bytes.Length - byteIndex;

// Fixed doesn't like 0 length arrays.
if (bytes.Length == 0)
bytes = new byte[1];

fixed (char* pChars = chars)
fixed (byte* pBytes = bytes)
// Remember that byteCount is # to decode, not size of array.
return GetBytes(pChars + charIndex, charCount,
pBytes + byteIndex, byteCount, null);
return EncodingForwarder.GetBytes(this, chars, charIndex, charCount, bytes, byteIndex);
}

// 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 @@ -148,43 +148,10 @@ public override int GetBytes(String s, int charIndex, int charCount,
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe int GetBytes(char[] chars, int charIndex, int charCount,
public override int GetBytes(char[] chars, int charIndex, int charCount,
byte[] bytes, int byteIndex)
{
// Validate parameters
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_IndexCountBuffer"));

if (byteIndex < 0 || byteIndex > bytes.Length)
throw new ArgumentOutOfRangeException("byteIndex",
Environment.GetResourceString("ArgumentOutOfRange_Index"));
Contract.EndContractBlock();

// If nothing to encode return 0, avoid fixed problem
if (charCount == 0)
return 0;

// Just call pointer version
int byteCount = bytes.Length - byteIndex;

// Fixed doesn't like 0 length arrays.
if (bytes.Length == 0)
bytes = new byte[1];

fixed (char* pChars = chars)
fixed (byte* pBytes = bytes)
// Remember that byteCount is # to decode, not size of array.
return GetBytes(pChars + charIndex, charCount,
pBytes + byteIndex, byteCount, null);
return EncodingForwarder.GetBytes(this, chars, charIndex, charCount, bytes, byteIndex);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down

0 comments on commit 0889431

Please sign in to comment.