src/mds_block.c

filter: Statementstmt 93.8% 1333 / 1421 Branchbran 74.7% 1323 / 1770 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.2% 2656 / 3191 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/mds_footnote.c ↑ Summary src/mds_inline.c »
1 /* mds_block.c — scalar CommonMark block scanner.
2 *
3 * Scope:
4 * - Thematic break (§4.1)
5 * - ATX heading (§4.2)
6 * - Fenced code (§4.5)
7 * - Paragraph (§4.8)
8 * - Block quote (§5.1) with lazy continuation
9 * - Bullet list + ordered list + list item (§5.2, §5.3)
10 *
11 * Inline content is emitted as raw text via cb->text; the inline
12 * tokeniser handles it downstream.
13 */
14  
15 #include "mds_block.h"
16 #include "mds_ir.h"
17 #include "mds_ctx.h"
18 #include "mds_arena.h"
19 #include "mds_linkref.h"
20 #include "mds_footnote.h"
21  
22 /* Forward decl — defined in mds_render_html.c which is unity-included
23 * AFTER this file. Used to emit the footnotes section in first-use
24 * order. NULL-safe in the unlikely case the caller installs a non-HTML
25 * renderer; iteration just stops at index 0. */
26 int mds_render_html_used_footnote(void* ud, size_t i,
27 const char** label_out,
28 size_t* label_len_out);
29 #include "mds_inline.h"
30 #include <string.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33  
34 #define MAX_DEPTH 1000
35  
36 typedef enum { CT_DOC, CT_QUOTE, CT_LIST, CT_LIST_ITEM } ct_kind;
37  
38 typedef struct {
39 ct_kind kind;
40 int ordered;
41 int start;
42 char marker;
43 int tight;
44 int had_blank_inside;
45 int pending_blank; /* CT_LIST only: a blank line was seen, awaiting next non-blank to decide loose-ness */
46 int content_col;
47 int opened;
48 int ev_idx; /* index of the buffered enter_block event */
49 int is_empty; /* CT_LIST_ITEM only: opened with no content */
50 int blank_after_empty; /* CT_LIST_ITEM only: blank line seen while still empty */
51 } ctn;
52  
53 typedef enum {
54 LF_NONE,
55 LF_PARAGRAPH,
56 LF_CODE_FENCED,
57 LF_CODE_INDENTED,
58 LF_HTML
59 } leaf_kind;
60  
61 typedef enum {
62 EV_ENTER_BLOCK,
63 EV_LEAVE_BLOCK,
64 EV_TEXT,
65 EV_RAW,
66 EV_INLINE /* bytes need mds_inline_scan at flush time */
67 } ev_type;
68  
69 typedef struct {
70 ev_type type;
71 union {
72 struct { mds_block_type t; mds_block_detail d; const char* info_ptr; size_t info_len; } enter;
73 struct { mds_block_type t; } leave;
74 struct { size_t off; size_t len; } bytes;
75 } u;
76 } ev_rec;
77  
78 typedef struct {
79 mds_ctx* ctx;
80 ctn stack[MAX_DEPTH];
81 int depth;
82 int list_depth; /* # CT_LIST containers currently on stack */
83 leaf_kind leaf;
84 int leaf_in_container;
85  
86 char* para;
87 size_t para_len;
88 size_t para_cap;
89  
90 char fence_char;
91 int fence_len;
92 int fence_indent;
93 char* fence_info;
94 size_t fence_info_len;
95 char* code_body;
96 size_t code_len;
97 size_t code_cap;
98 int pending_blanks;
99 int blank_pending; /* 1 = blank line seen, attribute to deepest surviving CT_LIST after next walk */
100 int setext_level;
101 int html_type; /* 1..7 when leaf == LF_HTML */
102 char* html_body;
103 size_t html_len;
104 size_t html_cap;
105  
106 /* Event buffer used while list_depth > 0 so we can patch is_tight on
107 * the LIST enter event once we know whether the list is loose. */
108 ev_rec* evbuf;
109 size_t ev_len;
110 size_t ev_cap;
111 char* bytepool;
112 size_t bp_len;
113 size_t bp_cap;
114 char* line_scratch; /* per-line buffer for tab-expanded line copy */
115 size_t line_scratch_cap;
116 } bscanner;
117  
118 /* ---------- heap buffer helpers ---------- */
119  
120 12210349 static void buf_grow(char** buf, size_t* cap, size_t need) {
121 size_t nc;
122 12210349 ▶ if (need <= *cap) return;
Branch: 1 branch
BranchTrueFalseTotal
if (need <= *cap) return; 11943094 267255 12210349
123 267255 ▶ nc = *cap ? *cap : 256;
Branch: 1 branch
BranchTrueFalseTotal
nc = *cap ? *cap : 256; 121229 146026 267255
124 388524 ▶ while (nc < need) nc = nc + (nc >> 1) + 64;
Branch: 1 branch
BranchTrueFalseTotal
while (nc < need) nc = nc + (nc >> 1) + 64; 121269 267255 388524
125 267255 *buf = (char*)realloc(*buf, nc);
126 267255 *cap = nc;
127 }
128 4892329 static void buf_append(char** buf, size_t* len, size_t* cap,
129 const char* s, size_t n) {
130 4892329 buf_grow(buf, cap, *len + n + 1);
131 4892329 memcpy(*buf + *len, s, n);
132 4892329 *len += n;
133 4892329 (*buf)[*len] = '\0';
134 4892329 }
135  
136 /* ---------- SAX dispatch (buffered while inside a list) ---------- */
137  
138 6124574 static size_t pool_intern(bscanner* b, const char* s, size_t n) {
139 size_t off;
140 6124574 buf_grow(&b->bytepool, &b->bp_cap, b->bp_len + n + 1);
141 6124574 off = b->bp_len;
142 6124574 ▶ if (n) memcpy(b->bytepool + off, s, n);
Branch: 1 branch
BranchTrueFalseTotal
if (n) memcpy(b->bytepool + off, s, n); 6124574 0 6124574
143 6124574 b->bp_len += n;
144 6124574 return off;
145 }
146 15953074 static ev_rec* ev_alloc(bscanner* b) {
147 15953074 ▶ if (b->ev_len == b->ev_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->ev_len == b->ev_cap) { 123514 15829560 15953074
148 123514 ▶ size_t nc = b->ev_cap ? b->ev_cap * 2 : 64;
Branch: 1 branch
BranchTrueFalseTotal
size_t nc = b->ev_cap ? b->ev_cap * 2 : 64; 60608 62906 123514
149 123514 b->evbuf = (ev_rec*)realloc(b->evbuf, nc * sizeof(ev_rec));
150 123514 b->ev_cap = nc;
151 }
152 15953074 return &b->evbuf[b->ev_len++];
153 }
154  
155 5318248 static int sax_enter(bscanner* b, mds_block_type t, const mds_block_detail* d) {
156 /* Always buffer at document level: defers inline_scan until linkref
157 * table is fully populated. Returns the event index. */
158 {
159 5318248 ev_rec* e = ev_alloc(b);
160 5318248 e->type = EV_ENTER_BLOCK;
161 5318248 e->u.enter.t = t;
162 5318248 ▶ e->u.enter.d = d ? *d : (mds_block_detail){0};
Branch: 1 branch
BranchTrueFalseTotal
e->u.enter.d = d ? *d : (mds_block_detail){0}; 5318248 0 5318248
163 5318248 e->u.enter.info_ptr = NULL;
164 5318248 e->u.enter.info_len = 0;
165 5318248 ▶ if (t == MDS_BLK_CODE_FENCED && d && d->u.code_fenced.info_len) {
Branch: 3 branches
BranchTrueFalseTotal
if (t == MDS_BLK_CODE_FENCED && d && d->u.code_fenced.info_len) { 808139 4510109 5318248
if (t == MDS_BLK_CODE_FENCED && d && d->u.code_fenced.info_len) { 808139 0 808139
if (t == MDS_BLK_CODE_FENCED && d && d->u.code_fenced.info_len) { 808043 96 808139
166 808043 size_t off = pool_intern(b, d->u.code_fenced.info, d->u.code_fenced.info_len);
167 808043 e->u.enter.info_ptr = (const char*)(uintptr_t)off;
168 808043 e->u.enter.info_len = d->u.code_fenced.info_len;
169 }
170 5318248 return (int)(b->ev_len - 1);
171 }
172 }
173 5318248 static void sax_leave(bscanner* b, mds_block_type t) {
174 5318248 ev_rec* e = ev_alloc(b);
175 5318248 e->type = EV_LEAVE_BLOCK;
176 5318248 e->u.leave.t = t;
177 5318248 }
178 808541 static void sax_text(bscanner* b, const char* s, size_t n) {
179 808541 size_t off = pool_intern(b, s, n);
180 808541 ev_rec* e = ev_alloc(b);
181 808541 e->type = EV_TEXT;
182 808541 e->u.bytes.off = off;
183 808541 e->u.bytes.len = n;
184 808541 }
185 200 static void sax_raw(bscanner* b, const char* s, size_t n) {
186 200 size_t off = pool_intern(b, s, n);
187 200 ev_rec* e = ev_alloc(b);
188 200 e->type = EV_RAW;
189 200 e->u.bytes.off = off;
190 200 e->u.bytes.len = n;
191 200 }
192  
193 4507790 static void sax_inline_text(bscanner* b, const char* s, size_t n) {
194 4507790 size_t off = pool_intern(b, s, n);
195 4507790 ev_rec* e = ev_alloc(b);
196 4507790 e->type = EV_INLINE;
197 4507790 e->u.bytes.off = off;
198 4507790 e->u.bytes.len = n;
199 4507790 }
200  
201 204525 static void sax_flush(bscanner* b) {
202 /* Hoist the callback table and bytepool base out of
203 * the per-event loop. The callbacks are guaranteed non-NULL by the
204 * HTML renderer setup, but the scanner has to remain generic, so
205 * we still null-check; MDS_LIKELY tells the compiler to fall
206 * through (the renderer is by far the most common consumer). */
207 204525 const mds_callbacks cb = b->ctx->cb;
208 204525 void* const ud = b->ctx->ud;
209 204525 const char* const pool = b->bytepool;
210 204525 const size_t n = b->ev_len;
211 204525 ev_rec* const evs = b->evbuf;
212 size_t i;
213 16157599 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 15953074 204525 16157599
214 15953074 ev_rec* e = &evs[i];
215 15953074 switch (e->type) {
216 5318248 case EV_ENTER_BLOCK:
217 5318248 ▶ if (e->u.enter.t == MDS_BLK_CODE_FENCED && e->u.enter.info_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (e->u.enter.t == MDS_BLK_CODE_FENCED && e->u.enter.info_len) { 808139 4510109 5318248
if (e->u.enter.t == MDS_BLK_CODE_FENCED && e->u.enter.info_len) { 808043 96 808139
218 808043 size_t off = (size_t)(uintptr_t)e->u.enter.info_ptr;
219 808043 e->u.enter.d.u.code_fenced.info = pool + off;
220 808043 e->u.enter.d.u.code_fenced.info_len = e->u.enter.info_len;
221 }
222 5318248 ▶ if (MDS_LIKELY(cb.enter_block != NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (MDS_LIKELY(cb.enter_block != NULL)) 5318248 0 5318248
223 5318248 cb.enter_block(ud, e->u.enter.t, &e->u.enter.d);
224 5318248 break;
225 5318248 case EV_LEAVE_BLOCK:
226 5318248 ▶ if (MDS_LIKELY(cb.leave_block != NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (MDS_LIKELY(cb.leave_block != NULL)) 5318248 0 5318248
227 5318248 cb.leave_block(ud, e->u.leave.t);
228 5318248 break;
229 808541 case EV_TEXT:
230 808541 ▶ if (MDS_LIKELY(cb.text != NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (MDS_LIKELY(cb.text != NULL)) 808541 0 808541
231 808541 cb.text(ud, pool + e->u.bytes.off, e->u.bytes.len);
232 808541 break;
233 200 case EV_RAW:
234 200 ▶ if (MDS_LIKELY(cb.raw != NULL))
Branch: 1 branch
BranchTrueFalseTotal
if (MDS_LIKELY(cb.raw != NULL)) 200 0 200
235 200 cb.raw(ud, pool + e->u.bytes.off, e->u.bytes.len);
236 200 break;
237 4507837 case EV_INLINE:
238 4507837 mds_inline_scan(b->ctx, pool + e->u.bytes.off, e->u.bytes.len);
239 4507837 break;
240 }
241 }
242 204525 b->ev_len = 0;
243 204525 b->bp_len = 0;
244 204525 }
245  
246 /* ---------- container helpers ---------- */
247  
248 2442 static ctn* top(bscanner* b) { return &b->stack[b->depth - 1]; }
249  
250 205676 static int push(bscanner* b, ct_kind k) {
251 ctn* c;
252 205676 ▶ if (b->depth >= MAX_DEPTH) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (b->depth >= MAX_DEPTH) return 0; 0 205676 205676
253 205676 c = &b->stack[b->depth++];
254 205676 memset(c, 0, sizeof *c);
255 205676 c->kind = k;
256 205676 c->tight = 1;
257 205676 c->ev_idx = -1;
258 205676 ▶ if (k == CT_LIST) b->list_depth++;
Branch: 1 branch
BranchTrueFalseTotal
if (k == CT_LIST) b->list_depth++; 379 205297 205676
259 205676 return 1;
260 }
261  
262 /* ---------- forward decls ---------- */
263  
264 static void emit_open(bscanner* b, int idx);
265 static void emit_close(bscanner* b, ctn* c);
266 static void finalize_leaf(bscanner* b);
267 static void close_containers_to(bscanner* b, int target_depth);
268  
269 /* ---------- link reference definition extractor ----------
270 *
271 * Tries to consume one definition starting at *p_in. Returns 1 if one was
272 * fully parsed, 0 otherwise. On success updates *p_in past the end of the
273 * definition.
274 *
275 * Grammar (simplified subset):
276 * ^ {0,3} '[' label ']' ':' ws+ url (ws+ title)? ws* \n?
277 * - label may contain anything but ']', '[', '\n' (no nesting).
278 * - url either <bracketed> with no '<>' inside, or a bareword with no
279 * spaces (parens balanced not enforced here).
280 * - title in "...", '...', or (...), possibly on next line.
281 */
282 4507987 static int parse_linkref(const char* p, const char* end, const char** p_out,
283 const char** lbl_s, const char** lbl_e,
284 const char** url_s, const char** url_e,
285 const char** tit_s, const char** tit_e) {
286 const char* q;
287 int lead;
288 const char* ls;
289 int label_nl;
290 const char* r;
291 const char* le;
292 int has_nonws;
293 int nl;
294 const char *us, *ue;
295 const char *ts, *te;
296 const char* save_after_url;
297 int saw_nl;
298 int saw_ws;
299 char open, close;
300 int blank_found;
301 const char* check;
302  
303 4507987 q = p;
304 4507987 lead = 0;
305 4507988 ▶ while (q < end && *q == ' ' && lead < 3) { q++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && *q == ' ' && lead < 3) { q++; lead++; } 4507988 0 4507988
while (q < end && *q == ' ' && lead < 3) { q++; lead++; } 1 4507987 4507988
while (q < end && *q == ' ' && lead < 3) { q++; lead++; } 1 0 1
306 4507987 ▶ if (q >= end || *q != '[') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (q >= end || *q != '[') return 0; 4507987 0 4507987
if (q >= end || *q != '[') return 0; 3699279 808708 4507987
307 808708 q++;
308 808708 ls = q;
309 /* Label may span multiple lines (no blank line). Allow \] inside. */
310 808708 label_nl = 0;
311 1619488 ▶ while (q < end && *q != ']') {
Branch: 2 branches
BranchTrueFalseTotal
while (q < end && *q != ']') { 1619488 0 1619488
while (q < end && *q != ']') { 810828 808660 1619488
312 810828 ▶ if (*q == '\\' && q + 1 < end) { q += 2; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (*q == '\\' && q + 1 < end) { q += 2; continue; } 21 810807 810828
if (*q == '\\' && q + 1 < end) { q += 2; continue; } 21 0 21
313 810807 ▶ if (*q == '[') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '[') return 0; 48 810759 810807
314 810759 ▶ if (*q == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '\n') { 15 810744 810759
315 15 ▶ if (++label_nl > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (++label_nl > 0) { 15 0 15
316 /* check for blank line */
317 15 r = q + 1;
318 15 ▶ while (r < end && (*r == ' ' || *r == '\t')) r++;
Branch: 3 branches
BranchTrueFalseTotal
while (r < end && (*r == ' ' || *r == '\t')) r++; 15 0 15
while (r < end && (*r == ' ' || *r == '\t')) r++; 0 15 15
while (r < end && (*r == ' ' || *r == '\t')) r++; 0 15 15
319 15 ▶ if (r >= end || *r == '\n') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (r >= end || *r == '\n') return 0; 15 0 15
if (r >= end || *r == '\n') return 0; 0 15 15
320 }
321 }
322 810759 q++;
323 }
324 808660 ▶ if (q >= end || *q != ']') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (q >= end || *q != ']') return 0; 808660 0 808660
if (q >= end || *q != ']') return 0; 0 808660 808660
325 808660 le = q;
326 808660 ▶ if (le == ls) return 0; /* empty label invalid */
Branch: 1 branch
BranchTrueFalseTotal
if (le == ls) return 0; /* empty label invalid */ 12 808648 808660
327 /* Label must contain at least one non-whitespace char. */
328 808648 has_nonws = 0;
329 808676 ▶ for (r = ls; r < le; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = ls; r < le; r++) { 808651 25 808676
330 808651 ▶ if (*r != ' ' && *r != '\t' && *r != '\n') { has_nonws = 1; break; }
Branch: 3 branches
BranchTrueFalseTotal
if (*r != ' ' && *r != '\t' && *r != '\n') { has_nonws = 1; break; } 808632 19 808651
if (*r != ' ' && *r != '\t' && *r != '\n') { has_nonws = 1; break; } 808632 0 808632
if (*r != ' ' && *r != '\t' && *r != '\n') { has_nonws = 1; break; } 808623 9 808632
331 }
332 808648 ▶ if (!has_nonws) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!has_nonws) return 0; 25 808623 808648
333 808623 q++;
334 808623 ▶ if (q >= end || *q != ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (q >= end || *q != ':') return 0; 808542 81 808623
if (q >= end || *q != ':') return 0; 262 808280 808542
335 808280 q++;
336 /* whitespace, may include up to one newline */
337 808280 nl = 0;
338 1616566 ▶ while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) {
Branch: 4 branches
BranchTrueFalseTotal
while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) { 1616563 3 1616566
while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) { 808277 808286 1616563
while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) { 0 808286 808286
while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) { 9 808277 808286
339 808286 ▶ if (*q == '\n') { if (++nl > 1) return 0; }
Branch: 2 branches
BranchTrueFalseTotal
if (*q == '\n') { if (++nl > 1) return 0; } 9 808277 808286
if (*q == '\n') { if (++nl > 1) return 0; } 0 9 9
340 808286 q++;
341 }
342 808280 ▶ if (q >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (q >= end) return 0; 3 808277 808280
343 /* url */
344 808277 ▶ if (*q == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '<') { 9 808268 808277
345 9 q++;
346 9 us = q;
347 36 ▶ while (q < end && *q != '>' && *q != '\n' && *q != '<') q++;
Branch: 4 branches
BranchTrueFalseTotal
while (q < end && *q != '>' && *q != '\n' && *q != '<') q++; 36 0 36
while (q < end && *q != '>' && *q != '\n' && *q != '<') q++; 27 9 36
while (q < end && *q != '>' && *q != '\n' && *q != '<') q++; 27 0 27
while (q < end && *q != '>' && *q != '\n' && *q != '<') q++; 27 0 27
348 9 ▶ if (q >= end || *q != '>') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (q >= end || *q != '>') return 0; 9 0 9
if (q >= end || *q != '>') return 0; 0 9 9
349 9 ue = q;
350 9 q++;
351 } else {
352 808268 us = q;
353 15353675 ▶ while (q < end && *q != ' ' && *q != '\t' && *q != '\n') {
Branch: 4 branches
BranchTrueFalseTotal
while (q < end && *q != ' ' && *q != '\t' && *q != '\n') { 14545543 808132 15353675
while (q < end && *q != ' ' && *q != '\t' && *q != '\n') { 14545437 106 14545543
while (q < end && *q != ' ' && *q != '\t' && *q != '\n') { 14545437 0 14545437
while (q < end && *q != ' ' && *q != '\t' && *q != '\n') { 14545407 30 14545437
354 /* disallow ASCII control */
355 14545407 ▶ if ((unsigned char)*q < 0x20) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if ((unsigned char)*q < 0x20) return 0; 0 14545407 14545407
356 14545407 q++;
357 }
358 808268 ue = q;
359 808268 ▶ if (ue == us) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ue == us) return 0; 0 808268 808268
360 }
361 /* optional title — must be separated from URL by at least one ws char
362 * (per CM §4.7). Without separator, '<bar>(baz)' is not a valid def. */
363 808277 ts = NULL; te = NULL;
364 808277 save_after_url = q;
365 808277 saw_nl = 0;
366 808277 saw_ws = 0;
367 808446 ▶ while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) {
Branch: 4 branches
BranchTrueFalseTotal
while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) { 311 808135 808446
while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) { 133 178 311
while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) { 0 178 178
while (q < end && (*q == ' ' || *q == '\t' || *q == '\n')) { 36 142 178
368 169 ▶ if (*q == '\n') saw_nl++;
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '\n') saw_nl++; 36 133 169
369 169 saw_ws = 1;
370 169 q++;
371 }
372 808277 ▶ if (q < end && (*q == '"' || *q == '\'' || *q == '(') && saw_nl <= 1 && saw_ws) {
Branch: 6 branches
BranchTrueFalseTotal
if (q < end && (*q == '"' || *q == '\'' || *q == '(') && saw_nl <= 1 && saw_ws) { 142 808135 808277
if (q < end && (*q == '"' || *q == '\'' || *q == '(') && saw_nl <= 1 && saw_ws) { 42 100 142
if (q < end && (*q == '"' || *q == '\'' || *q == '(') && saw_nl <= 1 && saw_ws) { 27 15 42
if (q < end && (*q == '"' || *q == '\'' || *q == '(') && saw_nl <= 1 && saw_ws) { 3 24 27
if (q < end && (*q == '"' || *q == '\'' || *q == '(') && saw_nl <= 1 && saw_ws) { 118 0 118
if (q < end && (*q == '"' || *q == '\'' || *q == '(') && saw_nl <= 1 && saw_ws) { 115 3 118
373 115 open = *q;
374 115 ▶ close = (open == '(') ? ')' : open;
Branch: 1 branch
BranchTrueFalseTotal
close = (open == '(') ? ')' : open; 115 0 115
375 115 q++;
376 115 ts = q;
377 /* Title may span multiple lines but cannot contain a blank line.
378 Detect blank line as: \n followed by (spaces|tabs)* \n . */
379 115 blank_found = 0;
380 893 ▶ while (q < end && *q != close) {
Branch: 2 branches
BranchTrueFalseTotal
while (q < end && *q != close) { 890 3 893
while (q < end && *q != close) { 778 112 890
381 778 ▶ if (open != '(' && *q == '\\' && q + 1 < end) { q += 2; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (open != '(' && *q == '\\' && q + 1 < end) { q += 2; continue; } 778 0 778
if (open != '(' && *q == '\\' && q + 1 < end) { q += 2; continue; } 9 769 778
if (open != '(' && *q == '\\' && q + 1 < end) { q += 2; continue; } 9 0 9
382 769 ▶ if (open == '(' && *q == '(') { /* unescaped '(' invalid in paren title */
Branch: 2 branches
BranchTrueFalseTotal
if (open == '(' && *q == '(') { /* unescaped '(' invalid in paren title */ 0 769 769
if (open == '(' && *q == '(') { /* unescaped '(' invalid in paren title */ 0 0 0
383 0 blank_found = 1; break;
384 }
385 769 ▶ if (*q == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '\n') { 12 757 769
386 12 r = q + 1;
387 12 ▶ while (r < end && (*r == ' ' || *r == '\t')) r++;
Branch: 3 branches
BranchTrueFalseTotal
while (r < end && (*r == ' ' || *r == '\t')) r++; 12 0 12
while (r < end && (*r == ' ' || *r == '\t')) r++; 0 12 12
while (r < end && (*r == ' ' || *r == '\t')) r++; 0 12 12
388 12 ▶ if (r >= end || *r == '\n') { blank_found = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (r >= end || *r == '\n') { blank_found = 1; break; } 12 0 12
if (r >= end || *r == '\n') { blank_found = 1; break; } 0 12 12
389 }
390 769 q++;
391 }
392 115 ▶ if (blank_found) { ts = NULL; te = NULL; q = save_after_url; goto end_title; }
Branch: 1 branch
BranchTrueFalseTotal
if (blank_found) { ts = NULL; te = NULL; q = save_after_url; goto end_title; } 0 115 115
393 115 ▶ if (q < end && *q == close) {
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == close) { 112 3 115
if (q < end && *q == close) { 112 0 112
394 112 te = q;
395 112 q++;
396 /* nothing but ws/newline allowed on remainder of title-line */
397 112 check = q;
398 124 ▶ while (check < end && check < end && *check != '\n') {
Branch: 3 branches
BranchTrueFalseTotal
while (check < end && check < end && *check != '\n') { 24 100 124
while (check < end && check < end && *check != '\n') { 24 0 24
while (check < end && check < end && *check != '\n') { 18 6 24
399 18 ▶ if (*check != ' ' && *check != '\t') { ts = NULL; te = NULL; q = save_after_url; goto end_title; }
Branch: 2 branches
BranchTrueFalseTotal
if (*check != ' ' && *check != '\t') { ts = NULL; te = NULL; q = save_after_url; goto end_title; } 6 12 18
if (*check != ' ' && *check != '\t') { ts = NULL; te = NULL; q = save_after_url; goto end_title; } 6 0 6
400 12 check++;
401 }
402 } else {
403 3 ts = NULL; te = NULL;
404 3 q = save_after_url;
405 }
406 } else {
407 808162 q = save_after_url;
408 }
409 808277 end_title:
410 /* consume trailing ws + one newline */
411 808289 ▶ while (q < end && (*q == ' ' || *q == '\t')) q++;
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && (*q == ' ' || *q == '\t')) q++; 54 808235 808289
while (q < end && (*q == ' ' || *q == '\t')) q++; 12 42 54
while (q < end && (*q == ' ' || *q == '\t')) q++; 0 42 42
412 808277 ▶ if (q < end && *q == '\n') q++;
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == '\n') q++; 42 808235 808277
if (q < end && *q == '\n') q++; 33 9 42
413 808244 ▶ else if (q < end) {
Branch: 1 branch
BranchTrueFalseTotal
else if (q < end) { 9 808235 808244
414 /* trailing content on same line invalidates the title; reparse without title */
415 9 ▶ if (ts) {
Branch: 1 branch
BranchTrueFalseTotal
if (ts) { 0 9 9
416 0 ts = NULL; te = NULL;
417 0 q = save_after_url;
418 0 ▶ while (q < end && (*q == ' ' || *q == '\t')) q++;
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && (*q == ' ' || *q == '\t')) q++; 0 0 0
while (q < end && (*q == ' ' || *q == '\t')) q++; 0 0 0
while (q < end && (*q == ' ' || *q == '\t')) q++; 0 0 0
419 0 ▶ if (q < end && *q == '\n') q++;
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == '\n') q++; 0 0 0
if (q < end && *q == '\n') q++; 0 0 0
420 0 ▶ else if (q < end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (q < end) return 0; 0 0 0
421 } else {
422 9 return 0;
423 }
424 }
425 808268 *p_out = q;
426 808268 *lbl_s = ls; *lbl_e = le;
427 808268 *url_s = us; *url_e = ue;
428 808268 *tit_s = ts; *tit_e = te;
429 808268 return 1;
430 }
431  
432 808262 static void ensure_linkref_tab(mds_ctx* ctx) {
433 808262 ▶ if (ctx->refs) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->refs) return; 767621 40641 808262
434 40641 ctx->refs = (struct mds_linkref_tab*)mds_arena_alloc(
435 &ctx->arena, sizeof(struct mds_linkref_tab));
436 40641 mds_linkref_init(ctx->refs, &ctx->arena);
437 }
438  
439 808021 static void ensure_footnote_tab(mds_ctx* ctx) {
440 808021 ▶ if (ctx->footnotes) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->footnotes) return; 767612 40409 808021
441 40409 ctx->footnotes = (struct mds_footnote_tab*)mds_arena_alloc(
442 &ctx->arena, sizeof(struct mds_footnote_tab));
443 40409 mds_footnote_init(ctx->footnotes, &ctx->arena);
444 }
445  
446 /* Tier E.1 (extended) — line-level preprocessing pass that walks the input
447 * once, identifies top-level `[^label]:` definitions, captures their
448 * multi-line bodies (continuation = blank lines or 4-space-indented lines,
449 * with 4-space dedent), registers them in the footnote table, and returns
450 * a cleaned input with those lines elided. Only invoked when
451 * MDS_FLAG_FOOTNOTES is set. Tracks fenced-code state to avoid matching
452 * `[^…]:` inside fenced blocks.
453 *
454 * The cleaned buffer is allocated from the arena so it survives the
455 * lifetime of the scan; the original ctx->input is left in place for
456 * sub-scans (which re-run mds_block_scan on individual def bodies).
457 *
458 * NOTE: this is a coarse, document-level pass — defs nested inside list
459 * items or blockquotes are still handled by finalize_paragraph's leading-
460 * defs strip. */
461 162122 static void preprocess_footnotes(mds_ctx* ctx) {
462 const char* in;
463 size_t ilen;
464 char* out;
465 size_t olen;
466 const char* p;
467 const char* end;
468 int in_fence;
469 char fence_ch;
470 int fence_len;
471 const char* le;
472 const char* nxt;
473 size_t lsz;
474 const char* q;
475 int ind;
476 int run;
477 const char* tail;
478 char fc;
479 int fl;
480 const char *lbs, *lbe, *bs;
481 char* body;
482 size_t blen;
483 const char *le2, *nxt2;
484 int blank;
485 const char* r;
486 int sp;
487  
488 162122 in = ctx->input;
489 162122 ilen = ctx->len;
490 162122 ▶ if (!ilen) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!ilen) return; 3 162119 162122
491  
492 /* Worst-case: same size as input. */
493 162119 out = (char*)mds_arena_alloc(&ctx->arena, ilen + 1);
494 162119 olen = 0;
495  
496 162119 p = in;
497 162119 end = in + ilen;
498 162119 in_fence = 0;
499 162119 fence_ch = 0;
500 162119 fence_len = 0;
501  
502 14707953 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 14545834 162119 14707953
503 14545834 le = (const char*)memchr(p, '\n', (size_t)(end - p));
504 14545834 ▶ if (!le) le = end;
Branch: 1 branch
BranchTrueFalseTotal
if (!le) le = end; 41 14545793 14545834
505 14545834 ▶ nxt = (le < end) ? le + 1 : end;
Branch: 1 branch
BranchTrueFalseTotal
nxt = (le < end) ? le + 1 : end; 14545793 41 14545834
506 14545834 lsz = (size_t)(nxt - p);
507  
508 /* Walk up to 3 leading spaces. */
509 14545834 q = p;
510 14545834 ind = 0;
511 14545930 ▶ while (q < le && *q == ' ' && ind < 3) { q++; ind++; }
Branch: 3 branches
BranchTrueFalseTotal
while (q < le && *q == ' ' && ind < 3) { q++; ind++; } 8889464 5656466 14545930
while (q < le && *q == ' ' && ind < 3) { q++; ind++; } 114 8889350 8889464
while (q < le && *q == ' ' && ind < 3) { q++; ind++; } 96 18 114
512  
513 14545834 ▶ if (in_fence) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_fence) { 1616060 12929774 14545834
514 /* Close-fence detection: run of fence_ch ≥ fence_len, then ws. */
515 1616060 run = 0;
516 4040150 ▶ while (q + run < le && q[run] == fence_ch) run++;
Branch: 2 branches
BranchTrueFalseTotal
while (q + run < le && q[run] == fence_ch) run++; 3232120 808030 4040150
while (q + run < le && q[run] == fence_ch) run++; 2424090 808030 3232120
517 1616060 ▶ if (run >= fence_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (run >= fence_len) { 808030 808030 1616060
518 808030 tail = q + run;
519 808030 ▶ while (tail < le && (*tail == ' ' || *tail == '\t')) tail++;
Branch: 3 branches
BranchTrueFalseTotal
while (tail < le && (*tail == ' ' || *tail == '\t')) tail++; 0 808030 808030
while (tail < le && (*tail == ' ' || *tail == '\t')) tail++; 0 0 0
while (tail < le && (*tail == ' ' || *tail == '\t')) tail++; 0 0 0
520 808030 ▶ if (tail == le) in_fence = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (tail == le) in_fence = 0; 808030 0 808030
521 }
522 1616060 memcpy(out + olen, p, lsz); olen += lsz;
523 1616060 p = nxt;
524 1616060 continue;
525 }
526  
527 /* Open-fence detection. */
528 12929774 ▶ if (le - q >= 3 && (q[0] == '`' || q[0] == '~') && q[1] == q[0] && q[2] == q[0]) {
Branch: 5 branches
BranchTrueFalseTotal
if (le - q >= 3 && (q[0] == '`' || q[0] == '~') && q[1] == q[0] && q[2] == q[0]) { 7273302 5656472 12929774
if (le - q >= 3 && (q[0] == '`' || q[0] == '~') && q[1] == q[0] && q[2] == q[0]) { 6465271 808031 7273302
if (le - q >= 3 && (q[0] == '`' || q[0] == '~') && q[1] == q[0] && q[2] == q[0]) { 12 6465259 6465271
if (le - q >= 3 && (q[0] == '`' || q[0] == '~') && q[1] == q[0] && q[2] == q[0]) { 808039 4 808043
if (le - q >= 3 && (q[0] == '`' || q[0] == '~') && q[1] == q[0] && q[2] == q[0]) { 808030 9 808039
529 808030 fc = q[0];
530 808030 fl = 3;
531 808030 ▶ while (q + fl < le && q[fl] == fc) fl++;
Branch: 2 branches
BranchTrueFalseTotal
while (q + fl < le && q[fl] == fc) fl++; 808025 5 808030
while (q + fl < le && q[fl] == fc) fl++; 0 808025 808025
532 /* Tilde fences can have any info; backtick fences must not
533 * contain backticks in info — but for our purposes (we only
534 * care about hiding defs inside the fence body) treat both
535 * the same. */
536 808030 fence_ch = fc;
537 808030 fence_len = fl;
538 808030 in_fence = 1;
539 808030 memcpy(out + olen, p, lsz); olen += lsz;
540 808030 p = nxt;
541 808030 continue;
542 }
543  
544 /* Footnote def? `[^label]:` */
545 12121744 ▶ if (le - q >= 4 && q[0] == '[' && q[1] == '^') {
Branch: 3 branches
BranchTrueFalseTotal
if (le - q >= 4 && q[0] == '[' && q[1] == '^') { 6465233 5656511 12121744
if (le - q >= 4 && q[0] == '[' && q[1] == '^') { 1616040 4849193 6465233
if (le - q >= 4 && q[0] == '[' && q[1] == '^') { 808021 808019 1616040
546 808021 lbs = q + 2;
547 808021 lbe = lbs;
548 1616249 ▶ while (lbe < le && *lbe != ']' && *lbe != '[' && *lbe != '\n') lbe++;
Branch: 4 branches
BranchTrueFalseTotal
while (lbe < le && *lbe != ']' && *lbe != '[' && *lbe != '\n') lbe++; 1616249 0 1616249
while (lbe < le && *lbe != ']' && *lbe != '[' && *lbe != '\n') lbe++; 808228 808021 1616249
while (lbe < le && *lbe != ']' && *lbe != '[' && *lbe != '\n') lbe++; 808228 0 808228
while (lbe < le && *lbe != ']' && *lbe != '[' && *lbe != '\n') lbe++; 808228 0 808228
549 808021 ▶ if (lbe < le && *lbe == ']' && lbe > lbs &&
Branch: 3 branches
BranchTrueFalseTotal
if (lbe < le && *lbe == ']' && lbe > lbs && 808021 0 808021
if (lbe < le && *lbe == ']' && lbe > lbs && 808021 0 808021
if (lbe < le && *lbe == ']' && lbe > lbs && 808021 0 808021
550 808021 ▶ lbe + 1 < le && lbe[1] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
lbe + 1 < le && lbe[1] == ':') { 808021 0 808021
lbe + 1 < le && lbe[1] == ':') { 808021 0 808021
551 /* Match. Capture body. */
552 808021 bs = lbe + 2;
553 1616054 ▶ while (bs < le && (*bs == ' ' || *bs == '\t')) bs++;
Branch: 3 branches
BranchTrueFalseTotal
while (bs < le && (*bs == ' ' || *bs == '\t')) bs++; 1616048 6 1616054
while (bs < le && (*bs == ' ' || *bs == '\t')) bs++; 808033 808015 1616048
while (bs < le && (*bs == ' ' || *bs == '\t')) bs++; 0 808015 808015
554  
555 /* Body buffer (arena). Worst case the rest of the input. */
556 808021 body = (char*)mds_arena_alloc(&ctx->arena,
557 808021 (size_t)(end - bs) + 1);
558 808021 blen = 0;
559 808021 ▶ if (bs < le) {
Branch: 1 branch
BranchTrueFalseTotal
if (bs < le) { 808015 6 808021
560 808015 memcpy(body + blen, bs, (size_t)(le - bs));
561 808015 blen += (size_t)(le - bs);
562 }
563  
564 808021 p = nxt;
565 /* Continuation lines: blank OR ≥4 spaces (or 1 tab) leading. */
566 1616051 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 1595842 20209 1616051
567 1595842 le2 = (const char*)memchr(p, '\n', (size_t)(end - p));
568 1595842 ▶ if (!le2) le2 = end;
Branch: 1 branch
BranchTrueFalseTotal
if (!le2) le2 = end; 0 1595842 1595842
569 1595842 ▶ nxt2 = (le2 < end) ? le2 + 1 : end;
Branch: 1 branch
BranchTrueFalseTotal
nxt2 = (le2 < end) ? le2 + 1 : end; 1595842 0 1595842
570  
571 /* Blank? (only ws up to newline) */
572 1595842 blank = 1;
573 1595914 ▶ for (r = p; r < le2; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = p; r < le2; r++) { 787896 808018 1595914
574 787896 ▶ if (*r != ' ' && *r != '\t') { blank = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (*r != ' ' && *r != '\t') { blank = 0; break; } 787824 72 787896
if (*r != ' ' && *r != '\t') { blank = 0; break; } 787824 0 787824
575 }
576 1595842 ▶ if (blank) {
Branch: 1 branch
BranchTrueFalseTotal
if (blank) { 808018 787824 1595842
577 808018 body[blen++] = '\n';
578 808018 p = nxt2;
579 808018 continue;
580 }
581  
582 /* Check ≥4 leading spaces (or one tab counts as 4). */
583 787824 sp = 0;
584 787824 r = p;
585 787872 ▶ while (r < le2 && sp < 4) {
Branch: 2 branches
BranchTrueFalseTotal
while (r < le2 && sp < 4) { 787872 0 787872
while (r < le2 && sp < 4) { 787860 12 787872
586 787860 ▶ if (*r == ' ') { sp++; r++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*r == ' ') { sp++; r++; } 48 787812 787860
587 787812 ▶ else if (*r == '\t') { sp = 4; r++; break; }
Branch: 1 branch
BranchTrueFalseTotal
else if (*r == '\t') { sp = 4; r++; break; } 0 787812 787812
588 787812 else break;
589 }
590 787824 ▶ if (sp < 4) break; /* end of def body */
Branch: 1 branch
BranchTrueFalseTotal
if (sp < 4) break; /* end of def body */ 787812 12 787824
591  
592 12 body[blen++] = '\n';
593 12 ▶ if (le2 > r) {
Branch: 1 branch
BranchTrueFalseTotal
if (le2 > r) { 12 0 12
594 12 memcpy(body + blen, r, (size_t)(le2 - r));
595 12 blen += (size_t)(le2 - r);
596 }
597 12 p = nxt2;
598 }
599  
600 /* Strip trailing blank/whitespace bytes for tidy rendering. */
601 1616033 ▶ while (blen > 0 && (body[blen-1] == '\n' ||
Branch: 2 branches
BranchTrueFalseTotal
while (blen > 0 && (body[blen-1] == '\n' || 1616033 0 1616033
while (blen > 0 && (body[blen-1] == '\n' || 808012 808021 1616033
602 808021 ▶ body[blen-1] == ' ' ||
Branch: 1 branch
BranchTrueFalseTotal
body[blen-1] == ' ' || 0 808021 808021
603 1616033 ▶ body[blen-1] == '\t')) blen--;
Branch: 1 branch
BranchTrueFalseTotal
body[blen-1] == '\t')) blen--; 0 808021 808021
604  
605 808021 ensure_footnote_tab(ctx);
606 808021 mds_footnote_add(ctx->footnotes,
607 808021 lbs, (size_t)(lbe - lbs),
608 body, blen);
609 /* Do NOT copy these lines into out. */
610 808021 continue;
611 }
612 }
613  
614 /* Default: copy line. */
615 11313723 memcpy(out + olen, p, lsz); olen += lsz;
616 11313723 p = nxt;
617 }
618  
619 162119 out[olen] = '\0';
620 162119 ctx->input = out;
621 162119 ctx->len = olen;
622 }
623  
624 /* GFM footnote definition: ` {0,3}[^label]:` followed by body bytes
625 * (continuation rules collapsed in this MVP — body is whatever remains
626 * of the paragraph buffer up to the next `[^...]:` line or end). Returns
627 * 1 if a def was consumed, advancing *p_in. Body bytes are the slice
628 * [body_s..body_e). */
629 4465126 static int parse_footnote_def(const char* p, const char* end,
630 const char** p_out,
631 const char** lbl_s, const char** lbl_e,
632 const char** body_s, const char** body_e) {
633 const char* q;
634 int lead;
635 const char* ls;
636 const char* le;
637 const char* bs;
638 const char* be;
639 const char* nxt;
640 const char* r;
641 int rlead;
642 const char* rr;
643  
644 4465126 q = p;
645 4465126 lead = 0;
646 4465127 ▶ while (q < end && *q == ' ' && lead < 3) { q++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && *q == ' ' && lead < 3) { q++; lead++; } 4465127 0 4465127
while (q < end && *q == ' ' && lead < 3) { q++; lead++; } 1 4465126 4465127
while (q < end && *q == ' ' && lead < 3) { q++; lead++; } 1 0 1
647 4465126 ▶ if (end - q < 4) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (end - q < 4) return 0; 35 4465091 4465126
648 4465091 ▶ if (q[0] != '[' || q[1] != '^') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (q[0] != '[' || q[1] != '^') return 0; 808079 3657012 4465091
if (q[0] != '[' || q[1] != '^') return 0; 808079 0 808079
649 0 q += 2;
650 0 ls = q;
651 0 ▶ while (q < end && *q != ']' && *q != '\n' && *q != '[') q++;
Branch: 4 branches
BranchTrueFalseTotal
while (q < end && *q != ']' && *q != '\n' && *q != '[') q++; 0 0 0
while (q < end && *q != ']' && *q != '\n' && *q != '[') q++; 0 0 0
while (q < end && *q != ']' && *q != '\n' && *q != '[') q++; 0 0 0
while (q < end && *q != ']' && *q != '\n' && *q != '[') q++; 0 0 0
652 0 ▶ if (q >= end || *q != ']' || q == ls) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (q >= end || *q != ']' || q == ls) return 0; 0 0 0
if (q >= end || *q != ']' || q == ls) return 0; 0 0 0
if (q >= end || *q != ']' || q == ls) return 0; 0 0 0
653 0 le = q;
654 0 q++;
655 0 ▶ if (q >= end || *q != ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (q >= end || *q != ':') return 0; 0 0 0
if (q >= end || *q != ':') return 0; 0 0 0
656 0 q++;
657 /* Spec: any number of spaces/tabs follow; they are stripped. */
658 0 ▶ while (q < end && (*q == ' ' || *q == '\t')) q++;
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && (*q == ' ' || *q == '\t')) q++; 0 0 0
while (q < end && (*q == ' ' || *q == '\t')) q++; 0 0 0
while (q < end && (*q == ' ' || *q == '\t')) q++; 0 0 0
659 /* Body runs until the next bare-line `[^...]:` or end of buffer.
660 * In this MVP we accept body across newlines but NOT across blank
661 * lines (the para buffer would already have been split). */
662 0 bs = q;
663 0 be = q;
664 0 ▶ while (q < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (q < end) { 0 0 0
665 0 ▶ if (*q == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '\n') { 0 0 0
666 0 nxt = q + 1;
667 /* Peek next line: another `[^...]:` ? -> stop here. */
668 0 r = nxt;
669 0 rlead = 0;
670 0 ▶ while (r < end && *r == ' ' && rlead < 3) { r++; rlead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (r < end && *r == ' ' && rlead < 3) { r++; rlead++; } 0 0 0
while (r < end && *r == ' ' && rlead < 3) { r++; rlead++; } 0 0 0
while (r < end && *r == ' ' && rlead < 3) { r++; rlead++; } 0 0 0
671 0 ▶ if (end - r >= 4 && r[0] == '[' && r[1] == '^') {
Branch: 3 branches
BranchTrueFalseTotal
if (end - r >= 4 && r[0] == '[' && r[1] == '^') { 0 0 0
if (end - r >= 4 && r[0] == '[' && r[1] == '^') { 0 0 0
if (end - r >= 4 && r[0] == '[' && r[1] == '^') { 0 0 0
672 0 rr = r + 2;
673 0 ▶ while (rr < end && *rr != ']' && *rr != '\n' && *rr != '[') rr++;
Branch: 4 branches
BranchTrueFalseTotal
while (rr < end && *rr != ']' && *rr != '\n' && *rr != '[') rr++; 0 0 0
while (rr < end && *rr != ']' && *rr != '\n' && *rr != '[') rr++; 0 0 0
while (rr < end && *rr != ']' && *rr != '\n' && *rr != '[') rr++; 0 0 0
while (rr < end && *rr != ']' && *rr != '\n' && *rr != '[') rr++; 0 0 0
674 0 ▶ if (rr < end && *rr == ']' && rr + 1 < end && rr[1] == ':') {
Branch: 4 branches
BranchTrueFalseTotal
if (rr < end && *rr == ']' && rr + 1 < end && rr[1] == ':') { 0 0 0
if (rr < end && *rr == ']' && rr + 1 < end && rr[1] == ':') { 0 0 0
if (rr < end && *rr == ']' && rr + 1 < end && rr[1] == ':') { 0 0 0
if (rr < end && *rr == ']' && rr + 1 < end && rr[1] == ':') { 0 0 0
675 0 be = q; /* don't include the \n */
676 0 q = nxt; /* next def starts here */
677 0 goto done;
678 }
679 }
680 0 be = q + 1; /* keep the \n in body */
681 0 q = nxt;
682 0 continue;
683 }
684 0 q++;
685 }
686 0 be = q;
687 0 done:
688 0 *p_out = q;
689 0 *lbl_s = ls; *lbl_e = le;
690 0 *body_s = bs; *body_e = be;
691 0 return 1;
692 }
693  
694 /* ---------- GFM tables ---------- */
695  
696 #include "mds.h"
697  
698 static const char* next_line(const char* p, const char* end,
699 const char** line_end_out);
700  
701 #define MDS_TBL_MAX_COLS 64
702  
703 /* Tables: memchr-driven pipe scan. libc memchr is SIMD-vectorised
704 * on every platform we care about (Apple libSystem uses NEON, glibc uses
705 * AVX2), so iterating candidate pipes is dramatically faster than a
706 * byte-by-byte loop. We confirm each pipe is unescaped by counting the
707 * run of preceding backslashes — odd count means escaped. */
708 MDS_HOT
709 384577 static const char* tbl_find_pipe(const char* p, const char* end) {
710 const char* q;
711 const char* bs;
712  
713 384608 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 384608 0 384608
714 384608 q = (const char*)memchr(p, '|', (size_t)(end - p));
715 384608 ▶ if (!q) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!q) return NULL; 384070 538 384608
716 /* Count preceding backslashes. */
717 538 bs = q;
718 569 ▶ while (bs > p && bs[-1] == '\\') bs--;
Branch: 2 branches
BranchTrueFalseTotal
while (bs > p && bs[-1] == '\\') bs--; 341 228 569
while (bs > p && bs[-1] == '\\') bs--; 31 310 341
719 538 ▶ if (((q - bs) & 1u) == 0u) return q; /* even ⇒ unescaped */
Branch: 1 branch
BranchTrueFalseTotal
if (((q - bs) & 1u) == 0u) return q; /* even ⇒ unescaped */ 507 31 538
720 31 p = q + 1;
721 }
722 0 return NULL;
723 }
724  
725 /* Does the cell contain a backslash? Cheap check that lets the fast
726 * path skip the per-byte unescape pass entirely. */
727 MDS_ALWAYS_INLINE static int tbl_cell_needs_unescape(const char* s, size_t n) {
728 324 return memchr(s, '\\', n) != NULL;
729 }
730  
731 /* Split a line on unescaped '|', trimming leading/trailing whitespace
732 * and any single leading/trailing pipe. Writes (start,len) pairs to
733 * cells[] up to max_cells. Returns the cell count (without truncation). */
734 246 static unsigned tbl_split_cells(const char* line, size_t len,
735 const char** out_s, size_t* out_n,
736 unsigned max_cells) {
737 const char* p;
738 const char* end;
739 int esc;
740 const char* q;
741 unsigned count;
742 const char* pipe;
743 const char* cell_end;
744 const char* cs;
745 const char* ce;
746  
747 246 p = line;
748 246 end = line + len;
749 /* Trim outer whitespace. */
750 246 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 246 0 246
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 246 246
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 246 246
751 249 ▶ while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--;
Branch: 3 branches
BranchTrueFalseTotal
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 249 0 249
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 3 246 249
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 0 246 246
752 /* Strip a single leading unescaped pipe. */
753 246 ▶ if (p < end && *p == '|') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '|') p++; 246 0 246
if (p < end && *p == '|') p++; 225 21 246
754 /* Strip a single trailing unescaped pipe (must not be escaped). */
755 246 ▶ if (end > p && end[-1] == '|') {
Branch: 2 branches
BranchTrueFalseTotal
if (end > p && end[-1] == '|') { 246 0 246
if (end > p && end[-1] == '|') { 213 33 246
756 213 esc = 0;
757 213 q = end - 1;
758 213 ▶ while (q > p && q[-1] == '\\') { esc = !esc; q--; }
Branch: 2 branches
BranchTrueFalseTotal
while (q > p && q[-1] == '\\') { esc = !esc; q--; } 213 0 213
while (q > p && q[-1] == '\\') { esc = !esc; q--; } 0 213 213
759 213 ▶ if (!esc) end--;
Branch: 1 branch
BranchTrueFalseTotal
if (!esc) end--; 213 0 213
760 }
761 246 count = 0;
762 507 ▶ while (p <= end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p <= end) { 507 0 507
763 507 pipe = tbl_find_pipe(p, end);
764 507 ▶ cell_end = pipe ? pipe : end;
Branch: 1 branch
BranchTrueFalseTotal
cell_end = pipe ? pipe : end; 261 246 507
765 507 cs = p;
766 507 ce = cell_end;
767 968 ▶ while (cs < ce && (*cs == ' ' || *cs == '\t')) cs++;
Branch: 3 branches
BranchTrueFalseTotal
while (cs < ce && (*cs == ' ' || *cs == '\t')) cs++; 959 9 968
while (cs < ce && (*cs == ' ' || *cs == '\t')) cs++; 461 498 959
while (cs < ce && (*cs == ' ' || *cs == '\t')) cs++; 0 498 498
768 954 ▶ while (ce > cs && (ce[-1] == ' ' || ce[-1] == '\t')) ce--;
Branch: 3 branches
BranchTrueFalseTotal
while (ce > cs && (ce[-1] == ' ' || ce[-1] == '\t')) ce--; 945 9 954
while (ce > cs && (ce[-1] == ' ' || ce[-1] == '\t')) ce--; 447 498 945
while (ce > cs && (ce[-1] == ' ' || ce[-1] == '\t')) ce--; 0 498 498
769 507 ▶ if (count < max_cells) {
Branch: 1 branch
BranchTrueFalseTotal
if (count < max_cells) { 507 0 507
770 507 out_s[count] = cs;
771 507 out_n[count] = (size_t)(ce - cs);
772 }
773 507 count++;
774 507 ▶ if (!pipe) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!pipe) break; 246 261 507
775 261 p = pipe + 1;
776 }
777 246 return count;
778 }
779  
780 /* Parse a separator row: zero or more spaces, optional |, then per cell
781 * `:?-+:?` separated by `|`, optional trailing |. Returns cell count and
782 * fills aligns[]. Returns 0 if not a valid separator. */
783 149 static unsigned tbl_parse_separator(const char* line, size_t len,
784 mds_align* aligns, unsigned max_cells) {
785 const char* p;
786 const char* end;
787 unsigned count;
788 const char* cs;
789 const char* ce;
790 int left, right;
791 const char* q;
792  
793 149 p = line;
794 149 end = line + len;
795 149 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 149 0 149
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 149 149
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 149 149
796 149 ▶ while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--;
Branch: 3 branches
BranchTrueFalseTotal
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 149 0 149
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 0 149 149
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) end--; 0 149 149
797 149 ▶ if (p >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return 0; 0 149 149
798 149 ▶ if (*p == '|') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '|') p++; 131 18 149
799 /* Strip trailing |. */
800 149 ▶ if (end > p && end[-1] == '|') end--;
Branch: 2 branches
BranchTrueFalseTotal
if (end > p && end[-1] == '|') end--; 149 0 149
if (end > p && end[-1] == '|') end--; 131 18 149
801 149 count = 0;
802 300 ▶ while (p <= end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p <= end) { 300 0 300
803 300 cs = p;
804 1900 ▶ while (p < end && *p != '|') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '|') p++; 1759 141 1900
while (p < end && *p != '|') p++; 1600 159 1759
805 300 ce = p;
806 /* Trim spaces around the cell. */
807 512 ▶ while (cs < ce && (*cs == ' ' || *cs == '\t')) cs++;
Branch: 3 branches
BranchTrueFalseTotal
while (cs < ce && (*cs == ' ' || *cs == '\t')) cs++; 512 0 512
while (cs < ce && (*cs == ' ' || *cs == '\t')) cs++; 212 300 512
while (cs < ce && (*cs == ' ' || *cs == '\t')) cs++; 0 300 300
808 506 ▶ while (ce > cs && (ce[-1] == ' ' || ce[-1] == '\t')) ce--;
Branch: 3 branches
BranchTrueFalseTotal
while (ce > cs && (ce[-1] == ' ' || ce[-1] == '\t')) ce--; 506 0 506
while (ce > cs && (ce[-1] == ' ' || ce[-1] == '\t')) ce--; 206 300 506
while (ce > cs && (ce[-1] == ' ' || ce[-1] == '\t')) ce--; 0 300 300
809 300 ▶ if (cs >= ce) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cs >= ce) return 0; 0 300 300
810 300 left = (*cs == ':');
811 300 ▶ if (left) cs++;
Branch: 1 branch
BranchTrueFalseTotal
if (left) cs++; 28 272 300
812 300 ▶ right = (ce > cs && ce[-1] == ':');
Branch: 2 branches
BranchTrueFalseTotal
right = (ce > cs && ce[-1] == ':'); 300 0 300
right = (ce > cs && ce[-1] == ':'); 28 272 300
813 300 ▶ if (right) ce--;
Branch: 1 branch
BranchTrueFalseTotal
if (right) ce--; 28 272 300
814 300 ▶ if (cs >= ce) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cs >= ce) return 0; 0 300 300
815 1358 ▶ for (q = cs; q < ce; q++)
Branch: 1 branch
BranchTrueFalseTotal
for (q = cs; q < ce; q++) 1066 292 1358
816 1066 ▶ if (*q != '-') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*q != '-') return 0; 8 1058 1066
817 292 ▶ if (count < max_cells) {
Branch: 1 branch
BranchTrueFalseTotal
if (count < max_cells) { 292 0 292
818 292 ▶ aligns[count] = left && right ? MDS_ALIGN_CENTER :
Branch: 4 branches
BranchTrueFalseTotal
aligns[count] = left && right ? MDS_ALIGN_CENTER : 28 264 292
aligns[count] = left && right ? MDS_ALIGN_CENTER : 14 14 28
aligns[count] = left && right ? MDS_ALIGN_CENTER : 264 14 278
aligns[count] = left && right ? MDS_ALIGN_CENTER : 14 250 264
819 left ? MDS_ALIGN_LEFT :
820 right ? MDS_ALIGN_RIGHT : MDS_ALIGN_NONE;
821 }
822 292 count++;
823 292 ▶ if (p >= end) break;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) break; 141 151 292
824 151 p++; /* skip '|' */
825 }
826 141 return count;
827 }
828  
829 /* Returns the next newline in [p,end) or end. *le_out is the line end
830 * (excluding CR). */
831 4081677 static const char* tbl_next_line(const char* p, const char* end, const char** le_out) {
832 4081677 const char* nl = (const char*)memchr(p, '\n', (size_t)(end - p));
833 4081677 ▶ const char* le = nl ? nl : end;
Branch: 1 branch
BranchTrueFalseTotal
const char* le = nl ? nl : end; 384131 3697546 4081677
834 4081677 ▶ if (le > p && le[-1] == '\r') le--;
Branch: 2 branches
BranchTrueFalseTotal
if (le > p && le[-1] == '\r') le--; 4081677 0 4081677
if (le > p && le[-1] == '\r') le--; 0 4081677 4081677
835 4081677 *le_out = le;
836 4081677 ▶ return nl ? nl + 1 : end;
Branch: 1 branch
BranchTrueFalseTotal
return nl ? nl + 1 : end; 384131 3697546 4081677
837 }
838  
839 /* Returns 1 when the cell text has no byte that could begin an inline
840 * construct, so the inline scanner can be bypassed entirely and the
841 * text emitted as a plain EV_TEXT event. Mirrors the trigger set used
842 * by mds_inline_scan's fast path. Tables of single-word cells (the
843 * common case in real-world reports) hit this on every cell. */
844 MDS_ALWAYS_INLINE static int tbl_cell_is_plain(const char* s, size_t n) {
845 size_t i;
846 277 ▶ if (n == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) return 1; 0 277 277
847 1336 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1089 247 1336
848 1089 unsigned char c = (unsigned char)s[i];
849 1089 ▶ if (c == '*' || c == '_' || c == '~' || c == '`' ||
Branch: 5 branches
BranchTrueFalseTotal
if (c == '*' || c == '_' || c == '~' || c == '`' || 1084 5 1089
if (c == '*' || c == '_' || c == '~' || c == '`' || 1072 12 1084
if (c == '*' || c == '_' || c == '~' || c == '`' || 1071 1 1072
if (c == '*' || c == '_' || c == '~' || c == '`' || 1071 0 1071
if (c == '*' || c == '_' || c == '~' || c == '`' || 1065 6 1071
850 1065 ▶ c == '[' || c == ']' || c == '!' || c == '<' ||
Branch: 4 branches
BranchTrueFalseTotal
c == '[' || c == ']' || c == '!' || c == '<' || 1065 0 1065
c == '[' || c == ']' || c == '!' || c == '<' || 1065 0 1065
c == '[' || c == ']' || c == '!' || c == '<' || 1059 6 1065
c == '[' || c == ']' || c == '!' || c == '<' || 1059 0 1059
851 1059 ▶ c == '&' || c == '\\' || c == '\n')
Branch: 2 branches
BranchTrueFalseTotal
c == '&' || c == '\\' || c == '\n') 1059 0 1059
c == '&' || c == '\\' || c == '\n') 0 1059 1059
852 30 return 0;
853 }
854 /* Reject trailing double-space (CommonMark hard-break candidate). */
855 247 ▶ if (n >= 2 && s[n-1] == ' ' && s[n-2] == ' ') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (n >= 2 && s[n-1] == ' ' && s[n-2] == ' ') return 0; 98 149 247
if (n >= 2 && s[n-1] == ' ' && s[n-2] == ' ') return 0; 0 98 98
if (n >= 2 && s[n-1] == ' ' && s[n-2] == ' ') return 0; 0 0 0
856 247 return 1;
857 }
858  
859 /* Emit a row whose cells are already split (cs[i], cn[i]).
860 * `count` is the number of cells the split produced; we always emit
861 * exactly `nhead` cells, padding with empties if needed. */
862 MDS_HOT
863 166 static void tbl_emit_row_pre(bscanner* b,
864 const char* const* cs, const size_t* cn,
865 unsigned count,
866 const mds_align* aligns, unsigned nhead) {
867 mds_block_detail d;
868 unsigned i;
869 mds_block_detail cd;
870 const char* s;
871 size_t n;
872 size_t off;
873 char* dst;
874 size_t bl;
875 size_t j;
876 ev_rec* e;
877  
878 166 d.u.table_cell.align = MDS_ALIGN_NONE; /* placeholder, row has no align */
879 166 sax_enter(b, MDS_BLK_TABLE_ROW, &d);
880 520 ▶ for (i = 0; i < nhead; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nhead; i++) { 354 166 520
881 354 cd.u.table_cell.align = aligns[i];
882 354 sax_enter(b, MDS_BLK_TABLE_CELL, &cd);
883 354 ▶ if (i < count && cn[i]) {
Branch: 2 branches
BranchTrueFalseTotal
if (i < count && cn[i]) { 333 21 354
if (i < count && cn[i]) { 324 9 333
884 324 s = cs[i];
885 324 n = cn[i];
886 324 ▶ if (MDS_LIKELY(!tbl_cell_needs_unescape(s, n))) {
Branch: 1 branch
BranchTrueFalseTotal
if (MDS_LIKELY(!tbl_cell_needs_unescape(s, n))) { 277 47 324
887 /* Fast path: no '\\' anywhere in the cell. If the cell
888 * is also free of inline triggers (the common single-
889 * word case), emit EV_TEXT directly so sax_flush calls
890 * cb.text without going through mds_inline_scan. Else
891 * fall back to EV_INLINE. Both branches use the same
892 * pool_intern, so input bytes are never double-copied. */
893 277 ▶ if (MDS_LIKELY(tbl_cell_is_plain(s, n))) {
Branch: 1 branch
BranchTrueFalseTotal
if (MDS_LIKELY(tbl_cell_is_plain(s, n))) { 247 30 277
894 247 sax_text(b, s, n);
895 } else {
896 30 sax_inline_text(b, s, n);
897 }
898 } else {
899 /* Slow path: copy-and-unescape '\\|' → '|' in one pass
900 * directly into the bytepool, then record the event. */
901 47 buf_grow(&b->bytepool, &b->bp_cap, b->bp_len + n + 1);
902 47 off = b->bp_len;
903 47 dst = b->bytepool + off;
904 47 bl = 0;
905 471 ▶ for (j = 0; j < n; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; j++) { 424 47 471
906 424 ▶ if (s[j] == '\\' && j + 1 < n && s[j+1] == '|') {
Branch: 3 branches
BranchTrueFalseTotal
if (s[j] == '\\' && j + 1 < n && s[j+1] == '|') { 83 341 424
if (s[j] == '\\' && j + 1 < n && s[j+1] == '|') { 77 6 83
if (s[j] == '\\' && j + 1 < n && s[j+1] == '|') { 29 48 77
907 29 dst[bl++] = '|'; j++;
908 } else {
909 395 dst[bl++] = s[j];
910 }
911 }
912 47 b->bp_len += bl;
913 47 e = ev_alloc(b);
914 47 e->type = EV_INLINE;
915 47 e->u.bytes.off = off;
916 47 e->u.bytes.len = bl;
917 }
918 }
919 354 sax_leave(b, MDS_BLK_TABLE_CELL);
920 }
921 166 sax_leave(b, MDS_BLK_TABLE_ROW);
922 166 }
923  
924 /* Convenience wrapper for body rows: split + emit. */
925 MDS_HOT
926 97 static void tbl_emit_row(bscanner* b, const char* line, size_t len,
927 const mds_align* aligns, unsigned nhead) {
928 const char* cs[MDS_TBL_MAX_COLS];
929 size_t cn[MDS_TBL_MAX_COLS];
930 unsigned count;
931  
932 97 count = tbl_split_cells(line, len, cs, cn, MDS_TBL_MAX_COLS);
933 97 tbl_emit_row_pre(b, cs, cn, count, aligns, nhead);
934 97 }
935  
936 /* Cheap detection: returns 1 if the first two lines starting at p form a
937 * valid GFM table header + separator (matching column count). Emits nothing. */
938 4081362 static int tbl_peek_header(const char* p, const char* end) {
939 const char* le1;
940 const char* p2;
941 const char* hs[MDS_TBL_MAX_COLS];
942 size_t hn[MDS_TBL_MAX_COLS];
943 unsigned nhead;
944 const char* le2;
945 mds_align aligns[MDS_TBL_MAX_COLS];
946 unsigned nsep;
947  
948 4081362 memset(aligns, 0, sizeof aligns);
949 4081362 p2 = tbl_next_line(p, end, &le1);
950 4081362 ▶ if (p2 >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p2 >= end) return 0; 3697458 383904 4081362
951 383904 ▶ if (!tbl_find_pipe(p, le1)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!tbl_find_pipe(p, le1)) return 0; 383824 80 383904
952 80 nhead = tbl_split_cells(p, (size_t)(le1 - p), hs, hn, MDS_TBL_MAX_COLS);
953 80 ▶ if (nhead < 1 || nhead > MDS_TBL_MAX_COLS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (nhead < 1 || nhead > MDS_TBL_MAX_COLS) return 0; 80 0 80
if (nhead < 1 || nhead > MDS_TBL_MAX_COLS) return 0; 0 80 80
954 80 (void)tbl_next_line(p2, end, &le2);
955 80 nsep = tbl_parse_separator(p2, (size_t)(le2 - p2), aligns, MDS_TBL_MAX_COLS);
956 80 return (nsep == nhead);
957 }
958  
959 /* Attempt to emit a table starting at the given paragraph buffer.
960 * Returns the number of bytes consumed, or 0 if no table starts here. */
961 69 static size_t try_emit_table(bscanner* b, const char* p, const char* end) {
962 /* Read first line. */
963 const char* le1;
964 const char* p2;
965 const char* hs[MDS_TBL_MAX_COLS];
966 size_t hn[MDS_TBL_MAX_COLS];
967 unsigned nhead;
968 const char* le2;
969 const char* p3;
970 mds_align aligns[MDS_TBL_MAX_COLS];
971 unsigned nsep;
972 mds_block_detail d;
973 mds_align* alstore;
974 mds_block_detail hd;
975 const char* row;
976 int body_open;
977 const char* le;
978 const char* nx;
979 mds_block_detail bd;
980  
981 69 memset(aligns, 0, sizeof aligns);
982 69 p2 = tbl_next_line(p, end, &le1);
983 69 ▶ if (p2 >= end) return 0; /* need a second line */
Branch: 1 branch
BranchTrueFalseTotal
if (p2 >= end) return 0; /* need a second line */ 0 69 69
984 /* Header must contain at least one unescaped pipe. */
985 69 ▶ if (!tbl_find_pipe(p, le1)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!tbl_find_pipe(p, le1)) return 0; 0 69 69
986 /* Count header cells. */
987 69 nhead = tbl_split_cells(p, (size_t)(le1 - p), hs, hn, MDS_TBL_MAX_COLS);
988 69 ▶ if (nhead < 1 || nhead > MDS_TBL_MAX_COLS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (nhead < 1 || nhead > MDS_TBL_MAX_COLS) return 0; 69 0 69
if (nhead < 1 || nhead > MDS_TBL_MAX_COLS) return 0; 0 69 69
989 /* Read separator line. */
990 69 p3 = tbl_next_line(p2, end, &le2);
991 69 nsep = tbl_parse_separator(p2, (size_t)(le2 - p2), aligns, MDS_TBL_MAX_COLS);
992 69 ▶ if (nsep != nhead) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (nsep != nhead) return 0; 0 69 69
993 /* Emit table. */
994 69 memset(&d, 0, sizeof d);
995 69 d.u.table.ncols = nhead;
996 /* Persist aligns in arena so the renderer can see them. */
997 69 alstore = (mds_align*)mds_arena_alloc(&b->ctx->arena,
998 sizeof(mds_align) * nhead);
999 69 memcpy(alstore, aligns, sizeof(mds_align) * nhead);
1000 69 d.u.table.aligns = alstore;
1001 69 sax_enter(b, MDS_BLK_TABLE, &d);
1002 /* Head. Reuse the already-split header instead of splitting again. */
1003 69 memset(&hd, 0, sizeof hd);
1004 69 sax_enter(b, MDS_BLK_TABLE_HEAD, &hd);
1005 69 tbl_emit_row_pre(b, hs, hn, nhead, aligns, nhead);
1006 69 sax_leave(b, MDS_BLK_TABLE_HEAD);
1007 /* Body: keep consuming lines that contain at least one unescaped pipe. */
1008 69 row = p3;
1009 69 body_open = 0;
1010 166 ▶ while (row < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (row < end) { 97 69 166
1011 97 nx = tbl_next_line(row, end, &le);
1012 97 ▶ if (le == row) { row = nx; break; } /* blank */
Branch: 1 branch
BranchTrueFalseTotal
if (le == row) { row = nx; break; } /* blank */ 0 97 97
1013 97 ▶ if (!tbl_find_pipe(row, le)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!tbl_find_pipe(row, le)) break; 0 97 97
1014 97 ▶ if (!body_open) {
Branch: 1 branch
BranchTrueFalseTotal
if (!body_open) { 57 40 97
1015 57 memset(&bd, 0, sizeof bd);
1016 57 sax_enter(b, MDS_BLK_TABLE_BODY, &bd);
1017 57 body_open = 1;
1018 }
1019 97 tbl_emit_row(b, row, (size_t)(le - row), aligns, nhead);
1020 97 row = nx;
1021 }
1022 69 ▶ if (body_open) sax_leave(b, MDS_BLK_TABLE_BODY);
Branch: 1 branch
BranchTrueFalseTotal
if (body_open) sax_leave(b, MDS_BLK_TABLE_BODY); 57 12 69
1023 69 sax_leave(b, MDS_BLK_TABLE);
1024 69 return (size_t)(row - p);
1025 }
1026  
1027 /* ---------- leaf finalisation ---------- */
1028  
1029 4507879 static void finalize_paragraph(bscanner* b) {
1030 /* Consume any leading link-reference and footnote definitions. */
1031 char* p;
1032 char* end;
1033 unsigned _bf;
1034 const char* np;
1035 const char *ls, *le_, *us, *ue, *ts, *te;
1036 const char *fls, *fle, *bs, *be;
1037 size_t blen;
1038 size_t rem;
1039 char c;
1040 size_t lead;
1041 int sx;
1042 mds_block_detail d;
1043 const char* tp;
1044 const char* tend;
1045 const char* run;
1046 const char* le;
1047 const char* nx;
1048 const char* re;
1049 size_t consumed;
1050  
1051 4507879 p = b->para;
1052 4507879 end = b->para + b->para_len;
1053 4507879 _bf = b->ctx->flags;
1054 5316141 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 4507903 808238 5316141
1055 /* Footnote def MUST be checked before linkref, since a label
1056 * beginning with `^` would otherwise be eaten as a linkref. */
1057 4507903 ▶ if ((_bf & MDS_FLAG_FOOTNOTES)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((_bf & MDS_FLAG_FOOTNOTES)) { 4465126 42777 4507903
1058 4465126 ▶ if (parse_footnote_def(p, end, &np, &fls, &fle, &bs, &be)) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_footnote_def(p, end, &np, &fls, &fle, &bs, &be)) { 0 4465126 4465126
1059 0 ensure_footnote_tab(b->ctx);
1060 0 blen = (size_t)(be - bs);
1061 0 ▶ while (blen > 0 && (bs[blen - 1] == '\n' || bs[blen - 1] == ' ' ||
Branch: 3 branches
BranchTrueFalseTotal
while (blen > 0 && (bs[blen - 1] == '\n' || bs[blen - 1] == ' ' || 0 0 0
while (blen > 0 && (bs[blen - 1] == '\n' || bs[blen - 1] == ' ' || 0 0 0
while (blen > 0 && (bs[blen - 1] == '\n' || bs[blen - 1] == ' ' || 0 0 0
1062 0 ▶ bs[blen - 1] == '\t')) blen--;
Branch: 1 branch
BranchTrueFalseTotal
bs[blen - 1] == '\t')) blen--; 0 0 0
1063 0 mds_footnote_add(b->ctx->footnotes,
1064 0 fls, (size_t)(fle - fls),
1065 bs, blen);
1066 0 p = (char*)np;
1067 0 continue;
1068 }
1069 }
1070 9015804 if (!(_bf & MDS_FLAG_NO_REFERENCES) &&
1071 4507901 parse_linkref(p, end, &np, &ls, &le_, &us, &ue, &ts, &te)) {
1072 808262 ensure_linkref_tab(b->ctx);
1073 1616418 mds_linkref_add(b->ctx->refs,
1074 808262 ls, (size_t)(le_ - ls),
1075 808262 us, (size_t)(ue - us),
1076 1616418 ▶ ts ? ts : "", ts ? (size_t)(te - ts) : 0);
Branch: 2 branches
BranchTrueFalseTotal
ts ? ts : "", ts ? (size_t)(te - ts) : 0); 106 808156 808262
ts ? ts : "", ts ? (size_t)(te - ts) : 0); 106 808156 808262
1077 808262 p = (char*)np;
1078 808262 continue;
1079 }
1080 3699641 break;
1081 }
1082 /* Shift remaining content to the start of the buffer. */
1083 4507879 ▶ if (p > b->para) {
Branch: 1 branch
BranchTrueFalseTotal
if (p > b->para) { 808244 3699635 4507879
1084 808244 rem = (size_t)(end - p);
1085 808244 ▶ if (rem) memmove(b->para, p, rem);
Branch: 1 branch
BranchTrueFalseTotal
if (rem) memmove(b->para, p, rem); 12 808232 808244
1086 808244 b->para_len = rem;
1087 }
1088 /* Trim trailing whitespace. */
1089 4507909 ▶ while (b->para_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (b->para_len > 0) { 3699671 808238 4507909
1090 3699671 c = b->para[b->para_len - 1];
1091 3699671 ▶ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') b->para_len--;
Branch: 4 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') b->para_len--; 3699644 27 3699671
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') b->para_len--; 3699641 3 3699644
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') b->para_len--; 3699641 0 3699641
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') b->para_len--; 0 3699641 3699641
1092 else break;
1093 }
1094 /* Trim leading whitespace. */
1095 4507879 lead = 0;
1096 4507880 ▶ while (lead < b->para_len &&
Branch: 1 branch
BranchTrueFalseTotal
while (lead < b->para_len && 3699642 808238 4507880
1097 3699642 ▶ (b->para[lead] == ' ' || b->para[lead] == '\t' || b->para[lead] == '\n'))
Branch: 3 branches
BranchTrueFalseTotal
(b->para[lead] == ' ' || b->para[lead] == '\t' || b->para[lead] == '\n')) 1 3699641 3699642
(b->para[lead] == ' ' || b->para[lead] == '\t' || b->para[lead] == '\n')) 0 3699641 3699641
(b->para[lead] == ' ' || b->para[lead] == '\t' || b->para[lead] == '\n')) 0 3699641 3699641
1098 1 lead++;
1099 4507879 ▶ if (lead) { memmove(b->para, b->para + lead, b->para_len - lead); b->para_len -= lead; }
Branch: 1 branch
BranchTrueFalseTotal
if (lead) { memmove(b->para, b->para + lead, b->para_len - lead); b->para_len -= lead; } 1 4507878 4507879
1100 4507879 ▶ if (b->para_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->para_len == 0) { 808238 3699641 4507879
1101 808238 b->setext_level = 0;
1102 808238 return;
1103 }
1104 3699641 sx = b->setext_level;
1105 3699641 b->setext_level = 0;
1106 3699641 memset(&d, 0, sizeof d);
1107 3699641 ▶ if (sx) {
Branch: 1 branch
BranchTrueFalseTotal
if (sx) { 80 3699561 3699641
1108 80 d.u.heading.level = sx;
1109 80 sax_enter(b, MDS_BLK_HEADING, &d);
1110 80 sax_inline_text(b, b->para, b->para_len);
1111 80 sax_leave(b, MDS_BLK_HEADING);
1112 3699561 ▶ } else if ((b->ctx->flags & MDS_FLAG_TABLES) &&
Branch: 1 branch
BranchTrueFalseTotal
} else if ((b->ctx->flags & MDS_FLAG_TABLES) && 3697528 2033 3699561
1113 3697528 ▶ b->para_len >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
b->para_len >= 3) { 3697527 1 3697528
1114 /* Try GFM table detection: header line | separator. May appear
1115 * embedded — split paragraph into pre-text, table(s), post-text. */
1116 3697527 tp = b->para;
1117 3697527 tend = b->para + b->para_len;
1118 3697527 run = tp;
1119 7778889 ▶ while (tp < tend) {
Branch: 1 branch
BranchTrueFalseTotal
while (tp < tend) { 4081362 3697527 7778889
1120 /* find current line bounds */
1121 4081362 nx = next_line(tp, tend, &le);
1122 4081362 ▶ if (tbl_peek_header(tp, tend)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tbl_peek_header(tp, tend)) { 69 4081293 4081362
1123 /* flush any prior text as paragraph BEFORE emitting table */
1124 69 ▶ if (tp > run) {
Branch: 1 branch
BranchTrueFalseTotal
if (tp > run) { 3 66 69
1125 3 re = tp;
1126 6 ▶ while (re > run && (re[-1] == '\n' || re[-1] == '\r' ||
Branch: 3 branches
BranchTrueFalseTotal
while (re > run && (re[-1] == '\n' || re[-1] == '\r' || 6 0 6
while (re > run && (re[-1] == '\n' || re[-1] == '\r' || 3 3 6
while (re > run && (re[-1] == '\n' || re[-1] == '\r' || 0 3 3
1127 6 ▶ re[-1] == ' ' || re[-1] == '\t')) re--;
Branch: 2 branches
BranchTrueFalseTotal
re[-1] == ' ' || re[-1] == '\t')) re--; 0 3 3
re[-1] == ' ' || re[-1] == '\t')) re--; 0 3 3
1128 3 ▶ if (re > run) {
Branch: 1 branch
BranchTrueFalseTotal
if (re > run) { 3 0 3
1129 3 sax_enter(b, MDS_BLK_PARAGRAPH, &d);
1130 3 sax_inline_text(b, run, (size_t)(re - run));
1131 3 sax_leave(b, MDS_BLK_PARAGRAPH);
1132 }
1133 }
1134 69 consumed = try_emit_table(b, tp, tend);
1135 69 tp = tp + consumed;
1136 69 run = tp;
1137 69 continue;
1138 }
1139 (void)le;
1140 4081293 tp = nx;
1141 }
1142 3697527 ▶ if (run < tend) {
Branch: 1 branch
BranchTrueFalseTotal
if (run < tend) { 3697458 69 3697527
1143 3697458 re = tend;
1144 3697458 ▶ while (re > run && (re[-1] == '\n' || re[-1] == '\r' ||
Branch: 3 branches
BranchTrueFalseTotal
while (re > run && (re[-1] == '\n' || re[-1] == '\r' || 3697458 0 3697458
while (re > run && (re[-1] == '\n' || re[-1] == '\r' || 0 3697458 3697458
while (re > run && (re[-1] == '\n' || re[-1] == '\r' || 0 3697458 3697458
1145 3697458 ▶ re[-1] == ' ' || re[-1] == '\t')) re--;
Branch: 2 branches
BranchTrueFalseTotal
re[-1] == ' ' || re[-1] == '\t')) re--; 0 3697458 3697458
re[-1] == ' ' || re[-1] == '\t')) re--; 0 3697458 3697458
1146 3697458 ▶ if (re > run) {
Branch: 1 branch
BranchTrueFalseTotal
if (re > run) { 3697458 0 3697458
1147 3697458 sax_enter(b, MDS_BLK_PARAGRAPH, &d);
1148 3697458 sax_inline_text(b, run, (size_t)(re - run));
1149 3697458 sax_leave(b, MDS_BLK_PARAGRAPH);
1150 }
1151 }
1152 } else {
1153 2034 sax_enter(b, MDS_BLK_PARAGRAPH, &d);
1154 2034 sax_inline_text(b, b->para, b->para_len);
1155 2034 sax_leave(b, MDS_BLK_PARAGRAPH);
1156 }
1157 3699641 b->para_len = 0;
1158 }
1159  
1160 170 static void finalize_code_indented(bscanner* b) {
1161 size_t i;
1162 size_t ls;
1163 size_t j;
1164 int blank;
1165 mds_block_detail d;
1166 /* Strip trailing blank lines. */
1167 173 ▶ while (b->code_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (b->code_len > 0) { 173 0 173
1168 173 i = b->code_len;
1169 /* find start of last line */
1170 173 ls = i;
1171 173 ▶ if (ls > 0) ls--; /* skip its '\n' */
Branch: 1 branch
BranchTrueFalseTotal
if (ls > 0) ls--; /* skip its '\n' */ 173 0 173
1172 1585 ▶ while (ls > 0 && b->code_body[ls - 1] != '\n') ls--;
Branch: 2 branches
BranchTrueFalseTotal
while (ls > 0 && b->code_body[ls - 1] != '\n') ls--; 1455 130 1585
while (ls > 0 && b->code_body[ls - 1] != '\n') ls--; 1412 43 1455
1173 173 blank = 1;
1174 221 ▶ for (j = ls; j + 1 < i; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = ls; j + 1 < i; j++) { 218 3 221
1175 218 ▶ if (b->code_body[j] != ' ' && b->code_body[j] != '\t') { blank = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (b->code_body[j] != ' ' && b->code_body[j] != '\t') { blank = 0; break; } 170 48 218
if (b->code_body[j] != ' ' && b->code_body[j] != '\t') { blank = 0; break; } 170 0 170
1176 }
1177 173 ▶ if (!blank) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!blank) break; 170 3 173
1178 3 b->code_len = ls;
1179 }
1180 170 memset(&d, 0, sizeof d);
1181 170 sax_enter(b, MDS_BLK_CODE_INDENTED, &d);
1182 170 ▶ if (b->code_len) sax_text(b, b->code_body, b->code_len);
Branch: 1 branch
BranchTrueFalseTotal
if (b->code_len) sax_text(b, b->code_body, b->code_len); 170 0 170
1183 170 sax_leave(b, MDS_BLK_CODE_INDENTED);
1184 170 b->code_len = 0;
1185 170 b->pending_blanks = 0;
1186 170 }
1187  
1188 808139 static void finalize_code_fenced(bscanner* b) {
1189 mds_block_detail d;
1190 808139 memset(&d, 0, sizeof d);
1191 808139 d.u.code_fenced.info = b->fence_info;
1192 808139 d.u.code_fenced.info_len = b->fence_info_len;
1193 808139 sax_enter(b, MDS_BLK_CODE_FENCED, &d);
1194 808139 ▶ if (b->code_len) sax_text(b, b->code_body, b->code_len);
Branch: 1 branch
BranchTrueFalseTotal
if (b->code_len) sax_text(b, b->code_body, b->code_len); 808124 15 808139
1195 808139 sax_leave(b, MDS_BLK_CODE_FENCED);
1196 808139 b->code_len = 0;
1197 808139 b->fence_info = NULL;
1198 808139 b->fence_info_len = 0;
1199 808139 }
1200  
1201 200 static void finalize_html(bscanner* b) {
1202 size_t i;
1203 size_t ls;
1204 size_t j;
1205 int blank;
1206 mds_block_detail d;
1207 /* trim trailing blank lines */
1208 200 ▶ while (b->html_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (b->html_len > 0) { 200 0 200
1209 200 i = b->html_len;
1210 200 ls = i;
1211 200 ▶ if (ls > 0) ls--;
Branch: 1 branch
BranchTrueFalseTotal
if (ls > 0) ls--; 200 0 200
1212 2666 ▶ while (ls > 0 && b->html_body[ls - 1] != '\n') ls--;
Branch: 2 branches
BranchTrueFalseTotal
while (ls > 0 && b->html_body[ls - 1] != '\n') ls--; 2562 104 2666
while (ls > 0 && b->html_body[ls - 1] != '\n') ls--; 2466 96 2562
1213 200 blank = 1;
1214 260 ▶ for (j = ls; j + 1 < i; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = ls; j + 1 < i; j++) { 260 0 260
1215 260 ▶ if (b->html_body[j] != ' ' && b->html_body[j] != '\t') { blank = 0; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (b->html_body[j] != ' ' && b->html_body[j] != '\t') { blank = 0; break; } 200 60 260
if (b->html_body[j] != ' ' && b->html_body[j] != '\t') { blank = 0; break; } 200 0 200
1216 }
1217 200 ▶ if (!blank) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!blank) break; 200 0 200
1218 0 b->html_len = ls;
1219 }
1220 200 memset(&d, 0, sizeof d);
1221 200 sax_enter(b, MDS_BLK_HTML, &d);
1222 200 ▶ if (b->html_len) sax_raw(b, b->html_body, b->html_len);
Branch: 1 branch
BranchTrueFalseTotal
if (b->html_len) sax_raw(b, b->html_body, b->html_len); 200 0 200
1223 200 sax_leave(b, MDS_BLK_HTML);
1224 200 b->html_len = 0;
1225 200 b->html_type = 0;
1226 200 }
1227  
1228 6672484 static void finalize_leaf(bscanner* b) {
1229 6672484 switch (b->leaf) {
1230 4507879 case LF_PARAGRAPH: finalize_paragraph(b); break;
1231 808139 case LF_CODE_FENCED: finalize_code_fenced(b); break;
1232 170 case LF_CODE_INDENTED: finalize_code_indented(b); break;
1233 200 case LF_HTML: finalize_html(b); break;
1234 1356096 case LF_NONE: break;
1235 }
1236 6672484 b->leaf = LF_NONE;
1237 6672484 }
1238  
1239 /* ---------- container emit ---------- */
1240  
1241 1151 static void emit_open(bscanner* b, int idx) {
1242 1151 ctn* c = &b->stack[idx];
1243 mds_block_detail d;
1244 1151 ▶ if (c->opened) return;
Branch: 1 branch
BranchTrueFalseTotal
if (c->opened) return; 0 1151 1151
1245 1151 c->opened = 1;
1246 1151 memset(&d, 0, sizeof d);
1247 1151 ▶ if (c->kind == CT_QUOTE) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->kind == CT_QUOTE) { 177 974 1151
1248 177 sax_enter(b, MDS_BLK_QUOTE, &d);
1249 974 ▶ } else if (c->kind == CT_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->kind == CT_LIST) { 379 595 974
1250 379 d.u.list.is_ordered = c->ordered;
1251 379 d.u.list.is_tight = c->tight;
1252 379 d.u.list.start = c->start;
1253 379 d.u.list.marker = c->marker;
1254 379 c->ev_idx = sax_enter(b, MDS_BLK_LIST, &d);
1255 595 ▶ } else if (c->kind == CT_LIST_ITEM) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->kind == CT_LIST_ITEM) { 595 0 595
1256 595 sax_enter(b, MDS_BLK_LIST_ITEM, &d);
1257 }
1258 }
1259  
1260 1151 static void emit_close(bscanner* b, ctn* c) {
1261 1151 ▶ if (!c->opened) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->opened) return; 0 1151 1151
1262 1151 ▶ if (c->kind == CT_QUOTE) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->kind == CT_QUOTE) { 177 974 1151
1263 177 sax_leave(b, MDS_BLK_QUOTE);
1264 974 ▶ } else if (c->kind == CT_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->kind == CT_LIST) { 379 595 974
1265 /* Patch is_tight on the buffered enter event if any. */
1266 379 ▶ if (c->ev_idx >= 0 && (size_t)c->ev_idx < b->ev_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->ev_idx >= 0 && (size_t)c->ev_idx < b->ev_len) { 379 0 379
if (c->ev_idx >= 0 && (size_t)c->ev_idx < b->ev_len) { 379 0 379
1267 379 ev_rec* e = &b->evbuf[c->ev_idx];
1268 379 ▶ if (e->type == EV_ENTER_BLOCK && e->u.enter.t == MDS_BLK_LIST) {
Branch: 2 branches
BranchTrueFalseTotal
if (e->type == EV_ENTER_BLOCK && e->u.enter.t == MDS_BLK_LIST) { 379 0 379
if (e->type == EV_ENTER_BLOCK && e->u.enter.t == MDS_BLK_LIST) { 379 0 379
1269 379 e->u.enter.d.u.list.is_tight = c->had_blank_inside ? 0 : 1;
1270 }
1271 }
1272 379 sax_leave(b, MDS_BLK_LIST);
1273 379 b->list_depth--;
1274 /* Doc-wide buffering: flush only at end of mds_block_scan. */
1275 595 ▶ } else if (c->kind == CT_LIST_ITEM) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->kind == CT_LIST_ITEM) { 595 0 595
1276 595 sax_leave(b, MDS_BLK_LIST_ITEM);
1277 }
1278 }
1279  
1280 6713816 static void close_containers_to(bscanner* b, int target_depth) {
1281 ctn* c;
1282 6714912 ▶ while (b->depth > target_depth) {
Branch: 1 branch
BranchTrueFalseTotal
while (b->depth > target_depth) { 1096 6713816 6714912
1283 1096 finalize_leaf(b);
1284 1096 c = &b->stack[b->depth - 1];
1285 1096 emit_close(b, c);
1286 1096 b->depth--;
1287 }
1288 6713816 }
1289  
1290 /* ---------- line helpers ---------- */
1291  
1292 17864402 static const char* next_line(const char* p, const char* end,
1293 const char** line_end_out) {
1294 const char* nl;
1295 const char* le;
1296 17864402 nl = (const char*)memchr(p, '\n', (size_t)(end - p));
1297 17864402 ▶ if (!nl) { *line_end_out = end; return end; }
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) { *line_end_out = end; return end; } 3737918 14126484 17864402
1298 14126484 le = nl;
1299 14126484 ▶ if (le > p && *(le - 1) == '\r') le--;
Branch: 2 branches
BranchTrueFalseTotal
if (le > p && *(le - 1) == '\r') le--; 8469329 5657155 14126484
if (le > p && *(le - 1) == '\r') le--; 0 8469329 8469329
1300 14126484 *line_end_out = le;
1301 14126484 return nl + 1;
1302 }
1303  
1304 532 static int consume_indent(const char** p, const char* end, int max) {
1305 532 int col = 0;
1306 532 const char* q = *p;
1307 2339 ▶ while (q < end && col < max) {
Branch: 2 branches
BranchTrueFalseTotal
while (q < end && col < max) { 2330 9 2339
while (q < end && col < max) { 1807 523 2330
1308 1807 ▶ if (*q == ' ') { col++; q++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*q == ' ') { col++; q++; } 1807 0 1807
1309 0 ▶ else if (*q == '\t') {
Branch: 1 branch
BranchTrueFalseTotal
else if (*q == '\t') { 0 0 0
1310 0 int adv = 4 - (col & 3);
1311 0 ▶ if (col + adv > max) break;
Branch: 1 branch
BranchTrueFalseTotal
if (col + adv > max) break; 0 0 0
1312 0 col += adv; q++;
1313 0 } else break;
1314 }
1315 532 *p = q;
1316 532 return col;
1317 }
1318  
1319 12230684 static int count_indent(const char* p, const char* end) {
1320 12230684 int col = 0;
1321 12233967 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 12233664 303 12233967
1322 12233664 ▶ if (*p == ' ') col++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ' ') col++; 3283 12230381 12233664
1323 12230381 ▶ else if (*p == '\t') col += 4 - (col & 3);
Branch: 1 branch
BranchTrueFalseTotal
else if (*p == '\t') col += 4 - (col & 3); 0 12230381 12230381
1324 12230381 else break;
1325 3283 p++;
1326 }
1327 12230684 return col;
1328 }
1329  
1330 20293328 static int is_blank(const char* p, const char* end) {
1331 20297940 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 14640719 5657221 20297940
1332 14640719 ▶ if (*p != ' ' && *p != '\t') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*p != ' ' && *p != '\t') return 0; 14636107 4612 14640719
if (*p != ' ' && *p != '\t') return 0; 14636107 0 14636107
1333 4612 p++;
1334 }
1335 5657221 return 1;
1336 }
1337  
1338 /* ---------- HTML block recognition (CommonMark §4.6) ---------- */
1339  
1340 2163 static int ascii_ieq(const char* a, const char* b, size_t n) {
1341 size_t i;
1342 char x, y;
1343 2967 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2813 154 2967
1344 2813 x = a[i]; y = b[i];
1345 2813 ▶ if (x >= 'A' && x <= 'Z') x = (char)(x + 32);
Branch: 2 branches
BranchTrueFalseTotal
if (x >= 'A' && x <= 'Z') x = (char)(x + 32); 2609 204 2813
if (x >= 'A' && x <= 'Z') x = (char)(x + 32); 126 2483 2609
1346 2813 ▶ if (y >= 'A' && y <= 'Z') y = (char)(y + 32);
Branch: 2 branches
BranchTrueFalseTotal
if (y >= 'A' && y <= 'Z') y = (char)(y + 32); 2813 0 2813
if (y >= 'A' && y <= 'Z') y = (char)(y + 32); 0 2813 2813
1347 2813 ▶ if (x != y) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (x != y) return 0; 2009 804 2813
1348 }
1349 154 return 1;
1350 }
1351  
1352 /* Type-6 block tag names (lowercased). Sorted by length then alpha for
1353 * a simple linear scan; the set is small enough that hashing isn't worth it. */
1354 static const char* const HTML6_TAGS[] = {
1355 "address","article","aside","base","basefont","blockquote","body","caption",
1356 "center","col","colgroup","dd","details","dialog","dir","div","dl","dt",
1357 "fieldset","figcaption","figure","footer","form","frame","frameset",
1358 "h1","h2","h3","h4","h5","h6","head","header","hr","html","iframe","legend",
1359 "li","link","main","menu","menuitem","nav","noframes","ol","optgroup","option",
1360 "p","param","search","section","summary","table","tbody","td","tfoot","th",
1361 "thead","title","tr","track","ul", NULL
1362 };
1363  
1364 255 static int is_html6_tag(const char* s, size_t n) {
1365 int i;
1366 12769 ▶ for (i = 0; HTML6_TAGS[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; HTML6_TAGS[i]; i++) { 12623 146 12769
1367 12623 size_t tl = strlen(HTML6_TAGS[i]);
1368 12623 ▶ if (tl == n && ascii_ieq(s, HTML6_TAGS[i], n)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (tl == n && ascii_ieq(s, HTML6_TAGS[i], n)) return 1; 1157 11466 12623
if (tl == n && ascii_ieq(s, HTML6_TAGS[i], n)) return 1; 109 1048 1157
1369 }
1370 146 return 0;
1371 }
1372  
1373 1748 static int is_alpha(char c) {
1374 1748 ▶ return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
Branch: 4 branches
BranchTrueFalseTotal
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); 1278 470 1748
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); 1215 63 1278
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); 1209 476 1685
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); 1209 0 1209
1375 }
1376 1551 static int is_alnum(char c) {
1377 1551 ▶ return is_alpha(c) || (c >= '0' && c <= '9');
Branch: 3 branches
BranchTrueFalseTotal
return is_alpha(c) || (c >= '0' && c <= '9'); 455 1096 1551
return is_alpha(c) || (c >= '0' && c <= '9'); 286 169 455
return is_alpha(c) || (c >= '0' && c <= '9'); 6 280 286
1378 }
1379  
1380 /* Type-7 helpers: validate a complete open or close tag on one line.
1381 * Returns 1 if `[p, end)` (after the initial '<') is a complete tag
1382 * followed only by whitespace. */
1383 134 static int is_type7_open_tag(const char* p, const char* end) {
1384 const char* name;
1385 size_t nlen;
1386 int i;
1387 const char* aws;
1388 const char* vs;
1389 char q;
1390 const char* uv;
1391 /* tag name: ASCII letter, then [A-Za-z0-9-]* */
1392 134 ▶ if (p >= end || !is_alpha(*p)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || !is_alpha(*p)) return 0; 134 0 134
if (p >= end || !is_alpha(*p)) return 0; 15 119 134
1393 119 name = p;
1394 119 p++;
1395 369 ▶ while (p < end && (is_alnum(*p) || *p == '-')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (is_alnum(*p) || *p == '-')) p++; 369 0 369
while (p < end && (is_alnum(*p) || *p == '-')) p++; 244 125 369
while (p < end && (is_alnum(*p) || *p == '-')) p++; 6 119 125
1396 119 nlen = (size_t)(p - name);
1397 /* disallowed tag names for type 7 */
1398 {
1399 static const char* const banned[] = {"script","pre","style","textarea",NULL};
1400 595 ▶ for (i = 0; banned[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; banned[i]; i++) { 476 119 595
1401 476 size_t bl = strlen(banned[i]);
1402 476 ▶ if (bl == nlen && ascii_ieq(name, banned[i], nlen)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (bl == nlen && ascii_ieq(name, banned[i], nlen)) return 0; 46 430 476
if (bl == nlen && ascii_ieq(name, banned[i], nlen)) return 0; 0 46 46
1403 }
1404 }
1405 /* attributes */
1406 149 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 146 3 149
1407 /* whitespace then attr-name */
1408 146 aws = p;
1409 194 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 194 0 194
while (p < end && (*p == ' ' || *p == '\t')) p++; 48 146 194
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 146 146
1410 146 ▶ if (p == aws) break; /* must have ws before attr */
Branch: 1 branch
BranchTrueFalseTotal
if (p == aws) break; /* must have ws before attr */ 101 45 146
1411 45 ▶ if (p >= end || *p == '/' || *p == '>') break;
Branch: 3 branches
BranchTrueFalseTotal
if (p >= end || *p == '/' || *p == '>') break; 45 0 45
if (p >= end || *p == '/' || *p == '>') break; 42 3 45
if (p >= end || *p == '/' || *p == '>') break; 42 0 42
1412 42 ▶ if (!is_alpha(*p) && *p != '_' && *p != ':') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!is_alpha(*p) && *p != '_' && *p != ':') return 0; 0 42 42
if (!is_alpha(*p) && *p != '_' && *p != ':') return 0; 0 0 0
if (!is_alpha(*p) && *p != '_' && *p != ':') return 0; 0 0 0
1413 42 p++;
1414 159 ▶ while (p < end && (is_alnum(*p) || *p == '_' || *p == ':' || *p == '.' || *p == '-')) p++;
Branch: 6 branches
BranchTrueFalseTotal
while (p < end && (is_alnum(*p) || *p == '_' || *p == ':' || *p == '.' || *p == '-')) p++; 159 0 159
while (p < end && (is_alnum(*p) || *p == '_' || *p == ':' || *p == '.' || *p == '-')) p++; 117 42 159
while (p < end && (is_alnum(*p) || *p == '_' || *p == ':' || *p == '.' || *p == '-')) p++; 0 42 42
while (p < end && (is_alnum(*p) || *p == '_' || *p == ':' || *p == '.' || *p == '-')) p++; 0 42 42
while (p < end && (is_alnum(*p) || *p == '_' || *p == ':' || *p == '.' || *p == '-')) p++; 0 42 42
while (p < end && (is_alnum(*p) || *p == '_' || *p == ':' || *p == '.' || *p == '-')) p++; 0 42 42
1415 /* optional value */
1416 42 vs = p;
1417 45 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 45 0 45
while (p < end && (*p == ' ' || *p == '\t')) p++; 3 42 45
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 42 42
1418 42 ▶ if (p < end && *p == '=') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '=') { 42 0 42
if (p < end && *p == '=') { 39 3 42
1419 39 p++;
1420 42 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 42 0 42
while (p < end && (*p == ' ' || *p == '\t')) p++; 3 39 42
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 39 39
1421 39 ▶ if (p >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return 0; 0 39 39
1422 39 ▶ if (*p == '"' || *p == '\'') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '"' || *p == '\'') { 6 33 39
if (*p == '"' || *p == '\'') { 6 0 6
1423 39 q = *p++;
1424 288 ▶ while (p < end && *p != q) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != q) p++; 276 12 288
while (p < end && *p != q) p++; 249 27 276
1425 39 ▶ if (p >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return 0; 12 27 39
1426 27 p++;
1427 } else {
1428 0 uv = p;
1429 0 ▶ while (p < end && *p != ' ' && *p != '\t' && *p != '\"'
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p != ' ' && *p != '\t' && *p != '\"' 0 0 0
while (p < end && *p != ' ' && *p != '\t' && *p != '\"' 0 0 0
while (p < end && *p != ' ' && *p != '\t' && *p != '\"' 0 0 0
1430 0 ▶ && *p != '\'' && *p != '=' && *p != '<' && *p != '>'
Branch: 4 branches
BranchTrueFalseTotal
&& *p != '\'' && *p != '=' && *p != '<' && *p != '>' 0 0 0
&& *p != '\'' && *p != '=' && *p != '<' && *p != '>' 0 0 0
&& *p != '\'' && *p != '=' && *p != '<' && *p != '>' 0 0 0
&& *p != '\'' && *p != '=' && *p != '<' && *p != '>' 0 0 0
1431 0 ▶ && *p != '`') p++;
Branch: 2 branches
BranchTrueFalseTotal
&& *p != '`') p++; 0 0 0
&& *p != '`') p++; 0 0 0
1432 0 ▶ if (p == uv) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p == uv) return 0; 0 0 0
1433 }
1434 } else {
1435 3 p = vs; /* no value, rewind */
1436 }
1437 }
1438 107 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 104 3 107
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 104 104
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 104 104
1439 107 ▶ if (p < end && *p == '/') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '/') p++; 104 3 107
if (p < end && *p == '/') p++; 6 98 104
1440 107 ▶ if (p >= end || *p != '>') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != '>') return 0; 104 3 107
if (p >= end || *p != '>') return 0; 66 38 104
1441 38 p++;
1442 38 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 17 21 38
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 17 17
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 17 17
1443 38 return p == end;
1444 }
1445  
1446 12 static int is_type7_close_tag(const char* p, const char* end) {
1447 const char* name;
1448 size_t nlen;
1449 int i;
1450 /* already past '</' */
1451 12 ▶ if (p >= end || !is_alpha(*p)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || !is_alpha(*p)) return 0; 12 0 12
if (p >= end || !is_alpha(*p)) return 0; 0 12 12
1452 12 name = p;
1453 12 p++;
1454 24 ▶ while (p < end && (is_alnum(*p) || *p == '-')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (is_alnum(*p) || *p == '-')) p++; 24 0 24
while (p < end && (is_alnum(*p) || *p == '-')) p++; 12 12 24
while (p < end && (is_alnum(*p) || *p == '-')) p++; 0 12 12
1455 12 nlen = (size_t)(p - name);
1456 {
1457 static const char* const banned[] = {"script","pre","style","textarea",NULL};
1458 60 ▶ for (i = 0; banned[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; banned[i]; i++) { 48 12 60
1459 48 size_t bl = strlen(banned[i]);
1460 48 ▶ if (bl == nlen && ascii_ieq(name, banned[i], nlen)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (bl == nlen && ascii_ieq(name, banned[i], nlen)) return 0; 6 42 48
if (bl == nlen && ascii_ieq(name, banned[i], nlen)) return 0; 0 6 6
1461 }
1462 }
1463 15 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 15 0 15
while (p < end && (*p == ' ' || *p == '\t')) p++; 3 12 15
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 12 12
1464 12 ▶ if (p >= end || *p != '>') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != '>') return 0; 12 0 12
if (p >= end || *p != '>') return 0; 3 9 12
1465 9 p++;
1466 9 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 3 6 9
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 3 3
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 3 3
1467 9 return p == end;
1468 }
1469  
1470 /* Detect HTML block start. Returns 1..7 on match, 0 otherwise.
1471 * Caller has already stripped container prefixes; `p` is the line start
1472 * after container content_col indent (but possibly with up to 3 leading
1473 * spaces left). `allow_type7` is 0 when inside a paragraph (rule 7
1474 * cannot interrupt). */
1475 6509182 static int detect_html_block_start(const char* p, const char* end, int allow_type7) {
1476 6509182 int lead = 0;
1477 const char* q;
1478 6509634 ▶ while (p < end && *p == ' ' && lead < 3) { p++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 6509625 9 6509634
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 501 6509124 6509625
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 452 49 501
1479 6509182 ▶ if (p >= end || *p != '<') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p >= end || *p != '<') return 0; 6509173 9 6509182
if (p >= end || *p != '<') return 0; 6508839 334 6509173
1480 334 q = p + 1;
1481 /* Type 2: <!-- */
1482 334 ▶ if (q + 2 < end && q[0] == '!' && q[1] == '-' && q[2] == '-') return 2;
Branch: 4 branches
BranchTrueFalseTotal
if (q + 2 < end && q[0] == '!' && q[1] == '-' && q[2] == '-') return 2; 331 3 334
if (q + 2 < end && q[0] == '!' && q[1] == '-' && q[2] == '-') return 2; 43 288 331
if (q + 2 < end && q[0] == '!' && q[1] == '-' && q[2] == '-') return 2; 34 9 43
if (q + 2 < end && q[0] == '!' && q[1] == '-' && q[2] == '-') return 2; 31 3 34
1483 /* Type 3: <? */
1484 303 ▶ if (q < end && *q == '?') return 3;
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == '?') return 3; 303 0 303
if (q < end && *q == '?') return 3; 3 300 303
1485 /* Type 5: <![CDATA[ */
1486 300 ▶ if (q + 7 < end && q[0] == '!' && q[1] == '[' && memcmp(q+2,"CDATA[",6) == 0) return 5;
Branch: 4 branches
BranchTrueFalseTotal
if (q + 7 < end && q[0] == '!' && q[1] == '[' && memcmp(q+2,"CDATA[",6) == 0) return 5; 198 102 300
if (q + 7 < end && q[0] == '!' && q[1] == '[' && memcmp(q+2,"CDATA[",6) == 0) return 5; 12 186 198
if (q + 7 < end && q[0] == '!' && q[1] == '[' && memcmp(q+2,"CDATA[",6) == 0) return 5; 3 9 12
if (q + 7 < end && q[0] == '!' && q[1] == '[' && memcmp(q+2,"CDATA[",6) == 0) return 5; 3 0 3
1487 /* Type 4: <! followed by ASCII letter */
1488 297 ▶ if (q + 1 < end && *q == '!' && is_alpha(q[1])) return 4;
Branch: 3 branches
BranchTrueFalseTotal
if (q + 1 < end && *q == '!' && is_alpha(q[1])) return 4; 294 3 297
if (q + 1 < end && *q == '!' && is_alpha(q[1])) return 4; 9 285 294
if (q + 1 < end && *q == '!' && is_alpha(q[1])) return 4; 3 6 9
1489 /* Type 1: <script, <pre, <style, <textarea, optionally case-insensitive,
1490 * followed by ws, '>' or EOL */
1491 {
1492 static const char* const t1[] = {"script","pre","style","textarea",NULL};
1493 int i;
1494 1404 ▶ for (i = 0; t1[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; t1[i]; i++) { 1134 270 1404
1495 1134 size_t tl = strlen(t1[i]);
1496 1134 ▶ if ((size_t)(end - q) >= tl && ascii_ieq(q, t1[i], tl)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((size_t)(end - q) >= tl && ascii_ieq(q, t1[i], tl)) { 906 228 1134
if ((size_t)(end - q) >= tl && ascii_ieq(q, t1[i], tl)) { 24 882 906
1497 24 const char* a = q + tl;
1498 24 ▶ if (a == end || *a == ' ' || *a == '\t' || *a == '>') return 1;
Branch: 4 branches
BranchTrueFalseTotal
if (a == end || *a == ' ' || *a == '\t' || *a == '>') return 1; 18 6 24
if (a == end || *a == ' ' || *a == '\t' || *a == '>') return 1; 12 6 18
if (a == end || *a == ' ' || *a == '\t' || *a == '>') return 1; 12 0 12
if (a == end || *a == ' ' || *a == '\t' || *a == '>') return 1; 12 0 12
1499 }
1500 }
1501 }
1502 /* Type 6: tag name in HTML6_TAGS, possibly with leading '/' for close */
1503 {
1504 270 const char* r = q;
1505 270 int closing = 0;
1506 const char* name;
1507 size_t nlen;
1508 270 ▶ if (r < end && *r == '/') { closing = 1; r++; }
Branch: 2 branches
BranchTrueFalseTotal
if (r < end && *r == '/') { closing = 1; r++; } 270 0 270
if (r < end && *r == '/') { closing = 1; r++; } 39 231 270
1509 270 name = r;
1510 999 ▶ while (r < end && is_alnum(*r)) r++;
Branch: 2 branches
BranchTrueFalseTotal
while (r < end && is_alnum(*r)) r++; 999 0 999
while (r < end && is_alnum(*r)) r++; 729 270 999
1511 270 nlen = (size_t)(r - name);
1512 270 ▶ if (nlen > 0 && is_html6_tag(name, nlen)) {
Branch: 2 branches
BranchTrueFalseTotal
if (nlen > 0 && is_html6_tag(name, nlen)) { 255 15 270
if (nlen > 0 && is_html6_tag(name, nlen)) { 109 146 255
1513 109 ▶ if (r == end || *r == ' ' || *r == '\t' || *r == '>'
Branch: 4 branches
BranchTrueFalseTotal
if (r == end || *r == ' ' || *r == '\t' || *r == '>' 109 0 109
if (r == end || *r == ' ' || *r == '\t' || *r == '>' 90 19 109
if (r == end || *r == ' ' || *r == '\t' || *r == '>' 90 0 90
if (r == end || *r == ' ' || *r == '\t' || *r == '>' 0 90 90
1514 0 ▶ || (!closing && r + 1 <= end && *r == '/' && r + 1 < end && r[1] == '>'))
Branch: 5 branches
BranchTrueFalseTotal
|| (!closing && r + 1 <= end && *r == '/' && r + 1 < end && r[1] == '>')) 0 0 0
|| (!closing && r + 1 <= end && *r == '/' && r + 1 < end && r[1] == '>')) 0 0 0
|| (!closing && r + 1 <= end && *r == '/' && r + 1 < end && r[1] == '>')) 0 0 0
|| (!closing && r + 1 <= end && *r == '/' && r + 1 < end && r[1] == '>')) 0 0 0
|| (!closing && r + 1 <= end && *r == '/' && r + 1 < end && r[1] == '>')) 0 0 0
1515 109 return 6;
1516 }
1517 (void)closing;
1518 }
1519 /* Type 7: complete open or close tag followed only by whitespace */
1520 161 ▶ if (allow_type7) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow_type7) { 146 15 161
1521 146 const char* r = q;
1522 146 ▶ if (r < end && *r == '/') {
Branch: 2 branches
BranchTrueFalseTotal
if (r < end && *r == '/') { 146 0 146
if (r < end && *r == '/') { 12 134 146
1523 12 ▶ if (is_type7_close_tag(r + 1, end)) return 7;
Branch: 1 branch
BranchTrueFalseTotal
if (is_type7_close_tag(r + 1, end)) return 7; 6 6 12
1524 } else {
1525 134 ▶ if (is_type7_open_tag(r, end)) return 7;
Branch: 1 branch
BranchTrueFalseTotal
if (is_type7_open_tag(r, end)) return 7; 21 113 134
1526 }
1527 }
1528 134 return 0;
1529 }
1530  
1531 /* Detect HTML block end for the given type, examining the whole line p..end. */
1532 464 static int detect_html_block_end(int type, const char* p, const char* end, int blank) {
1533 const char* q;
1534 464 ▶ if (blank) {
Branch: 1 branch
BranchTrueFalseTotal
if (blank) { 36 428 464
1535 36 ▶ return (type == 6 || type == 7);
Branch: 2 branches
BranchTrueFalseTotal
return (type == 6 || type == 7); 36 0 36
return (type == 6 || type == 7); 0 36 36
1536 }
1537 428 switch (type) {
1538 78 case 1: {
1539 /* line contains </script>, </pre>, </style>, or </textarea> (ci) */
1540 1172 ▶ for (q = p; q < end; q++) {
Branch: 1 branch
BranchTrueFalseTotal
for (q = p; q < end; q++) { 1115 57 1172
1541 1115 ▶ if (*q != '<') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (*q != '<') continue; 1064 51 1115
1542 51 ▶ if (q + 1 >= end || q[1] != '/') continue;
Branch: 2 branches
BranchTrueFalseTotal
if (q + 1 >= end || q[1] != '/') continue; 51 0 51
if (q + 1 >= end || q[1] != '/') continue; 27 24 51
1543 {
1544 24 const char* r = q + 2;
1545 static const char* const t1[] = {"script","pre","style","textarea",NULL};
1546 int i;
1547 60 ▶ for (i = 0; t1[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; t1[i]; i++) { 57 3 60
1548 57 size_t tl = strlen(t1[i]);
1549 57 ▶ if ((size_t)(end - r) >= tl + 1
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(end - r) >= tl + 1 48 9 57
1550 48 ▶ && ascii_ieq(r, t1[i], tl) && r[tl] == '>') return 1;
Branch: 2 branches
BranchTrueFalseTotal
&& ascii_ieq(r, t1[i], tl) && r[tl] == '>') return 1; 21 27 48
&& ascii_ieq(r, t1[i], tl) && r[tl] == '>') return 1; 21 0 21
1551 }
1552 }
1553 }
1554 57 return 0;
1555 }
1556 43 case 2:
1557 842 ▶ for (q = p; q + 2 < end; q++)
Branch: 1 branch
BranchTrueFalseTotal
for (q = p; q + 2 < end; q++) 830 12 842
1558 830 ▶ if (q[0] == '-' && q[1] == '-' && q[2] == '>') return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (q[0] == '-' && q[1] == '-' && q[2] == '>') return 1; 96 734 830
if (q[0] == '-' && q[1] == '-' && q[2] == '>') return 1; 68 28 96
if (q[0] == '-' && q[1] == '-' && q[2] == '>') return 1; 31 37 68
1559 12 return 0;
1560 9 case 3:
1561 51 ▶ for (q = p; q + 1 < end; q++)
Branch: 1 branch
BranchTrueFalseTotal
for (q = p; q + 1 < end; q++) 45 6 51
1562 45 ▶ if (q[0] == '?' && q[1] == '>') return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (q[0] == '?' && q[1] == '>') return 1; 6 39 45
if (q[0] == '?' && q[1] == '>') return 1; 3 3 6
1563 6 return 0;
1564 3 case 4:
1565 45 ▶ for (q = p; q < end; q++) if (*q == '>') return 1;
Branch: 2 branches
BranchTrueFalseTotal
for (q = p; q < end; q++) if (*q == '>') return 1; 3 42 45
for (q = p; q < end; q++) if (*q == '>') return 1; 45 0 45
1566 0 return 0;
1567 30 case 5:
1568 279 ▶ for (q = p; q + 2 < end; q++)
Branch: 1 branch
BranchTrueFalseTotal
for (q = p; q + 2 < end; q++) 252 27 279
1569 252 ▶ if (q[0] == ']' && q[1] == ']' && q[2] == '>') return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (q[0] == ']' && q[1] == ']' && q[2] == '>') return 1; 3 249 252
if (q[0] == ']' && q[1] == ']' && q[2] == '>') return 1; 3 0 3
if (q[0] == ']' && q[1] == ']' && q[2] == '>') return 1; 3 0 3
1570 27 return 0;
1571 265 case 6:
1572 case 7:
1573 265 return 0; /* only blank lines end these */
1574 }
1575 0 return 0;
1576 }
1577  
1578 /* ---------- leaf openers ---------- */
1579  
1580 12210393 static int try_thematic_break(const char* p, const char* end) {
1581 12210393 int lead = 0;
1582 char c;
1583 12210393 int count = 0;
1584 12211361 ▶ while (p < end && *p == ' ' && lead < 3) { p++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 12211343 18 12211361
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 1075 12210268 12211343
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 968 107 1075
1585 12210393 ▶ if (p >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return 0; 18 12210375 12210393
1586 12210375 c = *p;
1587 12210375 ▶ if (c != '-' && c != '_' && c != '*') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (c != '-' && c != '_' && c != '*') return 0; 12209650 725 12210375
if (c != '-' && c != '_' && c != '*') return 0; 12209336 314 12209650
if (c != '-' && c != '_' && c != '*') return 0; 12208677 659 12209336
1588 5849 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 5542 307 5849
1589 5542 ▶ if (*p == c) count++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == c) count++; 3100 2442 5542
1590 2442 ▶ else if (*p != ' ' && *p != '\t') return 0;
Branch: 2 branches
BranchTrueFalseTotal
else if (*p != ' ' && *p != '\t') return 0; 1391 1051 2442
else if (*p != ' ' && *p != '\t') return 0; 1391 0 1391
1591 4151 p++;
1592 }
1593 307 return count >= 3;
1594 }
1595  
1596 6508982 static int try_atx_heading(const char* p, const char* end,
1597 int* level_out,
1598 const char** body_start, const char** body_end) {
1599 6508982 int lead = 0;
1600 6508982 int n = 0;
1601 const char* bs;
1602 const char* be;
1603 const char* trim;
1604 6509407 ▶ while (p < end && *p == ' ' && lead < 3) { p++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 6509398 9 6509407
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 474 6508924 6509398
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 425 49 474
1605 8125388 ▶ while (p < end && *p == '#' && n < 7) { p++; n++; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p == '#' && n < 7) { p++; n++; } 8125376 12 8125388
while (p < end && *p == '#' && n < 7) { p++; n++; } 1616406 6508970 8125376
while (p < end && *p == '#' && n < 7) { p++; n++; } 1616406 0 1616406
1606 6508982 ▶ if (n == 0 || n > 6) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (n == 0 || n > 6) return 0; 808205 5700777 6508982
if (n == 0 || n > 6) return 0; 4 808201 808205
1607 808201 ▶ if (p < end && *p != ' ' && *p != '\t') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && *p != ' ' && *p != '\t') return 0; 808198 3 808201
if (p < end && *p != ' ' && *p != '\t') return 0; 10 808188 808198
if (p < end && *p != ' ' && *p != '\t') return 0; 7 3 10
1608 1616445 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 1616439 6 1616445
while (p < end && (*p == ' ' || *p == '\t')) p++; 808248 808191 1616439
while (p < end && (*p == ' ' || *p == '\t')) p++; 3 808188 808191
1609 808194 bs = p;
1610 808194 be = end;
1611 808282 ▶ while (be > bs && (be[-1] == ' ' || be[-1] == '\t')) be--;
Branch: 3 branches
BranchTrueFalseTotal
while (be > bs && (be[-1] == ' ' || be[-1] == '\t')) be--; 808276 6 808282
while (be > bs && (be[-1] == ' ' || be[-1] == '\t')) be--; 88 808188 808276
while (be > bs && (be[-1] == ' ' || be[-1] == '\t')) be--; 0 808188 808188
1612 /* optional trailing #s */
1613 808194 trim = be;
1614 808357 ▶ while (trim > bs && trim[-1] == '#') trim--;
Branch: 2 branches
BranchTrueFalseTotal
while (trim > bs && trim[-1] == '#') trim--; 808348 9 808357
while (trim > bs && trim[-1] == '#') trim--; 163 808185 808348
1615 808194 ▶ if (trim < be && (trim == bs || trim[-1] == ' ' || trim[-1] == '\t')) {
Branch: 4 branches
BranchTrueFalseTotal
if (trim < be && (trim == bs || trim[-1] == ' ' || trim[-1] == '\t')) { 31 808163 808194
if (trim < be && (trim == bs || trim[-1] == ' ' || trim[-1] == '\t')) { 28 3 31
if (trim < be && (trim == bs || trim[-1] == ' ' || trim[-1] == '\t')) { 12 16 28
if (trim < be && (trim == bs || trim[-1] == ' ' || trim[-1] == '\t')) { 0 12 12
1616 19 be = trim;
1617 44 ▶ while (be > bs && (be[-1] == ' ' || be[-1] == '\t')) be--;
Branch: 3 branches
BranchTrueFalseTotal
while (be > bs && (be[-1] == ' ' || be[-1] == '\t')) be--; 41 3 44
while (be > bs && (be[-1] == ' ' || be[-1] == '\t')) be--; 25 16 41
while (be > bs && (be[-1] == ' ' || be[-1] == '\t')) be--; 0 16 16
1618 }
1619 808194 *level_out = n;
1620 808194 *body_start = bs;
1621 808194 *body_end = be;
1622 808194 return 1;
1623 }
1624  
1625 5700685 static int try_fence_open(const char* p, const char* end,
1626 int* indent_out, char* ch_out, int* len_out,
1627 const char** info_start, const char** info_end) {
1628 5700685 int lead = 0;
1629 char ch;
1630 5700685 int n = 0;
1631 const char* is;
1632 const char* ie;
1633 const char* q;
1634 5701060 ▶ while (p < end && *p == ' ' && lead < 3) { p++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 5701051 9 5701060
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 424 5700627 5701051
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 375 49 424
1635 5700685 ▶ if (p >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return 0; 9 5700676 5700685
1636 5700676 ch = *p;
1637 5700676 ▶ if (ch != '`' && ch != '~') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (ch != '`' && ch != '~') return 0; 4892456 808220 5700676
if (ch != '`' && ch != '~') return 0; 4892420 36 4892456
1638 3232877 ▶ while (p < end && *p == ch) { p++; n++; }
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p == ch) { p++; n++; } 3232757 120 3232877
while (p < end && *p == ch) { p++; n++; } 2424621 808136 3232757
1639 808256 ▶ if (n < 3) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 3) return 0; 105 808151 808256
1640 808178 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 808079 99 808178
while (p < end && (*p == ' ' || *p == '\t')) p++; 27 808052 808079
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 808052 808052
1641 808151 is = p;
1642 808151 ie = end;
1643 808151 ▶ while (ie > is && (ie[-1] == ' ' || ie[-1] == '\t')) ie--;
Branch: 3 branches
BranchTrueFalseTotal
while (ie > is && (ie[-1] == ' ' || ie[-1] == '\t')) ie--; 808052 99 808151
while (ie > is && (ie[-1] == ' ' || ie[-1] == '\t')) ie--; 0 808052 808052
while (ie > is && (ie[-1] == ' ' || ie[-1] == '\t')) ie--; 0 808052 808052
1644 808151 ▶ if (ch == '`') {
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '`') { 808127 24 808151
1645 4040308 ▶ for (q = is; q < ie; q++) if (*q == '`') return 0;
Branch: 2 branches
BranchTrueFalseTotal
for (q = is; q < ie; q++) if (*q == '`') return 0; 9 3232181 3232190
for (q = is; q < ie; q++) if (*q == '`') return 0; 3232190 808118 4040308
1646 }
1647 808142 *indent_out = lead;
1648 808142 *ch_out = ch;
1649 808142 *len_out = n;
1650 808142 *info_start = is;
1651 808142 *info_end = ie;
1652 808142 return 1;
1653 }
1654  
1655 1616308 static int is_fence_close(const char* p, const char* end, char ch, int min_len) {
1656 1616308 int lead = 0;
1657 1616308 int n = 0;
1658 1616401 ▶ while (p < end && *p == ' ' && lead < 3) { p++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 1616386 15 1616401
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 99 1616287 1616386
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 93 6 99
1659 4040734 ▶ while (p < end && *p == ch) { p++; n++; }
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p == ch) { p++; n++; } 3232592 808142 4040734
while (p < end && *p == ch) { p++; n++; } 2424426 808166 3232592
1660 1616308 ▶ if (n < min_len) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n < min_len) return 0; 808187 808121 1616308
1661 808124 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 6 808118 808124
1662 6 ▶ if (*p != ' ' && *p != '\t') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*p != ' ' && *p != '\t') return 0; 3 3 6
if (*p != ' ' && *p != '\t') return 0; 3 0 3
1663 3 p++;
1664 }
1665 808118 return 1;
1666 }
1667  
1668 6510143 static int try_open_quote(const char** p, const char* end) {
1669 6510143 const char* q = *p;
1670 6510143 int lead = 0;
1671 6510880 ▶ while (q < end && *q == ' ' && lead < 3) { q++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && *q == ' ' && lead < 3) { q++; lead++; } 6510852 28 6510880
while (q < end && *q == ' ' && lead < 3) { q++; lead++; } 810 6510042 6510852
while (q < end && *q == ' ' && lead < 3) { q++; lead++; } 737 73 810
1672 6510143 ▶ if (q >= end || *q != '>') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (q >= end || *q != '>') return 0; 6510115 28 6510143
if (q >= end || *q != '>') return 0; 6509843 272 6510115
1673 272 q++;
1674 272 ▶ if (q < end && (*q == ' ' || *q == '\t')) q++;
Branch: 3 branches
BranchTrueFalseTotal
if (q < end && (*q == ' ' || *q == '\t')) q++; 248 24 272
if (q < end && (*q == ' ' || *q == '\t')) q++; 34 214 248
if (q < end && (*q == ' ' || *q == '\t')) q++; 0 34 34
1675 272 *p = q;
1676 272 return 1;
1677 }
1678  
1679 6509961 static int try_open_list_item(const char** p_inout, const char* end,
1680 int* ordered, int* start, char* marker,
1681 int* content_col, int* is_empty) {
1682 6509961 const char* p = *p_inout;
1683 6509961 int col = 0, lead = 0;
1684 int mc;
1685 char m;
1686 6509961 int ord = 0, st = 1, marker_len = 0;
1687 6509961 int spaces_after = 0;
1688 int empty;
1689 6510749 ▶ while (p < end && *p == ' ' && lead < 3) { p++; col++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p == ' ' && lead < 3) { p++; col++; lead++; } 6510740 9 6510749
while (p < end && *p == ' ' && lead < 3) { p++; col++; lead++; } 861 6509879 6510740
while (p < end && *p == ' ' && lead < 3) { p++; col++; lead++; } 788 73 861
1690 6509961 ▶ if (p >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return 0; 9 6509952 6509961
1691 6509952 mc = col;
1692 6509952 m = *p;
1693 6509952 ▶ if (m == '-' || m == '+' || m == '*') {
Branch: 3 branches
BranchTrueFalseTotal
if (m == '-' || m == '+' || m == '*') { 6509194 758 6509952
if (m == '-' || m == '+' || m == '*') { 6509182 12 6509194
if (m == '-' || m == '+' || m == '*') { 329 6508853 6509182
1694 1099 marker_len = 1;
1695 1099 p++; col++;
1696 6508853 ▶ } else if (m >= '0' && m <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
} else if (m >= '0' && m <= '9') { 4892425 1616428 6508853
} else if (m >= '0' && m <= '9') { 221 4892204 4892425
1697 221 const char* q = p;
1698 221 int digits = 0, val = 0;
1699 532 ▶ while (q < end && *q >= '0' && *q <= '9' && digits < 9) {
Branch: 4 branches
BranchTrueFalseTotal
while (q < end && *q >= '0' && *q <= '9' && digits < 9) { 526 6 532
while (q < end && *q >= '0' && *q <= '9' && digits < 9) { 323 203 526
while (q < end && *q >= '0' && *q <= '9' && digits < 9) { 314 9 323
while (q < end && *q >= '0' && *q <= '9' && digits < 9) { 311 3 314
1700 311 val = val * 10 + (*q - '0');
1701 311 q++; digits++;
1702 }
1703 221 ▶ if (digits == 0 || q >= end) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (digits == 0 || q >= end) return 0; 221 0 221
if (digits == 0 || q >= end) return 0; 6 215 221
1704 215 ▶ if (*q != '.' && *q != ')') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*q != '.' && *q != ')') return 0; 30 185 215
if (*q != '.' && *q != ')') return 0; 15 15 30
1705 200 m = *q;
1706 200 marker_len = (int)(q - p) + 1;
1707 200 col += marker_len;
1708 200 p = q + 1;
1709 200 ord = 1;
1710 200 st = val;
1711 } else {
1712 6508632 return 0;
1713 }
1714 1299 empty = (p >= end);
1715 1299 ▶ if (!empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (!empty) { 1239 60 1299
1716 1239 ▶ if (*p == ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == ' ') { 954 285 1239
1717 2052 ▶ while (p < end && spaces_after < 5 && *p == ' ') { p++; spaces_after++; col++; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && spaces_after < 5 && *p == ' ') { p++; spaces_after++; col++; } 2040 12 2052
while (p < end && spaces_after < 5 && *p == ' ') { p++; spaces_after++; col++; } 2031 9 2040
while (p < end && spaces_after < 5 && *p == ' ') { p++; spaces_after++; col++; } 1098 933 2031
1718 285 ▶ } else if (*p == '\t') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (*p == '\t') { 0 285 285
1719 0 int adv = 4 - (col & 3);
1720 0 spaces_after = adv;
1721 0 col += adv;
1722 0 p++;
1723 } else {
1724 285 return 0;
1725 }
1726 954 empty = is_blank(p, end);
1727 }
1728 1014 ▶ if (spaces_after >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (spaces_after >= 5) { 9 1005 1014
1729 9 p -= (spaces_after - 1);
1730 9 col -= (spaces_after - 1);
1731 9 spaces_after = 1;
1732 }
1733 1014 ▶ if (empty) {
Branch: 1 branch
BranchTrueFalseTotal
if (empty) { 72 942 1014
1734 72 *content_col = mc + marker_len + 1;
1735 } else {
1736 942 *content_col = col;
1737 }
1738 1014 *ordered = ord;
1739 1014 *start = st;
1740 1014 *marker = m;
1741 1014 *is_empty = empty;
1742 1014 *p_inout = p;
1743 1014 return 1;
1744 }
1745  
1746 /* Setext underline: 0-3 leading spaces, then a run of '=' (level 1) or
1747 * '-' (level 2), optional trailing spaces, end of line. Returns 1 or 2. */
1748 788593 static int try_setext_underline(const char* p, const char* end) {
1749 788593 int lead = 0;
1750 char c;
1751 788593 int n = 0;
1752 788740 ▶ while (p < end && *p == ' ' && lead < 3) { p++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 788740 0 788740
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 177 788563 788740
while (p < end && *p == ' ' && lead < 3) { p++; lead++; } 147 30 177
1753 788593 ▶ if (p >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return 0; 0 788593 788593
1754 788593 c = *p;
1755 788593 ▶ if (c != '=' && c != '-') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (c != '=' && c != '-') return 0; 788563 30 788593
if (c != '=' && c != '-') return 0; 788436 127 788563
1756 645 ▶ while (p < end && *p == c) { p++; n++; }
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p == c) { p++; n++; } 565 80 645
while (p < end && *p == c) { p++; n++; } 488 77 565
1757 249 ▶ while (p < end && (*p == ' ' || *p == '\t')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && (*p == ' ' || *p == '\t')) p++; 166 83 249
while (p < end && (*p == ' ' || *p == '\t')) p++; 92 74 166
while (p < end && (*p == ' ' || *p == '\t')) p++; 0 74 74
1758 157 ▶ if (p != end || n < 1) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (p != end || n < 1) return 0; 83 74 157
if (p != end || n < 1) return 0; 0 83 83
1759 83 ▶ return (c == '=') ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
return (c == '=') ? 1 : 2; 27 56 83
1760 }
1761  
1762 /* ---------- main scan ---------- */
1763  
1764 static void scan_line(bscanner* b, const char* line, const char* line_end);
1765  
1766 204525 MDS_HOT void mds_block_scan(mds_ctx* ctx) {
1767 bscanner b;
1768 mds_block_scratch* sc;
1769 const char* p;
1770 const char* end;
1771 204525 memset(&b, 0, sizeof b);
1772 204525 b.ctx = ctx;
1773  
1774 /* Borrow scratch buffers from the session (if any) so
1775 * we skip the per-parse malloc/free for para/code/html/ev/bytepool.
1776 * Lengths are reset to zero; capacities and pointers are taken
1777 * verbatim and written back at end-of-scan. */
1778 204525 sc = (mds_block_scratch*)ctx->scratch;
1779 204525 ▶ if (sc) {
Branch: 1 branch
BranchTrueFalseTotal
if (sc) { 141638 62887 204525
1780 141638 b.para = sc->para; b.para_cap = sc->para_cap;
1781 141638 b.code_body = sc->code_body; b.code_cap = sc->code_cap;
1782 141638 b.html_body = sc->html_body; b.html_cap = sc->html_cap;
1783 141638 b.evbuf = (ev_rec*)sc->evbuf; b.ev_cap = sc->ev_cap;
1784 141638 b.bytepool = sc->bytepool; b.bp_cap = sc->bp_cap;
1785 }
1786  
1787 204525 push(&b, CT_DOC);
1788 204525 b.stack[0].opened = 1;
1789  
1790 /* Tier E.1 extended — strip top-level footnote defs from the input so
1791 * the main scan never sees them (they would otherwise become stray
1792 * paragraphs). The captured bodies are arena-owned and re-parsed
1793 * later via a recursive mds_block_scan when the footnotes section
1794 * is emitted. */
1795 204525 ▶ if (ctx->flags & MDS_FLAG_FOOTNOTES) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->flags & MDS_FLAG_FOOTNOTES) { 162122 42403 204525
1796 162122 preprocess_footnotes(ctx);
1797 }
1798  
1799 204525 p = ctx->input;
1800 204525 end = ctx->input + ctx->len;
1801 13987565 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 13783040 204525 13987565
1802 const char* le;
1803 13783040 const char* nxt = next_line(p, end, &le);
1804 13783040 const char* lp = p;
1805 13783040 const char* lend = le;
1806 /* If line contains a TAB, expand into line_scratch with tab-stop=4.
1807 * Per CommonMark §2.2, tabs behave as if replaced by spaces with a
1808 * tab stop of 4 chars. Doing this once per line lets the rest of
1809 * the block scanner remain tab-agnostic. scan_line is responsible
1810 * for copying any text it needs to keep beyond the line's lifetime,
1811 * so the scratch may be reused for the next line. */
1812 /* Tabs that appear in "container indent" positions must be
1813 * expanded for proper column accounting per CommonMark spec.
1814 * Walk the line accepting WS and container markers (>, list
1815 * markers) until we hit text content, expanding any tab to
1816 * spaces using tab-stop=4 based on the current logical column.
1817 * Tabs in code content (after text starts) are preserved
1818 * literally. */
1819 13783040 ▶ if (memchr(p, '\t', (size_t)(le - p))) {
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(p, '\t', (size_t)(le - p))) { 43 13782997 13783040
1820 /* Find the end of the structural prefix. */
1821 43 const char* ws_end = p;
1822 43 int saw_tab = 0;
1823 140 ▶ while (ws_end < le) {
Branch: 1 branch
BranchTrueFalseTotal
while (ws_end < le) { 137 3 140
1824 137 char c = *ws_end;
1825 137 ▶ if (c == ' ' || c == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t') { 101 36 137
if (c == ' ' || c == '\t') { 43 58 101
1826 79 ▶ if (c == '\t') saw_tab = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\t') saw_tab = 1; 43 36 79
1827 79 ws_end++;
1828 79 continue;
1829 }
1830 /* blockquote marker */
1831 58 ▶ if (c == '>') { ws_end++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == '>') { ws_end++; continue; } 3 55 58
1832 /* unordered list marker followed by WS */
1833 55 ▶ if ((c == '-' || c == '+' || c == '*') &&
Branch: 3 branches
BranchTrueFalseTotal
if ((c == '-' || c == '+' || c == '*') && 49 6 55
if ((c == '-' || c == '+' || c == '*') && 49 0 49
if ((c == '-' || c == '+' || c == '*') && 9 40 49
1834 15 ▶ ws_end + 1 < le &&
Branch: 1 branch
BranchTrueFalseTotal
ws_end + 1 < le && 15 0 15
1835 15 ▶ (ws_end[1] == ' ' || ws_end[1] == '\t')) {
Branch: 2 branches
BranchTrueFalseTotal
(ws_end[1] == ' ' || ws_end[1] == '\t')) { 12 3 15
(ws_end[1] == ' ' || ws_end[1] == '\t')) { 12 0 12
1836 15 ws_end++; continue;
1837 }
1838 /* ordered list marker n.[ \t] or n)[ \t] */
1839 40 ▶ if (c >= '0' && c <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') { 34 6 40
if (c >= '0' && c <= '9') { 0 34 34
1840 0 const char* q = ws_end;
1841 0 int digits = 0;
1842 0 ▶ while (q < le && *q >= '0' && *q <= '9' && digits < 9) { q++; digits++; }
Branch: 4 branches
BranchTrueFalseTotal
while (q < le && *q >= '0' && *q <= '9' && digits < 9) { q++; digits++; } 0 0 0
while (q < le && *q >= '0' && *q <= '9' && digits < 9) { q++; digits++; } 0 0 0
while (q < le && *q >= '0' && *q <= '9' && digits < 9) { q++; digits++; } 0 0 0
while (q < le && *q >= '0' && *q <= '9' && digits < 9) { q++; digits++; } 0 0 0
1843 0 ▶ if (q < le && (*q == '.' || *q == ')') &&
Branch: 3 branches
BranchTrueFalseTotal
if (q < le && (*q == '.' || *q == ')') && 0 0 0
if (q < le && (*q == '.' || *q == ')') && 0 0 0
if (q < le && (*q == '.' || *q == ')') && 0 0 0
1844 0 ▶ q + 1 < le && (q[1] == ' ' || q[1] == '\t')) {
Branch: 3 branches
BranchTrueFalseTotal
q + 1 < le && (q[1] == ' ' || q[1] == '\t')) { 0 0 0
q + 1 < le && (q[1] == ' ' || q[1] == '\t')) { 0 0 0
q + 1 < le && (q[1] == ' ' || q[1] == '\t')) { 0 0 0
1845 0 ws_end = q + 1; continue;
1846 }
1847 }
1848 40 break;
1849 }
1850 43 ▶ if (saw_tab) {
Branch: 1 branch
BranchTrueFalseTotal
if (saw_tab) { 28 15 43
1851 size_t need;
1852 char* dst;
1853 int col;
1854 const char* s;
1855 28 need = (size_t)(ws_end - p) * 4 + (size_t)(le - ws_end) + 8;
1856 28 buf_grow(&b.line_scratch, &b.line_scratch_cap, need);
1857 28 dst = b.line_scratch;
1858 28 col = 0;
1859 101 ▶ for (s = p; s < ws_end; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = p; s < ws_end; s++) { 73 28 101
1860 73 ▶ if (*s == '\t') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '\t') { 43 30 73
1861 43 int adv = 4 - (col & 3);
1862 194 ▶ while (adv-- > 0) { *dst++ = ' '; col++; }
Branch: 1 branch
BranchTrueFalseTotal
while (adv-- > 0) { *dst++ = ' '; col++; } 151 43 194
1863 30 } else { *dst++ = *s; col++; }
1864 }
1865 28 ▶ if (le > ws_end) memcpy(dst, ws_end, (size_t)(le - ws_end));
Branch: 1 branch
BranchTrueFalseTotal
if (le > ws_end) memcpy(dst, ws_end, (size_t)(le - ws_end)); 25 3 28
1866 28 dst += (le - ws_end);
1867 28 lp = b.line_scratch;
1868 28 lend = dst;
1869 }
1870 }
1871 13783040 scan_line(&b, lp, lend);
1872 13783040 p = nxt;
1873 }
1874 204525 close_containers_to(&b, 1);
1875 204525 finalize_leaf(&b);
1876  
1877 204525 sax_flush(&b);
1878  
1879 /* Tier E.1 extended — emit FOOTNOTES_SECTION directly via callbacks
1880 * AFTER the main flush so the renderer has finished accumulating
1881 * the used-label set from inline FOOTNOTE_REF callbacks. For each
1882 * registered def we fire enter/leave around a recursive
1883 * mds_block_scan over the captured body bytes (with FOOTNOTES
1884 * disabled in the sub-context to prevent re-detection). Unused
1885 * defs are filtered by the renderer via its fn_skip flag. */
1886 204525 ▶ if ((ctx->flags & MDS_FLAG_FOOTNOTES) && ctx->footnotes && ctx->footnotes->len) {
Branch: 3 branches
BranchTrueFalseTotal
if ((ctx->flags & MDS_FLAG_FOOTNOTES) && ctx->footnotes && ctx->footnotes->len) { 162122 42403 204525
if ((ctx->flags & MDS_FLAG_FOOTNOTES) && ctx->footnotes && ctx->footnotes->len) { 40409 121713 162122
if ((ctx->flags & MDS_FLAG_FOOTNOTES) && ctx->footnotes && ctx->footnotes->len) { 40409 0 40409
1887 40409 const mds_callbacks cb = ctx->cb;
1888 40409 void* const ud = ctx->ud;
1889 40409 ▶ if (MDS_LIKELY(cb.enter_block != NULL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (MDS_LIKELY(cb.enter_block != NULL)) { 40409 0 40409
1890 /* Emit in renderer's first-use order (queried via the
1891 * public mds_render_html_used_footnote helper); fall back
1892 * to definition order for non-HTML renderers / when no
1893 * refs were seen (section will then be empty / all defs
1894 * are unused). */
1895 40409 size_t emit_n = 0;
1896 const char* lbl;
1897 size_t llen;
1898 80827 ▶ while (mds_render_html_used_footnote(ud, emit_n, &lbl, &llen)) emit_n++;
Branch: 1 branch
BranchTrueFalseTotal
while (mds_render_html_used_footnote(ud, emit_n, &lbl, &llen)) emit_n++; 40418 40409 80827
1899  
1900 40409 ▶ if (emit_n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (emit_n == 0) { 40409 0 40409
1901 /* No referenced footnotes — skip the whole section to
1902 * avoid an empty <section><ol></ol></section>. */
1903 } else {
1904 mds_block_detail d;
1905 size_t k;
1906 40409 memset(&d, 0, sizeof d);
1907 40409 cb.enter_block(ud, MDS_BLK_FOOTNOTES_SECTION, &d);
1908 80827 ▶ for (k = 0; k < emit_n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < emit_n; k++) { 40418 40409 80827
1909 const mds_footnote* fn;
1910 mds_block_detail fd;
1911 40418 mds_render_html_used_footnote(ud, k, &lbl, &llen);
1912 40418 fn = mds_footnote_get(ctx->footnotes, lbl, llen);
1913 40418 ▶ if (!fn) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!fn) continue; 0 40418 40418
1914 40418 memset(&fd, 0, sizeof fd);
1915 40418 fd.u.footnote_def.label = fn->label;
1916 40418 fd.u.footnote_def.label_len = fn->llen;
1917 40418 fd.u.footnote_def.body = fn->body;
1918 40418 fd.u.footnote_def.body_len = fn->blen;
1919 40418 cb.enter_block(ud, MDS_BLK_FOOTNOTE_DEF, &fd);
1920 40418 ▶ if (fn->blen) {
Branch: 1 branch
BranchTrueFalseTotal
if (fn->blen) { 40418 0 40418
1921 40418 mds_ctx sub = *ctx;
1922 40418 sub.input = fn->body;
1923 40418 sub.len = fn->blen;
1924 40418 sub.flags = ctx->flags & ~MDS_FLAG_FOOTNOTES;
1925 40418 sub.footnotes = NULL;
1926 40418 sub.scratch = NULL;
1927 40418 mds_block_scan(&sub);
1928 }
1929 40418 ▶ if (cb.leave_block != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (cb.leave_block != NULL) 40418 0 40418
1930 40418 cb.leave_block(ud, MDS_BLK_FOOTNOTE_DEF);
1931 }
1932 40409 ▶ if (cb.leave_block != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (cb.leave_block != NULL) 40409 0 40409
1933 40409 cb.leave_block(ud, MDS_BLK_FOOTNOTES_SECTION);
1934 }
1935 }
1936 }
1937  
1938 204525 ▶ if (sc) {
Branch: 1 branch
BranchTrueFalseTotal
if (sc) { 141638 62887 204525
1939 /* Write back potentially-grown buffers; ownership stays with sc. */
1940 141638 sc->para = b.para; sc->para_cap = b.para_cap;
1941 141638 sc->code_body = b.code_body; sc->code_cap = b.code_cap;
1942 141638 sc->html_body = b.html_body; sc->html_cap = b.html_cap;
1943 141638 sc->evbuf = b.evbuf; sc->ev_cap = b.ev_cap;
1944 141638 sc->bytepool = b.bytepool; sc->bp_cap = b.bp_cap;
1945 } else {
1946 62887 free(b.para);
1947 62887 free(b.code_body);
1948 62887 free(b.html_body);
1949 62887 free(b.evbuf);
1950 62887 free(b.bytepool);
1951 }
1952 204525 free(b.line_scratch);
1953 204525 }
1954  
1955 25 void mds_block_scratch_free(mds_block_scratch* s) {
1956 25 ▶ if (!s) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return; 0 25 25
1957 25 free(s->para); s->para = NULL; s->para_cap = 0;
1958 25 free(s->code_body); s->code_body = NULL; s->code_cap = 0;
1959 25 free(s->html_body); s->html_body = NULL; s->html_cap = 0;
1960 25 free(s->evbuf); s->evbuf = NULL; s->ev_cap = 0;
1961 25 free(s->bytepool); s->bytepool = NULL; s->bp_cap = 0;
1962 }
1963  
1964 13783040 MDS_HOT static void scan_line(bscanner* b, const char* line, const char* line_end) {
1965 13783040 const char* p = line;
1966 13783040 int blank = is_blank(p, line_end);
1967 int matched;
1968 int i;
1969 size_t r;
1970 int lazy;
1971 int level;
1972 const char* bs;
1973 const char* be;
1974 int findent;
1975 char fch;
1976 int flen;
1977 const char* ifs;
1978 const char* ife;
1979 const char* tp;
1980 int lead;
1981  
1982 /* If inside fenced code, only check for close OR continue accumulating. */
1983 13783040 ▶ if (b->leaf == LF_CODE_FENCED) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->leaf == LF_CODE_FENCED) { 1616314 12166726 13783040
1984 const char* cp;
1985 int strip;
1986 size_t add;
1987 /* still need to walk containers to count matched (for nested fenced code) */
1988 1616389 ▶ for (i = 1; i < b->depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < b->depth; i++) { 81 1616308 1616389
1989 81 ctn* c = &b->stack[i];
1990 81 ▶ if (c->kind == CT_QUOTE) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->kind == CT_QUOTE) { 9 72 81
1991 9 ▶ if (!try_open_quote(&p, line_end)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!try_open_quote(&p, line_end)) { 6 3 9
1992 /* container break terminates code */
1993 6 finalize_leaf(b);
1994 6 close_containers_to(b, i);
1995 6 goto regular;
1996 }
1997 72 ▶ } else if (c->kind == CT_LIST_ITEM) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->kind == CT_LIST_ITEM) { 36 36 72
1998 36 int col = count_indent(p, line_end);
1999 36 ▶ if (!blank && col < c->content_col) {
Branch: 2 branches
BranchTrueFalseTotal
if (!blank && col < c->content_col) { 30 6 36
if (!blank && col < c->content_col) { 0 30 30
2000 0 finalize_leaf(b);
2001 0 close_containers_to(b, i);
2002 0 goto regular;
2003 }
2004 36 consume_indent(&p, line_end, c->content_col);
2005 }
2006 }
2007 1616308 ▶ if (is_fence_close(p, line_end, b->fence_char, b->fence_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_fence_close(p, line_end, b->fence_char, b->fence_len)) { 808118 808190 1616308
2008 808118 finalize_leaf(b);
2009 8890711 return;
2010 }
2011 808190 cp = p;
2012 808190 strip = b->fence_indent;
2013 808223 ▶ while (cp < line_end && strip > 0 && *cp == ' ') { cp++; strip--; }
Branch: 3 branches
BranchTrueFalseTotal
while (cp < line_end && strip > 0 && *cp == ' ') { cp++; strip--; } 808211 12 808223
while (cp < line_end && strip > 0 && *cp == ' ') { cp++; strip--; } 48 808163 808211
while (cp < line_end && strip > 0 && *cp == ' ') { cp++; strip--; } 33 15 48
2014 808190 add = (size_t)(line_end - cp);
2015 808190 buf_grow(&b->code_body, &b->code_cap, b->code_len + add + 2);
2016 808190 ▶ if (add) memcpy(b->code_body + b->code_len, cp, add);
Branch: 1 branch
BranchTrueFalseTotal
if (add) memcpy(b->code_body + b->code_len, cp, add); 808178 12 808190
2017 808190 b->code_len += add;
2018 808190 b->code_body[b->code_len++] = '\n';
2019 808190 return;
2020 }
2021  
2022 /* If inside an HTML block, walk containers then either close (on end
2023 * condition) or append the raw line. */
2024 12166726 ▶ if (b->leaf == LF_HTML) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->leaf == LF_HTML) { 321 12166405 12166726
2025 size_t add;
2026 327 ▶ for (i = 1; i < b->depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < b->depth; i++) { 12 315 327
2027 12 ctn* c = &b->stack[i];
2028 12 ▶ if (c->kind == CT_QUOTE) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->kind == CT_QUOTE) { 6 6 12
2029 6 ▶ if (!try_open_quote(&p, line_end)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!try_open_quote(&p, line_end)) { 3 3 6
2030 3 finalize_leaf(b);
2031 3 close_containers_to(b, i);
2032 3 goto regular;
2033 }
2034 6 ▶ } else if (c->kind == CT_LIST_ITEM) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->kind == CT_LIST_ITEM) { 3 3 6
2035 3 int col = count_indent(p, line_end);
2036 3 ▶ if (!blank && col < c->content_col) {
Branch: 2 branches
BranchTrueFalseTotal
if (!blank && col < c->content_col) { 3 0 3
if (!blank && col < c->content_col) { 3 0 3
2037 3 finalize_leaf(b);
2038 3 close_containers_to(b, i);
2039 3 goto regular;
2040 }
2041 0 consume_indent(&p, line_end, c->content_col);
2042 }
2043 }
2044 315 ▶ if (blank && (b->html_type == 6 || b->html_type == 7)) {
Branch: 3 branches
BranchTrueFalseTotal
if (blank && (b->html_type == 6 || b->html_type == 7)) { 87 228 315
if (blank && (b->html_type == 6 || b->html_type == 7)) { 39 48 87
if (blank && (b->html_type == 6 || b->html_type == 7)) { 3 36 39
2045 51 finalize_leaf(b);
2046 51 return;
2047 }
2048 264 add = (size_t)(line_end - p);
2049 264 buf_grow(&b->html_body, &b->html_cap, b->html_len + add + 2);
2050 264 ▶ if (add) memcpy(b->html_body + b->html_len, p, add);
Branch: 1 branch
BranchTrueFalseTotal
if (add) memcpy(b->html_body + b->html_len, p, add); 228 36 264
2051 264 b->html_len += add;
2052 264 b->html_body[b->html_len++] = '\n';
2053 264 ▶ if (detect_html_block_end(b->html_type, p, line_end, blank)) {
Branch: 1 branch
BranchTrueFalseTotal
if (detect_html_block_end(b->html_type, p, line_end, blank)) { 27 237 264
2054 27 finalize_leaf(b);
2055 }
2056 264 return;
2057 }
2058 12166405 regular:
2059 ; /* empty statement so the following declaration is legal C89/C99 */
2060  
2061 /* ---- 1. WALK ---- */
2062 12166417 matched = 1;
2063 12167720 ▶ for (i = 1; i < b->depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < b->depth; i++) { 1661 12166059 12167720
2064 1661 ctn* c = &b->stack[i];
2065 1661 ▶ if (c->kind == CT_QUOTE) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->kind == CT_QUOTE) { 156 1505 1661
2066 156 ▶ if (try_open_quote(&p, line_end)) { matched = i + 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (try_open_quote(&p, line_end)) { matched = i + 1; } 89 67 156
2067 67 else break;
2068 1505 ▶ } else if (c->kind == CT_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->kind == CT_LIST) { 754 751 1505
2069 754 matched = i + 1;
2070 751 ▶ } else if (c->kind == CT_LIST_ITEM) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->kind == CT_LIST_ITEM) { 751 0 751
2071 751 int col = count_indent(p, line_end);
2072 751 ▶ if (blank) { matched = i + 1; }
Branch: 1 branch
BranchTrueFalseTotal
if (blank) { matched = i + 1; } 204 547 751
2073 547 ▶ else if (col >= c->content_col) {
Branch: 1 branch
BranchTrueFalseTotal
else if (col >= c->content_col) { 259 288 547
2074 /* CommonMark §5.2: an empty list item closes after one
2075 * blank line. The enclosing CT_LIST also closes (no new
2076 * item starter present). */
2077 259 ▶ if (c->is_empty && c->blank_after_empty) {
Branch: 2 branches
BranchTrueFalseTotal
if (c->is_empty && c->blank_after_empty) { 15 244 259
if (c->is_empty && c->blank_after_empty) { 3 12 15
2078 3 ▶ if (i > 0 && b->stack[i-1].kind == CT_LIST) matched = i - 1;
Branch: 2 branches
BranchTrueFalseTotal
if (i > 0 && b->stack[i-1].kind == CT_LIST) matched = i - 1; 3 0 3
if (i > 0 && b->stack[i-1].kind == CT_LIST) matched = i - 1; 3 0 3
2079 3 break;
2080 }
2081 256 consume_indent(&p, line_end, c->content_col);
2082 256 matched = i + 1;
2083 256 c->is_empty = 0;
2084 288 } else break;
2085 } else {
2086 0 matched = i + 1;
2087 }
2088 }
2089  
2090 /* After the walk, if all remaining bytes are whitespace, treat as blank.
2091 * This catches lines like ">" inside a blockquote, where the line is
2092 * not blank as a whole but becomes empty after consuming the quote
2093 * marker. The blank handler below will then close the paragraph but,
2094 * because matched == depth, leave the quote container open. */
2095 12166417 ▶ if (!blank && is_blank(p, line_end)) blank = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (!blank && is_blank(p, line_end)) blank = 1; 6509334 5657083 12166417
if (!blank && is_blank(p, line_end)) blank = 1; 24 6509310 6509334
2096  
2097 12166417 ▶ if (blank) {
Branch: 1 branch
BranchTrueFalseTotal
if (blank) { 5657107 6509310 12166417
2098 /* Inside indented code: a "blank" line with >= 4 columns of leading
2099 * whitespace is actually content (CommonMark §4.4: blank lines
2100 * preceding/following are stripped, but inner blank lines keep
2101 * their trailing whitespace beyond column 4). */
2102 5657107 ▶ if (b->leaf == LF_CODE_INDENTED) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->leaf == LF_CODE_INDENTED) { 78 5657029 5657107
2103 78 int col = count_indent(p, line_end);
2104 78 ▶ if (col >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (col >= 4) { 6 72 78
2105 size_t add;
2106 6 ▶ while (b->pending_blanks > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (b->pending_blanks > 0) { 0 6 6
2107 0 buf_grow(&b->code_body, &b->code_cap, b->code_len + 2);
2108 0 b->code_body[b->code_len++] = '\n';
2109 0 b->pending_blanks--;
2110 }
2111 6 consume_indent(&p, line_end, 4);
2112 6 add = (size_t)(line_end - p);
2113 6 buf_grow(&b->code_body, &b->code_cap, b->code_len + add + 2);
2114 6 ▶ if (add) memcpy(b->code_body + b->code_len, p, add);
Branch: 1 branch
BranchTrueFalseTotal
if (add) memcpy(b->code_body + b->code_len, p, add); 3 3 6
2115 6 b->code_len += add;
2116 6 b->code_body[b->code_len++] = '\n';
2117 6 return;
2118 }
2119 /* CM §5.1: a blank line without the blockquote marker still
2120 * ends the enclosing blockquote, even if indented code is
2121 * open inside it. Fall through to the normal blank-line
2122 * handling (paragraph finalise + quote close). */
2123 72 ▶ if (matched < b->depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (matched < b->depth) { 3 69 72
2124 3 int has_unmatched_quote = 0;
2125 3 ▶ for (i = matched; i < b->depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = matched; i < b->depth; i++) { 3 0 3
2126 3 ▶ if (b->stack[i].kind == CT_QUOTE) { has_unmatched_quote = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->stack[i].kind == CT_QUOTE) { has_unmatched_quote = 1; break; } 3 0 3
2127 }
2128 3 ▶ if (has_unmatched_quote) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_unmatched_quote) { 3 0 3
2129 /* flush code, then fall through. */
2130 3 finalize_leaf(b);
2131 } else {
2132 0 b->pending_blanks++;
2133 0 return;
2134 }
2135 } else {
2136 69 b->pending_blanks++;
2137 69 return;
2138 }
2139 }
2140 /* mark list loose flag, finalise paragraph.
2141 * CommonMark loose-ness rule: a blank line only affects a list if
2142 * it lies directly inside one of that list's items (not nested
2143 * inside an inner blockquote). Defer the attribution: just remember
2144 * a blank was seen, and attribute it to the deepest surviving
2145 * CT_LIST after the next non-blank line has walked containers.
2146 * Exception: if the matched stack has a CT_QUOTE *above* the
2147 * deepest matched CT_LIST_ITEM, the blank is quote-internal and
2148 * should not propagate to any outer list. */
2149 5657032 ▶ if (b->leaf == LF_PARAGRAPH) finalize_leaf(b);
Branch: 1 branch
BranchTrueFalseTotal
if (b->leaf == LF_PARAGRAPH) finalize_leaf(b); 4040905 1616127 5657032
2150 {
2151 5657032 int deepest_li = -1;
2152 5657032 int has_quote_above_li = 0;
2153 11314460 ▶ for (i = 0; i < matched; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < matched; i++) { 5657428 5657032 11314460
2154 5657428 ▶ if (b->stack[i].kind == CT_LIST_ITEM) deepest_li = i;
Branch: 1 branch
BranchTrueFalseTotal
if (b->stack[i].kind == CT_LIST_ITEM) deepest_li = i; 180 5657248 5657428
2155 }
2156 5657032 ▶ if (deepest_li >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (deepest_li >= 0) { 157 5656875 5657032
2157 157 ▶ for (i = deepest_li + 1; i < matched; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = deepest_li + 1; i < matched; i++) { 3 154 157
2158 3 ▶ if (b->stack[i].kind == CT_QUOTE) { has_quote_above_li = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (b->stack[i].kind == CT_QUOTE) { has_quote_above_li = 1; break; } 3 0 3
2159 }
2160 }
2161 5657032 ▶ if (!has_quote_above_li) b->blank_pending = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!has_quote_above_li) b->blank_pending = 1; 5657029 3 5657032
2162 }
2163 11314479 ▶ for (i = 0; i < b->depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->depth; i++) { 5657447 5657032 11314479
2164 5657447 ▶ if (b->stack[i].kind == CT_LIST_ITEM && b->stack[i].is_empty)
Branch: 2 branches
BranchTrueFalseTotal
if (b->stack[i].kind == CT_LIST_ITEM && b->stack[i].is_empty) 186 5657261 5657447
if (b->stack[i].kind == CT_LIST_ITEM && b->stack[i].is_empty) 6 180 186
2165 6 b->stack[i].blank_after_empty = 1;
2166 }
2167 /* CommonMark §5.1: a block quote ends at a blank line that doesn't
2168 * have its own quote marker. Close any CT_QUOTE that the walk didn't
2169 * match. (Lists handle blank lines via had_blank_inside; quotes don't
2170 * have a same notion.) */
2171 5657032 ▶ if (matched < b->depth) {
Branch: 1 branch
BranchTrueFalseTotal
if (matched < b->depth) { 19 5657013 5657032
2172 25 ▶ for (i = matched; i < b->depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = matched; i < b->depth; i++) { 19 6 25
2173 19 ▶ if (b->stack[i].kind == CT_QUOTE) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->stack[i].kind == CT_QUOTE) { 13 6 19
2174 13 close_containers_to(b, matched);
2175 13 break;
2176 }
2177 }
2178 }
2179 5657032 return;
2180 }
2181  
2182 /* ---- lazy continuation check ---- */
2183 6509310 lazy = 0;
2184 6509310 ▶ if (matched < b->depth && b->leaf == LF_PARAGRAPH) {
Branch: 2 branches
BranchTrueFalseTotal
if (matched < b->depth && b->leaf == LF_PARAGRAPH) { 339 6508971 6509310
if (matched < b->depth && b->leaf == LF_PARAGRAPH) { 244 95 339
2185 /* If line doesn't open something new on its own AND existing leaf is
2186 * paragraph, treat as continuation in original container. */
2187 int lvl;
2188 const char* ds; const char* de;
2189 int fi; char fc; int fl;
2190 const char* ifs; const char* ife;
2191 244 const char* tp = p;
2192 int ord, st, cc, em; char mk;
2193 244 unsigned _f = b->ctx->flags;
2194 244 ▶ if (!(_f & MDS_FLAG_NO_THEMATIC_BREAK) && try_thematic_break(p, line_end)) ;
Branch: 2 branches
BranchTrueFalseTotal
if (!(_f & MDS_FLAG_NO_THEMATIC_BREAK) && try_thematic_break(p, line_end)) ; 244 0 244
if (!(_f & MDS_FLAG_NO_THEMATIC_BREAK) && try_thematic_break(p, line_end)) ; 24 220 244
2195 220 ▶ else if (!(_f & MDS_FLAG_NO_HEADINGS) && try_atx_heading(p, line_end, &lvl, &ds, &de)) ;
Branch: 2 branches
BranchTrueFalseTotal
else if (!(_f & MDS_FLAG_NO_HEADINGS) && try_atx_heading(p, line_end, &lvl, &ds, &de)) ; 220 0 220
else if (!(_f & MDS_FLAG_NO_HEADINGS) && try_atx_heading(p, line_end, &lvl, &ds, &de)) ; 0 220 220
2196 220 ▶ else if (!(_f & MDS_FLAG_NO_FENCED_CODE) && try_fence_open(p, line_end, &fi, &fc, &fl, &ifs, &ife)) ;
Branch: 2 branches
BranchTrueFalseTotal
else if (!(_f & MDS_FLAG_NO_FENCED_CODE) && try_fence_open(p, line_end, &fi, &fc, &fl, &ifs, &ife)) ; 220 0 220
else if (!(_f & MDS_FLAG_NO_FENCED_CODE) && try_fence_open(p, line_end, &fi, &fc, &fl, &ifs, &ife)) ; 3 217 220
2197 217 ▶ else if (!(_f & MDS_FLAG_NO_HTML) && detect_html_block_start(p, line_end, 0)) ;
Branch: 2 branches
BranchTrueFalseTotal
else if (!(_f & MDS_FLAG_NO_HTML) && detect_html_block_start(p, line_end, 0)) ; 217 0 217
else if (!(_f & MDS_FLAG_NO_HTML) && detect_html_block_start(p, line_end, 0)) ; 0 217 217
2198 217 ▶ else if (!(_f & MDS_FLAG_NO_QUOTES) && try_open_quote(&tp, line_end)) ;
Branch: 2 branches
BranchTrueFalseTotal
else if (!(_f & MDS_FLAG_NO_QUOTES) && try_open_quote(&tp, line_end)) ; 217 0 217
else if (!(_f & MDS_FLAG_NO_QUOTES) && try_open_quote(&tp, line_end)) ; 0 217 217
2199 else { int _both_lists;
2200 217 tp = p;
2201 217 ▶ _both_lists = (_f & MDS_FLAG_NO_ORDERED_LISTS) && (_f & MDS_FLAG_NO_UNORDERED_LISTS);
Branch: 2 branches
BranchTrueFalseTotal
_both_lists = (_f & MDS_FLAG_NO_ORDERED_LISTS) && (_f & MDS_FLAG_NO_UNORDERED_LISTS); 0 217 217
_both_lists = (_f & MDS_FLAG_NO_ORDERED_LISTS) && (_f & MDS_FLAG_NO_UNORDERED_LISTS); 0 0 0
2202 217 ▶ if (!_both_lists && try_open_list_item(&tp, line_end, &ord, &st, &mk, &cc, &em)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!_both_lists && try_open_list_item(&tp, line_end, &ord, &st, &mk, &cc, &em)) { 217 0 217
if (!_both_lists && try_open_list_item(&tp, line_end, &ord, &st, &mk, &cc, &em)) { 173 44 217
2203 173 ▶ if ((ord && (_f & MDS_FLAG_NO_ORDERED_LISTS)) ||
Branch: 2 branches
BranchTrueFalseTotal
if ((ord && (_f & MDS_FLAG_NO_ORDERED_LISTS)) || 23 150 173
if ((ord && (_f & MDS_FLAG_NO_ORDERED_LISTS)) || 23 0 23
2204 173 ▶ (!ord && (_f & MDS_FLAG_NO_UNORDERED_LISTS))) lazy = 1;
Branch: 2 branches
BranchTrueFalseTotal
(!ord && (_f & MDS_FLAG_NO_UNORDERED_LISTS))) lazy = 1; 150 23 173
(!ord && (_f & MDS_FLAG_NO_UNORDERED_LISTS))) lazy = 1; 0 150 150
2205 44 } else lazy = 1; }
2206 }
2207 6509310 ▶ if (!lazy) close_containers_to(b, matched);
Branch: 1 branch
BranchTrueFalseTotal
if (!lazy) close_containers_to(b, matched); 6509266 44 6509310
2208  
2209 /* CommonMark §5.3: a list ends when followed by a non-list-item block
2210 * at the same indentation (after blank lines). If the walk closed the
2211 * last CT_LIST_ITEM but left the parent CT_LIST open, and the new line
2212 * doesn't start a same-kind item, close the LIST too so subsequent
2213 * blocks render outside it. */
2214 6509310 ▶ if (!lazy && b->depth > 1 && top(b)->kind == CT_LIST) {
Branch: 3 branches
BranchTrueFalseTotal
if (!lazy && b->depth > 1 && top(b)->kind == CT_LIST) { 6509266 44 6509310
if (!lazy && b->depth > 1 && top(b)->kind == CT_LIST) { 511 6508755 6509266
if (!lazy && b->depth > 1 && top(b)->kind == CT_LIST) { 271 240 511
2215 271 const char* pk = p;
2216 int ord2, st2, cc2, em2; char mk2;
2217 271 ctn* lst = top(b);
2218 271 int keep = 0;
2219 271 ▶ if (!((b->ctx->flags & MDS_FLAG_NO_ORDERED_LISTS) &&
Branch: 1 branch
BranchTrueFalseTotal
if (!((b->ctx->flags & MDS_FLAG_NO_ORDERED_LISTS) && 0 271 271
2220 271 (b->ctx->flags & MDS_FLAG_NO_UNORDERED_LISTS)) &&
2221 271 try_open_list_item(&pk, line_end, &ord2, &st2, &mk2, &cc2, &em2) &&
2222 230 ▶ ord2 == lst->ordered && mk2 == lst->marker) {
Branch: 2 branches
BranchTrueFalseTotal
ord2 == lst->ordered && mk2 == lst->marker) { 225 5 230
ord2 == lst->ordered && mk2 == lst->marker) { 219 6 225
2223 219 keep = 1;
2224 }
2225 271 ▶ if (!keep) {
Branch: 1 branch
BranchTrueFalseTotal
if (!keep) { 52 219 271
2226 52 emit_close(b, top(b));
2227 52 b->depth--;
2228 }
2229 }
2230  
2231 /* Attribute deferred blank line (if any) to the deepest CT_LIST that
2232 * is still alive after the container walk and post-list-close. Inner
2233 * lists that closed during the walk implicitly bubble their blank up
2234 * to their parent list (this is how 325/326 become loose at the outer
2235 * level while inner stays tight). */
2236 6509310 ▶ if (b->blank_pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->blank_pending) { 5515578 993732 6509310
2237 5515578 int deepest = -1;
2238 11031395 ▶ for (i = 0; i < b->depth; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->depth; i++) 5515817 5515578 11031395
2239 5515817 ▶ if (b->stack[i].kind == CT_LIST) deepest = i;
Branch: 1 branch
BranchTrueFalseTotal
if (b->stack[i].kind == CT_LIST) deepest = i; 126 5515691 5515817
2240 5515578 ▶ if (deepest >= 0) b->stack[deepest].pending_blank = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (deepest >= 0) b->stack[deepest].pending_blank = 1; 117 5515461 5515578
2241 5515578 b->blank_pending = 0;
2242 }
2243  
2244 /* Promote deferred blank-lines into loose-marks for any CT_LIST that is
2245 * still alive after the walk + post-list-close above. Lists that just
2246 * closed lose their pending blank silently (a trailing blank doesn't
2247 * make a closed list loose). */
2248 13019483 ▶ for (i = 0; i < b->depth; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < b->depth; i++) { 6510173 6509310 13019483
2249 6510173 ▶ if (b->stack[i].kind == CT_LIST && b->stack[i].pending_blank) {
Branch: 2 branches
BranchTrueFalseTotal
if (b->stack[i].kind == CT_LIST && b->stack[i].pending_blank) { 489 6509684 6510173
if (b->stack[i].kind == CT_LIST && b->stack[i].pending_blank) { 117 372 489
2250 117 b->stack[i].had_blank_inside = 1;
2251 117 b->stack[i].pending_blank = 0;
2252 }
2253 }
2254  
2255 /* ---- indented code continuation / open ---- */
2256 /* (Cannot interrupt a paragraph; cannot start where the container walk
2257 * left us with too little room.) */
2258 {
2259 6509310 int col = count_indent(p, line_end);
2260 6509310 ▶ if (col >= 4 && b->leaf != LF_PARAGRAPH &&
Branch: 2 branches
BranchTrueFalseTotal
if (col >= 4 && b->leaf != LF_PARAGRAPH && 256 6509054 6509310
if (col >= 4 && b->leaf != LF_PARAGRAPH && 217 39 256
2261 217 ▶ !(b->ctx->flags & MDS_FLAG_NO_INDENTED_CODE)) {
Branch: 1 branch
BranchTrueFalseTotal
!(b->ctx->flags & MDS_FLAG_NO_INDENTED_CODE)) { 216 1 217
2262 size_t add;
2263 /* This is an indented-code line. */
2264 216 consume_indent(&p, line_end, 4);
2265 216 ▶ if (b->leaf != LF_CODE_INDENTED) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->leaf != LF_CODE_INDENTED) { 152 64 216
2266 152 finalize_leaf(b);
2267 152 b->leaf = LF_CODE_INDENTED;
2268 152 b->leaf_in_container = b->depth - 1;
2269 152 b->pending_blanks = 0;
2270 } else {
2271 /* flush pending blanks */
2272 94 ▶ while (b->pending_blanks > 0) {
Branch: 1 branch
BranchTrueFalseTotal
while (b->pending_blanks > 0) { 30 64 94
2273 30 buf_grow(&b->code_body, &b->code_cap, b->code_len + 2);
2274 30 b->code_body[b->code_len++] = '\n';
2275 30 b->pending_blanks--;
2276 }
2277 }
2278 216 add = (size_t)(line_end - p);
2279 216 buf_grow(&b->code_body, &b->code_cap, b->code_len + add + 2);
2280 216 ▶ if (add) memcpy(b->code_body + b->code_len, p, add);
Branch: 1 branch
BranchTrueFalseTotal
if (add) memcpy(b->code_body + b->code_len, p, add); 216 0 216
2281 216 b->code_len += add;
2282 216 b->code_body[b->code_len++] = '\n';
2283 216 return;
2284 }
2285 6509094 ▶ if (b->leaf == LF_CODE_INDENTED) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->leaf == LF_CODE_INDENTED) { 54 6509040 6509094
2286 /* non-indented non-blank line ends indented code (blanks were buffered) */
2287 54 b->pending_blanks = 0;
2288 54 finalize_leaf(b);
2289 }
2290 }
2291  
2292 /* ---- setext upgrade ---- */
2293 /* CommonMark §4.3: a setext underline may not be a lazy continuation
2294 * line of a paragraph inside a container that this line doesn't fully
2295 * match. Skip the upgrade when we'd be relying on lazy continuation. */
2296 6509094 ▶ if (b->leaf == LF_PARAGRAPH && !(b->ctx->flags & MDS_FLAG_NO_HEADINGS) && !lazy) {
Branch: 3 branches
BranchTrueFalseTotal
if (b->leaf == LF_PARAGRAPH && !(b->ctx->flags & MDS_FLAG_NO_HEADINGS) && !lazy) { 5720456 788638 6509094
if (b->leaf == LF_PARAGRAPH && !(b->ctx->flags & MDS_FLAG_NO_HEADINGS) && !lazy) { 1 788637 788638
if (b->leaf == LF_PARAGRAPH && !(b->ctx->flags & MDS_FLAG_NO_HEADINGS) && !lazy) { 44 788593 788637
2297 788593 int sx = try_setext_underline(p, line_end);
2298 788593 ▶ if (sx) {
Branch: 1 branch
BranchTrueFalseTotal
if (sx) { 788510 83 788593
2299 /* CM §4.7: link-reference-definition lines do not contribute
2300 * content. If the entire paragraph buffer consists of ref-defs,
2301 * there is no real paragraph above to upgrade — treat the
2302 * '===' / '---' as a fresh paragraph (or thematic break,
2303 * already checked earlier). */
2304 83 int all_refs = 0;
2305 83 ▶ if (!(b->ctx->flags & MDS_FLAG_NO_REFERENCES) && b->para_len) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(b->ctx->flags & MDS_FLAG_NO_REFERENCES) && b->para_len) { 83 0 83
if (!(b->ctx->flags & MDS_FLAG_NO_REFERENCES) && b->para_len) { 83 0 83
2306 83 const char* rp = b->para;
2307 83 const char* re = b->para + b->para_len;
2308 89 ▶ while (rp < re) {
Branch: 1 branch
BranchTrueFalseTotal
while (rp < re) { 86 3 89
2309 const char* nrp;
2310 const char *xls, *xle, *xus, *xue, *xts, *xte;
2311 86 ▶ if (!parse_linkref(rp, re, &nrp,
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_linkref(rp, re, &nrp, 80 6 86
2312 &xls, &xle, &xus, &xue, &xts, &xte))
2313 80 break;
2314 6 rp = nrp;
2315 }
2316 /* Skip trailing whitespace. */
2317 83 ▶ while (rp < re && (*rp == ' ' || *rp == '\t' || *rp == '\n')) rp++;
Branch: 4 branches
BranchTrueFalseTotal
while (rp < re && (*rp == ' ' || *rp == '\t' || *rp == '\n')) rp++; 80 3 83
while (rp < re && (*rp == ' ' || *rp == '\t' || *rp == '\n')) rp++; 0 80 80
while (rp < re && (*rp == ' ' || *rp == '\t' || *rp == '\n')) rp++; 0 80 80
while (rp < re && (*rp == ' ' || *rp == '\t' || *rp == '\n')) rp++; 0 80 80
2318 83 ▶ if (rp == re) all_refs = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (rp == re) all_refs = 1; 3 80 83
2319 }
2320 83 ▶ if (!all_refs) {
Branch: 1 branch
BranchTrueFalseTotal
if (!all_refs) { 3 80 83
2321 80 b->setext_level = sx;
2322 80 finalize_leaf(b);
2323 80 return;
2324 }
2325 }
2326 }
2327  
2328 /* ---- 2. OPEN ---- */
2329 742 for (;;) {
2330 const char* before;
2331 unsigned _of;
2332 int ord, st, cc, em; char mk;
2333 const char* save;
2334 int _both_lists2;
2335 ctn* t;
2336 int nested_inside_item;
2337 int same_list;
2338 ctn* lc;
2339 ctn* it;
2340 6509756 before = p;
2341 6509756 _of = b->ctx->flags;
2342 6509756 ▶ if (!(_of & MDS_FLAG_NO_QUOTES) && try_open_quote(&p, line_end)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(_of & MDS_FLAG_NO_QUOTES) && try_open_quote(&p, line_end)) { 6509755 1 6509756
if (!(_of & MDS_FLAG_NO_QUOTES) && try_open_quote(&p, line_end)) { 177 6509578 6509755
2343 177 finalize_leaf(b);
2344 177 ▶ if (!push(b, CT_QUOTE)) { p = before; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!push(b, CT_QUOTE)) { p = before; break; } 0 177 177
2345 177 emit_open(b, b->depth - 1);
2346 742 continue;
2347 }
2348 6509579 save = p;
2349 6509579 ▶ _both_lists2 = (_of & MDS_FLAG_NO_ORDERED_LISTS) && (_of & MDS_FLAG_NO_UNORDERED_LISTS);
Branch: 2 branches
BranchTrueFalseTotal
_both_lists2 = (_of & MDS_FLAG_NO_ORDERED_LISTS) && (_of & MDS_FLAG_NO_UNORDERED_LISTS); 4 6509575 6509579
_both_lists2 = (_of & MDS_FLAG_NO_ORDERED_LISTS) && (_of & MDS_FLAG_NO_UNORDERED_LISTS); 2 2 4
2350 /* CommonMark §4.1: thematic break wins over a list-item interpretation. */
2351 13019157 if (!(_of & MDS_FLAG_NO_THEMATIC_BREAK) &&
2352 6509578 try_thematic_break(p, line_end)) {
2353 104 break;
2354 }
2355 6509475 ▶ if (!_both_lists2 && try_open_list_item(&p, line_end, &ord, &st, &mk, &cc, &em)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!_both_lists2 && try_open_list_item(&p, line_end, &ord, &st, &mk, &cc, &em)) { 6509473 2 6509475
if (!_both_lists2 && try_open_list_item(&p, line_end, &ord, &st, &mk, &cc, &em)) { 611 6508862 6509473
2356 611 ▶ if ((ord && (_of & MDS_FLAG_NO_ORDERED_LISTS)) ||
Branch: 2 branches
BranchTrueFalseTotal
if ((ord && (_of & MDS_FLAG_NO_ORDERED_LISTS)) || 137 474 611
if ((ord && (_of & MDS_FLAG_NO_ORDERED_LISTS)) || 135 2 137
2357 611 ▶ (!ord && (_of & MDS_FLAG_NO_UNORDERED_LISTS))) { p = save; break; }
Branch: 2 branches
BranchTrueFalseTotal
(!ord && (_of & MDS_FLAG_NO_UNORDERED_LISTS))) { p = save; break; } 474 135 609
(!ord && (_of & MDS_FLAG_NO_UNORDERED_LISTS))) { p = save; break; } 2 472 474
2358 /* CommonMark §5.2: a list item may only interrupt a paragraph
2359 * if (a) the marker isn't empty, and (b) for ordered lists,
2360 * the start number is 1. */
2361 607 ▶ if (b->leaf == LF_PARAGRAPH && (em || (ord && st != 1))) {
Branch: 4 branches
BranchTrueFalseTotal
if (b->leaf == LF_PARAGRAPH && (em || (ord && st != 1))) { 80 527 607
if (b->leaf == LF_PARAGRAPH && (em || (ord && st != 1))) { 71 9 80
if (b->leaf == LF_PARAGRAPH && (em || (ord && st != 1))) { 8 63 71
if (b->leaf == LF_PARAGRAPH && (em || (ord && st != 1))) { 3 5 8
2362 12 p = save;
2363 12 break;
2364 }
2365 595 finalize_leaf(b);
2366 595 ▶ t = (b->depth > 0) ? top(b) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
t = (b->depth > 0) ? top(b) : NULL; 595 0 595
2367 595 ▶ nested_inside_item = (t && t->kind == CT_LIST_ITEM);
Branch: 2 branches
BranchTrueFalseTotal
nested_inside_item = (t && t->kind == CT_LIST_ITEM); 595 0 595
nested_inside_item = (t && t->kind == CT_LIST_ITEM); 68 527 595
2368 595 ▶ same_list = (t && t->kind == CT_LIST
Branch: 1 branch
BranchTrueFalseTotal
same_list = (t && t->kind == CT_LIST 216 379 595
2369 1190 ▶ && t->ordered == ord && t->marker == mk);
Branch: 3 branches
BranchTrueFalseTotal
&& t->ordered == ord && t->marker == mk); 595 0 595
&& t->ordered == ord && t->marker == mk); 216 0 216
&& t->ordered == ord && t->marker == mk); 216 0 216
2370 595 ▶ if (nested_inside_item) {
Branch: 1 branch
BranchTrueFalseTotal
if (nested_inside_item) { 68 527 595
2371 /* Start a fresh nested list inside the current item. */
2372 68 ▶ if (!push(b, CT_LIST)) { p = save; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!push(b, CT_LIST)) { p = save; break; } 0 68 68
2373 68 lc = top(b);
2374 68 lc->ordered = ord;
2375 68 lc->start = st;
2376 68 lc->marker = mk;
2377 68 lc->tight = 1;
2378 68 emit_open(b, b->depth - 1);
2379 527 ▶ } else if (same_list) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (same_list) { 311 216 527
2380 /* close the previous list_item (one below top) is implicit;
2381 * actually no list_item is on top here — close prior sibling
2382 * item if any sits inside this list. */
2383 /* (no-op: a sibling item is added below.) */
2384 } else {
2385 /* Different list at root or beside another list — close any
2386 * trailing list/list_item below this depth and start anew. */
2387 311 ▶ while (b->depth > 1 &&
Branch: 1 branch
BranchTrueFalseTotal
while (b->depth > 1 && 15 296 311
2388 15 ▶ (top(b)->kind == CT_LIST_ITEM || top(b)->kind == CT_LIST)) {
Branch: 2 branches
BranchTrueFalseTotal
(top(b)->kind == CT_LIST_ITEM || top(b)->kind == CT_LIST)) { 0 15 15
(top(b)->kind == CT_LIST_ITEM || top(b)->kind == CT_LIST)) { 0 15 15
2389 0 emit_close(b, top(b));
2390 0 b->depth--;
2391 }
2392 311 ▶ if (!push(b, CT_LIST)) { p = save; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (!push(b, CT_LIST)) { p = save; break; } 0 311 311
2393 311 lc = top(b);
2394 311 lc->ordered = ord;
2395 311 lc->start = st;
2396 311 lc->marker = mk;
2397 311 lc->tight = 1;
2398 311 emit_open(b, b->depth - 1);
2399 }
2400 595 ▶ if (!push(b, CT_LIST_ITEM)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!push(b, CT_LIST_ITEM)) break; 0 595 595
2401 595 it = top(b);
2402 595 it->content_col = cc;
2403 595 it->is_empty = em;
2404 595 it->blank_after_empty = 0;
2405 595 emit_open(b, b->depth - 1);
2406 595 ▶ if (em) return;
Branch: 1 branch
BranchTrueFalseTotal
if (em) return; 30 565 595
2407 565 continue;
2408 }
2409 6508864 break;
2410 }
2411  
2412 /* ---- 3. LEAF ---- */
2413 /* Indented-code check (post-OPEN). When a new container (quote or
2414 * list-item) was opened in this line, the leading whitespace at `p`
2415 * may now reach 4 columns and be eligible for an indented code block.
2416 * (Cannot interrupt a paragraph.) */
2417 6508984 ▶ if (b->leaf != LF_PARAGRAPH && b->leaf != LF_CODE_INDENTED &&
Branch: 2 branches
BranchTrueFalseTotal
if (b->leaf != LF_PARAGRAPH && b->leaf != LF_CODE_INDENTED && 5720507 788477 6508984
if (b->leaf != LF_PARAGRAPH && b->leaf != LF_CODE_INDENTED && 5720507 0 5720507
2418 5720507 ▶ !(b->ctx->flags & MDS_FLAG_NO_INDENTED_CODE)) {
Branch: 1 branch
BranchTrueFalseTotal
!(b->ctx->flags & MDS_FLAG_NO_INDENTED_CODE)) { 5720506 1 5720507
2419 5720506 int col2 = count_indent(p, line_end);
2420 5720506 ▶ if (col2 >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (col2 >= 4) { 18 5720488 5720506
2421 size_t add;
2422 18 consume_indent(&p, line_end, 4);
2423 18 finalize_leaf(b);
2424 18 b->leaf = LF_CODE_INDENTED;
2425 18 b->leaf_in_container = b->depth - 1;
2426 18 b->pending_blanks = 0;
2427 18 add = (size_t)(line_end - p);
2428 18 buf_grow(&b->code_body, &b->code_cap, b->code_len + add + 2);
2429 18 ▶ if (add) memcpy(b->code_body + b->code_len, p, add);
Branch: 1 branch
BranchTrueFalseTotal
if (add) memcpy(b->code_body + b->code_len, p, add); 18 0 18
2430 18 b->code_len += add;
2431 18 b->code_body[b->code_len++] = '\n';
2432 18 return;
2433 }
2434 }
2435 /* HTML block start — types 1..6 can interrupt a paragraph; type 7 cannot. */
2436 6508966 ▶ if (!(b->ctx->flags & MDS_FLAG_NO_HTML)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(b->ctx->flags & MDS_FLAG_NO_HTML)) { 6508965 1 6508966
2437 6508965 int allow7 = (b->leaf != LF_PARAGRAPH);
2438 6508965 int htype = detect_html_block_start(p, line_end, allow7);
2439 6508965 ▶ if (htype) {
Branch: 1 branch
BranchTrueFalseTotal
if (htype) { 200 6508765 6508965
2440 size_t add;
2441 200 finalize_leaf(b);
2442 200 b->leaf = LF_HTML;
2443 200 b->leaf_in_container = b->depth - 1;
2444 200 b->html_type = htype;
2445 200 b->html_len = 0;
2446 200 add = (size_t)(line_end - p);
2447 200 buf_grow(&b->html_body, &b->html_cap, b->html_len + add + 2);
2448 200 ▶ if (add) memcpy(b->html_body + b->html_len, p, add);
Branch: 1 branch
BranchTrueFalseTotal
if (add) memcpy(b->html_body + b->html_len, p, add); 200 0 200
2449 200 b->html_len += add;
2450 200 b->html_body[b->html_len++] = '\n';
2451 200 ▶ if (detect_html_block_end(htype, p, line_end, blank)) {
Branch: 1 branch
BranchTrueFalseTotal
if (detect_html_block_end(htype, p, line_end, blank)) { 34 166 200
2452 34 finalize_leaf(b);
2453 }
2454 200 return;
2455 }
2456 }
2457 13017528 if (!(b->ctx->flags & MDS_FLAG_NO_HEADINGS) &&
2458 6508762 try_atx_heading(p, line_end, &level, &bs, &be)) {
2459 mds_block_detail d;
2460 808194 finalize_leaf(b);
2461 808194 memset(&d, 0, sizeof d);
2462 808194 d.u.heading.level = level;
2463 808194 sax_enter(b, MDS_BLK_HEADING, &d);
2464 808194 ▶ if (be > bs) sax_inline_text(b, bs, (size_t)(be - bs));
Branch: 1 branch
BranchTrueFalseTotal
if (be > bs) sax_inline_text(b, bs, (size_t)(be - bs)); 808185 9 808194
2465 808194 sax_leave(b, MDS_BLK_HEADING);
2466 808194 return;
2467 }
2468 11401143 if (!(b->ctx->flags & MDS_FLAG_NO_THEMATIC_BREAK) &&
2469 5700571 try_thematic_break(p, line_end)) {
2470 mds_block_detail d;
2471 104 finalize_leaf(b);
2472 /* CommonMark §4.1: a thematic break closes any enclosing list
2473 * whose item context has already been closed (top-of-stack is
2474 * CT_LIST without an open CT_LIST_ITEM). When the top is a
2475 * CT_LIST_ITEM we are inside a new item (e.g. "- * * *") and
2476 * the break legitimately renders within it. */
2477 107 ▶ while (b->depth > 1 && top(b)->kind == CT_LIST) {
Branch: 2 branches
BranchTrueFalseTotal
while (b->depth > 1 && top(b)->kind == CT_LIST) { 6 101 107
while (b->depth > 1 && top(b)->kind == CT_LIST) { 3 3 6
2478 3 emit_close(b, top(b));
2479 3 b->depth--;
2480 }
2481 104 memset(&d, 0, sizeof d);
2482 104 sax_enter(b, MDS_BLK_THEMATIC_BREAK, &d);
2483 104 sax_leave(b, MDS_BLK_THEMATIC_BREAK);
2484 104 return;
2485 }
2486 11400933 if (!(b->ctx->flags & MDS_FLAG_NO_FENCED_CODE) &&
2487 5700465 try_fence_open(p, line_end, &findent, &fch, &flen, &ifs, &ife)) {
2488 size_t iln;
2489 808139 finalize_leaf(b);
2490 808139 b->leaf = LF_CODE_FENCED;
2491 808139 b->leaf_in_container = b->depth - 1;
2492 808139 b->fence_char = fch;
2493 808139 b->fence_len = flen;
2494 808139 b->fence_indent = findent;
2495 808139 iln = (size_t)(ife - ifs);
2496 808139 ▶ if (iln) {
Branch: 1 branch
BranchTrueFalseTotal
if (iln) { 808043 96 808139
2497 size_t w;
2498 /* CommonMark: backslash-escape ASCII punctuation in info string. */
2499 808043 b->fence_info = (char*)mds_arena_alloc(&b->ctx->arena, iln + 1);
2500 808043 w = 0;
2501 4040299 ▶ for (r = 0; r < iln; r++) {
Branch: 1 branch
BranchTrueFalseTotal
for (r = 0; r < iln; r++) { 3232256 808043 4040299
2502 3232256 unsigned char ch = (unsigned char)ifs[r];
2503 3232256 ▶ if (ch == '\\' && r + 1 < iln) {
Branch: 2 branches
BranchTrueFalseTotal
if (ch == '\\' && r + 1 < iln) { 3 3232253 3232256
if (ch == '\\' && r + 1 < iln) { 3 0 3
2504 3 unsigned char nx = (unsigned char)ifs[r+1];
2505 3 ▶ if ((nx >= 0x21 && nx <= 0x2F) ||
Branch: 3 branches
BranchTrueFalseTotal
if ((nx >= 0x21 && nx <= 0x2F) || 3 0 3
if ((nx >= 0x21 && nx <= 0x2F) || 0 3 3
if ((nx >= 0x21 && nx <= 0x2F) || 0 0 0
2506 0 ▶ (nx >= 0x3A && nx <= 0x40) ||
Branch: 2 branches
BranchTrueFalseTotal
(nx >= 0x3A && nx <= 0x40) || 0 0 0
(nx >= 0x3A && nx <= 0x40) || 0 0 0
2507 0 ▶ (nx >= 0x5B && nx <= 0x60) ||
Branch: 2 branches
BranchTrueFalseTotal
(nx >= 0x5B && nx <= 0x60) || 0 0 0
(nx >= 0x5B && nx <= 0x60) || 0 0 0
2508 0 ▶ (nx >= 0x7B && nx <= 0x7E)) {
Branch: 1 branch
BranchTrueFalseTotal
(nx >= 0x7B && nx <= 0x7E)) { 0 0 0
2509 3 b->fence_info[w++] = (char)nx;
2510 3 r++;
2511 3 continue;
2512 }
2513 }
2514 3232253 b->fence_info[w++] = (char)ch;
2515 }
2516 808043 b->fence_info[w] = '\0';
2517 808043 b->fence_info_len = w;
2518 } else {
2519 96 b->fence_info = NULL;
2520 96 b->fence_info_len = 0;
2521 }
2522 808139 return;
2523 }
2524  
2525 /* paragraph (new or continuation) */
2526 4892329 ▶ if (b->leaf != LF_PARAGRAPH) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->leaf != LF_PARAGRAPH) { 4507879 384450 4892329
2527 4507879 b->leaf = LF_PARAGRAPH;
2528 4507879 b->leaf_in_container = b->depth - 1;
2529 }
2530 4892329 tp = p;
2531 4892329 lead = 0;
2532 4892566 ▶ while (tp < line_end && *tp == ' ' && lead < 3) { tp++; lead++; }
Branch: 3 branches
BranchTrueFalseTotal
while (tp < line_end && *tp == ' ' && lead < 3) { tp++; lead++; } 4892557 9 4892566
while (tp < line_end && *tp == ' ' && lead < 3) { tp++; lead++; } 277 4892280 4892557
while (tp < line_end && *tp == ' ' && lead < 3) { tp++; lead++; } 237 40 277
2533 4892329 ▶ if (b->para_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->para_len > 0) { 384447 4507882 4892329
2534 384447 buf_grow(&b->para, &b->para_cap, b->para_len + 2);
2535 384447 b->para[b->para_len++] = '\n';
2536 }
2537 4892329 buf_append(&b->para, &b->para_len, &b->para_cap,
2538 4892329 tp, (size_t)(line_end - tp));
2539 }
« src/mds_footnote.c ↑ Summary src/mds_inline.c »