Add span-based overloads to Encoder/Decoder
authorStephen Toub <stoub@microsoft.com>
Wed, 16 Aug 2017 23:19:17 +0000 (19:19 -0400)
committerStephen Toub <stoub@microsoft.com>
Wed, 16 Aug 2017 23:19:17 +0000 (19:19 -0400)
src/mscorlib/shared/System/Text/Decoder.cs
src/mscorlib/shared/System/Text/Encoder.cs

index f109f3d..82d33d5 100644 (file)
@@ -134,6 +134,14 @@ namespace System.Text
             return GetCharCount(arrbyte, 0, count);
         }
 
+        public virtual unsafe int GetCharCount(ReadOnlySpan<byte> bytes, bool flush)
+        {
+            fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference())
+            {
+                return GetCharCount(bytesPtr, bytes.Length, flush);
+            }
+        }
+
         // Decodes a range of bytes in a byte array into a range of characters
         // in a character array. The method decodes byteCount bytes from
         // bytes starting at index byteIndex, storing the resulting
@@ -220,6 +228,15 @@ namespace System.Text
             return charCount;
         }
 
+        public virtual unsafe int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars, bool flush)
+        {
+            fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference())
+            fixed (char* charsPtr = &chars.DangerousGetPinnableReference())
+            {
+                return GetChars(bytesPtr, bytes.Length, charsPtr, chars.Length, flush);
+            }
+        }
+
         // This method is used when the output buffer might not be large enough.
         // It will decode until it runs out of bytes, and then it will return
         // true if it the entire input was converted.  In either case it
@@ -326,5 +343,14 @@ namespace System.Text
             // Oops, we didn't have anything, we'll have to throw an overflow
             throw new ArgumentException(SR.Argument_ConversionOverflow);
         }
+
+        public virtual unsafe void Convert(ReadOnlySpan<byte> bytes, Span<char> chars, bool flush, out int bytesUsed, out int charsUsed, out bool completed)
+        {
+            fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference())
+            fixed (char* charsPtr = &chars.DangerousGetPinnableReference())
+            {
+                Convert(bytesPtr, bytes.Length, charsPtr, chars.Length, flush, out bytesUsed, out charsUsed, out completed);
+            }
+        }
     }
 }
index b0602f3..79a633f 100644 (file)
@@ -132,6 +132,14 @@ namespace System.Text
             return GetByteCount(arrChar, 0, count, flush);
         }
 
+        public virtual unsafe int GetByteCount(ReadOnlySpan<char> chars, bool flush)
+        {
+            fixed (char* charsPtr = &chars.DangerousGetPinnableReference())
+            {
+                return GetByteCount(charsPtr, chars.Length, flush);
+            }
+        }
+
         // Encodes a range of characters in a character array into a range of bytes
         // in a byte array. The method encodes charCount characters from
         // chars starting at index charIndex, storing the resulting
@@ -214,6 +222,15 @@ namespace System.Text
             return byteCount;
         }
 
+        public virtual unsafe int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, bool flush)
+        {
+            fixed (char* charsPtr = &chars.DangerousGetPinnableReference())
+            fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference())
+            {
+                return GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length, flush);
+            }
+        }
+
         // This method is used to avoid running out of output buffer space.
         // It will encode until it runs out of chars, and then it will return
         // true if it the entire input was converted.  In either case it
@@ -320,6 +337,15 @@ namespace System.Text
             // Oops, we didn't have anything, we'll have to throw an overflow
             throw new ArgumentException(SR.Argument_ConversionOverflow);
         }
+
+        public virtual unsafe void Convert(ReadOnlySpan<char> chars, Span<byte> bytes, bool flush, out int charsUsed, out int bytesUsed, out bool completed)
+        {
+            fixed (char* charsPtr = &chars.DangerousGetPinnableReference())
+            fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference())
+            {
+                Convert(charsPtr, chars.Length, bytesPtr, bytes.Length, flush, out charsUsed, out bytesUsed, out completed);
+            }
+        }
     }
 }