Files
c3c/lib/std/hash/fnv32a.c3
Christoffer Lerno dad97fc2d9 Improved #foo resolution inside of the compiler.
Deprecation of several `&` macros.
2025-01-08 12:55:20 +01:00

42 lines
762 B
Plaintext

// Copyright (c) 2021 Christoffer Lerno. All rights reserved.
// Use of this source code is governed by the MIT license
// a copy of which can be found in the LICENSE_STDLIB file.
module std::hash::fnv32a;
distinct Fnv32a = uint;
const FNV32A_START @private = 0x811c9dc5;
const FNV32A_MUL @private = 0x01000193;
macro void update(h, char x) @private => *h = (*h ^ ($typeof(*h))x) * FNV32A_MUL;
fn void Fnv32a.init(&self)
{
*self = FNV32A_START;
}
fn void Fnv32a.update(&self, char[] data)
{
Fnv32a h = *self;
foreach (char x : data)
{
update(&h, x);
}
*self = h;
}
macro void Fnv32a.update_char(&self, char c)
{
update(self, c);
}
fn uint encode(char[] data)
{
uint h = FNV32A_START;
foreach (char x : data)
{
update(&h, x);
}
return h;
}