Exemple #1
0
        public static bool EndsWith <T>(this ReadOnlySpan <T> span, ReadOnlySpan <T> value)
            where T : IEquatable <T>
        {
            int spanLength  = span.Length;
            int valueLength = value.Length;

            if (typeof(T) == typeof(byte))
            {
                return(valueLength <= spanLength &&
                       SpanHelpers.SequenceEqual(
                           ref Unsafe.As <T, byte>(ref Unsafe.Add(ref span.DangerousGetPinnableReference(), spanLength - valueLength)),
                           ref Unsafe.As <T, byte>(ref value.DangerousGetPinnableReference()),
                           valueLength));
            }
            return(valueLength <= spanLength &&
                   SpanHelpers.SequenceEqual(
                       ref Unsafe.Add(ref span.DangerousGetPinnableReference(), spanLength - valueLength),
                       ref value.DangerousGetPinnableReference(),
                       valueLength));
        }
Exemple #2
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_IndexMustBeLess);
            }

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

            int startSearchAt = startIndex + 1 - count;
            int result        = SpanHelpers.LastIndexOfValueType(ref Unsafe.As <char, short>(ref Unsafe.Add(ref _firstChar, startSearchAt)), (short)value, count);

            return(result < 0 ? result : result + startSearchAt);
        }
Exemple #3
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);
        }
        /// <summary>
        /// Reports the zero-based index of the first 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 IndexOf(this ReadOnlySpan <char> span, ReadOnlySpan <char> value, StringComparison comparisonType)
        {
            string.CheckStringComparison(comparisonType);

            if (value.Length == 0)
            {
                return(0);
            }

            if (span.Length == 0)
            {
                return(-1);
            }

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
                return(SpanHelpers.IndexOfCultureHelper(span, value, CultureInfo.CurrentCulture.CompareInfo));

            case StringComparison.CurrentCultureIgnoreCase:
                return(SpanHelpers.IndexOfCultureIgnoreCaseHelper(span, value, CultureInfo.CurrentCulture.CompareInfo));

            case StringComparison.InvariantCulture:
                return(SpanHelpers.IndexOfCultureHelper(span, value, CompareInfo.Invariant));

            case StringComparison.InvariantCultureIgnoreCase:
                return(SpanHelpers.IndexOfCultureIgnoreCaseHelper(span, value, CompareInfo.Invariant));

            case StringComparison.Ordinal:
                return(SpanHelpers.IndexOfOrdinalHelper(span, value, ignoreCase: false));

            case StringComparison.OrdinalIgnoreCase:
                return(SpanHelpers.IndexOfOrdinalHelper(span, value, ignoreCase: true));
            }

            Debug.Fail("StringComparison outside range");
            return(-1);
        }
        /// <summary>
        /// Reports the zero-based index of the first 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 IndexOf(this ReadOnlySpan <char> span, ReadOnlySpan <char> value, StringComparison comparisonType)
        {
            string.CheckStringComparison(comparisonType);

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

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

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

            default:
                Debug.Assert(comparisonType == StringComparison.OrdinalIgnoreCase);
                return(Ordinal.IndexOfOrdinalIgnoreCase(span, value));
            }
        }
Exemple #6
0
        public static bool SequenceEqual(this Span <char> first, ReadOnlySpan <char> second)
        {
            int length = first.Length;

            return(length == second.Length && SpanHelpers.SequenceEqual(ref first.DangerousGetPinnableReference(), ref second.DangerousGetPinnableReference(), length));
        }
Exemple #7
0
 public static int IndexOf(this Span <char> span, ReadOnlySpan <char> value)
 {
     return(SpanHelpers.IndexOf(ref span.DangerousGetPinnableReference(), span.Length, ref value.DangerousGetPinnableReference(), value.Length));
 }
Exemple #8
0
 public static int IndexOf <T>(this Span <T> span, ReadOnlySpan <T> value)
     where T : struct, IEquatable <T>
 {
     return(SpanHelpers.IndexOf <T>(ref span.DangerousGetPinnableReference(), span.Length, ref value.DangerousGetPinnableReference(), value.Length));
 }
Exemple #9
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.LastIndexOfValueType(ref Unsafe.As <char, short>(ref _firstChar), (short)value, Length);
Exemple #10
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);
Exemple #11
0
 internal static unsafe void ZeroMemory(byte *dest, nuint len)
 {
     SpanHelpers.ClearWithoutReferences(ref *dest, len);
 }
Exemple #12
0
 public static int IndexOfAny(this ReadOnlySpan <byte> span, ReadOnlySpan <byte> values)
 {
     return(SpanHelpers.IndexOfAny(ref span.DangerousGetPinnableReference(), span.Length, ref values.DangerousGetPinnableReference(), values.Length));
 }
Exemple #13
0
 public static int IndexOfAny(this ReadOnlySpan <byte> span, byte value0, byte value1, byte value2)
 {
     return(SpanHelpers.IndexOfAny(ref span.DangerousGetPinnableReference(), value0, value1, value2, span.Length));
 }
Exemple #14
0
 public bool Contains(char value)
 => SpanHelpers.ContainsValueType(ref Unsafe.As <char, short>(ref _firstChar), (short)value, Length);
Exemple #15
0
 internal static unsafe int wcslen(char *ptr) => SpanHelpers.IndexOfNullCharacter(ref *ptr);
Exemple #16
0
 internal static unsafe int strlen(byte *ptr) => SpanHelpers.IndexOfNullByte(ref *ptr);
Exemple #17
0
 public static int IndexOfAny(this ReadOnlySpan <byte> span, byte value0, byte value1, byte value2)
 {
     return(SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), value0, value1, value2, span.Length));
 }
 public bool Contains(char value) => SpanHelpers.Contains(ref _firstChar, value, Length);
Exemple #19
0
 public static int IndexOfAny(this ReadOnlySpan <byte> span, ReadOnlySpan <byte> values)
 {
     return(SpanHelpers.IndexOfAny(ref MemoryMarshal.GetReference(span), span.Length, ref MemoryMarshal.GetReference(values), values.Length));
 }
Exemple #20
0
 internal static int CompareOrdinal(ReadOnlySpan <char> strA, ReadOnlySpan <char> strB)
 => SpanHelpers.SequenceCompareTo(ref MemoryMarshal.GetReference(strA), strA.Length, ref MemoryMarshal.GetReference(strB), strB.Length);
Exemple #21
0
 public static int BinarySearch <T, TComparable>(
     this ReadOnlySpan <T> span, TComparable comparable)
     where TComparable : IComparable <T>
 {
     return(SpanHelpers.BinarySearch(span, comparable));
 }
Exemple #22
0
 public static int IndexOf(this Span <byte> span, byte value)
 {
     return(SpanHelpers.IndexOf(ref span.DangerousGetPinnableReference(), value, span.Length));
 }
Exemple #23
0
        public static bool StartsWith(this ReadOnlySpan <byte> span, ReadOnlySpan <byte> value)
        {
            int valueLength = value.Length;

            return(valueLength <= span.Length && SpanHelpers.SequenceEqual(ref span.DangerousGetPinnableReference(), ref value.DangerousGetPinnableReference(), valueLength));
        }