dict: lookups take a trie; a prefix search is its own function; no self-key rule

dictlookup took a Lang for two reasons that were not its business: to
know whether to walk below the key (the emoji dictionary) and to drop a
candidate equal to the key except there.  No dictionary lists its key
among its candidates — the rule was left from a first version that put
the reading first itself — so it goes, and the walk is dictprefix(),
called by the emoji search alone.
This commit is contained in:
2026-08-17 01:31:51 +09:00
parent 3d862bdc0d
commit 5faefd9a87
6 changed files with 59 additions and 51 deletions

View File

@@ -15,10 +15,10 @@ dictionary_candidates(struct ct *t)
lang = getlang(LangJP);
saved = lang->dict;
lang->dict = trienew();
trieput(lang->dict, "かな", strlen("かな"), " 候補1 かな 候補2 ",
strlen(" 候補1 かな 候補2 "));
trieput(lang->dict, "かな", strlen("かな"), " 候補1 候補2 ",
strlen(" 候補1 候補2 "));
key = mkstr("かな");
n = dictlookup(lang, &key, kouho, Maxkouho);
n = dictlookup(lang->dict, &key, kouho, Maxkouho);
if(CT_EQ_INT(t, 2, n)){
checkstr(t, "candidate 1", "候補1", &kouho[0]);
checkstr(t, "candidate 2", "候補2", &kouho[1]);
@@ -34,13 +34,13 @@ dictionary_candidates(struct ct *t)
*p = '\0';
trieput(lang->dict, "key", 3, many, strlen(many));
key = mkstr("key");
n = dictlookup(lang, &key, kouho, Maxkouho);
n = dictlookup(lang->dict, &key, kouho, Maxkouho);
if(CT_EQ_INT(t, Maxkouho, n)){
checkstr(t, "first capped candidate", "c00", &kouho[0]);
snprint(item, sizeof item, "c%02d", Maxkouho-1);
checkstr(t, "last capped candidate", item, &kouho[Maxkouho-1]);
}
CT_EQ_INT(t, 3, dictlookup(lang, &key, kouho, 3));
CT_EQ_INT(t, 3, dictlookup(lang->dict, &key, kouho, 3));
trieclose(lang->dict);
lang->dict = saved;
}
@@ -56,28 +56,33 @@ dictionary_misses(struct ct *t)
saved = lang->dict;
lang->dict = trienew();
key = mkstr("");
CT_EQ_INT(t, 0, dictlookup(lang, &key, kouho, Maxkouho));
CT_EQ_INT(t, 0, dictlookup(lang->dict, &key, kouho, Maxkouho));
key = mkstr("missing");
CT_EQ_INT(t, 0, dictlookup(lang, &key, kouho, Maxkouho));
CT_EQ_INT(t, 0, dictlookup(getlang(LangKO), &key, kouho, Maxkouho));
CT_EQ_INT(t, 0, dictlookup(lang->dict, &key, kouho, Maxkouho));
CT_EQ_INT(t, 0, dictlookup(getlang(LangKO)->dict, &key, kouho, Maxkouho));
trieclose(lang->dict);
lang->dict = saved;
}
void
dictionary_emoji_identity(struct ct *t)
dictionary_prefix(struct ct *t)
{
Str kouho[Maxkouho], key;
Trie *saved;
Lang *lang;
Trie *dict;
lang = getlang(LangEMOJI);
saved = lang->dict;
lang->dict = trienew();
trieput(lang->dict, "é", strlen("é"), "é", strlen("é"));
key = mkstr("é");
if(CT_EQ_INT(t, 1, dictlookup(lang, &key, kouho, Maxkouho)))
checkstr(t, "emoji identity candidate", "é", &kouho[0]);
trieclose(lang->dict);
lang->dict = saved;
dict = trienew();
trieput(dict, "smile", 5, "A B", 3);
trieput(dict, "smiley", 6, "B C", 3);
trieput(dict, "sad", 3, "D", 1);
key = mkstr("smile");
if(CT_EQ_INT(t, 4, dictprefix(dict, &key, kouho, Maxkouho))){
checkstr(t, "own entry first", "A", &kouho[0]);
checkstr(t, "entry below", "C", &kouho[3]);
}
key = mkstr("s");
CT_EQ_INT(t, 5, dictprefix(dict, &key, kouho, Maxkouho));
CT_EQ_INT(t, 2, dictprefix(dict, &key, kouho, 2));
key = mkstr("x");
CT_EQ_INT(t, 0, dictprefix(dict, &key, kouho, Maxkouho));
trieclose(dict);
}

View File

@@ -95,7 +95,7 @@ void engine_randomized_stress(struct ct*);
void engine_full_boundary_passthrough(struct ct*);
void dictionary_candidates(struct ct*);
void dictionary_misses(struct ct*);
void dictionary_emoji_identity(struct ct*);
void dictionary_prefix(struct ct*);
void ipc_masks_modifiers(struct ct*);
void ipc_control_and_caret_frames(struct ct*);
void ipc_runtime_path(struct ct*);

View File

@@ -118,7 +118,7 @@ static const struct ct_test tests[] = {
{ "engine/full-boundary-passthrough", engine_full_boundary_passthrough },
{ "dict/candidates", dictionary_candidates },
{ "dict/misses", dictionary_misses },
{ "dict/emoji-identity", dictionary_emoji_identity },
{ "dict/prefix", dictionary_prefix },
{ "ipc/masks-modifiers", ipc_masks_modifiers },
{ "ipc/control-caret-frames", ipc_control_and_caret_frames },
{ "ipc/runtime-path", ipc_runtime_path },