-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhash.c
More file actions
72 lines (62 loc) · 1.66 KB
/
Copy pathhash.c
File metadata and controls
72 lines (62 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "mudlle-config.h"
# include <assert.h>
#include <limits.h>
#include "charset.h"
#include "hash.h"
#include "mudlle-macro.h"
#include "mvalues.h"
/* FNV-1a hash from http://tools.ietf.org/html/draft-eastlake-fnv-03 */
#define FNV_PRIME (sizeof (long) == 4 \
? 0x01000193L \
: 0x00000100000001b3L)
#define FNV_OFFSET (sizeof (long) == 4 \
? 0x811c9dc5L \
: 0xcbf29ce484222325L)
CASSERT(sizeof (long) == 4 || sizeof (long) == 8);
ulong fold_hash(ulong code, int bits)
{
if (bits == 0)
return 0;
if (bits == CHAR_BIT * sizeof code)
return code;
assert(bits > 0 && bits < CHAR_BIT * sizeof code);
code ^= code >> bits;
ulong mask = ~0UL >> (CHAR_BIT * sizeof (ulong) - bits);
return code & mask;
}
/* case- and accentuation-insensitive */
ulong symbol_7inhash(const char *name, size_t len, int bits)
{
ulong code = FNV_OFFSET;
while (len--)
{
unsigned char c = *name++;
code ^= TO_7LOWER(c);
code *= FNV_PRIME;
}
return fold_hash(code, bits);
}
/* case- and accentuation-sensitive */
ulong symbol_nhash(const char *name, size_t len, int bits)
{
ulong code = FNV_OFFSET;
while (len--)
{
unsigned char c = *name++;
code ^= c;
code *= FNV_PRIME;
}
return fold_hash(code, bits);
}
unsigned int string_nhash(const char *s, size_t len)
{
return symbol_nhash(s, len, TAGGED_INT_BITS - 1);
}
unsigned int string_hash(const char *s)
{
return string_nhash(s, strlen(s));
}
unsigned int string_7hash(const char *s)
{
return symbol_7inhash(s, strlen(s), TAGGED_INT_BITS - 1);
}