summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Ko <jamesqko@gmail.com>2016-07-19 20:18:06 -0400
committerJan Kotas <jkotas@microsoft.com>2016-07-19 17:18:06 -0700
commit1ff9f6b76f1d9c91ce7169689f07d3e61836e7d5 (patch)
tree80a30d61aabe93ee7379b53cf5b70937c8a562ca
parent266108a9884775d10aa6f72a0f2161d8741f0d32 (diff)
downloadcoreclr-1ff9f6b76f1d9c91ce7169689f07d3e61836e7d5.tar.gz
coreclr-1ff9f6b76f1d9c91ce7169689f07d3e61836e7d5.tar.bz2
coreclr-1ff9f6b76f1d9c91ce7169689f07d3e61836e7d5.zip
Remove duplicate code from string.GetHashCode (#4696)
-rw-r--r--src/mscorlib/src/System/String.cs56
1 files changed, 4 insertions, 52 deletions
diff --git a/src/mscorlib/src/System/String.cs b/src/mscorlib/src/System/String.cs
index bd5d5030ce..9255c9bb22 100644
--- a/src/mscorlib/src/System/String.cs
+++ b/src/mscorlib/src/System/String.cs
@@ -880,64 +880,16 @@ namespace System {
// they will return the same hash code.
[System.Security.SecuritySafeCritical] // auto-generated
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
- public override int GetHashCode() {
-
+ public override int GetHashCode()
+ {
#if FEATURE_RANDOMIZED_STRING_HASHING
- if(HashHelpers.s_UseRandomizedStringHashing)
+ if (HashHelpers.s_UseRandomizedStringHashing)
{
return InternalMarvin32HashString(this, this.Length, 0);
}
#endif // FEATURE_RANDOMIZED_STRING_HASHING
- unsafe {
- fixed (char* src = &m_firstChar) {
- Contract.Assert(src[this.Length] == '\0', "src[this.Length] == '\\0'");
- Contract.Assert( ((int)src)%4 == 0, "Managed string should start at 4 bytes boundary");
-
-#if BIT64
- int hash1 = 5381;
-#else // !BIT64 (32)
- int hash1 = (5381<<16) + 5381;
-#endif
- int hash2 = hash1;
-#if BIT64
- int c;
- char *s = src;
- while ((c = s[0]) != 0) {
- hash1 = ((hash1 << 5) + hash1) ^ c;
- c = s[1];
- if (c == 0)
- break;
- hash2 = ((hash2 << 5) + hash2) ^ c;
- s += 2;
- }
-#else // !BIT64 (32)
- // 32 bit machines.
- int* pint = (int *)src;
- int len = this.Length;
- while (len > 2)
- {
- hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ pint[0];
- hash2 = ((hash2 << 5) + hash2 + (hash2 >> 27)) ^ pint[1];
- pint += 2;
- len -= 4;
- }
-
- if (len > 0)
- {
- 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);
- }
- }
+ return GetLegacyNonRandomizedHashCode();
}
// Use this if and only if you need the hashcode to not change across app domains (e.g. you have an app domain agile