using System.Runtime.CompilerServices;

namespace AyCode.Core.Serializers;

/// <summary>
/// FNV-1a 32-bit hash implementation.
/// Deterministic across processes (unlike string.GetHashCode()).
/// Used for property name hashing in UseMetadata mode.
/// </summary>
public static class FnvHash
{
    private const uint FnvOffsetBasis = 2166136261;
    private const uint FnvPrime = 16777619;

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static int ComputeString(string value)
    {
        var hash = FnvOffsetBasis;
        for (var i = 0; i < value.Length; i++)
        {
            hash ^= value[i];
            hash *= FnvPrime;
        }
        return (int)hash;
    }
}
