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

Annotation of mandoc/man_hash.c, Revision 1.17

1.17    ! kristaps    1: /*     $Id: man_hash.c,v 1.16 2010/01/01 17:14:28 kristaps Exp $ */
1.1       kristaps    2: /*
1.8       kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.15      kristaps   21: #include <sys/types.h>
                     22:
1.5       kristaps   23: #include <assert.h>
1.17    ! kristaps   24: #include <ctype.h>
1.12      kristaps   25: #include <limits.h>
1.1       kristaps   26: #include <stdlib.h>
                     27: #include <string.h>
                     28:
                     29: #include "libman.h"
                     30:
1.17    ! kristaps   31: #define        HASH_DEPTH       6
        !            32:
        !            33: #define        HASH_ROW(x) do { \
        !            34:                if ('.' == (x)) \
        !            35:                        (x) = 26; \
        !            36:                else if (isupper((u_char)(x))) \
        !            37:                        (x) -= 65; \
        !            38:                else \
        !            39:                        (x) -= 97; \
        !            40:                (x) *= HASH_DEPTH; \
        !            41:        } while (/* CONSTCOND */ 0)
        !            42:
        !            43: /*
        !            44:  * Lookup table is indexed first by lower-case first letter (plus one
        !            45:  * for the period, which is stored in the last row), then by lower or
        !            46:  * uppercase second letter.  Buckets correspond to the index of the
        !            47:  * macro (the integer value of the enum stored as a char to save a bit
        !            48:  * of space).
        !            49:  */
        !            50: static u_char           table[27 * HASH_DEPTH];
1.1       kristaps   51:
1.14      kristaps   52: /*
                     53:  * XXX - this hash has global scope, so if intended for use as a library
                     54:  * with multiple callers, it will need re-invocation protection.
                     55:  */
1.12      kristaps   56: void
                     57: man_hash_init(void)
1.1       kristaps   58: {
1.5       kristaps   59:        int              i, j, x;
                     60:
1.12      kristaps   61:        memset(table, UCHAR_MAX, sizeof(table));
1.5       kristaps   62:
1.17    ! kristaps   63:        assert(/* CONSTCOND */ MAN_MAX < UCHAR_MAX);
        !            64:
1.9       kristaps   65:        for (i = 0; i < MAN_MAX; i++) {
1.5       kristaps   66:                x = man_macronames[i][0];
                     67:
1.17    ! kristaps   68:                assert(isalpha((u_char)x) || '.' == x);
1.5       kristaps   69:
1.17    ! kristaps   70:                HASH_ROW(x);
        !            71:
        !            72:                for (j = 0; j < HASH_DEPTH; j++)
1.12      kristaps   73:                        if (UCHAR_MAX == table[x + j]) {
1.13      kristaps   74:                                table[x + j] = (u_char)i;
1.5       kristaps   75:                                break;
                     76:                        }
1.17    ! kristaps   77:
        !            78:                assert(j < HASH_DEPTH);
1.5       kristaps   79:        }
1.1       kristaps   80: }
                     81:
1.17    ! kristaps   82:
        !            83: enum mant
1.12      kristaps   84: man_hash_find(const char *tmp)
1.1       kristaps   85: {
1.17    ! kristaps   86:        int              x, y, i;
        !            87:        enum mant        tok;
1.2       kristaps   88:
1.17    ! kristaps   89:        if ('\0' == (x = tmp[0]))
1.5       kristaps   90:                return(MAN_MAX);
1.17    ! kristaps   91:        if ( ! (isalpha((u_char)x) || '.' == x))
1.5       kristaps   92:                return(MAN_MAX);
                     93:
1.17    ! kristaps   94:        HASH_ROW(x);
1.5       kristaps   95:
1.17    ! kristaps   96:        for (i = 0; i < HASH_DEPTH; i++) {
        !            97:                if (UCHAR_MAX == (y = table[x + i]))
1.5       kristaps   98:                        return(MAN_MAX);
1.17    ! kristaps   99:
        !           100:                tok = (enum mant)y;
1.5       kristaps  101:                if (0 == strcmp(tmp, man_macronames[tok]))
                    102:                        return(tok);
                    103:        }
1.1       kristaps  104:
                    105:        return(MAN_MAX);
                    106: }

CVSweb