Skip to content

Commit 6a67a62

Browse files
committed
FIXED: Fix error when pruning tokens
1 parent 070c99b commit 6a67a62

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Sources/libMultiMarkdown/token.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,28 @@ void token_remove_tail(token * head) {
279279
}
280280

281281

282+
/// Fix tail at head of token chain (e.g. after pruning)
283+
void fix_token_chain_tail(token * t) {
284+
if (t) {
285+
token * head = t;
286+
287+
// Find head of chain
288+
while (head->prev) {
289+
head = head->prev;
290+
}
291+
292+
// Find tail
293+
while (t->next) {
294+
t = t->next;
295+
}
296+
297+
// Fix tail
298+
head->tail = t;
299+
}
300+
}
301+
282302
/// Pop token out of it's chain, connecting head and tail of chain back together.
283303
/// Token must be freed if it is no longer needed.
284-
/// \todo: If t is the tail token of a chain, the tail is no longer correct on the start of chain.
285304
void token_pop_link_from_chain(token * t) {
286305
if (t == NULL) {
287306
return;
@@ -296,6 +315,8 @@ void token_pop_link_from_chain(token * t) {
296315

297316
if (prev) {
298317
prev->next = next;
318+
319+
fix_token_chain_tail(prev);
299320
}
300321

301322
if (next) {
@@ -315,6 +336,8 @@ void tokens_prune(token * first, token * last) {
315336

316337
if (prev != NULL) {
317338
prev->next = next;
339+
340+
fix_token_chain_tail(prev);
318341
}
319342

320343
if (next != NULL) {

0 commit comments

Comments
 (0)