| 10 | |
| 11 | #[inline] |
| 12 | pub const fn string_to_u64(s: &str) -> u64 { |
| 13 | let bytes = s.as_bytes(); |
| 14 | let len = bytes.len(); |
| 15 | if len == 0 { |
| 16 | return EMPTY_STRING_HASH; |
| 17 | } |
| 18 | |
| 19 | let mut hash = STRING_HASH_SEED ^ (len as u64).wrapping_mul(STRING_HASH_PRIME); |
| 20 | let mut i = 0usize; |
| 21 | |
| 22 | while i + 8 <= len { |
| 23 | hash ^= read_u64_le(bytes, i); |
| 24 | hash = hash.wrapping_mul(STRING_HASH_PRIME); |
| 25 | hash ^= hash >> 32; |
| 26 | i += 8; |
| 27 | } |
| 28 | |
| 29 | let mut tail = 0u64; |
| 30 | let mut shift = 0u32; |
| 31 | while i < len { |
| 32 | tail |= (bytes[i] as u64) << shift; |
| 33 | shift += 8; |
| 34 | i += 1; |
| 35 | } |
| 36 | |
| 37 | hash ^= tail; |
| 38 | hash = hash.wrapping_mul(STRING_HASH_SEED); |
| 39 | mix64(hash) |
| 40 | } |
| 41 | |
| 42 | const fn read_u64_le(bytes: &[u8], offset: usize) -> u64 { |
| 43 | (bytes[offset] as u64) |