1 /* $Id: man_hash.c,v 1.28 2014/08/10 23:54:41 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
19 #include <sys/types.h>
32 #define HASH_ROW(x) do { \
33 if (isupper((unsigned char)(x))) \
38 } while (/* CONSTCOND */ 0)
41 * Lookup table is indexed first by lower-case first letter (plus one
42 * for the period, which is stored in the last row), then by lower or
43 * uppercase second letter. Buckets correspond to the index of the
44 * macro (the integer value of the enum stored as a char to save a bit
47 static unsigned char table[26 * HASH_DEPTH];
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.
59 memset(table, UCHAR_MAX, sizeof(table));
61 assert(MAN_MAX < UCHAR_MAX);
63 for (i = 0; i < (int)MAN_MAX; i++) {
64 x = man_macronames[i][0];
66 assert(isalpha((unsigned char)x));
70 for (j = 0; j < HASH_DEPTH; j++)
71 if (UCHAR_MAX == table[x + j]) {
72 table[x + j] = (unsigned char)i;
76 assert(j < HASH_DEPTH);
81 man_hash_find(const char *tmp)
86 if ('\0' == (x = tmp[0]))
88 if ( ! (isalpha((unsigned char)x)))
93 for (i = 0; i < HASH_DEPTH; i++) {
94 if (UCHAR_MAX == (y = table[x + i]))
98 if (0 == strcmp(tmp, man_macronames[tok]))