]> www.ginac.de Git - cln.git/blob - src/base/string/cl_st_hashcode.cc
* */*: cl_istream -> std::istream, cl_ostream -> std::ostream.
[cln.git] / src / base / string / cl_st_hashcode.cc
1 // cln/string.hashcode().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cln/string.h"
8
9
10 // Implementation.
11
12 namespace cln {
13
14 unsigned long hashcode (const cl_string& str)
15 {
16     var unsigned long code = 0x61284AF3;
17     // We walk through all characters. It may take some time for very
18     // long strings, but it's better than completely ignoring some characters.
19     var long len = str.length();
20     var const char * ptr = str.asciz();
21     for (; len > 0; len--) {
22         var unsigned char c = *ptr++;
23         code = (code << 5) | (code >> 27); // rotate left 5 bits
24         code += (long)c << 16;
25         code ^= (long)c;
26         code &= 0xFFFFFFFF;
27     }
28     return code;
29 }
30
31 }  // namespace cln