Files
2026-05-31 14:27:52 -07:00

56 lines
1.3 KiB
C#

using Unity.Collections;
using Hash128 = Unity.Entities.Hash128;
using FixedStringName = Unity.Collections.FixedString512Bytes;
using Unity.Core;
////////////////////////////////////////////////////////////////////////////////////
namespace Rukhanka.Toolbox
{
public static class FixedStringExtensions
{
public static unsafe Hash128 CalculateHash128(in this FixedStringName s)
{
if (s.IsEmpty)
return default;
var hasher = new xxHash3.StreamingState();
hasher.Update(s.GetUnsafePtr(), s.Length);
var rv = new Hash128(hasher.DigestHash128());
return rv;
}
////////////////////////////////////////////////////////////////////////////////////
public static Hash128 CalculateHash128(this string s)
{
if (s == null)
return default;
var fs = new FixedStringName(s);
return fs.CalculateHash128();
}
////////////////////////////////////////////////////////////////////////////////////
public static unsafe uint CalculateHash32(in this FixedStringName s)
{
if (s.IsEmpty)
return default;
var rv = XXHash.Hash32(s.GetUnsafePtr(), s.Length);
return rv;
}
////////////////////////////////////////////////////////////////////////////////////
public static uint CalculateHash32(this string s)
{
if (s == null)
return 0;
var fs = new FixedStringName(s);
return fs.CalculateHash32();
}
}
}