Skip to content

Commit

Permalink
Add Encoding.GetBytes(string, offset, count) (dotnet/coreclr#8651)
Browse files Browse the repository at this point in the history


Commit migrated from dotnet/coreclr@8891481
  • Loading branch information
AlexRadch authored and jkotas committed Dec 19, 2016
1 parent 94efcbc commit 650ecef
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/coreclr/src/mscorlib/model.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7767,10 +7767,12 @@
<Member Name="GetByteCount(System.Char[])" />
<Member Name="GetByteCount(System.Char[],System.Int32,System.Int32)" />
<Member Name="GetByteCount(System.String)" />
<Member Name="GetByteCount(System.String,System.Int32,System.Int32)" />
<Member Name="GetBytes(System.Char[])" />
<Member Name="GetBytes(System.Char[],System.Int32,System.Int32)" />
<Member Name="GetBytes(System.Char[],System.Int32,System.Int32,System.Byte[],System.Int32)" />
<Member Name="GetBytes(System.String)" />
<Member Name="GetBytes(System.String,System.Int32,System.Int32)" />
<Member Name="GetBytes(System.String,System.Int32,System.Int32,System.Byte[],System.Int32)" />
<Member Name="GetBytes(System.Char*,System.Int32,System.Byte*,System.Int32)" />
<Member Name="GetCharCount(System.Byte[])" />
Expand Down
71 changes: 69 additions & 2 deletions src/coreclr/src/mscorlib/src/System/Text/Encoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ public virtual int GetByteCount(char[] chars)
[Pure]
public virtual int GetByteCount(String s)
{
if (s==null)
if (s == null)
throw new ArgumentNullException(nameof(s));
Contract.EndContractBlock();

Expand All @@ -884,6 +884,34 @@ public virtual int GetByteCount(String s)
[Pure]
public abstract int GetByteCount(char[] chars, int index, int count);

// Returns the number of bytes required to encode a string range.
//
[Pure]
public int GetByteCount(string s, int index, int count)
{
if (s == null)
throw new ArgumentNullException(nameof(s),
Environment.GetResourceString("ArgumentNull_String"));
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index),
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count),
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (index > s.Length - count)
throw new ArgumentOutOfRangeException(nameof(index),
Environment.GetResourceString("ArgumentOutOfRange_IndexCount"));
Contract.EndContractBlock();

unsafe
{
fixed (char* pChar = s)
{
return GetByteCount(pChar + index, count);
}
}
}

// We expect this to be the workhorse for NLS encodings
// unfortunately for existing overrides, it has to call the [] version,
// which is really slow, so this method should be avoided if you're calling
Expand Down Expand Up @@ -978,10 +1006,49 @@ public virtual byte[] GetBytes(String s)
return bytes;
}

// Returns a byte array containing the encoded representation of the given
// string range.
//
[Pure]
public byte[] GetBytes(string s, int index, int count)
{
if (s == null)
throw new ArgumentNullException(nameof(s),
Environment.GetResourceString("ArgumentNull_String"));
if (index < 0)
throw new ArgumentOutOfRangeException(nameof(index),
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count),
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (index > s.Length - count)
throw new ArgumentOutOfRangeException(nameof(index),
Environment.GetResourceString("ArgumentOutOfRange_IndexCount"));
Contract.EndContractBlock();

unsafe
{
fixed (char* pChar = s)
{
int byteCount = GetByteCount(pChar + index, count);
if (byteCount == 0)
return Array.Empty<byte>();

byte[] bytes = new byte[byteCount];
fixed (byte* pBytes = &bytes[0])
{
int bytesReceived = GetBytes(pChar + index, count, pBytes, byteCount);
Debug.Assert(byteCount == bytesReceived);
}
return bytes;
}
}
}

public virtual int GetBytes(String s, int charIndex, int charCount,
byte[] bytes, int byteIndex)
{
if (s==null)
if (s == null)
throw new ArgumentNullException(nameof(s));
Contract.EndContractBlock();
return GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);
Expand Down

0 comments on commit 650ecef

Please sign in to comment.