/// <summary>
        /// Reports the zero-based index of the last occurrence of the specified <paramref name="value"/> in the current <paramref name="span"/>.
        /// <param name="span">The source span.</param>
        /// <param name="value">The value to seek within the source span.</param>
        /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
        /// </summary>
        public static int LastIndexOf(this ReadOnlySpan <char> span, ReadOnlySpan <char> value, StringComparison comparisonType)
        {
            string.CheckStringComparison(comparisonType);

            if (comparisonType == StringComparison.Ordinal)
            {
                return(SpanHelpers.LastIndexOf(
                           ref MemoryMarshal.GetReference(span),
                           span.Length,
                           ref MemoryMarshal.GetReference(value),
                           value.Length));
            }

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
            case StringComparison.CurrentCultureIgnoreCase:
                return(CultureInfo.CurrentCulture.CompareInfo.LastIndexOf(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType)));

            case StringComparison.InvariantCulture:
            case StringComparison.InvariantCultureIgnoreCase:
                return(CompareInfo.Invariant.LastIndexOf(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType)));

            default:
                Debug.Assert(comparisonType == StringComparison.OrdinalIgnoreCase);
                return(Ordinal.LastIndexOfOrdinalIgnoreCase(span, value));
            }
        }
Example #2
0
 public static int LastIndexOf <T>(this Span <T> span, T value)
     where T : IEquatable <T>
 {
     if (typeof(T) == typeof(byte))
     {
         return(SpanHelpers.LastIndexOf(
                    ref Unsafe.As <T, byte>(ref MemoryMarshal.GetReference(span)),
                    Unsafe.As <T, byte>(ref value),
                    span.Length));
     }
     return(SpanHelpers.LastIndexOf <T>(ref MemoryMarshal.GetReference(span), value, span.Length));
 }
Example #3
0
 public static int LastIndexOf <T>(this Span <T> span, T value)
     where T : IEquatable <T>
 {
     if (typeof(T) == typeof(byte))
     {
         return(SpanHelpers.LastIndexOf(
                    ref Unsafe.As <T, byte>(ref span.DangerousGetPinnableReference()),
                    Unsafe.As <T, byte>(ref value),
                    span.Length));
     }
     return(SpanHelpers.LastIndexOf <T>(ref span.DangerousGetPinnableReference(), value, span.Length));
 }
Example #4
0
        public unsafe int LastIndexOf(char value, int startIndex, int count)
        {
            if (Length == 0)
            {
                return(-1);
            }

            if ((uint)startIndex >= (uint)Length)
            {
                throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_Index);
            }

            if ((uint)count > (uint)startIndex + 1)
            {
                throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_Count);
            }

            int startSearchAt = startIndex + 1 - count;
            int result        = SpanHelpers.LastIndexOf(ref Unsafe.Add(ref _firstChar, startSearchAt), value, count);

            return(result == -1 ? result : result + startSearchAt);
        }
Example #5
0
        public unsafe int LastIndexOf(char value, int startIndex, int count)
        {
            if (Length == 0)
            {
                return(-1);
            }

            if ((uint)startIndex >= (uint)Length)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
            }

            if ((uint)count > (uint)startIndex + 1)
            {
                ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_Count);
            }

            int startSearchAt = startIndex + 1 - count;
            int result        = SpanHelpers.LastIndexOf(ref Unsafe.Add(ref _firstChar, startSearchAt), value, count);

            return(result < 0 ? result : result + startSearchAt);
        }
Example #6
0
 // Returns the index of the last occurrence of a specified character in the current instance.
 // The search starts at startIndex and runs backwards to startIndex - count + 1.
 // The character at position startIndex is included in the search.  startIndex is the larger
 // index within the string.
 //
 public int LastIndexOf(char value) => SpanHelpers.LastIndexOf(ref _firstChar, value, Length);