Move simple_hash into its own module.

This commit is contained in:
Jakob Stoklund Olesen
2016-08-09 11:28:51 -07:00
parent 36ad7da3ec
commit d152719d93
3 changed files with 24 additions and 10 deletions

View File

@@ -0,0 +1,21 @@
/// A primitive hash function for matching opcodes.
/// Must match `meta/constant_hash.py`.
pub fn simple_hash(s: &str) -> u32 {
let mut h: u32 = 5381;
for c in s.chars() {
h = (h ^ c as u32).wrapping_add(h.rotate_right(6));
}
h
}
#[cfg(test)]
mod tests {
use super::simple_hash;
#[test]
fn basic() {
// c.f. meta/constant_hash.py tests.
assert_eq!(simple_hash("Hello"), 0x2fa70c01);
assert_eq!(simple_hash("world"), 0x5b0c31d5);
}
}