Ejemplo n.º 1
0
        public Boolean StartsWith(String value, StringComparison comparisonType)
        {
            if ((Object)value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (comparisonType < StringComparison.CurrentCulture || comparisonType > StringComparison.OrdinalIgnoreCase)
            {
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }

            if ((Object)this == (Object)value)
            {
                return(true);
            }

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

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
                return(FormatProvider.IsPrefix(this, value));

            case StringComparison.CurrentCultureIgnoreCase:
                return(FormatProvider.IsPrefixIgnoreCase(this, value));

            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
                       StartsWithOrdinalHelper(this, value));

            case StringComparison.OrdinalIgnoreCase:
                if (this.Length < value.Length)
                {
                    return(false);
                }
                return(FormatProvider.CompareOrdinalIgnoreCase(this, 0, value.Length, value, 0, value.Length) == 0);

            case StringComparison.InvariantCulture:
                return(CultureInfo.InvariantCulture.CompareInfo.IsPrefix(this, value, CompareOptions.None));

            case StringComparison.InvariantCultureIgnoreCase:
                return(CultureInfo.InvariantCulture.CompareInfo.IsPrefix(this, value, CompareOptions.IgnoreCase));

            default:
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }
        }
Ejemplo n.º 2
0
        // Provides a more flexible function for string comparision. See StringComparison
        // for meaning of different comparisonType.
        public static int Compare(String strA, String strB, StringComparison comparisonType)
        {
            if (comparisonType < StringComparison.CurrentCulture || comparisonType > StringComparison.OrdinalIgnoreCase)
            {
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }

            if (object.ReferenceEquals(strA, strB))
            {
                return(0);
            }

            // They can't both be null at this point.
            if (strA == null)
            {
                return(-1);
            }
            if (strB == null)
            {
                return(1);
            }


            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
                return(FormatProvider.Compare(strA, 0, strA.Length, strB, 0, strB.Length));

            case StringComparison.CurrentCultureIgnoreCase:
                return(FormatProvider.CompareIgnoreCase(strA, 0, strA.Length, strB, 0, strB.Length));

            case StringComparison.Ordinal:
                // Most common case: first character is different.
                // Returns false for empty strings.
                if (strA._firstChar != strB._firstChar)
                {
                    return(strA._firstChar - strB._firstChar);
                }

                return(CompareOrdinalHelper(strA, strB));

            case StringComparison.OrdinalIgnoreCase:
                return(FormatProvider.CompareOrdinalIgnoreCase(strA, 0, strA.Length, strB, 0, strB.Length));

            case StringComparison.InvariantCulture:
                return(CultureInfo.InvariantCulture.CompareInfo.Compare(strA, strB, CompareOptions.None));

            case StringComparison.InvariantCultureIgnoreCase:
                return(CultureInfo.InvariantCulture.CompareInfo.Compare(strA, strB, CompareOptions.IgnoreCase));

            default:
                throw new NotSupportedException(SR.NotSupported_StringComparison);
            }
        }
Ejemplo n.º 3
0
        public static bool Equals(String a, String b, StringComparison comparisonType)
        {
            if (comparisonType < StringComparison.CurrentCulture || comparisonType > StringComparison.OrdinalIgnoreCase)
            {
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }

            if ((Object)a == (Object)b)
            {
                return(true);
            }

            if ((Object)a == null || (Object)b == null)
            {
                return(false);
            }

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
                return(FormatProvider.Compare(a, 0, a.Length, b, 0, b.Length) == 0);

            case StringComparison.CurrentCultureIgnoreCase:
                return(FormatProvider.CompareIgnoreCase(a, 0, a.Length, b, 0, b.Length) == 0);

            case StringComparison.Ordinal:
                if (a.Length != b.Length)
                {
                    return(false);
                }
                return(EqualsHelper(a, b));

            case StringComparison.OrdinalIgnoreCase:
                if (a.Length != b.Length)
                {
                    return(false);
                }
                else
                {
                    return(FormatProvider.CompareOrdinalIgnoreCase(a, 0, a.Length, b, 0, b.Length) == 0);
                }

            case StringComparison.InvariantCulture:
                return(CultureInfo.InvariantCulture.CompareInfo.Compare(a, b, CompareOptions.None) == 0);

            case StringComparison.InvariantCultureIgnoreCase:
                return(CultureInfo.InvariantCulture.CompareInfo.Compare(a, b, CompareOptions.IgnoreCase) == 0);

            default:
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }
        }
Ejemplo n.º 4
0
        public bool Equals(String value, StringComparison comparisonType)
        {
            if (comparisonType < StringComparison.CurrentCulture || comparisonType > StringComparison.OrdinalIgnoreCase)
            {
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }

            if ((Object)this == (Object)value)
            {
                return(true);
            }

            if ((Object)value == null)
            {
                return(false);
            }

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
                return(FormatProvider.Compare(this, 0, this.Length, value, 0, value.Length) == 0);

            case StringComparison.CurrentCultureIgnoreCase:
                return(FormatProvider.CompareIgnoreCase(this, 0, this.Length, value, 0, value.Length) == 0);

            case StringComparison.Ordinal:
                if (this.Length != value.Length)
                {
                    return(false);
                }
                return(EqualsHelper(this, value));

            case StringComparison.OrdinalIgnoreCase:
                if (this.Length != value.Length)
                {
                    return(false);
                }
                else
                {
                    return(FormatProvider.CompareOrdinalIgnoreCase(this, 0, this.Length, value, 0, value.Length) == 0);
                }

            case StringComparison.InvariantCulture:
                return(CultureInfo.InvariantCulture.CompareInfo.Compare(this, value, CompareOptions.None) == 0);

            case StringComparison.InvariantCultureIgnoreCase:
                return(CultureInfo.InvariantCulture.CompareInfo.Compare(this, value, CompareOptions.IgnoreCase) == 0);

            default:
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }
        }
Ejemplo n.º 5
0
        public static bool Equals(String a, String b, StringComparison comparisonType)
        {
            if (comparisonType < StringComparison.CurrentCulture || comparisonType > StringComparison.OrdinalIgnoreCase)
            {
                throw new ArgumentException(SR.NotSupported_StringComparison, "comparisonType");
            }

            if ((Object)a == (Object)b)
            {
                return(true);
            }

            if ((Object)a == null || (Object)b == null)
            {
                return(false);
            }

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
                return(FormatProvider.Compare(a, 0, a.Length, b, 0, b.Length) == 0);

            case StringComparison.CurrentCultureIgnoreCase:
                return(FormatProvider.CompareIgnoreCase(a, 0, a.Length, b, 0, b.Length) == 0);

            case StringComparison.Ordinal:
                if (a.Length != b.Length)
                {
                    return(false);
                }
                return(OrdinalCompareEqualLengthStrings(a, b));

            case StringComparison.OrdinalIgnoreCase:
                if (a.Length != b.Length)
                {
                    return(false);
                }
                else
                {
                    return(FormatProvider.CompareOrdinalIgnoreCase(a, 0, a.Length, b, 0, b.Length) == 0);
                }

            default:
                throw new ArgumentException(SR.NotSupported_StringComparison, "comparisonType");
            }
        }
Ejemplo n.º 6
0
        public bool Equals(String value, StringComparison comparisonType)
        {
            if (comparisonType < StringComparison.CurrentCulture || comparisonType > StringComparison.OrdinalIgnoreCase)
            {
                throw new ArgumentException(SR.NotSupported_StringComparison, "comparisonType");
            }

            if ((Object)this == (Object)value)
            {
                return(true);
            }

            if ((Object)value == null)
            {
                return(false);
            }

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
                return(FormatProvider.Compare(this, 0, this.Length, value, 0, value.Length) == 0);

            case StringComparison.CurrentCultureIgnoreCase:
                return(FormatProvider.CompareIgnoreCase(this, 0, this.Length, value, 0, value.Length) == 0);

            case StringComparison.Ordinal:
                if (this.Length != value.Length)
                {
                    return(false);
                }
                return(OrdinalCompareEqualLengthStrings(this, value));

            case StringComparison.OrdinalIgnoreCase:
                if (this.Length != value.Length)
                {
                    return(false);
                }
                else
                {
                    return(FormatProvider.CompareOrdinalIgnoreCase(this, 0, this.Length, value, 0, value.Length) == 0);
                }

            default:
                throw new ArgumentException(SR.NotSupported_StringComparison, "comparisonType");
            }
        }
Ejemplo n.º 7
0
        public Boolean EndsWith(String value, StringComparison comparisonType)
        {
            if ((Object)value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (comparisonType < StringComparison.CurrentCulture || comparisonType > StringComparison.OrdinalIgnoreCase)
            {
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }

            if ((Object)this == (Object)value)
            {
                return(true);
            }

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

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
                return(FormatProvider.IsSuffix(this, value));

            case StringComparison.CurrentCultureIgnoreCase:
                return(FormatProvider.IsSuffixIgnoreCase(this, value));

            case StringComparison.Ordinal:
                return(this.Length < value.Length ? false : (CompareOrdinalHelper(this, this.Length - value.Length, value.Length, value, 0, value.Length) == 0));

            case StringComparison.OrdinalIgnoreCase:
                return(this.Length < value.Length ? false : (FormatProvider.CompareOrdinalIgnoreCase(this, this.Length - value.Length, value.Length, value, 0, value.Length) == 0));

            case StringComparison.InvariantCulture:
                return(CultureInfo.InvariantCulture.CompareInfo.IsSuffix(this, value, CompareOptions.None));

            case StringComparison.InvariantCultureIgnoreCase:
                return(CultureInfo.InvariantCulture.CompareInfo.IsSuffix(this, value, CompareOptions.IgnoreCase));

            default:
                throw new ArgumentException(SR.NotSupported_StringComparison, nameof(comparisonType));
            }
        }
Ejemplo n.º 8
0
        public static int Compare(String strA, int indexA, String strB, int indexB, int length, StringComparison comparisonType)
        {
            if (comparisonType < StringComparison.CurrentCulture || comparisonType > StringComparison.OrdinalIgnoreCase)
            {
                throw new ArgumentException(SR.NotSupported_StringComparison, "comparisonType");
            }

            if (strA == null || strB == null)
            {
                if (object.ReferenceEquals(strA, strB))
                {
                    // They're both null
                    return(0);
                }

                return(strA == null ? -1 : 1);
            }

            // @TODO: Spec#: Figure out what to do here with the return statement above.
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException("length", SR.ArgumentOutOfRange_NegativeLength);
            }

            if (indexA < 0 || indexB < 0)
            {
                string paramName = indexA < 0 ? "indexA" : "indexB";
                throw new ArgumentOutOfRangeException(paramName, SR.ArgumentOutOfRange_Index);
            }

            if (strA.Length - indexA < 0 || strB.Length - indexB < 0)
            {
                string paramName = strA.Length - indexA < 0 ? "indexA" : "indexB";
                throw new ArgumentOutOfRangeException(paramName, SR.ArgumentOutOfRange_Index);
            }

            if (length == 0 || (object.ReferenceEquals(strA, strB) && indexA == indexB))
            {
                return(0);
            }

            int lengthA = Math.Min(length, strA.Length - indexA);
            int lengthB = Math.Min(length, strB.Length - indexB);

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
                return(FormatProvider.Compare(strA, indexA, lengthA, strB, indexB, lengthB));

            case StringComparison.CurrentCultureIgnoreCase:
                return(FormatProvider.CompareIgnoreCase(strA, indexA, lengthA, strB, indexB, lengthB));

            case StringComparison.Ordinal:
                return(CompareOrdinalHelper(strA, indexA, lengthA, strB, indexB, lengthB));

            case StringComparison.OrdinalIgnoreCase:
                return(FormatProvider.CompareOrdinalIgnoreCase(strA, indexA, lengthA, strB, indexB, lengthB));

            default:
                throw new ArgumentException(SR.NotSupported_StringComparison);
            }
        }