text-fx 0.4.0

A collection of text processing utilities for Rust.
Documentation
/// Computes a hash for a UTF-8 string slice using the method described by Bruno Haible.
///
/// This function iterates over each byte of the string, updating the hash value at each step.
/// It is suitable for hashing Rust string slices (`&str`), not raw C pointers.
///
/// See: <https://www.haible.de/bruno/hashfunc.html>
///
/// # Examples
///
/// ```
/// use text_fx::hash::string_hash;
///
/// let hash = string_hash("hello");
/// assert_eq!(hash, string_hash("hello"));
/// assert_ne!(hash, string_hash("world"));
/// ```
pub fn string_hash(s: &str) -> usize {
    let mut h = 0;
    for &b in s.as_bytes() {
        h = b as usize + (h << 9) | (h >> (usize::BITS - 9));
    }
    h
}

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

    #[test]
    fn test_string_hash() {
        assert_eq!(string_hash("hello"), string_hash("hello"));
        assert_ne!(string_hash("hello"), string_hash("world"));
    }
}