]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_hash.c
1 /* $Id: mdoc_hash.c,v 1.5 2009/06/16 19:55:28 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 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 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.
26 * Routines for the perfect-hash hashtable used by the parser to look up
27 * tokens by their string-ified names (`.Fl' -> MDOC_Fl). The
28 * allocation penalty for this is 27 * 26 * sizeof(ptr).
32 mdoc_hash_free(void *htab
)
42 int i
, major
, minor
, ind
;
45 htab
= calloc(27 * 26 * 3, sizeof(struct mdoc_macro
*));
49 for (i
= 0; i
< MDOC_MAX
; i
++) {
50 major
= mdoc_macronames
[i
][0];
51 assert((major
>= 65 && major
<= 90) ||
59 minor
= mdoc_macronames
[i
][1];
60 assert((minor
>= 65 && minor
<= 90) ||
62 (minor
>= 97 && minor
<= 122));
71 assert(major
>= 0 && major
< 27);
72 assert(minor
>= 0 && minor
< 26);
74 ind
= (major
* 27 * 3) + (minor
* 3);
76 if (NULL
== htab
[ind
]) {
77 htab
[ind
] = &mdoc_macros
[i
];
81 if (NULL
== htab
[++ind
]) {
82 htab
[ind
] = &mdoc_macros
[i
];
86 assert(NULL
== htab
[++ind
]);
87 htab
[ind
] = &mdoc_macros
[i
];
95 mdoc_hash_find(const void *arg
, const char *tmp
)
97 int major
, minor
, ind
, slot
;
103 if (0 == tmp
[0] || 0 == tmp
[1])
105 if (tmp
[2] && tmp
[3])
108 if ( ! (tmp
[0] == 37 || (tmp
[0] >= 65 && tmp
[0] <= 90)))
111 if ( ! ((tmp
[1] >= 65 && tmp
[1] <= 90) ||
113 (tmp
[1] >= 97 && tmp
[1] <= 122)))
123 else if (tmp
[1] <= 90)
128 ind
= (major
* 27 * 3) + (minor
* 3);
129 if (ind
< 0 || ind
>= (27 * 26 * 3))
133 slot
= htab
[ind
] - /* LINTED */
135 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
136 slot
/= sizeof(struct mdoc_macro
);
137 if (mdoc_macronames
[slot
][0] == tmp
[0] &&
138 mdoc_macronames
[slot
][1] == tmp
[1] &&
140 mdoc_macronames
[slot
][2] == tmp
[2]))
146 slot
= htab
[ind
] - /* LINTED */
148 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
149 slot
/= sizeof(struct mdoc_macro
);
150 if (mdoc_macronames
[slot
][0] == tmp
[0] &&
151 mdoc_macronames
[slot
][1] == tmp
[1] &&
153 mdoc_macronames
[slot
][2] == tmp
[2]))
158 if (NULL
== htab
[ind
])
160 slot
= htab
[ind
] - /* LINTED */
162 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
163 slot
/= sizeof(struct mdoc_macro
);
164 if (mdoc_macronames
[slot
][0] == tmp
[0] &&
165 mdoc_macronames
[slot
][1] == tmp
[1] &&
167 mdoc_macronames
[slot
][2] == tmp
[2]))