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)); }
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); }
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)); } }
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)); }
public static int IndexOf(this Span <char> span, ReadOnlySpan <char> value) { return(SpanHelpers.IndexOf(ref span.DangerousGetPinnableReference(), span.Length, ref value.DangerousGetPinnableReference(), value.Length)); }
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)); }
// 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);
// 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);
internal static unsafe void ZeroMemory(byte *dest, nuint len) { SpanHelpers.ClearWithoutReferences(ref *dest, len); }
public static int IndexOfAny(this ReadOnlySpan <byte> span, ReadOnlySpan <byte> values) { return(SpanHelpers.IndexOfAny(ref span.DangerousGetPinnableReference(), span.Length, ref values.DangerousGetPinnableReference(), values.Length)); }
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)); }
public bool Contains(char value) => SpanHelpers.ContainsValueType(ref Unsafe.As <char, short>(ref _firstChar), (short)value, Length);
internal static unsafe int wcslen(char *ptr) => SpanHelpers.IndexOfNullCharacter(ref *ptr);
internal static unsafe int strlen(byte *ptr) => SpanHelpers.IndexOfNullByte(ref *ptr);
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);
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)); }
internal static int CompareOrdinal(ReadOnlySpan <char> strA, ReadOnlySpan <char> strB) => SpanHelpers.SequenceCompareTo(ref MemoryMarshal.GetReference(strA), strA.Length, ref MemoryMarshal.GetReference(strB), strB.Length);
public static int BinarySearch <T, TComparable>( this ReadOnlySpan <T> span, TComparable comparable) where TComparable : IComparable <T> { return(SpanHelpers.BinarySearch(span, comparable)); }
public static int IndexOf(this Span <byte> span, byte value) { return(SpanHelpers.IndexOf(ref span.DangerousGetPinnableReference(), value, span.Length)); }
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)); }