From 241d9344282bd7eb39234b8df954c4296063589f Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Tue, 27 Mar 2018 00:56:41 -0700 Subject: [PATCH] Move String.Comparison.cs to shared CoreLib partition --- src/mscorlib/System.Private.CoreLib.csproj | 1 - .../System.Private.CoreLib.Shared.projitems | 1 + .../System/String.Comparison.cs | 112 ++++++++---------- 3 files changed, 52 insertions(+), 62 deletions(-) rename src/mscorlib/{src => shared}/System/String.Comparison.cs (91%) diff --git a/src/mscorlib/System.Private.CoreLib.csproj b/src/mscorlib/System.Private.CoreLib.csproj index 27e5702d31dd..a32d2e3249e8 100644 --- a/src/mscorlib/System.Private.CoreLib.csproj +++ b/src/mscorlib/System.Private.CoreLib.csproj @@ -322,7 +322,6 @@ - diff --git a/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems b/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems index 4f7e0614e318..1143580173f1 100644 --- a/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/mscorlib/shared/System.Private.CoreLib.Shared.projitems @@ -495,6 +495,7 @@ + diff --git a/src/mscorlib/src/System/String.Comparison.cs b/src/mscorlib/shared/System/String.Comparison.cs similarity index 91% rename from src/mscorlib/src/System/String.Comparison.cs rename to src/mscorlib/shared/System/String.Comparison.cs index 34c20ba7dd0f..2a8be32d3f54 100644 --- a/src/mscorlib/src/System/String.Comparison.cs +++ b/src/mscorlib/shared/System/String.Comparison.cs @@ -2,11 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Collections; using System.Diagnostics; using System.Globalization; using System.Runtime.CompilerServices; -using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using Internal.Runtime.CompilerServices; @@ -21,7 +19,7 @@ namespace System { public partial class String { - private static unsafe int CompareOrdinalIgnoreCaseHelper(String strA, String strB) + private static unsafe int CompareOrdinalIgnoreCaseHelper(string strA, string strB) { Debug.Assert(strA != null); Debug.Assert(strB != null); @@ -62,7 +60,7 @@ private static unsafe int CompareOrdinalIgnoreCaseHelper(String strA, String str // [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static bool EqualsHelper(String strA, String strB) + private static bool EqualsHelper(string strA, string strB) { Debug.Assert(strA != null); Debug.Assert(strB != null); @@ -75,7 +73,7 @@ ref Unsafe.As(ref strB.GetRawStringData()), } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static int CompareOrdinalHelper(String strA, int indexA, int countA, String strB, int indexB, int countB) + private static int CompareOrdinalHelper(string strA, int indexA, int countA, string strB, int indexB, int countB) { Debug.Assert(strA != null); Debug.Assert(strB != null); @@ -86,7 +84,7 @@ private static int CompareOrdinalHelper(String strA, int indexA, int countA, Str return SpanHelpers.SequenceCompareTo(ref Unsafe.Add(ref strA.GetRawStringData(), indexA), countA, ref Unsafe.Add(ref strB.GetRawStringData(), indexB), countB); } - private static unsafe bool EqualsIgnoreCaseAsciiHelper(String strA, String strB) + private static unsafe bool EqualsIgnoreCaseAsciiHelper(string strA, string strB) { Debug.Assert(strA != null); Debug.Assert(strB != null); @@ -124,7 +122,7 @@ private static unsafe bool EqualsIgnoreCaseAsciiHelper(String strA, String strB) } } - private static unsafe int CompareOrdinalHelper(String strA, String strB) + private static unsafe int CompareOrdinalHelper(string strA, string strB) { Debug.Assert(strA != null); Debug.Assert(strB != null); @@ -152,9 +150,8 @@ private static unsafe int CompareOrdinalHelper(String strA, String strB) // and 64-bit (16 bytes) platforms. // For empty strings, the second char will be null due to padding. - // The start of the string (not including sync block pointer) - // is the method table pointer + string length, which takes up - // 8 bytes on 32-bit, 12 on x64. For empty strings the null + // The start of the string is the type pointer + string length, which + // takes up 8 bytes on 32-bit, 12 on x64. For empty strings the null // terminator immediately follows, leaving us with an object // 10/14 bytes in size. Since everything needs to be a multiple // of 4/8, this will get padded and zeroed out. @@ -248,7 +245,7 @@ private static unsafe int CompareOrdinalHelper(String strA, String strB) // to determine whether it is lexicographically less, equal, or greater, and then returns // either a negative integer, 0, or a positive integer; respectively. // - public static int Compare(String strA, String strB) + public static int Compare(string strA, string strB) { return Compare(strA, strB, StringComparison.CurrentCulture); } @@ -259,16 +256,16 @@ public static int Compare(String strA, String strB) // negative integer, 0, or a positive integer is returned; respectively. // The case-sensitive option is set by ignoreCase // - public static int Compare(String strA, String strB, bool ignoreCase) + public static int Compare(string strA, string strB, bool ignoreCase) { var comparisonType = ignoreCase ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture; return Compare(strA, strB, comparisonType); } - // Provides a more flexible function for string comparision. See StringComparison + // Provides a more flexible function for string comparison. See StringComparison // for meaning of different comparisonType. - public static int Compare(String strA, String strB, StringComparison comparisonType) + public static int Compare(string strA, string strB, StringComparison comparisonType) { if (object.ReferenceEquals(strA, strB)) { @@ -332,7 +329,7 @@ public static int Compare(String strA, String strB, StringComparison comparisonT // to determine whether it is lexicographically less, equal, or greater, and then a // negative integer, 0, or a positive integer is returned; respectively. // - public static int Compare(String strA, String strB, CultureInfo culture, CompareOptions options) + public static int Compare(string strA, string strB, CultureInfo culture, CompareOptions options) { if (culture == null) { @@ -350,17 +347,17 @@ public static int Compare(String strA, String strB, CultureInfo culture, Compare // The case-sensitive option is set by ignoreCase, and the culture is set // by culture // - public static int Compare(String strA, String strB, bool ignoreCase, CultureInfo culture) + public static int Compare(string strA, string strB, bool ignoreCase, CultureInfo culture) { var options = ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None; return Compare(strA, strB, culture, options); } // Determines whether two string regions match. The substring of strA beginning - // at indexA of length count is compared with the substring of strB + // at indexA of given length is compared with the substring of strB // beginning at indexB of the same length. // - public static int Compare(String strA, int indexA, String strB, int indexB, int length) + public static int Compare(string strA, int indexA, string strB, int indexB, int length) { // NOTE: It's important we call the boolean overload, and not the StringComparison // one. The two have some subtly different behavior (see notes in the former). @@ -368,10 +365,10 @@ public static int Compare(String strA, int indexA, String strB, int indexB, int } // Determines whether two string regions match. The substring of strA beginning - // at indexA of length count is compared with the substring of strB + // at indexA of given length 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) + 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. @@ -409,7 +406,7 @@ public static int Compare(String strA, int indexA, String strB, int indexB, int // beginning at indexB of the same length. Case sensitivity is determined by the ignoreCase boolean, // and the culture is set by culture. // - public static int Compare(String strA, int indexA, String strB, int indexB, int length, bool ignoreCase, CultureInfo culture) + public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase, CultureInfo culture) { var options = ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None; return Compare(strA, indexA, strB, indexB, length, culture, options); @@ -420,7 +417,7 @@ public static int Compare(String strA, int indexA, String strB, int indexB, int // at indexA of length length is compared with the substring of strB // beginning at indexB of the same length. // - public static int Compare(String strA, int indexA, String strB, int indexB, int length, CultureInfo culture, CompareOptions options) + public static int Compare(string strA, int indexA, string strB, int indexB, int length, CultureInfo culture, CompareOptions options) { if (culture == null) { @@ -443,7 +440,7 @@ public static int Compare(String strA, int indexA, String strB, int indexB, int return culture.CompareInfo.Compare(strA, indexA, lengthA, strB, indexB, lengthB, options); } - public static int Compare(String strA, int indexA, String strB, int indexB, int length, StringComparison comparisonType) + public static int Compare(string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType) { CheckStringComparison(comparisonType); @@ -511,7 +508,7 @@ public static int Compare(String strA, int indexA, String strB, int indexB, int // Compares strA and strB using an ordinal (code-point) comparison. // - public static int CompareOrdinal(String strA, String strB) + public static int CompareOrdinal(string strA, string strB) { if (object.ReferenceEquals(strA, strB)) { @@ -544,7 +541,7 @@ internal static int CompareOrdinal(ReadOnlySpan strA, ReadOnlySpan s // Compares strA and strB using an ordinal (code-point) comparison. // - public static int CompareOrdinal(String strA, int indexA, String strB, int indexB, int length) + public static int CompareOrdinal(string strA, int indexA, string strB, int indexB, int length) { if (strA == null || strB == null) { @@ -592,7 +589,7 @@ public static int CompareOrdinal(String strA, int indexA, String strB, int index // indicates the relationship. This method returns a value less than 0 if this is less than value, 0 // if this is equal to value, or a value greater than 0 if this is greater than value. // - public int CompareTo(Object value) + public int CompareTo(object value) { if (value == null) { @@ -611,7 +608,7 @@ public int CompareTo(Object value) // Determines the sorting relation of StrB to the current instance. // - public int CompareTo(String strB) + public int CompareTo(string strB) { return string.Compare(this, strB, StringComparison.CurrentCulture); } @@ -621,19 +618,19 @@ public int CompareTo(String strB) // The case-sensitive and culture-sensitive option is set by options, // and the default culture is used. // - public Boolean EndsWith(String value) + public bool EndsWith(string value) { return EndsWith(value, StringComparison.CurrentCulture); } - public Boolean EndsWith(String value, StringComparison comparisonType) + public bool EndsWith(string value, StringComparison comparisonType) { - if ((Object)value == null) + if ((object)value == null) { throw new ArgumentNullException(nameof(value)); } - if ((Object)this == (Object)value) + if ((object)this == (object)value) { CheckStringComparison(comparisonType); return true; @@ -670,7 +667,7 @@ public Boolean EndsWith(String value, StringComparison comparisonType) } } - public Boolean EndsWith(String value, Boolean ignoreCase, CultureInfo culture) + public bool EndsWith(string value, bool ignoreCase, CultureInfo culture) { if (null == value) { @@ -693,7 +690,7 @@ public bool EndsWith(char value) } // Determines whether two strings match. - public override bool Equals(Object obj) + public override bool Equals(object obj) { if (object.ReferenceEquals(this, obj)) return true; @@ -709,7 +706,7 @@ public override bool Equals(Object obj) } // Determines whether two strings match. - public bool Equals(String value) + public bool Equals(string value) { if (object.ReferenceEquals(this, value)) return true; @@ -727,15 +724,15 @@ public bool Equals(String value) return EqualsHelper(this, value); } - public bool Equals(String value, StringComparison comparisonType) + public bool Equals(string value, StringComparison comparisonType) { - if ((Object)this == (Object)value) + if ((object)this == (object)value) { CheckStringComparison(comparisonType); return true; } - if ((Object)value == null) + if ((object)value == null) { CheckStringComparison(comparisonType); return false; @@ -779,14 +776,14 @@ public bool Equals(String value, StringComparison comparisonType) // Determines whether two Strings match. - public static bool Equals(String a, String b) + public static bool Equals(string a, string b) { - if ((Object)a == (Object)b) + if ((object)a == (object)b) { return true; } - if ((Object)a == null || (Object)b == null || a.Length != b.Length) + if ((object)a == null || (object)b == null || a.Length != b.Length) { return false; } @@ -794,15 +791,15 @@ public static bool Equals(String a, String b) return EqualsHelper(a, b); } - public static bool Equals(String a, String b, StringComparison comparisonType) + public static bool Equals(string a, string b, StringComparison comparisonType) { - if ((Object)a == (Object)b) + if ((object)a == (object)b) { CheckStringComparison(comparisonType); return true; } - if ((Object)a == null || (Object)b == null) + if ((object)a == null || (object)b == null) { CheckStringComparison(comparisonType); return false; @@ -844,14 +841,14 @@ public static bool Equals(String a, String b, StringComparison comparisonType) } } - public static bool operator ==(String a, String b) + public static bool operator ==(string a, string b) { - return String.Equals(a, b); + return string.Equals(a, b); } - public static bool operator !=(String a, String b) + public static bool operator !=(string a, string b) { - return !String.Equals(a, b); + return !string.Equals(a, b); } // Gets a hash code for this string. If strings A and B are such that A.Equals(B), then @@ -862,7 +859,7 @@ public override int GetHashCode() } // Gets a hash code for this string and this comparison. If strings A and B and comparison C are such - // that String.Equals(A, B, C), then they will return the same hash code with this comparison C. + // that string.Equals(A, B, C), then they will return the same hash code with this comparison C. public int GetHashCode(StringComparison comparisonType) => StringComparer.FromComparison(comparisonType).GetHashCode(this); // Use this if and only if you need the hashcode to not change across app domains (e.g. you have an app domain agile @@ -910,13 +907,6 @@ internal int GetLegacyNonRandomizedHashCode() { hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ pint[0]; } -#endif -#if DEBUG - // We want to ensure we can change our hash function daily. - // This is perfectly fine as long as you don't persist the - // value from GetHashCode to disk or count on String A - // hashing before string B. Those are bugs in your code. - hash1 ^= ThisAssembly.DailyBuildNumber; #endif return hash1 + (hash2 * 1566083941); } @@ -925,23 +915,23 @@ internal int GetLegacyNonRandomizedHashCode() // Determines whether a specified string is a prefix of the current instance // - public Boolean StartsWith(String value) + public bool StartsWith(string value) { - if ((Object)value == null) + if ((object)value == null) { throw new ArgumentNullException(nameof(value)); } return StartsWith(value, StringComparison.CurrentCulture); } - public Boolean StartsWith(String value, StringComparison comparisonType) + public bool StartsWith(string value, StringComparison comparisonType) { - if ((Object)value == null) + if ((object)value == null) { throw new ArgumentNullException(nameof(value)); } - if ((Object)this == (Object)value) + if ((object)this == (object)value) { CheckStringComparison(comparisonType); return true; @@ -991,7 +981,7 @@ ref Unsafe.As(ref value.GetRawStringData()), } } - public Boolean StartsWith(String value, Boolean ignoreCase, CultureInfo culture) + public bool StartsWith(string value, bool ignoreCase, CultureInfo culture) { if (null == value) {