From 5d3442943b4fdf6a63ffe7782d938edff9a15277 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 16 Aug 2017 19:19:17 -0400 Subject: [PATCH] Add span-based overloads to Encoder/Decoder --- src/mscorlib/shared/System/Text/Decoder.cs | 26 ++++++++++++++++++++++++++ src/mscorlib/shared/System/Text/Encoder.cs | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/mscorlib/shared/System/Text/Decoder.cs b/src/mscorlib/shared/System/Text/Decoder.cs index f109f3d..82d33d5 100644 --- a/src/mscorlib/shared/System/Text/Decoder.cs +++ b/src/mscorlib/shared/System/Text/Decoder.cs @@ -134,6 +134,14 @@ namespace System.Text return GetCharCount(arrbyte, 0, count); } + public virtual unsafe int GetCharCount(ReadOnlySpan 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 bytes, Span 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 bytes, Span 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); + } + } } } diff --git a/src/mscorlib/shared/System/Text/Encoder.cs b/src/mscorlib/shared/System/Text/Encoder.cs index b0602f3..79a633f 100644 --- a/src/mscorlib/shared/System/Text/Encoder.cs +++ b/src/mscorlib/shared/System/Text/Encoder.cs @@ -132,6 +132,14 @@ namespace System.Text return GetByteCount(arrChar, 0, count, flush); } + public virtual unsafe int GetByteCount(ReadOnlySpan 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 chars, Span 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 chars, Span 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); + } + } } } -- 2.7.4