]>
git.cameronkatri.com Git - mandoc.git/blob - chars.c
1 /* $Id: chars.c,v 1.56 2014/03/23 11:25:25 schwarze Exp $ */
3 * Copyright (c) 2009, 2010, 2011 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.
28 #include "mandoc_aux.h"
29 #include "libmandoc.h"
43 #define CHAR(in, ch, code) \
44 { NULL, (in), (ch), (code) },
46 #define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
47 #define CHAR_TBL_END };
55 static const struct ln
*find(const struct mchars
*,
56 const char *, size_t);
59 mchars_free(struct mchars
*arg
)
75 * Constructs a very basic chaining hashtable. The hash routine
76 * is simply the integral value of the first character.
77 * Subsequent entries are chained in the order they're processed.
80 tab
= mandoc_malloc(sizeof(struct mchars
));
81 htab
= mandoc_calloc(PRINT_HI
- PRINT_LO
+ 1, sizeof(struct ln
*));
83 for (i
= 0; i
< LINES_MAX
; i
++) {
84 hash
= (int)lines
[i
].code
[0] - PRINT_LO
;
86 if (NULL
== (pp
= htab
[hash
])) {
87 htab
[hash
] = &lines
[i
];
91 for ( ; pp
->next
; pp
= pp
->next
)
101 mchars_spec2cp(const struct mchars
*arg
, const char *p
, size_t sz
)
105 ln
= find(arg
, p
, sz
);
112 mchars_num2char(const char *p
, size_t sz
)
116 if ((i
= mandoc_strntoi(p
, sz
, 10)) < 0)
118 return(i
> 0 && i
< 256 && isprint(i
) ?
119 /* LINTED */ i
: '\0');
123 mchars_num2uc(const char *p
, size_t sz
)
127 if ((i
= mandoc_strntoi(p
, sz
, 16)) < 0)
129 /* FIXME: make sure we're not in a bogus range. */
130 return(i
> 0x80 && i
<= 0x10FFFF ? i
: '\0');
134 mchars_spec2str(const struct mchars
*arg
,
135 const char *p
, size_t sz
, size_t *rsz
)
139 ln
= find(arg
, p
, sz
);
145 *rsz
= strlen(ln
->ascii
);
149 static const struct ln
*
150 find(const struct mchars
*tab
, const char *p
, size_t sz
)
157 if (0 == sz
|| p
[0] < PRINT_LO
|| p
[0] > PRINT_HI
)
160 hash
= (int)p
[0] - PRINT_LO
;
162 for (pp
= tab
->htab
[hash
]; pp
; pp
= pp
->next
)
163 if (0 == strncmp(pp
->code
, p
, sz
) &&
164 '\0' == pp
->code
[(int)sz
])