]>
git.cameronkatri.com Git - mandoc.git/blob - chars.c
1 /* $Id: chars.c,v 1.44 2011/05/17 11:50:20 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.
29 #include "libmandoc.h"
40 #define CHARS_CHAR (1 << 0)
41 #define CHARS_STRING (1 << 1)
42 #define CHARS_BOTH (CHARS_CHAR | CHARS_STRING)
47 #define CHAR(in, ch, code) \
48 { NULL, (in), (ch), (code), CHARS_CHAR },
49 #define STRING(in, ch, code) \
50 { NULL, (in), (ch), (code), CHARS_STRING },
51 #define BOTH(in, ch, code) \
52 { NULL, (in), (ch), (code), CHARS_BOTH },
54 #define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
55 #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 mchars
*, const char *, size_t, int);
68 mchars_free(struct mchars
*arg
)
84 * Constructs a very basic chaining hashtable. The hash routine
85 * is simply the integral value of the first character.
86 * Subsequent entries are chained in the order they're processed
87 * (they're in-line re-ordered during lookup).
90 tab
= mandoc_malloc(sizeof(struct mchars
));
91 htab
= mandoc_calloc(PRINT_HI
- PRINT_LO
+ 1, sizeof(struct ln
**));
93 for (i
= 0; i
< LINES_MAX
; i
++) {
94 hash
= (int)lines
[i
].code
[0] - PRINT_LO
;
96 if (NULL
== (pp
= htab
[hash
])) {
97 htab
[hash
] = &lines
[i
];
101 for ( ; pp
->next
; pp
= pp
->next
)
103 pp
->next
= &lines
[i
];
112 * Special character to Unicode codepoint.
115 mchars_spec2cp(struct mchars
*arg
, const char *p
, size_t sz
)
119 ln
= find(arg
, p
, sz
, CHARS_CHAR
);
127 * Reserved word to Unicode codepoint.
130 mchars_res2cp(struct mchars
*arg
, const char *p
, size_t sz
)
134 ln
= find(arg
, p
, sz
, CHARS_STRING
);
141 * Numbered character string to ASCII codepoint.
142 * This can only be a printable character (i.e., alnum, punct, space) so
143 * prevent the character from ruining our state (backspace, newline, and
145 * If the character is illegal, returns '\0'.
148 mchars_num2char(const char *p
, size_t sz
)
152 if ((i
= mandoc_strntou(p
, sz
, 10)) < 0)
154 return(isprint(i
) ? i
: '\0');
158 * Hex character string to Unicode codepoint.
159 * If the character is illegal, returns '\0'.
162 mchars_num2uc(const char *p
, size_t sz
)
166 if ((i
= mandoc_strntou(p
, sz
, 16)) < 0)
168 /* FIXME: make sure we're not in a bogus range. */
169 return(i
> 0x80 && i
<= 0x10FFFF ? i
: '\0');
173 * Special character to string array.
176 mchars_spec2str(struct mchars
*arg
, const char *p
, size_t sz
, size_t *rsz
)
180 ln
= find(arg
, p
, sz
, CHARS_CHAR
);
184 *rsz
= strlen(ln
->ascii
);
189 * Reserved word to string array.
192 mchars_res2str(struct mchars
*arg
, const char *p
, size_t sz
, size_t *rsz
)
196 ln
= find(arg
, p
, sz
, CHARS_STRING
);
200 *rsz
= strlen(ln
->ascii
);
204 static const struct ln
*
205 find(struct mchars
*tab
, const char *p
, size_t sz
, int type
)
207 struct ln
*pp
, *prev
;
215 if (p
[0] < PRINT_LO
|| p
[0] > PRINT_HI
)
219 * Lookup the symbol in the symbol hash. See ascii2htab for the
220 * hashtable specs. This dynamically re-orders the hash chain
221 * to optimise for repeat hits.
224 hash
= (int)p
[0] - PRINT_LO
;
227 if (NULL
== (pp
= htab
[hash
]))
230 for (prev
= NULL
; pp
; pp
= pp
->next
) {
231 if ( ! match(pp
, p
, sz
, type
)) {
237 prev
->next
= pp
->next
;
238 pp
->next
= htab
[hash
];
249 match(const struct ln
*ln
, const char *p
, size_t sz
, int type
)
252 if ( ! (ln
->type
& type
))
254 if (strncmp(ln
->code
, p
, sz
))
256 return('\0' == ln
->code
[(int)sz
]);