Skip to content

Commit

Permalink
Use EncodingForwarder for GetByteCount(char[], int, int)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesqo committed Jul 4, 2016
1 parent c928700 commit ebfc3ce
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 132 deletions.
1 change: 1 addition & 0 deletions src/mscorlib/mscorlib.shared.sources.props
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@
<TextSources Include="$(BclSourcesRoot)\System\Text\EncoderFallback.cs" />
<TextSources Include="$(BclSourcesRoot)\System\Text\EncoderReplacementFallback.cs" />
<TextSources Include="$(BclSourcesRoot)\System\Text\Encoding.cs" />
<TextSources Include="$(BclSourcesRoot)\System\Text\EncodingForwarder.cs" />
<TextSources Include="$(BclSourcesRoot)\System\Text\EncodingInfo.cs" />
<TextSources Include="$(BclSourcesRoot)\System\Text\EncodingNLS.cs" />
<TextSources Include="$(BclSourcesRoot)\System\Text\EncodingProvider.cs" />
Expand Down
24 changes: 2 additions & 22 deletions src/mscorlib/src/System/Text/ASCIIEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,9 @@ internal override void SetDefaultFallbacks()
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe int GetByteCount(char[] chars, int index, int count)
public override int GetByteCount(char[] chars, int index, int count)
{
// Validate input parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

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

// If no input, return 0, avoid fixed empty array problem
if (count == 0)
return 0;

// Just call the pointer version
fixed (char* pChars = chars)
return GetByteCount(pChars + index, count, null);
return EncodingForwarder.GetByteCount(this, chars, index, count);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
53 changes: 53 additions & 0 deletions src/mscorlib/src/System/Text/EncodingForwarder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.Contracts;

namespace System.Text
{
// Shared implementations for commonly overriden Encoding methods

internal static class EncodingForwarder
{
// We normally have to duplicate a lot of code between UTF8Encoding,
// UTF7Encoding, EncodingNLS, etc. because we want to override many
// of the methods in all of those classes to just forward to the unsafe
// version. (e.g. GetBytes(char[]))
// Ideally, everything would just derive from EncodingNLS, but that's
// not exposed in the public API, and C# prohibits a public class from
// inheriting from an internal one. So, we have to override each of the
// methods in question and repeat the argument validation/logic.

// These set of methods exist so instead of duplicating code, we can
// simply have those overriden methods call here to do the actual work.

public unsafe static int GetByteCount(Encoding encoding, char[] chars, int index, int count)
{
// Validate parameters

Contract.Assert(encoding != null); // this parameter should only be affected internally, so just do a debug check here
if (chars == null)
{
throw new ArgumentNullException("chars", Environment.GetResourceString("ArgumentNull_Array"));
}
if (index < 0 || count < 0)
{
throw new ArgumentOutOfRangeException(index < 0 ? "index" : "count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
}
if (chars.Length - index < count)
{
throw new ArgumentOutOfRangeException("chars", Environment.GetResourceString("ArgumentOutOfRange_IndexCountBuffer"));
}
Contract.EndContractBlock();

// If no input, return 0, avoid fixed empty array problem
if (count == 0)
return 0;

// Just call the pointer version
fixed (char* pChars = chars)
return encoding.GetByteCount(pChars + index, count, null);
}
}
}
24 changes: 2 additions & 22 deletions src/mscorlib/src/System/Text/EncodingNLS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,9 @@ protected EncodingNLS(int codePage) : base(codePage)
// EncodingNLS, UTF7Encoding, UTF8Encoding, UTF32Encoding, ASCIIEncoding, UnicodeEncoding
// parent method is safe
[System.Security.SecuritySafeCritical] // overrides public transparent member
public override unsafe int GetByteCount(char[] chars, int index, int count)
public override int GetByteCount(char[] chars, int index, int count)
{
// Validate input parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

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

// If no input, return 0, avoid fixed empty array problem
if (count == 0)
return 0;

// Just call the pointer version
fixed (char* pChars = chars)
return GetByteCount(pChars + index, count, null);
return EncodingForwarder.GetByteCount(this, chars, index, count);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
24 changes: 2 additions & 22 deletions src/mscorlib/src/System/Text/UTF32Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,9 @@ internal override void SetDefaultFallbacks()
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe int GetByteCount(char[] chars, int index, int count)
public override int GetByteCount(char[] chars, int index, int count)
{
// Validate input parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

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

// If no input, return 0, avoid fixed empty array problem
if (count == 0)
return 0;

// Just call the pointer version
fixed (char* pChars = chars)
return GetByteCount(pChars + index, count, null);
return EncodingForwarder.GetByteCount(this, chars, index, count);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
24 changes: 2 additions & 22 deletions src/mscorlib/src/System/Text/UTF7Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,29 +161,9 @@ public override int GetHashCode()
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe int GetByteCount(char[] chars, int index, int count)
public override int GetByteCount(char[] chars, int index, int count)
{
// Validate input parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

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

// If no input, return 0, avoid fixed empty array problem
if (count == 0)
return 0;

// Just call the pointer version
fixed (char* pChars = chars)
return GetByteCount(pChars + index, count, null);
return EncodingForwarder.GetByteCount(this, chars, index, count);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
24 changes: 2 additions & 22 deletions src/mscorlib/src/System/Text/UTF8Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,9 @@ internal override void SetDefaultFallbacks()
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe int GetByteCount(char[] chars, int index, int count)
public override int GetByteCount(char[] chars, int index, int count)
{
// Validate input parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

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

// If no input, return 0, avoid fixed empty array problem
if (count == 0)
return 0;

// Just call the pointer version
fixed (char* pChars = chars)
return GetByteCount(pChars + index, count, null);
return EncodingForwarder.GetByteCount(this, chars, index, count);
}

// All of our public Encodings that don't use EncodingNLS must have this (including EncodingNLS)
Expand Down
24 changes: 2 additions & 22 deletions src/mscorlib/src/System/Text/UnicodeEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,9 @@ internal override void SetDefaultFallbacks()
// parent method is safe

[System.Security.SecuritySafeCritical] // auto-generated
public override unsafe int GetByteCount(char[] chars, int index, int count)
public override int GetByteCount(char[] chars, int index, int count)
{
// Validate input parameters
if (chars == null)
throw new ArgumentNullException("chars",
Environment.GetResourceString("ArgumentNull_Array"));

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

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

// If no input, return 0, avoid fixed empty array problem
if (count == 0)
return 0;

// Just call the pointer version
fixed (char* pChars = chars)
return GetByteCount(pChars + index, count, null);
return EncodingForwarder.GetByteCount(this, chars, index, count);
}

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

0 comments on commit ebfc3ce

Please sign in to comment.