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

Annotation of mandoc/man_hash.c, Revision 1.33

1.33    ! schwarze    1: /*     $Id: man_hash.c,v 1.32 2015/04/18 17:01:58 schwarze Exp $ */
1.1       kristaps    2: /*
1.23      schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.7       kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.7       kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.16      kristaps   17: #include "config.h"
                     18:
1.15      kristaps   19: #include <sys/types.h>
                     20:
1.5       kristaps   21: #include <assert.h>
1.17      kristaps   22: #include <ctype.h>
1.12      kristaps   23: #include <limits.h>
1.1       kristaps   24: #include <string.h>
                     25:
1.30      schwarze   26: #include "roff.h"
1.24      kristaps   27: #include "man.h"
1.1       kristaps   28: #include "libman.h"
                     29:
1.17      kristaps   30: #define        HASH_DEPTH       6
                     31:
                     32: #define        HASH_ROW(x) do { \
1.25      kristaps   33:                if (isupper((unsigned char)(x))) \
1.17      kristaps   34:                        (x) -= 65; \
                     35:                else \
                     36:                        (x) -= 97; \
                     37:                (x) *= HASH_DEPTH; \
                     38:        } while (/* CONSTCOND */ 0)
                     39:
                     40: /*
                     41:  * Lookup table is indexed first by lower-case first letter (plus one
                     42:  * for the period, which is stored in the last row), then by lower or
                     43:  * uppercase second letter.  Buckets correspond to the index of the
                     44:  * macro (the integer value of the enum stored as a char to save a bit
                     45:  * of space).
                     46:  */
1.25      kristaps   47: static unsigned char    table[26 * HASH_DEPTH];
1.1       kristaps   48:
1.27      schwarze   49:
1.12      kristaps   50: void
                     51: man_hash_init(void)
1.1       kristaps   52: {
1.5       kristaps   53:        int              i, j, x;
1.32      schwarze   54:
                     55:        if (*table != '\0')
                     56:                return;
1.5       kristaps   57:
1.12      kristaps   58:        memset(table, UCHAR_MAX, sizeof(table));
1.5       kristaps   59:
1.19      kristaps   60:        for (i = 0; i < (int)MAN_MAX; i++) {
1.5       kristaps   61:                x = man_macronames[i][0];
                     62:
1.25      kristaps   63:                assert(isalpha((unsigned char)x));
1.5       kristaps   64:
1.17      kristaps   65:                HASH_ROW(x);
                     66:
                     67:                for (j = 0; j < HASH_DEPTH; j++)
1.12      kristaps   68:                        if (UCHAR_MAX == table[x + j]) {
1.25      kristaps   69:                                table[x + j] = (unsigned char)i;
1.5       kristaps   70:                                break;
                     71:                        }
1.17      kristaps   72:
                     73:                assert(j < HASH_DEPTH);
1.5       kristaps   74:        }
1.1       kristaps   75: }
1.17      kristaps   76:
1.31      schwarze   77: int
1.12      kristaps   78: man_hash_find(const char *tmp)
1.1       kristaps   79: {
1.17      kristaps   80:        int              x, y, i;
1.31      schwarze   81:        int              tok;
1.2       kristaps   82:
1.17      kristaps   83:        if ('\0' == (x = tmp[0]))
1.33    ! schwarze   84:                return(TOKEN_NONE);
1.25      kristaps   85:        if ( ! (isalpha((unsigned char)x)))
1.33    ! schwarze   86:                return(TOKEN_NONE);
1.5       kristaps   87:
1.17      kristaps   88:        HASH_ROW(x);
1.5       kristaps   89:
1.17      kristaps   90:        for (i = 0; i < HASH_DEPTH; i++) {
                     91:                if (UCHAR_MAX == (y = table[x + i]))
1.33    ! schwarze   92:                        return(TOKEN_NONE);
1.17      kristaps   93:
1.31      schwarze   94:                tok = y;
1.5       kristaps   95:                if (0 == strcmp(tmp, man_macronames[tok]))
                     96:                        return(tok);
                     97:        }
1.1       kristaps   98:
1.33    ! schwarze   99:        return(TOKEN_NONE);
1.1       kristaps  100: }

CVSweb