xref: /haiku/src/system/kernel/util/StringHash.cpp (revision 71452e98334eaac603bf542d159e24788a46bebb)
1 /*
2  * Copyright 2002-2013, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Copyright 2001, Travis Geiselbrecht. All rights reserved.
6  * Distributed under the terms of the NewOS License.
7  */
8 
9 
10 #include <util/StringHash.h>
11 
12 
13 uint32
14 hash_hash_string(const char* string)
15 {
16 	uint32 hash = 0;
17 	char c;
18 
19 	// we assume hash to be at least 32 bits
20 	while ((c = *string++) != 0) {
21 		hash ^= hash >> 28;
22 		hash <<= 4;
23 		hash ^= c;
24 	}
25 
26 	return hash;
27 }
28 
29 
30 uint32
31 hash_hash_string_part(const char* string, size_t maxLength)
32 {
33 	uint32 hash = 0;
34 	char c;
35 
36 	// we assume hash to be at least 32 bits
37 	while (maxLength-- > 0 && (c = *string++) != 0) {
38 		hash ^= hash >> 28;
39 		hash <<= 4;
40 		hash ^= c;
41 	}
42 
43 	return hash;
44 }
45