// Determines whether two string regions match. The substring of strA beginning // at indexA of length count is compared with the substring of strB // beginning at indexB of the same length. Case sensitivity is determined by the ignoreCase boolean. // public static int Compare(String strA, int indexA, String strB, int indexB, int length, bool ignoreCase) { // Ideally we would just forward to the string.Compare overload that takes // a StringComparison parameter, and just pass in CurrentCulture/CurrentCultureIgnoreCase. // That function will return early if an optimization can be applied, e.g. if // (object)strA == strB && indexA == indexB then it will return 0 straightaway. // There are a couple of subtle behavior differences that prevent us from doing so // however: // - string.Compare(null, -1, null, -1, -1, StringComparison.CurrentCulture) works // since that method also returns early for nulls before validation. It shouldn't // for this overload. // - Since we originally forwarded to FormatProvider for all of the argument // validation logic, the ArgumentOutOfRangeExceptions thrown will contain different // parameter names. // Therefore, we have to duplicate some of the logic here. int lengthA = length; int lengthB = length; if (strA != null) { lengthA = Math.Min(lengthA, strA.Length - indexA); } if (strB != null) { lengthB = Math.Min(lengthB, strB.Length - indexB); } return(ignoreCase ? FormatProvider.CompareIgnoreCase(strA, indexA, lengthA, strB, indexB, lengthB) : FormatProvider.Compare(strA, indexA, lengthA, strB, indexB, lengthB)); }
// 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); } }
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)); } }
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)); } }
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"); } }
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"); } }
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); } }