1 /***********************************************************
2 * Copyright 1987, 1998 The Open Group
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * Except as contained in this notice, the name of The Open Group shall not be
21 * used in advertising or otherwise to promote the sale, use or other dealings
22 * in this Software without prior written authorization from The Open Group.
25 * Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
29 * Permission to use, copy, modify, and distribute this software and its
30 * documentation for any purpose and without fee is hereby granted,
31 * provided that the above copyright notice appear in all copies and that
32 * both that copyright notice and this permission notice appear in
33 * supporting documentation, and that the name of Digital not be
34 * used in advertising or publicity pertaining to distribution of the
35 * software without specific, written prior permission.
37 * DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
38 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
39 * DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
40 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
41 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
42 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45 ******************************************************************/
47 /************************************************************
48 * Copyright 1994 by Silicon Graphics Computer Systems, Inc.
50 * Permission to use, copy, modify, and distribute this
51 * software and its documentation for any purpose and without
52 * fee is hereby granted, provided that the above copyright
53 * notice appear in all copies and that both that copyright
54 * notice and this permission notice appear in supporting
55 * documentation, and that the name of Silicon Graphics not be
56 * used in advertising or publicity pertaining to distribution
57 * of the software without specific prior written permission.
58 * Silicon Graphics makes no representation about the suitability
59 * of this software for any purpose. It is provided "as is"
60 * without any express or implied warranty.
62 * SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
63 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
64 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
65 * GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
66 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
67 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
68 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
69 * THE USE OR PERFORMANCE OF THIS SOFTWARE.
71 ********************************************************/
77 xkb_atom_t left, right;
79 unsigned int fingerprint;
85 darray(struct atom_node) table;
91 struct atom_table *table;
93 table = calloc(1, sizeof(*table));
97 darray_init(table->table);
98 /* The original throw-away root is here, at the illegal atom 0. */
99 darray_resize0(table->table, 1);
105 atom_table_free(struct atom_table *table)
107 struct atom_node *node;
112 darray_foreach(node, table->table)
114 darray_free(table->table);
119 atom_text(struct atom_table *table, xkb_atom_t atom)
121 if (atom == XKB_ATOM_NONE || atom >= darray_size(table->table))
124 return darray_item(table->table, atom).string;
128 find_atom_pointer(struct atom_table *table, const char *string, size_t len,
129 xkb_atom_t **atomp_out, unsigned int *fingerprint_out)
131 xkb_atom_t *atomp = &table->root;
132 unsigned int fingerprint = 0;
135 for (size_t i = 0; i < (len + 1) / 2; i++) {
136 fingerprint = fingerprint * 27 + string[i];
137 fingerprint = fingerprint * 27 + string[len - 1 - i];
140 while (*atomp != XKB_ATOM_NONE) {
141 struct atom_node *node = &darray_item(table->table, *atomp);
143 if (fingerprint < node->fingerprint) {
146 else if (fingerprint > node->fingerprint) {
147 atomp = &node->right;
150 /* Now start testing the strings. */
151 const int cmp = strncmp(string, node->string, len);
152 if (cmp < 0 || (cmp == 0 && len < strlen(node->string))) {
156 atomp = &node->right;
166 *fingerprint_out = fingerprint;
173 atom_lookup(struct atom_table *table, const char *string, size_t len)
178 return XKB_ATOM_NONE;
180 if (!find_atom_pointer(table, string, len, &atomp, NULL))
181 return XKB_ATOM_NONE;
187 * If steal is true, we do not strdup @string; therefore it must be
188 * dynamically allocated, NUL-terminated, not be free'd by the caller
189 * and not be used afterwards. Use to avoid some redundant allocations.
192 atom_intern(struct atom_table *table, const char *string, size_t len,
196 struct atom_node node;
197 unsigned int fingerprint;
200 return XKB_ATOM_NONE;
202 if (find_atom_pointer(table, string, len, &atomp, &fingerprint)) {
204 free(UNCONSTIFY(string));
209 node.string = UNCONSTIFY(string);
212 node.string = strndup(string, len);
214 return XKB_ATOM_NONE;
217 node.left = node.right = XKB_ATOM_NONE;
218 node.fingerprint = fingerprint;
219 node.atom = darray_size(table->table);
220 /* Do this before the append, as it may realloc and change the offsets. */
222 darray_append(table->table, node);