=================================================================== RCS file: /cvs/mandoc/chars.c,v retrieving revision 1.1 retrieving revision 1.67 diff -u -p -r1.1 -r1.67 --- mandoc/chars.c 2009/09/17 07:41:28 1.1 +++ mandoc/chars.c 2015/10/06 18:32:19 1.67 @@ -1,6 +1,7 @@ -/* $Id: chars.c,v 1.1 2009/09/17 07:41:28 kristaps Exp $ */ +/* $Id: chars.c,v 1.67 2015/10/06 18:32:19 schwarze Exp $ */ /* - * Copyright (c) 2009 Kristaps Dzonsons + * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons + * Copyright (c) 2011, 2014 Ingo Schwarze * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,68 +15,59 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "config.h" + +#include + #include -#include +#include #include #include -#include "chars.h" +#include "mandoc.h" +#include "mandoc_aux.h" +#include "libmandoc.h" -#define ASCII_PRINT_HI 126 -#define ASCII_PRINT_LO 32 +#define PRINT_HI 126 +#define PRINT_LO 32 struct ln { struct ln *next; const char *code; - const char *out; - size_t codesz; - size_t outsz; - int type; -#define CHARS_CHAR (1 << 0) -#define CHARS_STRING (1 << 1) -#define CHARS_BOTH (0x03) + const char *ascii; + int unicode; }; -#define LINES_MAX 266 +#define LINES_MAX 332 -#define CHAR(w, x, y, z) \ - { NULL, (w), (y), (x), (z), CHARS_CHAR }, -#define STRING(w, x, y, z) \ - { NULL, (w), (y), (x), (z), CHARS_STRING }, -#define BOTH(w, x, y, z) \ - { NULL, (w), (y), (x), (z), CHARS_BOTH }, +#define CHAR(in, ch, code) \ + { NULL, (in), (ch), (code) }, -static struct ln lines[LINES_MAX] = { +#define CHAR_TBL_START static struct ln lines[LINES_MAX] = { +#define CHAR_TBL_END }; + #include "chars.in" -}; -struct tbl { +struct mchars { struct ln **htab; }; -static inline int match(const struct ln *, - const char *, size_t, int); -static const char *find(struct tbl *, const char *, - size_t, size_t *, int); +static const struct ln *find(const struct mchars *, + const char *, size_t); void -chars_free(void *arg) +mchars_free(struct mchars *arg) { - struct tbl *tab; - tab = (struct tbl *)arg; - - free(tab->htab); - free(tab); + free(arg->htab); + free(arg); } - -/* ARGSUSED */ -void * -chars_init(enum chars type) +struct mchars * +mchars_alloc(void) { - struct tbl *tab; + struct mchars *tab; struct ln **htab; struct ln *pp; int i, hash; @@ -83,26 +75,15 @@ chars_init(enum chars type) /* * Constructs a very basic chaining hashtable. The hash routine * is simply the integral value of the first character. - * Subsequent entries are chained in the order they're processed - * (they're in-line re-ordered during lookup). + * Subsequent entries are chained in the order they're processed. */ - if (NULL == (tab = malloc(sizeof(struct tbl)))) - err(1, "malloc"); + tab = mandoc_malloc(sizeof(struct mchars)); + htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln *)); - htab = calloc(ASCII_PRINT_HI - ASCII_PRINT_LO + 1, - sizeof(struct ln **)); - - if (NULL == htab) - err(1, "malloc"); - for (i = 0; i < LINES_MAX; i++) { - assert(lines[i].codesz > 0); - assert(lines[i].code); - assert(lines[i].out); + hash = (int)lines[i].code[0] - PRINT_LO; - hash = (int)lines[i].code[0] - ASCII_PRINT_LO; - if (NULL == (pp = htab[hash])) { htab[hash] = &lines[i]; continue; @@ -110,92 +91,85 @@ chars_init(enum chars type) for ( ; pp->next; pp = pp->next) /* Scan ahead. */ ; - pp->next = &lines[i]; } tab->htab = htab; - return(tab); + return tab; } - -const char * -chars_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz) +int +mchars_spec2cp(const struct mchars *arg, const char *p, size_t sz) { + const struct ln *ln; - return(find((struct tbl *)arg, p, sz, rsz, CHARS_CHAR)); + ln = find(arg, p, sz); + return ln != NULL ? ln->unicode : sz == 1 ? (unsigned char)*p : -1; } - -const char * -chars_a2res(void *arg, const char *p, size_t sz, size_t *rsz) +int +mchars_num2char(const char *p, size_t sz) { + int i; - return(find((struct tbl *)arg, p, sz, rsz, CHARS_STRING)); + i = mandoc_strntoi(p, sz, 10); + return i >= 0 && i < 256 ? i : -1; } - -static const char * -find(struct tbl *tab, const char *p, size_t sz, size_t *rsz, int type) +int +mchars_num2uc(const char *p, size_t sz) { - struct ln *pp, *prev; - struct ln **htab; - int hash; + int i; - assert(p); - assert(sz > 0); + i = mandoc_strntoi(p, sz, 16); + assert(i >= 0 && i <= 0x10FFFF); + return i; +} - if (p[0] < ASCII_PRINT_LO || p[0] > ASCII_PRINT_HI) - return(NULL); +const char * +mchars_spec2str(const struct mchars *arg, + const char *p, size_t sz, size_t *rsz) +{ + const struct ln *ln; - /* - * Lookup the symbol in the symbol hash. See ascii2htab for the - * hashtable specs. This dynamically re-orders the hash chain - * to optimise for repeat hits. - */ - - hash = (int)p[0] - ASCII_PRINT_LO; - htab = tab->htab; - - if (NULL == (pp = htab[hash])) - return(NULL); - - if (NULL == pp->next) { - if ( ! match(pp, p, sz, type)) - return(NULL); - *rsz = pp->outsz; - return(pp->out); + ln = find(arg, p, sz); + if (ln == NULL) { + *rsz = 1; + return sz == 1 ? p : NULL; } - for (prev = NULL; pp; pp = pp->next) { - if ( ! match(pp, p, sz, type)) { - prev = pp; - continue; - } + *rsz = strlen(ln->ascii); + return ln->ascii; +} - /* Re-order the hash chain. */ +const char * +mchars_uc2str(int uc) +{ + int i; - if (prev) { - prev->next = pp->next; - pp->next = htab[hash]; - htab[hash] = pp; - } + for (i = 0; i < LINES_MAX; i++) + if (uc == lines[i].unicode) + return lines[i].ascii; + return ""; +} - *rsz = pp->outsz; - return(pp->out); - } +static const struct ln * +find(const struct mchars *tab, const char *p, size_t sz) +{ + const struct ln *pp; + int hash; - return(NULL); -} + assert(p); + if (0 == sz || p[0] < PRINT_LO || p[0] > PRINT_HI) + return NULL; -static inline int -match(const struct ln *ln, const char *p, size_t sz, int type) -{ + hash = (int)p[0] - PRINT_LO; - if ( ! (ln->type & type)) - return(0); - if (ln->codesz != sz) - return(0); - return(0 == strncmp(ln->code, p, sz)); + for (pp = tab->htab[hash]; pp; pp = pp->next) + if (0 == strncmp(pp->code, p, sz) && + '\0' == pp->code[(int)sz]) + return pp; + + return NULL; }