-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy default unicode hash function into the Tests project. Adjust ref…
…erences and usings.
- Loading branch information
1 parent
78f8134
commit e4dc206
Showing
4 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Globalization; | ||
|
||
namespace NetTally.Tests.Platform | ||
{ | ||
public static class UnicodeHashFunction | ||
{ | ||
/// <summary> | ||
/// Hashes the provided string using the given CompareOptions. | ||
/// Doing this allows all custom compare options to be applied in determining the hash value. | ||
/// </summary> | ||
/// <param name="str">The string.</param> | ||
/// <param name="info">The CompareInfo object doing the unicode-aware comparison.</param> | ||
/// <param name="options">The options to apply to the comparison.</param> | ||
/// <returns>Returns the hash code for the string.</returns> | ||
public static int HashFunction(string str, CompareInfo info, CompareOptions options) | ||
{ | ||
if (info == null) | ||
throw new ArgumentNullException(nameof(info)); | ||
|
||
if (string.IsNullOrEmpty(str)) | ||
return 0; | ||
|
||
SortKey sortOrder = info.GetSortKey(str, options); | ||
|
||
int hash = GetByteArrayHash(sortOrder.KeyData); | ||
|
||
return hash; | ||
} | ||
|
||
/// <summary> | ||
/// Generates a hash code for an array of bytes. | ||
/// </summary> | ||
/// <param name="keyData">The key data.</param> | ||
/// <returns>Returns a hash code value.</returns> | ||
private static int GetByteArrayHash(byte[] keyData) | ||
{ | ||
unchecked | ||
{ | ||
const int p = 16777619; | ||
int hash = (int)2166136261; | ||
|
||
for (int i = 0; i < keyData.Length; i++) | ||
hash = (hash ^ keyData[i]) * p; | ||
|
||
hash += hash << 13; | ||
hash ^= hash >> 7; | ||
hash += hash << 3; | ||
hash ^= hash >> 17; | ||
hash += hash << 5; | ||
return hash; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters