]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_hash.c
1 /* $Id: mdoc_hash.c,v 1.6 2009/07/17 10:56:27 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.
25 #define ADJUST_MAJOR(x) \
27 (x) = 0; /* % -> 00 */ \
29 (x) -= 64; /* A-Z -> 01 - 26 */ \
31 (x) -= 70; /* a-z -> 27 - 52 */ \
32 while (/*CONSTCOND*/0)
34 #define ADJUST_MINOR(y) \
36 (y) = 0; /* 1 -> 00 */ \
38 (y) -= 65; /* A-Z -> 00 - 25 */ \
40 (y) -= 97; /* a-z -> 00 - 25 */ \
41 while (/*CONSTCOND*/0)
43 #define INDEX(maj, min) \
44 ((maj) * 26 * 3) + ((min) * 3)
46 #define SLOTCMP(slot, val) \
47 (mdoc_macronames[(slot)][0] == (val)[0] && \
48 mdoc_macronames[(slot)][1] == (val)[1] && \
50 mdoc_macronames[(slot)][2] == (val)[2]))
54 mdoc_hash_free(void *htab
)
65 int i
, major
, minor
, ind
;
68 htab
= calloc(26 * 3 * 52, sizeof(struct mdoc_macro
*));
72 for (i
= 0; i
< MDOC_MAX
; i
++) {
73 major
= mdoc_macronames
[i
][0];
74 assert(isalpha((u_char
)major
) || 37 == major
);
78 minor
= mdoc_macronames
[i
][1];
79 assert(isalpha((u_char
)minor
) || 49 == minor
);
83 ind
= INDEX(major
, minor
);
85 if (NULL
== htab
[ind
]) {
86 htab
[ind
] = &mdoc_macros
[i
];
90 if (NULL
== htab
[++ind
]) {
91 htab
[ind
] = &mdoc_macros
[i
];
95 assert(NULL
== htab
[++ind
]);
96 htab
[ind
] = &mdoc_macros
[i
];
104 mdoc_hash_find(const void *arg
, const char *tmp
)
106 int major
, minor
, ind
, slot
;
112 if (0 == (major
= tmp
[0]))
114 if (0 == (minor
= tmp
[1]))
117 if (tmp
[2] && tmp
[3])
120 if (37 != major
&& ! isalpha((u_char
)major
))
122 if (49 != minor
&& ! isalpha((u_char
)minor
))
128 ind
= INDEX(major
, minor
);
131 slot
= htab
[ind
] - /* LINTED */
133 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
134 slot
/= sizeof(struct mdoc_macro
);
135 if (SLOTCMP(slot
, tmp
))
141 slot
= htab
[ind
] - /* LINTED */
143 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
144 slot
/= sizeof(struct mdoc_macro
);
145 if (SLOTCMP(slot
, tmp
))
150 if (NULL
== htab
[ind
])
152 slot
= htab
[ind
] - /* LINTED */
154 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
155 slot
/= sizeof(struct mdoc_macro
);
156 if (SLOTCMP(slot
, tmp
))