1 /* $Id: chars.c,v 1.34 2011/03/22 10:13:01 kristaps Exp $ */
3 * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
39 #define CHARS_CHAR (1 << 0)
40 #define CHARS_STRING (1 << 1)
41 #define CHARS_BOTH (CHARS_CHAR | CHARS_STRING)
46 #define CHAR(in, ch, code) \
47 { NULL, (in), (ch), (code), CHARS_CHAR },
48 #define STRING(in, ch, code) \
49 { NULL, (in), (ch), (code), CHARS_STRING },
50 #define BOTH(in, ch, code) \
51 { NULL, (in), (ch), (code), CHARS_BOTH },
53 #define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
54 #define CHAR_TBL_END };
63 static inline int match(const struct ln *,
64 const char *, size_t, int);
65 static const struct ln *find(struct ctab *, const char *, size_t, int);
73 tab = (struct ctab *)arg;
81 chars_init(enum chars type)
89 * Constructs a very basic chaining hashtable. The hash routine
90 * is simply the integral value of the first character.
91 * Subsequent entries are chained in the order they're processed
92 * (they're in-line re-ordered during lookup).
95 tab = mandoc_malloc(sizeof(struct ctab));
96 htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
98 for (i = 0; i < LINES_MAX; i++) {
99 hash = (int)lines[i].code[0] - PRINT_LO;
101 if (NULL == (pp = htab[hash])) {
102 htab[hash] = &lines[i];
106 for ( ; pp->next; pp = pp->next)
108 pp->next = &lines[i];
118 * Special character to Unicode codepoint.
121 chars_spec2cp(void *arg, const char *p, size_t sz)
125 ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
133 * Reserved word to Unicode codepoint.
136 chars_res2cp(void *arg, const char *p, size_t sz)
140 ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
148 * Numbered character to literal character,
149 * represented as a null-terminated string for additional safety.
152 chars_num2char(const char *p, size_t sz)
160 if (i < 0 || i > 255)
169 * Special character to string array.
172 chars_spec2str(void *arg, const char *p, size_t sz, size_t *rsz)
176 ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
180 *rsz = strlen(ln->ascii);
186 * Reserved word to string array.
189 chars_res2str(void *arg, const char *p, size_t sz, size_t *rsz)
193 ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
197 *rsz = strlen(ln->ascii);
202 static const struct ln *
203 find(struct ctab *tab, const char *p, size_t sz, int type)
205 struct ln *pp, *prev;
213 if (p[0] < PRINT_LO || p[0] > PRINT_HI)
217 * Lookup the symbol in the symbol hash. See ascii2htab for the
218 * hashtable specs. This dynamically re-orders the hash chain
219 * to optimise for repeat hits.
222 hash = (int)p[0] - PRINT_LO;
225 if (NULL == (pp = htab[hash]))
228 for (prev = NULL; pp; pp = pp->next) {
229 if ( ! match(pp, p, sz, type)) {
235 prev->next = pp->next;
236 pp->next = htab[hash];
248 match(const struct ln *ln, const char *p, size_t sz, int type)
251 if ( ! (ln->type & type))
253 if (strncmp(ln->code, p, sz))
255 return('\0' == ln->code[(int)sz]);