diff options
author | Jan Kotas <jkotas@microsoft.com> | 2018-03-04 10:25:20 +0300 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2018-03-04 18:39:17 +0300 |
commit | 2532df088c9db52eedb4de898764b3754b03b9c8 (patch) | |
tree | 149bbc79da04cf15dce78ccfa6acb90b650ac830 /src | |
parent | 99aa9df1e2df834e45a1d8eb0d04007e2e736bdb (diff) |
Port changes in non-shared CoreLib partition
Diffstat (limited to 'src')
-rw-r--r-- | src/System.Private.CoreLib/src/System/String.Comparison.cs | 62 | ||||
-rw-r--r-- | src/System.Private.CoreLib/src/System/ThrowHelper.cs | 8 |
2 files changed, 37 insertions, 33 deletions
diff --git a/src/System.Private.CoreLib/src/System/String.Comparison.cs b/src/System.Private.CoreLib/src/System/String.Comparison.cs index 914fa6aa1..357c305d9 100644 --- a/src/System.Private.CoreLib/src/System/String.Comparison.cs +++ b/src/System.Private.CoreLib/src/System/String.Comparison.cs @@ -166,7 +166,7 @@ namespace System // // Common worker for the various Equality methods. The caller must have already ensured that // both strings are non-null and that their lengths are equal. Ther caller should also have - // done the Object.ReferenceEquals() fastpath check as we won't repeat it here. + // done the object.ReferenceEquals() fastpath check as we won't repeat it here. // private static unsafe bool EqualsHelper(String strA, String strB) { @@ -436,19 +436,19 @@ namespace System { if (object.ReferenceEquals(strA, strB)) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return 0; } // They can't both be null at this point. if (strA == null) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return -1; } if (strB == null) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return 1; } @@ -601,7 +601,7 @@ namespace System public static int Compare(String strA, int indexA, String strB, int indexB, int length, StringComparison comparisonType) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); if (strA == null || strB == null) { @@ -614,7 +614,6 @@ namespace System return strA == null ? -1 : 1; } - // @TODO: Spec#: Figure out what to do here with the return statement above. if (length < 0) { throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_NegativeLength); @@ -805,7 +804,7 @@ namespace System return string.Compare(this, strB, StringComparison.CurrentCulture); } - // Determines whether a specified string is a suffix of the the current instance. + // Determines whether a specified string is a suffix of the current instance. // // The case-sensitive and culture-sensitive option is set by options, // and the default culture is used. @@ -825,13 +824,13 @@ namespace System if ((Object)this == (Object)value) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return true; } if (value.Length == 0) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return true; } @@ -872,12 +871,7 @@ namespace System return true; } - CultureInfo referenceCulture; - if (culture == null) - referenceCulture = CultureInfo.CurrentCulture; - else - referenceCulture = culture; - + CultureInfo referenceCulture = culture ?? CultureInfo.CurrentCulture; return referenceCulture.CompareInfo.IsSuffix(this, value, ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None); } @@ -891,10 +885,10 @@ namespace System public override bool Equals(Object obj) { - if (Object.ReferenceEquals(this, obj)) + if (object.ReferenceEquals(this, obj)) return true; - String str = obj as String; + string str = obj as string; if (str == null) return false; @@ -909,7 +903,7 @@ namespace System public bool Equals(String value) { - if (Object.ReferenceEquals(this, value)) + if (object.ReferenceEquals(this, value)) return true; // NOTE: No need to worry about casting to object here. @@ -930,13 +924,13 @@ namespace System { if ((Object)this == (Object)value) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return true; } if ((Object)value == null) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return false; } @@ -993,13 +987,13 @@ namespace System { if ((Object)a == (Object)b) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return true; } if ((Object)a == null || (Object)b == null) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return false; } @@ -1037,7 +1031,7 @@ namespace System public static bool operator ==(String a, String b) { - if (Object.ReferenceEquals(a, b)) + if (object.ReferenceEquals(a, b)) return true; if (a == null || b == null || a.Length != b.Length) return false; @@ -1046,7 +1040,7 @@ namespace System public static bool operator !=(String a, String b) { - if (Object.ReferenceEquals(a, b)) + if (object.ReferenceEquals(a, b)) return false; if (a == null || b == null || a.Length != b.Length) return true; @@ -1135,13 +1129,13 @@ namespace System if ((Object)this == (Object)value) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return true; } if (value.Length == 0) { - StringSpanHelpers.CheckStringComparison(comparisonType); + CheckStringComparison(comparisonType); return true; } @@ -1192,15 +1186,19 @@ namespace System return true; } - CultureInfo referenceCulture; - if (culture == null) - referenceCulture = CultureInfo.CurrentCulture; - else - referenceCulture = culture; - + CultureInfo referenceCulture = culture ?? CultureInfo.CurrentCulture; return referenceCulture.CompareInfo.IsPrefix(this, value, ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None); } public bool StartsWith(char value) => Length != 0 && _firstChar == value; + + internal static void CheckStringComparison(StringComparison comparisonType) + { + // Single comparison to check if comparisonType is within [CurrentCulture .. OrdinalIgnoreCase] + if ((uint)(comparisonType - StringComparison.CurrentCulture) > (StringComparison.OrdinalIgnoreCase - StringComparison.CurrentCulture)) + { + ThrowHelper.ThrowArgumentException(ExceptionResource.NotSupported_StringComparison, ExceptionArgument.comparisonType); + } + } } } diff --git a/src/System.Private.CoreLib/src/System/ThrowHelper.cs b/src/System.Private.CoreLib/src/System/ThrowHelper.cs index 27b030de0..8169b507c 100644 --- a/src/System.Private.CoreLib/src/System/ThrowHelper.cs +++ b/src/System.Private.CoreLib/src/System/ThrowHelper.cs @@ -323,6 +323,8 @@ namespace System return "state"; case ExceptionArgument.length: return "length"; + case ExceptionArgument.comparisonType: + return "comparisonType"; default: Debug.Fail("The enum value is not defined, please check the ExceptionArgument Enum."); return ""; @@ -371,6 +373,8 @@ namespace System return SR.TaskCompletionSourceT_TrySetException_NullException; case ExceptionResource.TaskCompletionSourceT_TrySetException_NoExceptions: return SR.TaskCompletionSourceT_TrySetException_NoExceptions; + case ExceptionResource.NotSupported_StringComparison: + return SR.NotSupported_StringComparison; default: Debug.Assert(false, "The enum value is not defined, please check the ExceptionResource Enum."); @@ -417,7 +421,8 @@ namespace System comparable, source, state, - length + length, + comparisonType, } // @@ -444,5 +449,6 @@ namespace System TaskT_TransitionToFinal_AlreadyCompleted, TaskCompletionSourceT_TrySetException_NullException, TaskCompletionSourceT_TrySetException_NoExceptions, + NotSupported_StringComparison, } } |