Skip to content

Commit

Permalink
fix: Adds support for ROS to HashUtility (#1740)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman committed Apr 25, 2024
1 parent 9cd84ba commit 1c10b6d
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions Projects/Server/Utilities/HashUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.IO.Hashing;
using System.Numerics;
using System.Runtime.InteropServices;

namespace Server;

Expand All @@ -33,39 +34,31 @@ public static class HashUtility
[ThreadStatic]
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;
}

var hasher = _xxHash3 ??= new XxHash3(unchecked((long)xxHash3Seed));

fixed (char* src = &str.GetPinnableReference())
{
hasher.Append(new ReadOnlySpan<byte>(src, str.Length * 2));
}
hasher.Append(MemoryMarshal.Cast<char, byte>(str));

var result = hasher.GetCurrentHashAsUInt64();
hasher.Reset();

return result;
}

public static unsafe uint ComputeHash32(string? str)
public static uint ComputeHash32(ReadOnlySpan<char> str)
{
if (str == null)
{
return 0;
}

var hasher = _xxHash32 ??= new XxHash32(unchecked((int)xxHash1Seed));

fixed (char* src = &str.GetPinnableReference())
{
hasher.Append(new ReadOnlySpan<byte>(src, str.Length * 2));
}
hasher.Append(MemoryMarshal.Cast<char, byte>(str));

var result = hasher.GetCurrentHashAsUInt32();
hasher.Reset();
Expand Down

0 comments on commit 1c10b6d

Please sign in to comment.