Delimited blocks, then the headings outside them. */
| 183 | |
| 184 | /* Delimited blocks, then the headings outside them. */ |
| 185 | static void ad_structure(ad_doc_t *d) { |
| 186 | int open = -1; |
| 187 | for (int i = 0; i < d->n; i++) { |
| 188 | int dl = ad_delim(&d->L[i]); |
| 189 | if (open < 0) { |
| 190 | if (dl) { |
| 191 | open = i; |
| 192 | d->in_block[i] = d->L[i].s[0] == '/' ? 2 : 1; |
| 193 | } |
| 194 | continue; |
| 195 | } |
| 196 | const ad_line_t *o = &d->L[open]; |
| 197 | d->in_block[i] = d->L[open].s[0] == '/' ? 2 : 1; |
| 198 | if (dl && dl == ad_rlen(o) && memcmp(d->L[i].s, o->s, (size_t)dl) == 0) { |
| 199 | open = -1; |
| 200 | } |
| 201 | } |
| 202 | d->heads = (ad_head_t *)cbm_arena_alloc(d->scratch, (size_t)d->n * sizeof(*d->heads)); |
| 203 | if (!d->heads) { |
| 204 | d->failed = true; |
| 205 | return; |
| 206 | } |
| 207 | for (int i = 0; i < d->n; i++) { |
| 208 | int ta; |
| 209 | int tb; |
| 210 | if (d->in_block[i] || ad_heading(&d->L[i], &ta, &tb) == 0) { |
| 211 | continue; |
| 212 | } |
| 213 | char *title = cbm_arena_strndup(d->ctx->arena, d->L[i].s + ta, (size_t)(tb - ta)); |
| 214 | if (!title) { |
| 215 | d->failed = true; |
| 216 | return; |
| 217 | } |
| 218 | d->heads[d->nheads++] = (ad_head_t){.line = i, .title = title}; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /* qn_safe_segment (extract_defs.c): whitespace runs become '-'. */ |
| 223 | static const char *ad_qn_segment(CBMArena *a, const char *name) { |
no test coverage detected