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

Commit

Permalink
Restoring hashcode randomization and adding resource string helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Dec 21, 2016
1 parent f1bb46f commit eb12c23
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
10 changes: 10 additions & 0 deletions src/mscorlib/corefx/SR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,14 @@ public static string Format(string formatString, params object[] args)
{
return string.Format(CultureInfo.CurrentCulture, formatString, args);
}

internal static string ArgumentException_ValueTupleIncorrectType
{
get { return Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType"); }
}

internal static string ArgumentException_ValueTupleLastArgumentNotATuple
{
get { return Environment.GetResourceString("ArgumentException_ValueTupleLastArgumentNotATuple"); }
}
}
2 changes: 1 addition & 1 deletion src/mscorlib/src/System/Numerics/Hashing/HashHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace System.Numerics.Hashing

internal static class HashHelpers
{
public static readonly int RandomSeed = Guid.NewGuid().GetHashCode();
public static readonly int RandomSeed = new Random().Next(Int32.MinValue, Int32.MaxValue);

public static int Combine(int h1, int h2)
{
Expand Down
56 changes: 27 additions & 29 deletions src/mscorlib/src/System/ValueTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using HashHelpers = System.Numerics.Hashing.HashHelpers;

namespace System
{
Expand Down Expand Up @@ -59,7 +60,7 @@ int IComparable.CompareTo(object other)

if (!(other is ValueTuple))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

return 0;
Expand All @@ -84,7 +85,7 @@ int IStructuralComparable.CompareTo(object other, IComparer comparer)

if (!(other is ValueTuple))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

return 0;
Expand Down Expand Up @@ -259,40 +260,37 @@ public static ValueTuple<T1, T2, T3, T4, T5, T6, T7, ValueTuple<T8>> Create<T1,

internal static int CombineHashCodes(int h1, int h2)
{
// Forward to helper class in Common for this
// We keep the actual hashing logic there, so
// other classes can use it for hashing
return System.Numerics.Hashing.HashHelpers.Combine(h1, h2);
return HashHelpers.Combine(HashHelpers.Combine(HashHelpers.RandomSeed, h1), h2);
}

internal static int CombineHashCodes(int h1, int h2, int h3)
{
return CombineHashCodes(CombineHashCodes(h1, h2), h3);
return HashHelpers.Combine(CombineHashCodes(h1, h2), h3);
}

internal static int CombineHashCodes(int h1, int h2, int h3, int h4)
{
return CombineHashCodes(CombineHashCodes(h1, h2, h3), h4);
return HashHelpers.Combine(CombineHashCodes(h1, h2, h3), h4);
}

internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5)
{
return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4), h5);
return HashHelpers.Combine(CombineHashCodes(h1, h2, h3, h4), h5);
}

internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6)
{
return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4, h5), h6);
return HashHelpers.Combine(CombineHashCodes(h1, h2, h3, h4, h5), h6);
}

internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7)
{
return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4, h5, h6), h7);
return HashHelpers.Combine(CombineHashCodes(h1, h2, h3, h4, h5, h6), h7);
}

internal static int CombineHashCodes(int h1, int h2, int h3, int h4, int h5, int h6, int h7, int h8)
{
return CombineHashCodes(CombineHashCodes(h1, h2, h3, h4, h5, h6, h7), h8);
return HashHelpers.Combine(CombineHashCodes(h1, h2, h3, h4, h5, h6, h7), h8);
}
}

Expand Down Expand Up @@ -363,7 +361,7 @@ int IComparable.CompareTo(object other)

if (!(other is ValueTuple<T1>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

var objTuple = (ValueTuple<T1>)other;
Expand All @@ -390,7 +388,7 @@ int IStructuralComparable.CompareTo(object other, IComparer comparer)

if (!(other is ValueTuple<T1>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

var objTuple = (ValueTuple<T1>)other;
Expand Down Expand Up @@ -555,7 +553,7 @@ int IComparable.CompareTo(object other)

if (!(other is ValueTuple<T1, T2>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

return CompareTo((ValueTuple<T1, T2>)other);
Expand Down Expand Up @@ -583,7 +581,7 @@ int IStructuralComparable.CompareTo(object other, IComparer comparer)

if (!(other is ValueTuple<T1, T2>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

var objTuple = (ValueTuple<T1, T2>)other;
Expand Down Expand Up @@ -753,7 +751,7 @@ int IComparable.CompareTo(object other)

if (!(other is ValueTuple<T1, T2, T3>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

return CompareTo((ValueTuple<T1, T2, T3>)other);
Expand Down Expand Up @@ -784,7 +782,7 @@ int IStructuralComparable.CompareTo(object other, IComparer comparer)

if (!(other is ValueTuple<T1, T2, T3>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

var objTuple = (ValueTuple<T1, T2, T3>)other;
Expand Down Expand Up @@ -968,7 +966,7 @@ int IComparable.CompareTo(object other)

if (!(other is ValueTuple<T1, T2, T3, T4>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

return CompareTo((ValueTuple<T1, T2, T3, T4>)other);
Expand Down Expand Up @@ -1002,7 +1000,7 @@ int IStructuralComparable.CompareTo(object other, IComparer comparer)

if (!(other is ValueTuple<T1, T2, T3, T4>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

var objTuple = (ValueTuple<T1, T2, T3, T4>)other;
Expand Down Expand Up @@ -1202,7 +1200,7 @@ int IComparable.CompareTo(object other)

if (!(other is ValueTuple<T1, T2, T3, T4, T5>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

return CompareTo((ValueTuple<T1, T2, T3, T4, T5>)other);
Expand Down Expand Up @@ -1239,7 +1237,7 @@ int IStructuralComparable.CompareTo(object other, IComparer comparer)

if (!(other is ValueTuple<T1, T2, T3, T4, T5>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

var objTuple = (ValueTuple<T1, T2, T3, T4, T5>)other;
Expand Down Expand Up @@ -1455,7 +1453,7 @@ int IComparable.CompareTo(object other)

if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

return CompareTo((ValueTuple<T1, T2, T3, T4, T5, T6>)other);
Expand Down Expand Up @@ -1495,7 +1493,7 @@ int IStructuralComparable.CompareTo(object other, IComparer comparer)

if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6>)other;
Expand Down Expand Up @@ -1727,7 +1725,7 @@ int IComparable.CompareTo(object other)

if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

return CompareTo((ValueTuple<T1, T2, T3, T4, T5, T6, T7>)other);
Expand Down Expand Up @@ -1770,7 +1768,7 @@ int IStructuralComparable.CompareTo(object other, IComparer comparer)

if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6, T7>)other;
Expand Down Expand Up @@ -1949,7 +1947,7 @@ public ValueTuple(T1 item1, T2 item2, T3 item3, T4 item4, T5 item5, T6 item6, T7
{
if (!(rest is IValueTupleInternal))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleLastArgumentNotAValueTuple"));
throw new ArgumentException(SR.ArgumentException_ValueTupleLastArgumentNotATuple);
}

Item1 = item1;
Expand Down Expand Up @@ -2024,7 +2022,7 @@ int IComparable.CompareTo(object other)

if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

return CompareTo((ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)other);
Expand Down Expand Up @@ -2070,7 +2068,7 @@ int IStructuralComparable.CompareTo(object other, IComparer comparer)

if (!(other is ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>))
{
throw new ArgumentException(Environment.GetResourceString("ArgumentException_ValueTupleIncorrectType", this.GetType().ToString()), nameof(other));
throw new ArgumentException(SR.Format(SR.ArgumentException_ValueTupleIncorrectType, this.GetType().ToString()), nameof(other));
}

var objTuple = (ValueTuple<T1, T2, T3, T4, T5, T6, T7, TRest>)other;
Expand Down

0 comments on commit eb12c23

Please sign in to comment.