composed: insert: require key to be unique

This commit is contained in:
Daniel Eklöf 2021-06-24 19:12:25 +02:00
parent 4a6dea04c2
commit f20956ff1b
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
3 changed files with 9 additions and 15 deletions

View file

@ -20,14 +20,14 @@ composed_lookup(struct composed *root, uint32_t key)
return NULL; return NULL;
} }
uint32_t void
composed_insert(struct composed **root, struct composed *node) composed_insert(struct composed **root, struct composed *node)
{ {
node->left = node->right = NULL; node->left = node->right = NULL;
if (*root == NULL) { if (*root == NULL) {
*root = node; *root = node;
return node->key; return;
} }
uint32_t key = node->key; uint32_t key = node->key;
@ -36,10 +36,7 @@ composed_insert(struct composed **root, struct composed *node)
struct composed *n = *root; struct composed *n = *root;
while (n != NULL) { while (n != NULL) {
if (n->key == node->key) { xassert(n->key != node->key);
/* TODO: wrap around at (CELL_COMB_CHARS_HI - CELL_COMB_CHARS_LO) */
key++;
}
prev = n; prev = n;
n = key < n->key ? n->left : n->right; n = key < n->key ? n->left : n->right;
@ -48,9 +45,6 @@ composed_insert(struct composed **root, struct composed *node)
xassert(prev != NULL); xassert(prev != NULL);
xassert(n == NULL); xassert(n == NULL);
/* May have been changed */
node->key = key;
if (key < prev->key) { if (key < prev->key) {
xassert(prev->left == NULL); xassert(prev->left == NULL);
prev->left = node; prev->left = node;
@ -58,8 +52,6 @@ composed_insert(struct composed **root, struct composed *node)
xassert(prev->right == NULL); xassert(prev->right == NULL);
prev->right = node; prev->right = node;
} }
return key;
} }
void void

View file

@ -13,6 +13,6 @@ struct composed {
}; };
struct composed *composed_lookup(struct composed *root, uint32_t key); struct composed *composed_lookup(struct composed *root, uint32_t key);
uint32_t composed_insert(struct composed **root, struct composed *node); void composed_insert(struct composed **root, struct composed *node);
void composed_free(struct composed *root); void composed_free(struct composed *root);

8
vt.c
View file

@ -761,11 +761,13 @@ action_utf8_print(struct terminal *term, wchar_t wc)
new_cc->width = grapheme_width; new_cc->width = grapheme_width;
term->composed_count++; term->composed_count++;
key = composed_insert(&term->composed, new_cc); composed_insert(&term->composed, new_cc);
wc = CELL_COMB_CHARS_LO + key;
xassert(wc <= CELL_COMB_CHARS_HI);
wc = CELL_COMB_CHARS_LO + key;
width = grapheme_width; width = grapheme_width;
xassert(wc >= CELL_COMB_CHARS_LO);
xassert(wc <= CELL_COMB_CHARS_HI);
goto out; goto out;
} }
} }