/// <summary> /// Determines whether the end of the <paramref name="span"/> matches the specified <paramref name="value"/> when compared using the specified <paramref name="comparisonType"/> option. /// </summary> /// <param name="span">The source span.</param> /// <param name="value">The sequence to compare to the end of 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> public static bool EndsWith(this ReadOnlySpan <char> span, ReadOnlySpan <char> value, StringComparison comparisonType) { string.CheckStringComparison(comparisonType); if (value.Length == 0) { return(true); } if (comparisonType >= StringComparison.Ordinal || GlobalizationMode.Invariant) { if (string.GetCaseCompareOfComparisonCulture(comparisonType) == CompareOptions.None) { return(span.EndsWith(value)); } return((span.Length >= value.Length) ? (CompareInfo.CompareOrdinalIgnoreCase(span.Slice(span.Length - value.Length), value) == 0) : false); } if (span.Length == 0) { return(false); } return((comparisonType >= StringComparison.InvariantCulture) ? CompareInfo.Invariant.IsSuffix(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType)) : CultureInfo.CurrentCulture.CompareInfo.IsSuffix(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType))); }
/// <summary> /// Determines whether the end of the <paramref name="span"/> matches the specified <paramref name="value"/> when compared using the specified <paramref name="comparisonType"/> option. /// </summary> /// <param name="span">The source span.</param> /// <param name="value">The sequence to compare to the end of 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> public static bool EndsWith(this ReadOnlySpan <char> span, ReadOnlySpan <char> value, StringComparison comparisonType) { if (value.Length == 0) { string.CheckStringComparison(comparisonType); return(true); } switch (comparisonType) { case StringComparison.CurrentCulture: return(SpanHelpers.EndsWithCultureHelper(span, value, CultureInfo.CurrentCulture.CompareInfo)); case StringComparison.CurrentCultureIgnoreCase: return(SpanHelpers.EndsWithCultureIgnoreCaseHelper(span, value, CultureInfo.CurrentCulture.CompareInfo)); case StringComparison.InvariantCulture: return(SpanHelpers.EndsWithCultureHelper(span, value, CompareInfo.Invariant)); case StringComparison.InvariantCultureIgnoreCase: return(SpanHelpers.EndsWithCultureIgnoreCaseHelper(span, value, CompareInfo.Invariant)); case StringComparison.Ordinal: return(span.EndsWith(value)); //TODO: Optimize - https://github.com/dotnet/corefx/issues/27487 case StringComparison.OrdinalIgnoreCase: return(SpanHelpers.EndsWithOrdinalIgnoreCaseHelper(span, value)); default: throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType)); } }
/// <summary> /// Determines whether the end of the <paramref name="span"/> matches the specified <paramref name="other"/> when compared using the specified <paramref name="comparisonType"/> option. /// </summary> /// <param name="span">The source span.</param> /// <param name="other">The sequence to compare to the end of the source span.</param> /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="other"/> are compared.</param> public static bool EndsWith(this ReadOnlySpan <char> span, ReadOnlySpan <char> other, StringComparison comparisonType) { if (comparisonType == StringComparison.Ordinal) { return(span.EndsWith <char>(other)); } string sourceString = span.ToString(); string valueString = other.ToString(); return(sourceString.EndsWith(valueString, comparisonType)); }
public static bool EndsWithCultureHelper(ReadOnlySpan <char> span, ReadOnlySpan <char> value, CompareInfo compareInfo) { Debug.Assert(value.Length != 0); if (GlobalizationMode.Invariant) { return(span.EndsWith(value)); } if (span.Length == 0) { return(false); } return(compareInfo.IsSuffix(span, value, CompareOptions.None)); }
/// <summary> /// Determines whether the end of the <paramref name="span"/> matches the specified <paramref name="value"/> when compared using the specified <paramref name="comparisonType"/> option. /// </summary> /// <param name="span">The source span.</param> /// <param name="value">The sequence to compare to the end of 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> public static bool EndsWith(this ReadOnlySpan <char> span, ReadOnlySpan <char> value, StringComparison comparisonType) { if (comparisonType == StringComparison.Ordinal) { return(span.EndsWith <char>(value)); } else if (comparisonType == StringComparison.OrdinalIgnoreCase) { return(value.Length <= span.Length && EqualsOrdinalIgnoreCase(span.Slice(span.Length - value.Length), value)); } string sourceString = span.ToString(); string valueString = value.ToString(); return(sourceString.EndsWith(valueString, comparisonType)); }
/// <summary> /// Determines whether the end of the <paramref name="span"/> matches the specified <paramref name="value"/> when compared using the specified <paramref name="comparisonType"/> option. /// </summary> /// <param name="span">The source span.</param> /// <param name="value">The sequence to compare to the end of 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> public static bool EndsWith(this ReadOnlySpan <char> span, ReadOnlySpan <char> value, StringComparison comparisonType) { string.CheckStringComparison(comparisonType); switch (comparisonType) { case StringComparison.CurrentCulture: case StringComparison.CurrentCultureIgnoreCase: return(CultureInfo.CurrentCulture.CompareInfo.IsSuffix(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType))); case StringComparison.InvariantCulture: case StringComparison.InvariantCultureIgnoreCase: return(CompareInfo.Invariant.IsSuffix(span, value, string.GetCaseCompareOfComparisonCulture(comparisonType))); case StringComparison.Ordinal: return(span.EndsWith(value)); default: Debug.Assert(comparisonType == StringComparison.OrdinalIgnoreCase); return(span.EndsWithOrdinalIgnoreCase(value)); } }