From 503c618969e0e34bec6cf93632763b6ec339ec55 Mon Sep 17 00:00:00 2001 From: Ahson Ahmed Khan Date: Tue, 31 Jan 2017 21:43:43 -0800 Subject: [PATCH] Renaming Span/ReadOnlySpan Extension classes to avoid conflict with CoreFX. (dotnet/coreclr#9182) * Moving all extension methods to a class called Span * Adding known workaround for JIT bug. Commit migrated from https://github.com/dotnet/coreclr/commit/68cb3c7cab52b1648b4f4c2246f08652092ac2f3 --- src/coreclr/src/mscorlib/model.xml | 5 +- .../src/mscorlib/src/System/ReadOnlySpan.cs | 58 --------------------- src/coreclr/src/mscorlib/src/System/Span.cs | 60 +++++++++++++++++++++- 3 files changed, 60 insertions(+), 63 deletions(-) diff --git a/src/coreclr/src/mscorlib/model.xml b/src/coreclr/src/mscorlib/model.xml index 324c8b5..53a7f38 100644 --- a/src/coreclr/src/mscorlib/model.xml +++ b/src/coreclr/src/mscorlib/model.xml @@ -12659,6 +12659,7 @@ + @@ -12686,13 +12687,11 @@ - + - - diff --git a/src/coreclr/src/mscorlib/src/System/ReadOnlySpan.cs b/src/coreclr/src/mscorlib/src/System/ReadOnlySpan.cs index c7125eb..e67cc30 100644 --- a/src/coreclr/src/mscorlib/src/System/ReadOnlySpan.cs +++ b/src/coreclr/src/mscorlib/src/System/ReadOnlySpan.cs @@ -327,62 +327,4 @@ namespace System /// public static ReadOnlySpan Empty => default(ReadOnlySpan); } - - public static class ReadOnlySpanExtensions - { - /// - /// 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). - public static ReadOnlySpan Slice(this string text) - { - if (text == null) - ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); - - return new ReadOnlySpan(ref text.GetFirstCharRef(), text.Length); - } - - /// - /// Creates a new readonly span over the portion of the target string, beginning at 'start'. - /// - /// The target string. - /// The index at which to begin this slice. - /// Thrown when is a null - /// reference (Nothing in Visual Basic). - /// - /// Thrown when the specified index is not in range (<0 or >Length). - /// - public static ReadOnlySpan Slice(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.GetFirstCharRef(), start), text.Length - start); - } - - /// - /// Creates a new readonly span over the portion of the target string, beginning at , of given . - /// - /// The target string. - /// The index at which to begin this slice. - /// The number of items in the span. - /// Thrown when is a null - /// reference (Nothing in Visual Basic). - /// - /// Thrown when the specified or end index is not in range (<0 or >&eq;Length). - /// - public static ReadOnlySpan Slice(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.GetFirstCharRef(), start), length); - } - } } diff --git a/src/coreclr/src/mscorlib/src/System/Span.cs b/src/coreclr/src/mscorlib/src/System/Span.cs index a6e46d5..5f6a7ae 100644 --- a/src/coreclr/src/mscorlib/src/System/Span.cs +++ b/src/coreclr/src/mscorlib/src/System/Span.cs @@ -217,7 +217,8 @@ namespace System // TODO: https://github.com/dotnet/corefx/issues/13681 // Until we get over the hurdle of C# 7 tooling, this temporary method will simulate the intended "ref T" indexer for those // who need bypass the workaround for performance. - [MethodImpl(MethodImplOptions.AggressiveInlining)] + //[MethodImpl(MethodImplOptions.AggressiveInlining)] + [MethodImpl(MethodImplOptions.NoOptimization)] // TODO-SPAN: Workaround for https://github.com/dotnet/coreclr/issues/7894 public ref T GetItem(int index) { if ((uint)index >= ((uint)_length)) @@ -396,7 +397,7 @@ namespace System public static Span Empty => default(Span); } - public static class SpanExtensions + public static class Span { /// /// Casts a Span of one primitive type to Span of bytes. @@ -485,6 +486,61 @@ namespace System ref Unsafe.As(ref source.DangerousGetPinnableReference()), checked((int)((long)source.Length * Unsafe.SizeOf() / Unsafe.SizeOf()))); } + + /// + /// 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). + public static ReadOnlySpan Slice(this string text) + { + if (text == null) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + return new ReadOnlySpan(ref text.GetFirstCharRef(), text.Length); + } + + /// + /// Creates a new readonly span over the portion of the target string, beginning at 'start'. + /// + /// The target string. + /// The index at which to begin this slice. + /// Thrown when is a null + /// reference (Nothing in Visual Basic). + /// + /// Thrown when the specified index is not in range (<0 or >Length). + /// + public static ReadOnlySpan Slice(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.GetFirstCharRef(), start), text.Length - start); + } + + /// + /// Creates a new readonly span over the portion of the target string, beginning at , of given . + /// + /// The target string. + /// The index at which to begin this slice. + /// The number of items in the span. + /// Thrown when is a null + /// reference (Nothing in Visual Basic). + /// + /// Thrown when the specified or end index is not in range (<0 or >&eq;Length). + /// + public static ReadOnlySpan Slice(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.GetFirstCharRef(), start), length); + } } internal static class SpanHelper -- 2.7.4