[BACK]Return to man_hash.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/man_hash.c, Revision 1.34

1.34    ! schwarze    1: /*     $Id: man_hash.c,v 1.33 2015/04/19 14:00:19 schwarze Exp $ */
1.1       kristaps    2: /*
1.23      schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.34    ! schwarze    4:  * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.7       kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.7       kristaps   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.
1.1       kristaps   17:  */
1.16      kristaps   18: #include "config.h"
                     19:
1.15      kristaps   20: #include <sys/types.h>
                     21:
1.5       kristaps   22: #include <assert.h>
1.17      kristaps   23: #include <ctype.h>
1.12      kristaps   24: #include <limits.h>
1.1       kristaps   25: #include <string.h>
                     26:
1.30      schwarze   27: #include "roff.h"
1.24      kristaps   28: #include "man.h"
1.1       kristaps   29: #include "libman.h"
                     30:
1.17      kristaps   31: #define        HASH_DEPTH       6
                     32:
                     33: #define        HASH_ROW(x) do { \
1.25      kristaps   34:                if (isupper((unsigned char)(x))) \
1.17      kristaps   35:                        (x) -= 65; \
                     36:                else \
                     37:                        (x) -= 97; \
                     38:                (x) *= HASH_DEPTH; \
                     39:        } while (/* CONSTCOND */ 0)
                     40:
                     41: /*
                     42:  * Lookup table is indexed first by lower-case first letter (plus one
                     43:  * for the period, which is stored in the last row), then by lower or
                     44:  * uppercase second letter.  Buckets correspond to the index of the
                     45:  * macro (the integer value of the enum stored as a char to save a bit
                     46:  * of space).
                     47:  */
1.25      kristaps   48: static unsigned char    table[26 * HASH_DEPTH];
1.1       kristaps   49:
1.27      schwarze   50:
1.12      kristaps   51: void
                     52: man_hash_init(void)
1.1       kristaps   53: {
1.5       kristaps   54:        int              i, j, x;
1.32      schwarze   55:
                     56:        if (*table != '\0')
                     57:                return;
1.5       kristaps   58:
1.12      kristaps   59:        memset(table, UCHAR_MAX, sizeof(table));
1.5       kristaps   60:
1.19      kristaps   61:        for (i = 0; i < (int)MAN_MAX; i++) {
1.5       kristaps   62:                x = man_macronames[i][0];
                     63:
1.25      kristaps   64:                assert(isalpha((unsigned char)x));
1.5       kristaps   65:
1.17      kristaps   66:                HASH_ROW(x);
                     67:
                     68:                for (j = 0; j < HASH_DEPTH; j++)
1.12      kristaps   69:                        if (UCHAR_MAX == table[x + j]) {
1.25      kristaps   70:                                table[x + j] = (unsigned char)i;
1.5       kristaps   71:                                break;
                     72:                        }
1.17      kristaps   73:
                     74:                assert(j < HASH_DEPTH);
1.5       kristaps   75:        }
1.1       kristaps   76: }
1.17      kristaps   77:
1.31      schwarze   78: int
1.12      kristaps   79: man_hash_find(const char *tmp)
1.1       kristaps   80: {
1.17      kristaps   81:        int              x, y, i;
1.31      schwarze   82:        int              tok;
1.2       kristaps   83:
1.17      kristaps   84:        if ('\0' == (x = tmp[0]))
1.34    ! schwarze   85:                return TOKEN_NONE;
1.25      kristaps   86:        if ( ! (isalpha((unsigned char)x)))
1.34    ! schwarze   87:                return TOKEN_NONE;
1.5       kristaps   88:
1.17      kristaps   89:        HASH_ROW(x);
1.5       kristaps   90:
1.17      kristaps   91:        for (i = 0; i < HASH_DEPTH; i++) {
                     92:                if (UCHAR_MAX == (y = table[x + i]))
1.34    ! schwarze   93:                        return TOKEN_NONE;
1.17      kristaps   94:
1.31      schwarze   95:                tok = y;
1.5       kristaps   96:                if (0 == strcmp(tmp, man_macronames[tok]))
1.34    ! schwarze   97:                        return tok;
1.5       kristaps   98:        }
1.1       kristaps   99:
1.34    ! schwarze  100:        return TOKEN_NONE;
1.1       kristaps  101: }

CVSweb