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

Commit 8891481

Browse files
AlexRadchjkotas
authored andcommitted
Add Encoding.GetBytes(string, offset, count) (#8651)
1 parent dec5a1f commit 8891481

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/mscorlib/model.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7767,10 +7767,12 @@
77677767
<Member Name="GetByteCount(System.Char[])" />
77687768
<Member Name="GetByteCount(System.Char[],System.Int32,System.Int32)" />
77697769
<Member Name="GetByteCount(System.String)" />
7770+
<Member Name="GetByteCount(System.String,System.Int32,System.Int32)" />
77707771
<Member Name="GetBytes(System.Char[])" />
77717772
<Member Name="GetBytes(System.Char[],System.Int32,System.Int32)" />
77727773
<Member Name="GetBytes(System.Char[],System.Int32,System.Int32,System.Byte[],System.Int32)" />
77737774
<Member Name="GetBytes(System.String)" />
7775+
<Member Name="GetBytes(System.String,System.Int32,System.Int32)" />
77747776
<Member Name="GetBytes(System.String,System.Int32,System.Int32,System.Byte[],System.Int32)" />
77757777
<Member Name="GetBytes(System.Char*,System.Int32,System.Byte*,System.Int32)" />
77767778
<Member Name="GetCharCount(System.Byte[])" />

src/mscorlib/src/System/Text/Encoding.cs

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ public virtual int GetByteCount(char[] chars)
869869
[Pure]
870870
public virtual int GetByteCount(String s)
871871
{
872-
if (s==null)
872+
if (s == null)
873873
throw new ArgumentNullException(nameof(s));
874874
Contract.EndContractBlock();
875875

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

887+
// Returns the number of bytes required to encode a string range.
888+
//
889+
[Pure]
890+
public int GetByteCount(string s, int index, int count)
891+
{
892+
if (s == null)
893+
throw new ArgumentNullException(nameof(s),
894+
Environment.GetResourceString("ArgumentNull_String"));
895+
if (index < 0)
896+
throw new ArgumentOutOfRangeException(nameof(index),
897+
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
898+
if (count < 0)
899+
throw new ArgumentOutOfRangeException(nameof(count),
900+
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
901+
if (index > s.Length - count)
902+
throw new ArgumentOutOfRangeException(nameof(index),
903+
Environment.GetResourceString("ArgumentOutOfRange_IndexCount"));
904+
Contract.EndContractBlock();
905+
906+
unsafe
907+
{
908+
fixed (char* pChar = s)
909+
{
910+
return GetByteCount(pChar + index, count);
911+
}
912+
}
913+
}
914+
887915
// We expect this to be the workhorse for NLS encodings
888916
// unfortunately for existing overrides, it has to call the [] version,
889917
// which is really slow, so this method should be avoided if you're calling
@@ -978,10 +1006,49 @@ public virtual byte[] GetBytes(String s)
9781006
return bytes;
9791007
}
9801008

1009+
// Returns a byte array containing the encoded representation of the given
1010+
// string range.
1011+
//
1012+
[Pure]
1013+
public byte[] GetBytes(string s, int index, int count)
1014+
{
1015+
if (s == null)
1016+
throw new ArgumentNullException(nameof(s),
1017+
Environment.GetResourceString("ArgumentNull_String"));
1018+
if (index < 0)
1019+
throw new ArgumentOutOfRangeException(nameof(index),
1020+
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1021+
if (count < 0)
1022+
throw new ArgumentOutOfRangeException(nameof(count),
1023+
Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
1024+
if (index > s.Length - count)
1025+
throw new ArgumentOutOfRangeException(nameof(index),
1026+
Environment.GetResourceString("ArgumentOutOfRange_IndexCount"));
1027+
Contract.EndContractBlock();
1028+
1029+
unsafe
1030+
{
1031+
fixed (char* pChar = s)
1032+
{
1033+
int byteCount = GetByteCount(pChar + index, count);
1034+
if (byteCount == 0)
1035+
return Array.Empty<byte>();
1036+
1037+
byte[] bytes = new byte[byteCount];
1038+
fixed (byte* pBytes = &bytes[0])
1039+
{
1040+
int bytesReceived = GetBytes(pChar + index, count, pBytes, byteCount);
1041+
Debug.Assert(byteCount == bytesReceived);
1042+
}
1043+
return bytes;
1044+
}
1045+
}
1046+
}
1047+
9811048
public virtual int GetBytes(String s, int charIndex, int charCount,
9821049
byte[] bytes, int byteIndex)
9831050
{
984-
if (s==null)
1051+
if (s == null)
9851052
throw new ArgumentNullException(nameof(s));
9861053
Contract.EndContractBlock();
9871054
return GetBytes(s.ToCharArray(), charIndex, charCount, bytes, byteIndex);

0 commit comments

Comments
 (0)