Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Adds support for ROS to HashUtility #1740

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading