/rust/registry/src/index.crates.io-1949cf8c6b5b557f/cranelift-codegen-shared-0.129.1/src/constant_hash.rs
Line | Count | Source |
1 | | //! This module provides a primitive hash function. |
2 | | |
3 | | /// A primitive hash function for matching opcodes. |
4 | 35.1k | pub fn simple_hash(s: &str) -> usize { |
5 | 35.1k | let mut h: u32 = 5381; |
6 | 463k | for c in s.chars() { |
7 | 463k | h = (h ^ c as u32).wrapping_add(h.rotate_right(6)); |
8 | 463k | } |
9 | 35.1k | h as usize |
10 | 35.1k | } |
11 | | |
12 | | #[cfg(test)] |
13 | | mod tests { |
14 | | use super::simple_hash; |
15 | | |
16 | | #[test] |
17 | | fn basic() { |
18 | | assert_eq!(simple_hash("Hello"), 0x2fa70c01); |
19 | | assert_eq!(simple_hash("world"), 0x5b0c31d5); |
20 | | } |
21 | | } |