Commit: 688bafd77960cfb1c4e9887a1b701d09f2617dea
Parent: 56d26f751375f6b5cb0ce7c24dedf84d750f83a6
Author: Randy Palamar
Date: Sat, 22 Oct 2022 14:09:16 -0600
handle more than a single consecutive duplicate correctly
Diffstat:
1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/jdict.c b/jdict.c
@@ -70,17 +70,20 @@ merge_ents(DictEnt *a, DictEnt *b)
static DictEnt *
dedup(DictEnt *ents, size_t *nents)
{
- size_t i, len = 0;
+ 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])) {
- /* merge and copy then skip the next ent */
- merge_ents(&ents[i], &ents[i+1]);
- memcpy(&dents[len++], &ents[i++], sizeof(DictEnt));
- /* don't leak memory after merging */
- free(ents[i].term);
- free(ents[i].defs);
+ 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));
}