From e814d85cb3d740884035a48cfe458399d4fedd2e Mon Sep 17 00:00:00 2001 From: Atsushi Kanamori Date: Wed, 10 Jan 2018 08:43:03 -0800 Subject: [PATCH 1/2] Underlying CoreCLR support for new string slicing overloads https://github.com/dotnet/corefx/issues/25254 These add the underlying support for the fast versions of these extension methods. --- src/mscorlib/shared/System/Span.NonGeneric.cs | 80 ++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/src/mscorlib/shared/System/Span.NonGeneric.cs b/src/mscorlib/shared/System/Span.NonGeneric.cs index 775426f6b1db..70c6b7e10e6a 100644 --- a/src/mscorlib/shared/System/Span.NonGeneric.cs +++ b/src/mscorlib/shared/System/Span.NonGeneric.cs @@ -28,13 +28,45 @@ public static class Span public static ReadOnlyMemory AsReadOnlyMemory(this string text) { if (text == null) - { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); - } return new ReadOnlyMemory(text, 0, text.Length); } + /// Creates a new over the portion of the target string. + /// The target string. + /// Thrown when is a null reference (Nothing in Visual Basic). + /// + /// Thrown when the specified index is not in range (<0 or >=text.Length). + /// + public static ReadOnlyMemory AsReadOnlyMemory(this string text, int start) + { + if (text == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + if ((uint)start > (uint)text.Length) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new ReadOnlyMemory(text, start, text.Length - start); + } + + /// Creates a new over the portion of the target string. + /// The target string. + /// Thrown when is a null reference (Nothing in Visual Basic). + /// + /// Thrown when the specified index or is not in range. + /// + public static ReadOnlyMemory AsReadOnlyMemory(this string text, int start, int length) + { + if (text == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new ReadOnlyMemory(text, start, length); + } + /// Attempts to get the underlying from a . /// The memory that may be wrapping a object. /// The string. @@ -166,6 +198,50 @@ public static ReadOnlySpan AsReadOnlySpan(this string text) return new ReadOnlySpan(ref text.GetRawStringData(), text.Length); } + /// + /// Creates a new readonly span over the portion of the target string. + /// + /// The target string. + /// Thrown when is a null + /// reference (Nothing in Visual Basic). + /// + /// + /// Thrown when the specified index is not in range (<0 or >=text.Length). + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan AsReadOnlySpan(this string text, int start) + { + if (text == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + if ((uint)start > (uint)text.Length) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new ReadOnlySpan(ref Unsafe.Add(ref text.GetRawStringData(), start), text.Length - start); + } + + /// + /// Creates a new readonly span over the portion of the target string. + /// + /// The target string. + /// Thrown when is a null + /// reference (Nothing in Visual Basic). + /// + /// + /// Thrown when the specified index or is not in range. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ReadOnlySpan AsReadOnlySpan(this string text, int start, int length) + { + if (text == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + if ((uint)start > (uint)text.Length || (uint)length > (uint)(text.Length - start)) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new ReadOnlySpan(ref Unsafe.Add(ref text.GetRawStringData(), start), length); + } + internal static unsafe void CopyTo(ref T destination, ref T source, int elementsCount) { if (Unsafe.AreSame(ref destination, ref source)) From bcc7344d8ee3e96d709e1df5b27858bc6eb2620e Mon Sep 17 00:00:00 2001 From: Atsushi Kanamori Date: Wed, 10 Jan 2018 11:34:22 -0800 Subject: [PATCH 2/2] Underlying CoreCLR support for new string slicing overloads https://github.com/dotnet/corefx/issues/24072 These add the underlying support for the fast versions of these extension methods. --- src/mscorlib/shared/System/Span.NonGeneric.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/mscorlib/shared/System/Span.NonGeneric.cs b/src/mscorlib/shared/System/Span.NonGeneric.cs index 70c6b7e10e6a..f2ad245bae89 100644 --- a/src/mscorlib/shared/System/Span.NonGeneric.cs +++ b/src/mscorlib/shared/System/Span.NonGeneric.cs @@ -22,7 +22,7 @@ namespace System /// public static class Span { - /// Creates a new over the portion of the target string. + /// Creates a new over the portion of the target string. /// The target string. /// Thrown when is a null reference (Nothing in Visual Basic). public static ReadOnlyMemory AsReadOnlyMemory(this string text) @@ -33,11 +33,11 @@ public static ReadOnlyMemory AsReadOnlyMemory(this string text) return new ReadOnlyMemory(text, 0, text.Length); } - /// Creates a new over the portion of the target string. + /// Creates a new over the portion of the target string. /// The target string. /// Thrown when is a null reference (Nothing in Visual Basic). /// - /// Thrown when the specified index is not in range (<0 or >=text.Length). + /// Thrown when the specified index is not in range (<0 or >text.Length). /// public static ReadOnlyMemory AsReadOnlyMemory(this string text, int start) { @@ -50,7 +50,7 @@ public static ReadOnlyMemory AsReadOnlyMemory(this string text, int start) return new ReadOnlyMemory(text, start, text.Length - start); } - /// Creates a new over the portion of the target string. + /// Creates a new over the portion of the target string. /// The target string. /// Thrown when is a null reference (Nothing in Visual Basic). /// @@ -206,7 +206,7 @@ public static ReadOnlySpan AsReadOnlySpan(this string text) /// reference (Nothing in Visual Basic). /// /// - /// Thrown when the specified index is not in range (<0 or >=text.Length). + /// Thrown when the specified index is not in range (<0 or >text.Length). /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ReadOnlySpan AsReadOnlySpan(this string text, int start)