1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains the main loop of the Zephyr server
4 * Created by: Lucien W. Van Elsen
8 * Copyright (c) 1991 by the Massachusetts Institute of Technology.
9 * For copying and distribution information, see the file
13 #include <zephyr/mit-copyright.h>
18 static const char rcsid_zstring_c[] =
23 static String *zhash[STRING_HASH_TABLE_SIZE];
25 int valid_utf8_p(const char* s)
30 while ((len = utf8proc_iterate((const unsigned char *)s, -1, &uc))) {
31 if (len <=0) return 0; /* Not valid UTF-8 encoding. */
32 if (!(utf8proc_codepoint_valid(uc))) return 0; /* Not valid unicode codepoint. */
33 if (uc == 0) return 1; /* NULL, we're done. */
36 return 0; /* We shouldn't get here. */
39 static char *zdowncase(const char* s)
43 if (valid_utf8_p(s)) {
44 /* Use utf8proc if we're dealing with UTF-8.
45 * Rather than downcase, casefold and normalize to NFKC.
47 utf8proc_map((const unsigned char *)s, 0, (unsigned char **)&new_s,
48 UTF8PROC_NULLTERM | UTF8PROC_STABLE
49 | UTF8PROC_CASEFOLD | UTF8PROC_COMPAT
52 /* If not, fall back to old methods. */
56 if (isascii(*p) && isupper(*p))
78 new_z = find_string(new_s,0);
86 /* Initialize new String */
90 new_z = (String *) malloc(sizeof(String));
91 new_z->string = new_s;
94 /* Add to beginning of hash table */
95 new_z->hash_val = hash(new_s);
96 i = new_z->hash_val % STRING_HASH_TABLE_SIZE;
108 free_string(String *z)
110 if (z == (String *) NULL)
114 if (z->ref_count > 0)
117 /* delete string completely */
119 zhash[hash(z->string) % STRING_HASH_TABLE_SIZE] = z->next;
121 z->prev->next = z->next;
124 z->next->prev = z->prev;
138 new_s = zdowncase(s);
143 z = zhash[hash(new_s) % STRING_HASH_TABLE_SIZE];
145 if (strcmp(new_s, z->string) == 0)
157 comp_string(String *a,
160 if (a->hash_val > b->hash_val)
162 if (a->hash_val < b->hash_val)
164 return strcmp(a->string,b->string);
168 print_string_table(FILE *f)
173 for(i = 0; i < STRING_HASH_TABLE_SIZE; i++) {
175 while (p != (String *) NULL) {
176 fprintf(f,"[%d] %s\n",p->ref_count,p->string);
183 dup_string(String *z)