internal static bool EqualsOrdinalIgnoreCase(this ReadOnlySpan <char> span, ReadOnlySpan <char> value)
 {
     if (span.Length != value.Length)
     {
         return(false);
     }
     if (value.Length == 0)  // span.Length == value.Length == 0
     {
         return(true);
     }
     return(Ordinal.EqualsIgnoreCase(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(value), span.Length));
 }
        public bool StartsWith(string value, StringComparison comparisonType)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if ((object)this == (object)value)
            {
                CheckStringComparison(comparisonType);
                return(true);
            }

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

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
            case StringComparison.CurrentCultureIgnoreCase:
                return(CultureInfo.CurrentCulture.CompareInfo.IsPrefix(this, value, GetCaseCompareOfComparisonCulture(comparisonType)));

            case StringComparison.InvariantCulture:
            case StringComparison.InvariantCultureIgnoreCase:
                return(CompareInfo.Invariant.IsPrefix(this, value, GetCaseCompareOfComparisonCulture(comparisonType)));

            case StringComparison.Ordinal:
                if (this.Length < value.Length || _firstChar != value._firstChar)
                {
                    return(false);
                }
                return((value.Length == 1) ?
                       true :                      // First char is the same and thats all there is to compare
                       SpanHelpers.SequenceEqual(
                           ref Unsafe.As <char, byte>(ref this.GetRawStringData()),
                           ref Unsafe.As <char, byte>(ref value.GetRawStringData()),
                           ((nuint)value.Length) * 2));

            case StringComparison.OrdinalIgnoreCase:
                if (this.Length < value.Length)
                {
                    return(false);
                }
                return(Ordinal.EqualsIgnoreCase(ref this.GetRawStringData(), ref value.GetRawStringData(), value.Length));

            default:
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }
        }
Exemple #3
0
        public bool EndsWith(string value, StringComparison comparisonType)
        {
            ArgumentNullException.ThrowIfNull(value);

            if ((object)this == (object)value)
            {
                CheckStringComparison(comparisonType);
                return(true);
            }

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

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
            case StringComparison.CurrentCultureIgnoreCase:
                return(CultureInfo.CurrentCulture.CompareInfo.IsSuffix(this, value, GetCaseCompareOfComparisonCulture(comparisonType)));

            case StringComparison.InvariantCulture:
            case StringComparison.InvariantCultureIgnoreCase:
                return(CompareInfo.Invariant.IsSuffix(this, value, GetCaseCompareOfComparisonCulture(comparisonType)));

            case StringComparison.Ordinal:
                int offset = this.Length - value.Length;
                return((uint)offset <= (uint)this.Length && this.AsSpan(offset).SequenceEqual(value));

            case StringComparison.OrdinalIgnoreCase:
                return(Length >= value.Length &&
                       Ordinal.EqualsIgnoreCase(ref Unsafe.Add(ref GetRawStringData(), Length - value.Length),
                                                ref value.GetRawStringData(),
                                                value.Length));

            default:
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }
        }
Exemple #4
0
        private static bool EqualsOrdinalIgnoreCaseNoLengthCheck(string strA, string strB)
        {
            Debug.Assert(strA.Length == strB.Length);

            return(Ordinal.EqualsIgnoreCase(ref strA.GetRawStringData(), ref strB.GetRawStringData(), strB.Length));
        }
 internal static bool StartsWithOrdinalIgnoreCase(this ReadOnlySpan <char> span, ReadOnlySpan <char> value)
 => value.Length <= span.Length &&
 Ordinal.EqualsIgnoreCase(ref MemoryMarshal.GetReference(span), ref MemoryMarshal.GetReference(value), value.Length);