6 //compare dict_entries by first letter ascending, then by length descending
7 static int compar_dict_entry(const void *ap, const void *bp) {
8 const struct dict_entry *a=ap, *b=bp;
9 unsigned int first_a = (unsigned int)a->str[0];
10 unsigned int first_b = (unsigned int)b->str[0];
11 if (first_a < first_b)
13 else if (first_a > first_b)
16 size_t len_a = strlen(a->str);
17 size_t len_b = strlen(b->str);
20 else if (len_a < len_b)
27 struct dict *dict_build(void *ctx, const struct dict_entry *entries, size_t count) {
28 struct dict *dict = talloc_zero(ctx, struct dict);
29 struct dict_entry *ent;
35 ent = talloc_array(dict, struct dict_entry, count);
36 memcpy(ent, entries, count*sizeof(struct dict_entry));
37 qsort(ent, count, sizeof(*ent), compar_dict_entry);
43 if (count && ent->str[0]==0) {
44 fprintf(stderr, "dict_entry array contains multiple empty strings\n");
49 for (i=1; i<256; i++) {
52 if (ent->str[0] == (char)i)
53 dict->by_first_letter[i-1] = ent;
54 while (count && ent->str[0] == (char)i)
61 struct dict_entry *dict_lookup(struct dict *dict, const char **sp, const char *e) {
62 struct dict_entry *de;
66 first = (unsigned int)**sp & 0xFF;
74 de = dict->by_first_letter[first-1];
78 for (;de->str[0]==(char)first; de++) {
80 const char *ds = de->str;