Skip to content

Commit

Permalink
Use EncodingForwarder for GetCharCount(byte*, int)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesqo committed Jul 4, 2016
1 parent 6de8ede commit f83ecbe
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 66 deletions.
12 changes: 1 addition & 11 deletions src/mscorlib/src/System/Text/ASCIIEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,7 @@ public override int GetCharCount(byte[] bytes, int index, int count)
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe int GetCharCount(byte* bytes, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (count < 0)
throw new ArgumentOutOfRangeException("count",
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
Contract.EndContractBlock();

return GetCharCount(bytes, count, null);
return EncodingForwarder.GetCharCount(this, bytes, count);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
24 changes: 24 additions & 0 deletions src/mscorlib/src/System/Text/EncodingForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ internal static class EncodingForwarder
// These set of methods exist so instead of duplicating code, we can
// simply have those overriden methods call here to do the actual work.

// NOTE: This class should ONLY be called from Encodings that override
// the internal methods which accept an Encoder/DecoderNLS. The reason
// for this is that by default, those methods just call the same overload
// except without the encoder/decoder parameter. If an overriden method
// without that parameter calls this class, which calls the overload with
// the parameter, it will call the same method again, which will eventually
// lead to a StackOverflowException.

public unsafe static int GetByteCount(Encoding encoding, char[] chars, int index, int count)
{
// Validate parameters
Expand Down Expand Up @@ -205,5 +213,21 @@ public unsafe static int GetCharCount(Encoding encoding, byte[] bytes, int index
fixed (byte* pBytes = bytes)
return encoding.GetCharCount(pBytes + index, count, decoder: null);
}

public unsafe static int GetCharCount(Encoding encoding, byte* bytes, int count)
{
Contract.Assert(encoding != null);
if (bytes == null)
{
throw new ArgumentNullException("bytes", Environment.GetResourceString("ArgumentNull_Array"));
}
if (count < 0)
{
throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
Contract.EndContractBlock();

return encoding.GetCharCount(bytes, count, decoder: null);
}
}
}
12 changes: 1 addition & 11 deletions src/mscorlib/src/System/Text/EncodingNLS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,7 @@ public override int GetCharCount(byte[] bytes, int index, int count)
[System.Security.SecurityCritical] // auto-generated
public override unsafe int GetCharCount(byte* bytes, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (count < 0)
throw new ArgumentOutOfRangeException("count",
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
Contract.EndContractBlock();

return GetCharCount(bytes, count, null);
return EncodingForwarder.GetCharCount(this, bytes, count);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
12 changes: 1 addition & 11 deletions src/mscorlib/src/System/Text/UTF32Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,7 @@ public override int GetCharCount(byte[] bytes, int index, int count)
[CLSCompliant(false)]
public override unsafe int GetCharCount(byte* bytes, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (count < 0)
throw new ArgumentOutOfRangeException("count",
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
Contract.EndContractBlock();

return GetCharCount(bytes, count, null);
return EncodingForwarder.GetCharCount(this, bytes, count);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
12 changes: 1 addition & 11 deletions src/mscorlib/src/System/Text/UTF7Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,7 @@ public override int GetCharCount(byte[] bytes, int index, int count)
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe int GetCharCount(byte* bytes, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (count < 0)
throw new ArgumentOutOfRangeException("count",
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
Contract.EndContractBlock();

return GetCharCount(bytes, count, null);
return EncodingForwarder.GetCharCount(this, bytes, count);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
12 changes: 1 addition & 11 deletions src/mscorlib/src/System/Text/UTF8Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,7 @@ public override int GetCharCount(byte[] bytes, int index, int count)
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe int GetCharCount(byte* bytes, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (count < 0)
throw new ArgumentOutOfRangeException("count",
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
Contract.EndContractBlock();

return GetCharCount(bytes, count, null);
return EncodingForwarder.GetCharCount(this, bytes, count);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
12 changes: 1 addition & 11 deletions src/mscorlib/src/System/Text/UnicodeEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,7 @@ public override int GetCharCount(byte[] bytes, int index, int count)
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe int GetCharCount(byte* bytes, int count)
{
// Validate Parameters
if (bytes == null)
throw new ArgumentNullException("bytes",
Environment.GetResourceString("ArgumentNull_Array"));

if (count < 0)
throw new ArgumentOutOfRangeException("count",
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
Contract.EndContractBlock();

return GetCharCount(bytes, count, null);
return EncodingForwarder.GetCharCount(this, bytes, count);
}

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

0 comments on commit f83ecbe

Please sign in to comment.