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

Commit

Permalink
Use EncodingForwarder for GetByteCount(char*, int)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesqo committed Jul 4, 2016
1 parent 9668b5f commit 56c687e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 72 deletions.
13 changes: 1 addition & 12 deletions src/mscorlib/src/System/Text/ASCIIEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,7 @@ public override int GetByteCount(String chars)
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe int GetByteCount(char* chars, int count)
{
// Validate Parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

// Call it with empty encoder
return GetByteCount(chars, count, null);
return EncodingForwarder.GetByteCount(this, chars, count);
}

// Parent method is safe.
Expand Down
18 changes: 18 additions & 0 deletions src/mscorlib/src/System/Text/EncodingForwarder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public unsafe static int GetByteCount(Encoding encoding, char[] chars, int index

public unsafe static int GetByteCount(Encoding encoding, string s)
{
Contract.Assert(encoding != null);
if (s == null)
{
throw new ArgumentNullException("s");
Expand All @@ -69,5 +70,22 @@ public unsafe static int GetByteCount(Encoding encoding, string s)
fixed (char* pChars = s)
return encoding.GetByteCount(pChars, s.Length, encoder: null);
}

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

// Call the internal version, with an empty encoder
return encoding.GetByteCount(chars, count, encoder: null);
}
}
}
13 changes: 1 addition & 12 deletions src/mscorlib/src/System/Text/EncodingNLS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,7 @@ public override int GetByteCount(String s)
[System.Security.SecurityCritical] // auto-generated
public override unsafe int GetByteCount(char* chars, int count)
{
// Validate Parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

// Call it with empty encoder
return GetByteCount(chars, count, null);
return EncodingForwarder.GetByteCount(this, chars, count);
}

// Parent method is safe.
Expand Down
13 changes: 1 addition & 12 deletions src/mscorlib/src/System/Text/UTF32Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,7 @@ public override int GetByteCount(String s)
[CLSCompliant(false)]
public override unsafe int GetByteCount(char* chars, int count)
{
// Validate Parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

// Call it with empty encoder
return GetByteCount(chars, count, null);
return EncodingForwarder.GetByteCount(this, chars, count);
}

// Parent method is safe.
Expand Down
13 changes: 1 addition & 12 deletions src/mscorlib/src/System/Text/UTF7Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,7 @@ public override int GetByteCount(String s)
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe int GetByteCount(char* chars, int count)
{
// Validate Parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

// Call it with empty encoder
return GetByteCount(chars, count, null);
return EncodingForwarder.GetByteCount(this, chars, count);
}

// Parent method is safe.
Expand Down
13 changes: 1 addition & 12 deletions src/mscorlib/src/System/Text/UTF8Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,7 @@ public override int GetByteCount(String chars)
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe int GetByteCount(char* chars, int count)
{
// Validate Parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

// Call it with empty encoder
return GetByteCount(chars, count, null);
return EncodingForwarder.GetByteCount(this, chars, count);
}

// Parent method is safe.
Expand Down
13 changes: 1 addition & 12 deletions src/mscorlib/src/System/Text/UnicodeEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,7 @@ public override int GetByteCount(String s)
[System.Runtime.InteropServices.ComVisible(false)]
public override unsafe int GetByteCount(char* chars, int count)
{
// Validate Parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

// Call it with empty encoder
return GetByteCount(chars, count, null);
return EncodingForwarder.GetByteCount(this, chars, count);
}

// Parent method is safe.
Expand Down

0 comments on commit 56c687e

Please sign in to comment.