composed: refactor: break out lookup with collision detection

This commit is contained in:
Daniel Eklöf 2025-01-24 14:15:01 +01:00
parent 1181f74d19
commit e248e73753
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 54 additions and 53 deletions

View file

@ -51,7 +51,7 @@ UNITTEST
xassert(k3 == k4);
}
struct composed *
const struct composed *
composed_lookup(struct composed *root, uint32_t key)
{
struct composed *node = root;
@ -66,6 +66,41 @@ composed_lookup(struct composed *root, uint32_t key)
return NULL;
}
const struct composed *
composed_lookup_without_collision(struct composed *root, uint32_t *key,
const char32_t *prefix_text, size_t prefix_len,
char32_t wc, int forced_width)
{
while (true) {
const struct composed *cc = composed_lookup(root, *key);
if (cc == NULL)
return NULL;
bool match = cc->count == prefix_len + 1 &&
cc->forced_width == forced_width &&
cc->chars[prefix_len] == wc;
if (match) {
for (size_t i = 0; i < prefix_len; i++) {
if (cc->chars[i] != prefix_text[i]) {
match = false;
break;
}
}
}
if (match)
return cc;
(*key)++;
*key &= CELL_COMB_CHARS_HI - CELL_COMB_CHARS_LO;
/* TODO: this will loop infinitly if the composed table is full */
}
return NULL;
}
void
composed_insert(struct composed **root, struct composed *node)
{