From 029568f64921f4fefe6935eae6834831fcdc688e Mon Sep 17 00:00:00 2001 From: Tarek Mahmoud Sayed Date: Wed, 6 Feb 2019 15:15:46 -0800 Subject: [PATCH] Index and Range updates (dotnet/coreclr#22331) * Index and Range updates * Address @mikedn feedback * Address Feedback * more feedback * Use Deconstruct in Range.GetOffsetAndLength * Rename GetArrayRange to GetSubArray * Temporary disable the old Corefx Range tests * Return back the TimeSpan test disabling * Fix Range jit test * Exclude the jit test * revert the changes in the jit Range test * Address Suggested Feedback Signed-off-by: dotnet-bot (cherry picked from commit b0b16179288058f42d95fe89faa76bf86496bbb9) --- src/Common/src/CoreLib/System/Index.cs | 122 ++++++++++++++++-- src/Common/src/CoreLib/System/Memory.cs | 29 +++++ .../CoreLib/System/MemoryExtensions.Fast.cs | 88 +++++++++++++ .../src/CoreLib/System/MemoryExtensions.cs | 119 +++++++++++++---- src/Common/src/CoreLib/System/Range.cs | 90 ++++++++++++- .../src/CoreLib/System/ReadOnlyMemory.cs | 29 +++++ .../src/CoreLib/System/ReadOnlySpan.Fast.cs | 41 ++++-- src/Common/src/CoreLib/System/Span.Fast.cs | 36 ++++-- .../src/CoreLib/System/String.Manipulation.cs | 14 ++ src/Common/src/CoreLib/System/String.cs | 15 ++- 10 files changed, 517 insertions(+), 66 deletions(-) diff --git a/src/Common/src/CoreLib/System/Index.cs b/src/Common/src/CoreLib/System/Index.cs index 887506ec629a..9767b981ef20 100644 --- a/src/Common/src/CoreLib/System/Index.cs +++ b/src/Common/src/CoreLib/System/Index.cs @@ -3,34 +3,138 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Runtime.CompilerServices; namespace System { + /// Represent a type can be used to index a collection either from the start or the end. + /// + /// Index is used by the C# compiler to support the new index syntax + /// + /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 } ; + /// int lastElement = someArray[^1]; // lastElement = 5 + /// + /// public readonly struct Index : IEquatable { private readonly int _value; - public Index(int value, bool fromEnd) + /// Construct an Index using a value and indicating if the index is from the start or from the end. + /// The index value. it has to be zero or positive number. + /// Indicating if the index is from the start or from the end. + /// + /// If the Index constructed from the end, index value 1 means pointing at the last element and index value 0 means pointing at beyond last element. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Index(int value, bool fromEnd = false) { if (value < 0) { ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); } - _value = fromEnd ? ~value : value; + if (fromEnd) + _value = ~value; + else + _value = value; } - public int Value => _value < 0 ? ~_value : _value; - public bool FromEnd => _value < 0; + // The following private constructors mainly created for perf reason to avoid the checks + private Index(int value) + { + _value = value; + } + + /// Create an Index pointing at first element. + public static Index Start => new Index(0); + + /// Create an Index pointing at beyond last element. + public static Index End => new Index(~0); + + /// Create an Index from the start at the position indicated by the value. + /// The index value from the start. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Index FromStart(int value) + { + if (value < 0) + { + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); + } + + return new Index(value); + } + + /// Create an Index from the end at the position indicated by the value. + /// The index value from the end. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Index FromEnd(int value) + { + if (value < 0) + { + ThrowHelper.ThrowValueArgumentOutOfRange_NeedNonNegNumException(); + } + + return new Index(~value); + } + + /// Returns the index value. + public int Value + { + get + { + if (_value < 0) + return ~_value; + else + return _value; + } + } + + /// Indicates whether the index is from the start or the end. + public bool IsFromEnd => _value < 0; + + /// Calculate the offset from the start using the giving collection length. + /// The length of the collection that the Index will be used with. length has to be a positive value + /// + /// For performance reason, we don't validate the input length parameter and the returned offset value against negative values. + /// we don't validate either the returned offset is greater than the input length. + /// It is expected Index will be used with collections which always have non negative length/count. If the returned offset is negative and + /// then used to index a collection will get out of range exception which will be same affect as the validation. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public int GetOffset(int length) + { + int offset; + + if (IsFromEnd) + offset = length - (~_value); + else + offset = _value; + + return offset; + } + + /// Indicates whether the current Index object is equal to another object of the same type. + /// An object to compare with this object public override bool Equals(object value) => value is Index && _value == ((Index)value)._value; + + /// Indicates whether the current Index object is equal to another Index object. + /// An object to compare with this object public bool Equals (Index other) => _value == other._value; - public override int GetHashCode() + /// Returns the hash code for this instance. + public override int GetHashCode() => _value; + + /// Converts integer number to an Index. + public static implicit operator Index(int value) => FromStart(value); + + /// Converts the value of the current Index object to its equivalent string representation. + public override string ToString() { - return _value; - } + if (IsFromEnd) + return ToStringFromEnd(); - public override string ToString() => FromEnd ? ToStringFromEnd() : ((uint)Value).ToString(); + return ((uint)Value).ToString(); + } private string ToStringFromEnd() { @@ -41,7 +145,5 @@ private string ToStringFromEnd() return new string(span.Slice(0, charsWritten + 1)); } - public static implicit operator Index(int value) - => new Index(value, fromEnd: false); } } diff --git a/src/Common/src/CoreLib/System/Memory.cs b/src/Common/src/CoreLib/System/Memory.cs index 20134c2ebc43..2aacfea6fe31 100644 --- a/src/Common/src/CoreLib/System/Memory.cs +++ b/src/Common/src/CoreLib/System/Memory.cs @@ -262,6 +262,35 @@ public Memory Slice(int start, int length) return new Memory(_object, _index + start, length | (capturedLength & ~RemoveFlagsBitMask)); } + /// + /// Forms a slice out of the given memory, beginning at 'startIndex' + /// + /// The index at which to begin this slice. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Memory Slice(Index startIndex) + { + int actualIndex = startIndex.GetOffset(_length); + return Slice(actualIndex); + } + + /// + /// Forms a slice out of the given memory using the range start and end indexes. + /// + /// The range used to slice the memory using its start and end indexes. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Memory Slice(Range range) + { + (int start, int length) = range.GetOffsetAndLength(_length); + // It is expected for _index + start to be negative if the memory is already pre-pinned. + return new Memory(_object, _index + start, length); + } + + /// + /// Forms a slice out of the given memory using the range start and end indexes. + /// + /// The range used to slice the memory using its start and end indexes. + public Memory this[Range range] => Slice(range); + /// /// Returns a span from the memory. /// diff --git a/src/Common/src/CoreLib/System/MemoryExtensions.Fast.cs b/src/Common/src/CoreLib/System/MemoryExtensions.Fast.cs index d256887a9f7e..b27084c29c38 100644 --- a/src/Common/src/CoreLib/System/MemoryExtensions.Fast.cs +++ b/src/Common/src/CoreLib/System/MemoryExtensions.Fast.cs @@ -376,6 +376,54 @@ public static Span AsSpan(this T[] array, int start) return new Span(ref Unsafe.Add(ref Unsafe.As(ref array.GetRawSzArrayData()), start), array.Length - start); } + /// + /// Creates a new span over the portion of the target array. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span AsSpan(this T[] array, Index startIndex) + { + if (array == null) + { + if (!startIndex.Equals(Index.Start)) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); + + return default; + } + + if (default(T) == null && array.GetType() != typeof(T[])) + ThrowHelper.ThrowArrayTypeMismatchException(); + + int actualIndex = startIndex.GetOffset(array.Length); + if ((uint)actualIndex > (uint)array.Length) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new Span(ref Unsafe.Add(ref Unsafe.As(ref array.GetRawSzArrayData()), actualIndex), array.Length - actualIndex); + } + + /// + /// Creates a new span over the portion of the target array. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span AsSpan(this T[] array, Range range) + { + if (array == null) + { + Index startIndex = range.Start; + Index endIndex = range.End; + + if (!startIndex.Equals(Index.Start) || !endIndex.Equals(Index.Start)) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); + + return default; + } + + if (default(T) == null && array.GetType() != typeof(T[])) + ThrowHelper.ThrowArrayTypeMismatchException(); + + (int start, int length) = range.GetOffsetAndLength(array.Length); + return new Span(ref Unsafe.Add(ref Unsafe.As(ref array.GetRawSzArrayData()), start), length); + } + /// /// Creates a new readonly span over the portion of the target string. /// @@ -474,6 +522,26 @@ public static ReadOnlyMemory AsMemory(this string text, int start) return new ReadOnlyMemory(text, start, text.Length - start); } + /// Creates a new over the portion of the target string. + /// The target string. + /// The index at which to begin this slice. + public static ReadOnlyMemory AsMemory(this string text, Index startIndex) + { + if (text == null) + { + if (!startIndex.Equals(Index.Start)) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + return default; + } + + int actualIndex = startIndex.GetOffset(text.Length); + if ((uint)actualIndex > (uint)text.Length) + ThrowHelper.ThrowArgumentOutOfRangeException(); + + return new ReadOnlyMemory(text, actualIndex, text.Length - actualIndex); + } + /// Creates a new over the portion of the target string. /// The target string. /// The index at which to begin this slice. @@ -496,5 +564,25 @@ public static ReadOnlyMemory AsMemory(this string text, int start, int len return new ReadOnlyMemory(text, start, length); } + + /// Creates a new over the portion of the target string. + /// The target string. + /// The range used to indicate the start and length of the sliced string. + public static ReadOnlyMemory AsMemory(this string text, Range range) + { + if (text == null) + { + Index startIndex = range.Start; + Index endIndex = range.End; + + if (!startIndex.Equals(Index.Start) || !endIndex.Equals(Index.Start)) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.text); + + return default; + } + + (int start, int length) = range.GetOffsetAndLength(text.Length); + return new ReadOnlyMemory(text, start, length); + } } } diff --git a/src/Common/src/CoreLib/System/MemoryExtensions.cs b/src/Common/src/CoreLib/System/MemoryExtensions.cs index a706b7b68595..6eccc6d98584 100644 --- a/src/Common/src/CoreLib/System/MemoryExtensions.cs +++ b/src/Common/src/CoreLib/System/MemoryExtensions.cs @@ -106,7 +106,7 @@ public static ReadOnlySpan TrimEnd(this ReadOnlySpan span, char trim } /// - /// Removes all leading and trailing occurrences of a set of characters specified + /// Removes all leading and trailing occurrences of a set of characters specified /// in a readonly span from the span. /// /// The source span from which the characters are removed. @@ -118,7 +118,7 @@ public static ReadOnlySpan Trim(this ReadOnlySpan span, ReadOnlySpan } /// - /// Removes all leading occurrences of a set of characters specified + /// Removes all leading occurrences of a set of characters specified /// in a readonly span from the span. /// /// The source span from which the characters are removed. @@ -147,7 +147,7 @@ public static ReadOnlySpan TrimStart(this ReadOnlySpan span, ReadOnl } /// - /// Removes all trailing occurrences of a set of characters specified + /// Removes all trailing occurrences of a set of characters specified /// in a readonly span from the span. /// /// The source span from which the characters are removed. @@ -212,7 +212,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)), } /// - /// Searches for the specified sequence and returns the index of its first occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). + /// Searches for the specified sequence and returns the index of its first occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). /// /// The span to search. /// The sequence to search for. @@ -231,7 +231,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(value)), } /// - /// Searches for the specified value and returns the index of its last occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). + /// Searches for the specified value and returns the index of its last occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). /// /// The span to search. /// The value to search for. @@ -254,7 +254,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)), } /// - /// Searches for the specified sequence and returns the index of its last occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). + /// Searches for the specified sequence and returns the index of its last occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). /// /// The span to search. /// The sequence to search for. @@ -273,7 +273,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(value)), } /// - /// Determines whether two sequences are equal by comparing the elements using IEquatable{T}.Equals(T). + /// Determines whether two sequences are equal by comparing the elements using IEquatable{T}.Equals(T). /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool SequenceEqual(this Span span, ReadOnlySpan other) @@ -292,7 +292,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(other)), } /// - /// Determines the relative order of the sequences being compared by comparing the elements using IComparable{T}.CompareTo(T). + /// Determines the relative order of the sequences being compared by comparing the elements using IComparable{T}.CompareTo(T). /// public static int SequenceCompareTo(this Span span, ReadOnlySpan other) where T : IComparable @@ -315,7 +315,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(other)), } /// - /// Searches for the specified value and returns the index of its first occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). + /// Searches for the specified value and returns the index of its first occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). /// /// The span to search. /// The value to search for. @@ -338,7 +338,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)), } /// - /// Searches for the specified sequence and returns the index of its first occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). + /// Searches for the specified sequence and returns the index of its first occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). /// /// The span to search. /// The sequence to search for. @@ -357,7 +357,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(value)), } /// - /// Searches for the specified value and returns the index of its last occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). + /// Searches for the specified value and returns the index of its last occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). /// /// The span to search. /// The value to search for. @@ -380,7 +380,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)), } /// - /// Searches for the specified sequence and returns the index of its last occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). + /// Searches for the specified sequence and returns the index of its last occurrence. If not found, returns -1. Values are compared using IEquatable{T}.Equals(T). /// /// The span to search. /// The sequence to search for. @@ -441,7 +441,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)), } /// - /// Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator. If not found, returns -1. + /// Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator. If not found, returns -1. /// /// The span to search. /// The set of values to search for. @@ -480,7 +480,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)), } /// - /// Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator. If not found, returns -1. + /// Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator. If not found, returns -1. /// /// The span to search. /// One of the values to search for. @@ -502,7 +502,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)), } /// - /// Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator. If not found, returns -1. + /// Searches for the first index of any of the specified values similar to calling IndexOf several times with the logical OR operator. If not found, returns -1. /// /// The span to search. /// The set of values to search for. @@ -563,7 +563,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)), } /// - /// Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator. If not found, returns -1. + /// Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator. If not found, returns -1. /// /// The span to search. /// The set of values to search for. @@ -602,7 +602,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)), } /// - /// Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator. If not found, returns -1. + /// Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator. If not found, returns -1. /// /// The span to search. /// One of the values to search for. @@ -624,7 +624,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)), } /// - /// Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator. If not found, returns -1. + /// Searches for the last index of any of the specified values similar to calling LastIndexOf several times with the logical OR operator. If not found, returns -1. /// /// The span to search. /// The set of values to search for. @@ -643,7 +643,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(values)), } /// - /// Determines whether two sequences are equal by comparing the elements using IEquatable{T}.Equals(T). + /// Determines whether two sequences are equal by comparing the elements using IEquatable{T}.Equals(T). /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool SequenceEqual(this ReadOnlySpan span, ReadOnlySpan other) @@ -661,7 +661,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(other)), } /// - /// Determines the relative order of the sequences being compared by comparing the elements using IComparable{T}.CompareTo(T). + /// Determines the relative order of the sequences being compared by comparing the elements using IComparable{T}.CompareTo(T). /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int SequenceCompareTo(this ReadOnlySpan span, ReadOnlySpan other) @@ -840,6 +840,19 @@ public static Span AsSpan(this ArraySegment segment, int start) return new Span(segment.Array, segment.Offset + start, segment.Count - start); } + /// + /// Creates a new Span over the portion of the target array beginning + /// at 'startIndex' and ending at the end of the segment. + /// + /// The target array. + /// The index at which to begin the Span. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span AsSpan(this ArraySegment segment, Index startIndex) + { + int actualIndex = startIndex.GetOffset(segment.Count); + return AsSpan(segment, actualIndex); + } + /// /// Creates a new Span over the portion of the target array beginning /// at 'start' index and ending at 'end' index (exclusive). @@ -863,6 +876,18 @@ public static Span AsSpan(this ArraySegment segment, int start, int len return new Span(segment.Array, segment.Offset + start, length); } + /// + /// Creates a new Span over the portion of the target array using the range start and end indexes + /// + /// The target array. + /// The range which has start and end indexes to use for slicing the array. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Span AsSpan(this ArraySegment segment, Range range) + { + (int start, int length) = range.GetOffsetAndLength(segment.Count); + return new Span(segment.Array, segment.Offset + start, length); + } + /// /// Creates a new memory over the target array. /// @@ -881,6 +906,24 @@ public static Span AsSpan(this ArraySegment segment, int start, int len /// public static Memory AsMemory(this T[] array, int start) => new Memory(array, start); + /// + /// Creates a new memory over the portion of the target array starting from + /// 'startIndex' to the end of the array. + /// + public static Memory AsMemory(this T[] array, Index startIndex) + { + if (array == null) + { + if (!startIndex.Equals(Index.Start)) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); + + return default; + } + + int actualIndex = startIndex.GetOffset(array.Length); + return new Memory(array, actualIndex); + } + /// /// Creates a new memory over the portion of the target array beginning /// at 'start' index and ending at 'end' index (exclusive). @@ -895,6 +938,26 @@ public static Span AsSpan(this ArraySegment segment, int start, int len /// public static Memory AsMemory(this T[] array, int start, int length) => new Memory(array, start, length); + /// + /// Creates a new memory over the portion of the target array beginning at inclusive start index of the range + /// and ending at the exclusive end index of the range. + /// + public static Memory AsMemory(this T[] array, Range range) + { + if (array == null) + { + Index startIndex = range.Start; + Index endIndex = range.End; + if (!startIndex.Equals(Index.Start) || !endIndex.Equals(Index.Start)) + ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array); + + return default; + } + + (int start, int length) = range.GetOffsetAndLength(array.Length); + return new Memory(array, start, length); + } + /// /// Creates a new memory over the portion of the target array. /// @@ -945,7 +1008,7 @@ public static Memory AsMemory(this ArraySegment segment, int start, int /// Copies the contents of the array into the span. If the source /// and destinations overlap, this method behaves as if the original values in /// a temporary location before the destination is overwritten. - /// + /// ///The array to copy items from. /// The span to copy items into. /// @@ -962,7 +1025,7 @@ public static void CopyTo(this T[] source, Span destination) /// Copies the contents of the array into the memory. If the source /// and destinations overlap, this method behaves as if the original values are in /// a temporary location before the destination is overwritten. - /// + /// ///The array to copy items from. /// The memory to copy items into. /// @@ -1061,16 +1124,16 @@ public static void CopyTo(this T[] source, Memory destination) // nuint x2 = xLength // nuint y1 = (nuint)Unsafe.ByteOffset(xRef, yRef) // nuint y2 = y1 + yLength - // + // // xRef relative to xRef is 0. - // + // // x2 is simply x1 + xLength. This cannot overflow. - // + // // yRef relative to xRef is (yRef - xRef). If (yRef - xRef) is // negative, casting it to an unsigned 32-bit integer turns it into // (yRef - xRef + 2³²). So, in the example above, y1 moves to the right // of x2. - // + // // y2 is simply y1 + yLength. Note that this can overflow, as in the // example above, which must be avoided. // @@ -1097,11 +1160,11 @@ public static void CopyTo(this T[] source, Memory destination) // integers: // // == (y1 < xLength) || (y1 > -yLength) - // + // // Due to modulo arithmetic, this gives exactly same result *except* if // yLength is zero, since 2³² - 0 is 0 and not 2³². So the case // y.IsEmpty must be handled separately first. - // + // /// /// Determines whether two sequences overlap in memory. diff --git a/src/Common/src/CoreLib/System/Range.cs b/src/Common/src/CoreLib/System/Range.cs index b858da2fb438..0098dea17ff7 100644 --- a/src/Common/src/CoreLib/System/Range.cs +++ b/src/Common/src/CoreLib/System/Range.cs @@ -3,20 +3,38 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Runtime.CompilerServices; namespace System { + /// Represent a range has start and end indexes. + /// + /// Range is used by the C# compiler to support the range syntax. + /// + /// int[] someArray = new int[5] { 1, 2, 3, 4, 5 }; + /// int[] subArray1 = someArray[0..2]; // { 1, 2 } + /// int[] subArray2 = someArray[1..^0]; // { 2, 3, 4, 5 } + /// + /// public readonly struct Range : IEquatable { + /// Represent the inclusive start index of the Range. public Index Start { get; } + + /// Represent the exclusive end index of the Range. public Index End { get; } - private Range(Index start, Index end) + /// Construct a Range object using the start and end indexes. + /// Represent the inclusive start index of the range. + /// Represent the exclusive end index of the range. + public Range(Index start, Index end) { Start = start; End = end; } + /// Indicates whether the current Range object is equal to another object of the same type. + /// An object to compare with this object public override bool Equals(object value) { if (value is Range) @@ -28,20 +46,24 @@ public override bool Equals(object value) return false; } + /// Indicates whether the current Range object is equal to another Range object. + /// An object to compare with this object public bool Equals (Range other) => other.Start.Equals(Start) && other.End.Equals(End); + /// Returns the hash code for this instance. public override int GetHashCode() { return HashCode.Combine(Start.GetHashCode(), End.GetHashCode()); } + /// Converts the value of the current Range object to its equivalent string representation. public override string ToString() { Span span = stackalloc char[2 + (2 * 11)]; // 2 for "..", then for each index 1 for '^' and 10 for longest possible uint int charsWritten; int pos = 0; - if (Start.FromEnd) + if (Start.IsFromEnd) { span[0] = '^'; pos = 1; @@ -53,7 +75,7 @@ public override string ToString() span[pos++] = '.'; span[pos++] = '.'; - if (End.FromEnd) + if (End.IsFromEnd) { span[pos++] = '^'; } @@ -64,9 +86,63 @@ public override string ToString() return new string(span.Slice(0, pos)); } - public static Range Create(Index start, Index end) => new Range(start, end); - public static Range FromStart(Index start) => new Range(start, new Index(0, fromEnd: true)); - public static Range ToEnd(Index end) => new Range(new Index(0, fromEnd: false), end); - public static Range All() => new Range(new Index(0, fromEnd: false), new Index(0, fromEnd: true)); + /// Create a Range object starting from start index to the end of the collection. + public static Range StartAt(Index start) => new Range(start, Index.End); + + /// Create a Range object starting from first element in the collection to the end Index. + public static Range EndAt(Index end) => new Range(Index.Start, end); + + /// Create a Range object starting from first element to the end. + public static Range All => new Range(Index.Start, Index.End); + + /// Destruct the range object according to a collection length and return the start offset from the beginning and the length of this range. + /// The length of the collection that the range will be used with. length has to be a positive value + /// + /// For performance reason, we don't validate the input length parameter against negative values. + /// It is expected Range will be used with collections which always have non negative length/count. + /// We validate the range is inside the length scope though. + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public OffsetAndLength GetOffsetAndLength(int length) + { + int start; + Index startIndex = Start; + if (startIndex.IsFromEnd) + start = length - startIndex.Value; + else + start = startIndex.Value; + + int end; + Index endIndex = End; + if (endIndex.IsFromEnd) + end = length - endIndex.Value; + else + end = endIndex.Value; + + if ((uint)end > (uint)length || (uint)start > (uint)end) + { + ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length); + } + + return new OffsetAndLength(start, end - start); + } + + public readonly struct OffsetAndLength + { + public int Offset { get; } + public int Length { get; } + + public OffsetAndLength(int offset, int length) + { + Offset = offset; + Length = length; + } + + public void Deconstruct(out int offset, out int length) + { + offset = Offset; + length = Length; + } + } } } diff --git a/src/Common/src/CoreLib/System/ReadOnlyMemory.cs b/src/Common/src/CoreLib/System/ReadOnlyMemory.cs index c9f9a720b8e7..747aefd907e6 100644 --- a/src/Common/src/CoreLib/System/ReadOnlyMemory.cs +++ b/src/Common/src/CoreLib/System/ReadOnlyMemory.cs @@ -184,6 +184,35 @@ public ReadOnlyMemory Slice(int start, int length) return new ReadOnlyMemory(_object, _index + start, length | (capturedLength & ~RemoveFlagsBitMask)); } + /// + /// Forms a slice out of the given memory, beginning at 'startIndex' + /// + /// The index at which to begin this slice. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ReadOnlyMemory Slice(Index startIndex) + { + int actualIndex = startIndex.GetOffset(_length); + return Slice(actualIndex); + } + + /// + /// Forms a slice out of the given memory using the range start and end indexes. + /// + /// The range used to slice the memory using its start and end indexes. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ReadOnlyMemory Slice(Range range) + { + (int start, int length) = range.GetOffsetAndLength(_length); + // It is expected for _index + start to be negative if the memory is already pre-pinned. + return new ReadOnlyMemory(_object, _index + start, length); + } + + /// + /// Forms a slice out of the given memory using the range start and end indexes. + /// + /// The range used to slice the memory using its start and end indexes. + public ReadOnlyMemory this[Range range] => Slice(range); + /// /// Returns a span from the memory. /// diff --git a/src/Common/src/CoreLib/System/ReadOnlySpan.Fast.cs b/src/Common/src/CoreLib/System/ReadOnlySpan.Fast.cs index 550543939eff..7bfd3e0ef9e0 100644 --- a/src/Common/src/CoreLib/System/ReadOnlySpan.Fast.cs +++ b/src/Common/src/CoreLib/System/ReadOnlySpan.Fast.cs @@ -153,19 +153,13 @@ public ref readonly T this[Index index] { get { - return ref this [index.FromEnd ? _length - index.Value : index.Value]; + // Evaluate the actual index first because it helps performance + int actualIndex = index.GetOffset(_length); + return ref this [actualIndex]; } } - public ReadOnlySpan this[Range range] - { - get - { - int start = range.Start.FromEnd ? _length - range.Start.Value : range.Start.Value; - int end = range.End.FromEnd ? _length - range.End.Value : range.End.Value; - return Slice(start, end - start); - } - } + public ReadOnlySpan this[Range range] => Slice(range); /// /// Returns a reference to the 0th element of the Span. If the Span is empty, returns null reference. @@ -279,6 +273,33 @@ public ReadOnlySpan Slice(int start, int length) return new ReadOnlySpan(ref Unsafe.Add(ref _pointer.Value, start), length); } + /// + /// Forms a slice out of the given read-only span, beginning at 'startIndex' + /// + /// The index at which to begin this slice. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ReadOnlySpan Slice(Index startIndex) + { + int actualIndex; + if (startIndex.IsFromEnd) + actualIndex = _length - startIndex.Value; + else + actualIndex = startIndex.Value; + + return Slice(actualIndex); + } + + /// + /// Forms a slice out of the given read-only span, beginning at range start index to the range end + /// + /// The range which has the start and end indexes used to slice the span. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ReadOnlySpan Slice(Range range) + { + (int start, int length) = range.GetOffsetAndLength(_length); + return new ReadOnlySpan(ref Unsafe.Add(ref _pointer.Value, start), length); + } + /// /// Copies the contents of this read-only span into a new array. This heap /// allocates, so should generally be avoided, however it is sometimes diff --git a/src/Common/src/CoreLib/System/Span.Fast.cs b/src/Common/src/CoreLib/System/Span.Fast.cs index 23c01d2db44f..54873173cf91 100644 --- a/src/Common/src/CoreLib/System/Span.Fast.cs +++ b/src/Common/src/CoreLib/System/Span.Fast.cs @@ -158,19 +158,13 @@ public ref T this[Index index] { get { - return ref this [index.FromEnd ? _length - index.Value : index.Value]; + // Evaluate the actual index first because it helps performance + int actualIndex = index.GetOffset(_length); + return ref this [actualIndex]; } } - public Span this[Range range] - { - get - { - int start = range.Start.FromEnd ? _length - range.Start.Value : range.Start.Value; - int end = range.End.FromEnd ? _length - range.End.Value : range.End.Value; - return Slice(start, end - start); - } - } + public Span this[Range range] => Slice(range); /// /// Returns a reference to the 0th element of the Span. If the Span is empty, returns null reference. @@ -358,6 +352,28 @@ public Span Slice(int start, int length) return new Span(ref Unsafe.Add(ref _pointer.Value, start), length); } + /// + /// Forms a slice out of the given span, beginning at 'startIndex' + /// + /// The index at which to begin this slice. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Span Slice(Index startIndex) + { + int actualIndex = startIndex.GetOffset(_length); + return Slice(actualIndex); + } + + /// + /// Forms a slice out of the given span, beginning at range start index to the range end + /// + /// The range which has the start and end indexes used to slice the span. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Span Slice(Range range) + { + (int start, int length) = range.GetOffsetAndLength(_length); + return new Span(ref Unsafe.Add(ref _pointer.Value, start), length); + } + /// /// Copies the contents of this span into a new array. This heap /// allocates, so should generally be avoided, however it is sometimes diff --git a/src/Common/src/CoreLib/System/String.Manipulation.cs b/src/Common/src/CoreLib/System/String.Manipulation.cs index 69609aacfb3b..c74da7f8304b 100644 --- a/src/Common/src/CoreLib/System/String.Manipulation.cs +++ b/src/Common/src/CoreLib/System/String.Manipulation.cs @@ -1626,6 +1626,20 @@ public string Substring(int startIndex, int length) return InternalSubString(startIndex, length); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public string Substring(Index startIndex) + { + int actualIndex = startIndex.GetOffset(Length); + return Substring(actualIndex); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public string Substring(Range range) + { + (int start, int length) = range.GetOffsetAndLength(Length); + return Substring(start, length); + } + private unsafe string InternalSubString(int startIndex, int length) { Debug.Assert(startIndex >= 0 && startIndex <= this.Length, "StartIndex is out of range!"); diff --git a/src/Common/src/CoreLib/System/String.cs b/src/Common/src/CoreLib/System/String.cs index 00aec0515a19..5e552b0c0dcf 100644 --- a/src/Common/src/CoreLib/System/String.cs +++ b/src/Common/src/CoreLib/System/String.cs @@ -458,6 +458,19 @@ public static bool IsNullOrEmpty(string value) return (value == null || 0u >= (uint)value.Length) ? true : false; } + [System.Runtime.CompilerServices.IndexerName("Chars")] + public char this[Index index] + { + get + { + int actualIndex = index.GetOffset(Length); + return this[actualIndex]; + } + } + + [System.Runtime.CompilerServices.IndexerName("Chars")] + public string this[Range range] => Substring(range); + public static bool IsNullOrWhiteSpace(string value) { if (value == null) return true; @@ -648,7 +661,7 @@ internal static unsafe int wcslen(char* ptr) // // IConvertible implementation - // + // public TypeCode GetTypeCode() {