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

Commit

Permalink
Make it easier to use StringComparison & StringComparer with GetHashC…
Browse files Browse the repository at this point in the history
…ode (#8633)

* Make it easier to use StringComparison & StringComparer with GetHashCode

* model.xml
  • Loading branch information
AlexRadch authored and danmoseley committed Dec 15, 2016
1 parent 7e9bce2 commit b497978
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
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)]
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 abstract class StringComparer : IComparer, IEqualityComparer, IComparer<s
}
}

// 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

1 comment on commit b497978

@jkotas
Copy link
Member

@jkotas jkotas commented on b497978 Dec 16, 2016

Choose a reason for hiding this comment

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

@AlexRadch Could you please port this change to corert when you get a chance? Thank you!

Please sign in to comment.