From 1c10b6dbed0aba36d41e5ca95bfa1d881b4f8bb3 Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Thu, 25 Apr 2024 00:42:10 -0700 Subject: [PATCH] fix: Adds support for ROS to HashUtility (#1740) --- Projects/Server/Utilities/HashUtility.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/Projects/Server/Utilities/HashUtility.cs b/Projects/Server/Utilities/HashUtility.cs index 028a3f48d..8758466a6 100644 --- a/Projects/Server/Utilities/HashUtility.cs +++ b/Projects/Server/Utilities/HashUtility.cs @@ -16,6 +16,7 @@ using System; using System.IO.Hashing; using System.Numerics; +using System.Runtime.InteropServices; namespace Server; @@ -33,19 +34,15 @@ public static class HashUtility [ThreadStatic] private static XxHash32 _xxHash32; - public static unsafe ulong ComputeHash64(string? str) + public static ulong ComputeHash64(ReadOnlySpan str) { - if (str == null) + if (str.Length == 0) { return 0; } var hasher = _xxHash3 ??= new XxHash3(unchecked((long)xxHash3Seed)); - - fixed (char* src = &str.GetPinnableReference()) - { - hasher.Append(new ReadOnlySpan(src, str.Length * 2)); - } + hasher.Append(MemoryMarshal.Cast(str)); var result = hasher.GetCurrentHashAsUInt64(); hasher.Reset(); @@ -53,7 +50,7 @@ public static class HashUtility return result; } - public static unsafe uint ComputeHash32(string? str) + public static uint ComputeHash32(ReadOnlySpan str) { if (str == null) { @@ -61,11 +58,7 @@ public static class HashUtility } var hasher = _xxHash32 ??= new XxHash32(unchecked((int)xxHash1Seed)); - - fixed (char* src = &str.GetPinnableReference()) - { - hasher.Append(new ReadOnlySpan(src, str.Length * 2)); - } + hasher.Append(MemoryMarshal.Cast(str)); var result = hasher.GetCurrentHashAsUInt32(); hasher.Reset();