]> git.cameronkatri.com Git - mandoc.git/blob - man_hash.c
Removed `.' entry from -man hashtable.
[mandoc.git] / man_hash.c
1 /* $Id: man_hash.c,v 1.20 2010/05/15 22:49:58 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <ctype.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "libman.h"
30
31 #define HASH_DEPTH 6
32
33 #define HASH_ROW(x) do { \
34 if (isupper((u_char)(x))) \
35 (x) -= 65; \
36 else \
37 (x) -= 97; \
38 (x) *= HASH_DEPTH; \
39 } while (/* CONSTCOND */ 0)
40
41 /*
42 * Lookup table is indexed first by lower-case first letter (plus one
43 * for the period, which is stored in the last row), then by lower or
44 * uppercase second letter. Buckets correspond to the index of the
45 * macro (the integer value of the enum stored as a char to save a bit
46 * of space).
47 */
48 static u_char table[26 * HASH_DEPTH];
49
50 /*
51 * XXX - this hash has global scope, so if intended for use as a library
52 * with multiple callers, it will need re-invocation protection.
53 */
54 void
55 man_hash_init(void)
56 {
57 int i, j, x;
58
59 memset(table, UCHAR_MAX, sizeof(table));
60
61 assert(/* LINTED */
62 MAN_MAX < UCHAR_MAX);
63
64 for (i = 0; i < (int)MAN_MAX; i++) {
65 x = man_macronames[i][0];
66
67 assert(isalpha((u_char)x));
68
69 HASH_ROW(x);
70
71 for (j = 0; j < HASH_DEPTH; j++)
72 if (UCHAR_MAX == table[x + j]) {
73 table[x + j] = (u_char)i;
74 break;
75 }
76
77 assert(j < HASH_DEPTH);
78 }
79 }
80
81
82 enum mant
83 man_hash_find(const char *tmp)
84 {
85 int x, y, i;
86 enum mant tok;
87
88 if ('\0' == (x = tmp[0]))
89 return(MAN_MAX);
90 if ( ! (isalpha((u_char)x)))
91 return(MAN_MAX);
92
93 HASH_ROW(x);
94
95 for (i = 0; i < HASH_DEPTH; i++) {
96 if (UCHAR_MAX == (y = table[x + i]))
97 return(MAN_MAX);
98
99 tok = (enum mant)y;
100 if (0 == strcmp(tmp, man_macronames[tok]))
101 return(tok);
102 }
103
104 return(MAN_MAX);
105 }