Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Make it easier to use StringComparison & StringComparer with GetHashCode #8633

Merged
merged 2 commits into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/mscorlib/model.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7393,6 +7393,7 @@
<Member Name="get_Length" />
<Member Name="GetEnumerator" />
<Member Name="GetHashCode" />
<Member Name="GetHashCode(System.StringComparison)" />
<Member Name="GetTypeCode" />
<Member Name="IndexOf(System.Char)" />
<Member Name="IndexOf(System.Char,System.Int32)" />
Expand Down Expand Up @@ -7500,6 +7501,7 @@
<Member Name="Create(System.Globalization.CultureInfo,System.Boolean)" />
<Member Name="Equals(System.Object,System.Object)" />
<Member Name="Equals(System.String,System.String)" />
<Member Name="FromComparison(System.StringComparison)" />
<Member Name="get_CurrentCulture" />
<Member Name="get_CurrentCultureIgnoreCase" />
<Member Name="get_InvariantCulture" />
Expand Down
5 changes: 5 additions & 0 deletions src/mscorlib/src/System/String.Comparison.cs
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,11 @@ public override int GetHashCode()
return GetLegacyNonRandomizedHashCode();
}

// Gets a hash code for this string and this comparison. If strings A and B and comparition C are such
// that String.Equals(A, B, C), then they will return the same hash code with this comparison C.
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas is ReliabilityContract meaningful in Core? If not I will open an issue to remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the ReliabilityContract attributes can be removed.

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
// hash table).
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
Expand Down
22 changes: 22 additions & 0 deletions src/mscorlib/src/System/StringComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ public static StringComparer OrdinalIgnoreCase {
}
}

// Convert a StringComparison to a StringComparer
public static StringComparer FromComparison(StringComparison comparisonType)
{
switch (comparisonType)
{
case StringComparison.CurrentCulture:
return CurrentCulture;
case StringComparison.CurrentCultureIgnoreCase:
return CurrentCultureIgnoreCase;
case StringComparison.InvariantCulture:
return InvariantCulture;
case StringComparison.InvariantCultureIgnoreCase:
return InvariantCultureIgnoreCase;
case StringComparison.Ordinal:
return Ordinal;
case StringComparison.OrdinalIgnoreCase:
return OrdinalIgnoreCase;
default:
throw new ArgumentException(Environment.GetResourceString("NotSupported_StringComparison"), nameof(comparisonType));
}
}

public static StringComparer Create(CultureInfo culture, bool ignoreCase) {
if( culture == null) {
throw new ArgumentNullException(nameof(culture));
Expand Down