The level of the heading on line k: `#` count, or 1/2 for a setext * underline of `=`/`-` on the next line; 0 when the line is no heading. */
| 339 | /* The level of the heading on line k: `#` count, or 1/2 for a setext |
| 340 | * underline of `=`/`-` on the next line; 0 when the line is no heading. */ |
| 341 | static int adr_heading_level(const adr_lines_t *L, int k, const char **title, size_t *title_n) { |
| 342 | const adr_line_t *l = &L->v[k]; |
| 343 | int i = 0; |
| 344 | while (i < l->n && i < (PAIR_LEN + SKIP_ONE) && l->s[i] == ' ') { |
| 345 | i++; |
| 346 | } |
| 347 | int hashes = 0; |
| 348 | while (i + hashes < l->n && l->s[i + hashes] == '#') { |
| 349 | hashes++; |
| 350 | } |
| 351 | if (hashes > 0 && hashes <= (PAIR_LEN * PAIR_LEN + PAIR_LEN) && |
| 352 | (i + hashes == l->n || adr_blank(l->s[i + hashes]))) { |
| 353 | const char *t = l->s + i + hashes; |
| 354 | size_t tn = (size_t)(l->n - i - hashes); |
| 355 | while (tn > 0 && (t[tn - SKIP_ONE] == '#' || adr_blank(t[tn - SKIP_ONE]))) { |
| 356 | tn--; |
| 357 | } |
| 358 | adr_trim(&t, &tn); |
| 359 | *title = t; |
| 360 | *title_n = tn; |
| 361 | return hashes; |
| 362 | } |
| 363 | if (k + SKIP_ONE < L->count) { |
| 364 | const adr_line_t *u = &L->v[k + SKIP_ONE]; |
| 365 | int j = 0; |
| 366 | while (j < u->n && u->s[j] == ' ') { |
| 367 | j++; |
| 368 | } |
| 369 | char c = j < u->n ? u->s[j] : 0; |
| 370 | int run = 0; |
| 371 | while (j + run < u->n && u->s[j + run] == c) { |
| 372 | run++; |
| 373 | } |
| 374 | bool rest_blank = true; |
| 375 | for (int r = j + run; r < u->n; r++) { |
| 376 | rest_blank = rest_blank && adr_blank(u->s[r]); |
| 377 | } |
| 378 | if ((c == '=' || c == '-') && run >= SKIP_ONE && rest_blank) { |
| 379 | const char *t = l->s; |
| 380 | size_t tn = (size_t)l->n; |
| 381 | adr_trim(&t, &tn); |
| 382 | *title = t; |
| 383 | *title_n = tn; |
| 384 | return c == '=' ? SKIP_ONE : PAIR_LEN; |
| 385 | } |
| 386 | } |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | /* Section kinds the facts and the detection read. */ |
| 391 | typedef enum { |
no test coverage detected