]>
git.cameronkatri.com Git - mandoc.git/blob - hash.c
1 /* $Id: hash.c,v 1.10 2009/03/11 00:39:58 kristaps Exp $ */
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
29 * Routines for the perfect-hash hashtable used by the parser to look up
30 * tokens by their string-ified names (`.Fl' -> MDOC_Fl). The
31 * allocation penalty for this is 27 * 26 * sizeof(ptr).
35 mdoc_tokhash_free(void *htab
)
43 mdoc_tokhash_alloc(void)
45 int i
, major
, minor
, ind
;
48 htab
= calloc(27 * 26 * 3, sizeof(struct mdoc_macro
*));
52 for (i
= 1; i
< MDOC_MAX
; i
++) {
53 major
= mdoc_macronames
[i
][0];
54 assert((major
>= 65 && major
<= 90) ||
62 minor
= mdoc_macronames
[i
][1];
63 assert((minor
>= 65 && minor
<= 90) ||
65 (minor
>= 97 && minor
<= 122));
74 assert(major
>= 0 && major
< 27);
75 assert(minor
>= 0 && minor
< 26);
77 ind
= (major
* 27 * 3) + (minor
* 3);
79 if (NULL
== htab
[ind
]) {
80 htab
[ind
] = &mdoc_macros
[i
];
84 if (NULL
== htab
[++ind
]) {
85 htab
[ind
] = &mdoc_macros
[i
];
89 assert(NULL
== htab
[++ind
]);
90 htab
[ind
] = &mdoc_macros
[i
];
98 mdoc_tokhash_find(const void *arg
, const char *tmp
)
100 int major
, minor
, ind
, slot
;
106 if (0 == tmp
[0] || 0 == tmp
[1])
108 if (tmp
[2] && tmp
[3])
111 if ( ! (tmp
[0] == 37 || (tmp
[0] >= 65 && tmp
[0] <= 90)))
114 if ( ! ((tmp
[1] >= 65 && tmp
[1] <= 90) ||
116 (tmp
[1] >= 97 && tmp
[1] <= 122)))
126 else if (tmp
[1] <= 90)
131 ind
= (major
* 27 * 3) + (minor
* 3);
132 if (ind
< 0 || ind
>= (27 * 26 * 3))
136 slot
= htab
[ind
] - /* LINTED */
138 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
139 slot
/= sizeof(struct mdoc_macro
);
140 if (mdoc_macronames
[slot
][0] == tmp
[0] &&
141 mdoc_macronames
[slot
][1] == tmp
[1] &&
143 mdoc_macronames
[slot
][2] == tmp
[2]))
149 slot
= htab
[ind
] - /* LINTED */
151 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
152 slot
/= sizeof(struct mdoc_macro
);
153 if (mdoc_macronames
[slot
][0] == tmp
[0] &&
154 mdoc_macronames
[slot
][1] == tmp
[1] &&
156 mdoc_macronames
[slot
][2] == tmp
[2]))
161 if (NULL
== htab
[ind
])
163 slot
= htab
[ind
] - /* LINTED */
165 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
166 slot
/= sizeof(struct mdoc_macro
);
167 if (mdoc_macronames
[slot
][0] == tmp
[0] &&
168 mdoc_macronames
[slot
][1] == tmp
[1] &&
170 mdoc_macronames
[slot
][2] == tmp
[2]))