fix: Adds support for ROS to HashUtility (#1740)

This commit is contained in:
Kamron Batman 2024-04-25 00:42:10 -07:00 • committed by GitHub
parent 9cd84ba3d6
commit 1c10b6dbed
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -16,6 +16,7 @@
using System; using System;
using System.IO.Hashing; using System.IO.Hashing;
using System.Numerics; using System.Numerics;
using System.Runtime.InteropServices;
namespace Server; namespace Server;
@ -33,19 +34,15 @@ public static class HashUtility
[ThreadStatic] [ThreadStatic]
private static XxHash32 _xxHash32; private static XxHash32 _xxHash32;
public static unsafe ulong ComputeHash64(string? str) public static ulong ComputeHash64(ReadOnlySpan<char> str)
{ {
if (str == null) if (str.Length == 0)
{ {
return 0; return 0;
} }
var hasher = _xxHash3 ??= new XxHash3(unchecked((long)xxHash3Seed)); var hasher = _xxHash3 ??= new XxHash3(unchecked((long)xxHash3Seed));
hasher.Append(MemoryMarshal.Cast<char, byte>(str));
fixed (char* src = &str.GetPinnableReference())
{
hasher.Append(new ReadOnlySpan<byte>(src, str.Length * 2));
}
var result = hasher.GetCurrentHashAsUInt64(); var result = hasher.GetCurrentHashAsUInt64();
hasher.Reset(); hasher.Reset();
@ -53,7 +50,7 @@ public static class HashUtility
return result; return result;
} }
public static unsafe uint ComputeHash32(string? str) public static uint ComputeHash32(ReadOnlySpan<char> str)
{ {
if (str == null) if (str == null)
{ {
@ -61,11 +58,7 @@ public static class HashUtility
} }
var hasher = _xxHash32 ??= new XxHash32(unchecked((int)xxHash1Seed)); var hasher = _xxHash32 ??= new XxHash32(unchecked((int)xxHash1Seed));
hasher.Append(MemoryMarshal.Cast<char, byte>(str));
fixed (char* src = &str.GetPinnableReference())
{
hasher.Append(new ReadOnlySpan<byte>(src, str.Length * 2));
}
var result = hasher.GetCurrentHashAsUInt32(); var result = hasher.GetCurrentHashAsUInt32();
hasher.Reset(); hasher.Reset();