rapidstr2int 1.0.30

Fast dependency-free string to 32-bit integer hashing (FNV-1a)
Documentation
//! rapidstr2int — fast, dependency-free string -> 32-bit unsigned integer hashing.
//!
//! Implements FNV-1a (Fowler–Noll–Vo) over the UTF-8 bytes of the input. Deterministic
//! and stable across platforms; useful for hash tables, shard keys, bloom filters and
//! cache bucketing.

const FNV_OFFSET: u32 = 0x811c_9dc5;
const FNV_PRIME: u32 = 0x0100_0193;

/// Hash a string to a 32-bit unsigned integer (FNV-1a over UTF-8 bytes).
pub fn str_to_int(s: &str) -> u32 {
    let mut h = FNV_OFFSET;
    for &b in s.as_bytes() {
        h ^= b as u32;
        h = h.wrapping_mul(FNV_PRIME);
    }
    h
}

/// Same hash as a fixed-width 8-char lowercase hex string.
pub fn str_to_hex(s: &str) -> String {
    format!("{:08x}", str_to_int(s))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn known_vectors() {
        assert_eq!(str_to_int(""), 0x811c9dc5);
        assert_eq!(str_to_hex("hello"), "4f9f2cab");
        assert_eq!(str_to_hex("a"), "e40c292c");
    }
}