jdict

command line tool for looking up terms in yomidict dictionaries
git clone anongit@rnpnr.xyz:jdict.git
Log | Files | Refs | Feed | README | LICENSE

Commit: e641117a4c5d018ee71882649fb97f5a9c8db637
Parent: 688bafd77960cfb1c4e9887a1b701d09f2617dea
Author: Randy Palamar
Date:   Sat, 22 Oct 2022 18:55:21 -0600

dedup(): simplify loop condition and ensure j doesn't exceed nents

Diffstat:
Mjdict.c | 20+++++++-------------
1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/jdict.c b/jdict.c @@ -73,20 +73,14 @@ dedup(DictEnt *ents, size_t *nents) size_t i, j, len = 0; DictEnt *dents = xreallocarray(NULL, *nents, sizeof(DictEnt)); - for (i = 0; i < *nents - 1; i++) { - if (!entcmp(&ents[i], &ents[i+1])) { - for (j = i+1; !entcmp(&ents[i], &ents[j]); j++) { - merge_ents(&ents[i], &ents[j]); - /* don't leak memory after merging */ - free(ents[j].term); - free(ents[j].defs); - } - memcpy(&dents[len++], &ents[i], sizeof(DictEnt)); - /* skip over duplicates */ - i = j; - } else { - memcpy(&dents[len++], &ents[i], sizeof(DictEnt)); + for (i = 0; i < *nents - 1; i = j) { + for (j = i+1; j < *nents && !entcmp(&ents[i], &ents[j]); j++) { + merge_ents(&ents[i], &ents[j]); + /* don't leak memory after merging */ + free(ents[j].term); + free(ents[j].defs); } + memcpy(&dents[len++], &ents[i], sizeof(DictEnt)); } /* move last ent if it wasn't a duplicate */ if (i + 1 < *nents)