summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Radchenko <radchenkosasha@gmail.com>2016-12-20 02:39:26 +0700
committerJan Kotas <jkotas@microsoft.com>2016-12-19 11:39:26 -0800
commit8891481aba943dc4f455f3790bdfa49877ca2525 (patch)
tree9c1cb550450ddf603baf205394ab29fe5b605d20
parentdec5a1ff4088780bf0b8226002a5a51d7d6c4d3a (diff)
downloadcoreclr-8891481aba943dc4f455f3790bdfa49877ca2525.tar.gz
coreclr-8891481aba943dc4f455f3790bdfa49877ca2525.tar.bz2
coreclr-8891481aba943dc4f455f3790bdfa49877ca2525.zip
Add Encoding.GetBytes(string, offset, count) (#8651)
-rw-r--r--src/mscorlib/model.xml2
-rw-r--r--src/mscorlib/src/System/Text/Encoding.cs71
2 files changed, 71 insertions, 2 deletions
diff --git a/src/mscorlib/model.xml b/src/mscorlib/model.xml
index c9bf5dd8ad..c0e9746306 100644
--- a/src/mscorlib/model.xml
+++ b/src/mscorlib/model.xml
@@ -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[])" />
diff --git a/src/mscorlib/src/System/Text/Encoding.cs b/src/mscorlib/src/System/Text/Encoding.cs
index b2284d76e7..658bdbb133 100644
--- a/src/mscorlib/src/System/Text/Encoding.cs
+++ b/src/mscorlib/src/System/Text/Encoding.cs
@@ -869,7 +869,7 @@ namespace System.Text
[Pure]
public virtual int GetByteCount(String s)
{
- if (s==null)
+ if (s == null)
throw new ArgumentNullException(nameof(s));
Contract.EndContractBlock();
@@ -884,6 +884,34 @@ namespace System.Text
[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
@@ -978,10 +1006,49 @@ namespace System.Text
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);