Skip to content

Commit

Permalink
Use EncodingForwarder for GetString(byte[], int, int)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesqo committed Jul 4, 2016
1 parent ab47c22 commit 372fcd3
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 127 deletions.
26 changes: 4 additions & 22 deletions src/mscorlib/src/System/Text/ASCIIEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,29 +191,11 @@ public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe String GetString(byte[] bytes, int byteIndex, int byteCount)
public override String GetString(byte[] bytes, int byteIndex, int byteCount)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
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"));
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (byteCount == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
pBytes + byteIndex, byteCount, this);
// NOTE: If the byteIndex/byteCount parameters are invalid, this will
// throw an ArgumentOutOfRange for "index" or "count" instead of those names.
return EncodingForwarder.GetString(this, bytes, byteIndex, byteCount);
}

//
Expand Down
34 changes: 34 additions & 0 deletions src/mscorlib/src/System/Text/EncodingForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,5 +283,39 @@ public unsafe static int GetChars(Encoding encoding, byte* bytes, int byteCount,

return encoding.GetChars(bytes, byteCount, chars, charCount, decoder: null);
}

public unsafe static string GetString(Encoding encoding, byte[] bytes, int index, int count)
{
Contract.Assert(encoding != null);
if (bytes == null)
{
throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
}
if (index < 0 || count < 0)
{
throw new ArgumentOutOfRangeException(index < 0 ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (bytes.Length - index < count)
{
throw new ArgumentOutOfRangeException("bytes", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
}
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (count == 0)
return string.Empty;

// Call string.CreateStringFromEncoding here, which
// allocates a string and lets the Encoding modify
// it in place. This way, we don't have to allocate
// an intermediary char[] to decode into and then
// call the string constructor; instead we decode
// directly into the string.

fixed (byte* pBytes = bytes)
{
return string.CreateStringFromEncoding(pBytes + index, count, encoding);
}
}
}
}
23 changes: 2 additions & 21 deletions src/mscorlib/src/System/Text/EncodingNLS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,9 @@ public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int
// EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding
// parent method is safe
[System.Security.SecuritySafeCritical] // overrides public transparent member
public override unsafe String GetString(byte[] bytes, int index, int count)
public override String GetString(byte[] bytes, int index, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index < 0 ? "index" : "count"),
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));

if (bytes.Length - index < count)
throw new ArgumentOutOfRangeException("bytes",
Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (count == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
pBytes + index, count, this);
return EncodingForwarder.GetString(this, bytes, index, count);
}

public override Decoder GetDecoder()
Expand Down
23 changes: 2 additions & 21 deletions src/mscorlib/src/System/Text/UTF32Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,28 +223,9 @@ public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe String GetString(byte[] bytes, int index, int count)
public override String GetString(byte[] bytes, int index, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index < 0 ? "index" : "count"),
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));

if (bytes.Length - index < count)
throw new ArgumentOutOfRangeException("bytes",
Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (count == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
pBytes + index, count, this);
return EncodingForwarder.GetString(this, bytes, index, count);
}

//
Expand Down
23 changes: 2 additions & 21 deletions src/mscorlib/src/System/Text/UTF7Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,28 +296,9 @@ public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int

[System.Security.SecuritySafeCritical] // auto-generated
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe String GetString(byte[] bytes, int index, int count)
public override String GetString(byte[] bytes, int index, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index < 0 ? "index" : "count"),
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));

if (bytes.Length - index < count)
throw new ArgumentOutOfRangeException("bytes",
Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (count == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
pBytes + index, count, this);
return EncodingForwarder.GetString(this, bytes, index, count);
}

//
Expand Down
23 changes: 2 additions & 21 deletions src/mscorlib/src/System/Text/UTF8Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,28 +254,9 @@ public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int

[System.Security.SecuritySafeCritical] // auto-generated
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe String GetString(byte[] bytes, int index, int count)
public override String GetString(byte[] bytes, int index, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index < 0 ? "index" : "count"),
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));

if (bytes.Length - index < count)
throw new ArgumentOutOfRangeException("bytes",
Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (count == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
pBytes + index, count, this);
return EncodingForwarder.GetString(this, bytes, index, count);
}

//
Expand Down
23 changes: 2 additions & 21 deletions src/mscorlib/src/System/Text/UnicodeEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,28 +226,9 @@ public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int

[System.Security.SecuritySafeCritical] // auto-generated
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe String GetString(byte[] bytes, int index, int count)
public override String GetString(byte[] bytes, int index, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (index < 0 || count < 0)
throw new ArgumentOutOfRangeException((index < 0 ? "index" : "count"),
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));

if (bytes.Length - index < count)
throw new ArgumentOutOfRangeException("bytes",
Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
Contract.EndContractBlock();

// Avoid problems with empty input buffer
if (count == 0) return String.Empty;

fixed (byte* pBytes = bytes)
return String.CreateStringFromEncoding(
pBytes + index, count, this);
return EncodingForwarder.GetString(this, bytes, index, count);
}

//
Expand Down

0 comments on commit 372fcd3

Please sign in to comment.