1 /* $Vendor-Id: chars.c,v 1.13 2009/11/05 07:21:01 kristaps Exp $ */
3 * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
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.
36 #define CHARS_CHAR (1 << 0)
37 #define CHARS_STRING (1 << 1)
38 #define CHARS_BOTH (CHARS_CHAR | CHARS_STRING)
43 #define CHAR(w, x, y, z, a, b) \
44 { NULL, (w), (y), (a), (x), (z), (b), CHARS_CHAR },
45 #define STRING(w, x, y, z, a, b) \
46 { NULL, (w), (y), (a), (x), (z), (b), CHARS_STRING },
47 #define BOTH(w, x, y, z, a, b) \
48 { NULL, (w), (y), (a), (x), (z), (b), CHARS_BOTH },
50 #define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
51 #define CHAR_TBL_END };
60 static inline int match(const struct ln
*,
61 const char *, size_t, int);
62 static const char *find(struct tbl
*, const char *,
63 size_t, size_t *, int);
71 tab
= (struct tbl
*)arg
;
79 chars_init(enum chars type
)
87 * Constructs a very basic chaining hashtable. The hash routine
88 * is simply the integral value of the first character.
89 * Subsequent entries are chained in the order they're processed
90 * (they're in-line re-ordered during lookup).
93 tab
= malloc(sizeof(struct tbl
));
99 htab
= calloc(PRINT_HI
- PRINT_LO
+ 1, sizeof(struct ln
**));
105 for (i
= 0; i
< LINES_MAX
; i
++) {
106 hash
= (int)lines
[i
].code
[0] - PRINT_LO
;
108 if (NULL
== (pp
= htab
[hash
])) {
109 htab
[hash
] = &lines
[i
];
113 for ( ; pp
->next
; pp
= pp
->next
)
115 pp
->next
= &lines
[i
];
125 chars_a2ascii(void *arg
, const char *p
, size_t sz
, size_t *rsz
)
128 return(find((struct tbl
*)arg
, p
, sz
, rsz
, CHARS_CHAR
));
133 chars_a2res(void *arg
, const char *p
, size_t sz
, size_t *rsz
)
136 return(find((struct tbl
*)arg
, p
, sz
, rsz
, CHARS_STRING
));
141 find(struct tbl
*tab
, const char *p
, size_t sz
, size_t *rsz
, int type
)
143 struct ln
*pp
, *prev
;
150 if (p
[0] < PRINT_LO
|| p
[0] > PRINT_HI
)
154 * Lookup the symbol in the symbol hash. See ascii2htab for the
155 * hashtable specs. This dynamically re-orders the hash chain
156 * to optimise for repeat hits.
159 hash
= (int)p
[0] - PRINT_LO
;
162 if (NULL
== (pp
= htab
[hash
]))
165 if (NULL
== pp
->next
) {
166 if ( ! match(pp
, p
, sz
, type
))
169 if (CHARS_HTML
== tab
->type
) {
177 for (prev
= NULL
; pp
; pp
= pp
->next
) {
178 if ( ! match(pp
, p
, sz
, type
)) {
184 prev
->next
= pp
->next
;
185 pp
->next
= htab
[hash
];
189 if (CHARS_HTML
== tab
->type
) {
202 match(const struct ln
*ln
, const char *p
, size_t sz
, int type
)
205 if ( ! (ln
->type
& type
))
207 if (ln
->codesz
!= sz
)
209 return(0 == strncmp(ln
->code
, p
, sz
));