| 1 |
|
| /* mds_footnote.c — GFM footnote definitions table. |
| 2 |
|
| * |
| 3 |
|
| * Lookup is by case-folded label (matching link-reference rules so that |
| 4 |
|
| * `[^Foo]: x` is found by `[^foo]`). The implementation reuses the |
| 5 |
|
| * same fold/normalise approach as mds_linkref by simply lowercasing |
| 6 |
|
| * ASCII and collapsing inner whitespace; GFM spec examples only need |
| 7 |
|
| * that subset for footnote labels (no Unicode case-folding in tests). |
| 8 |
|
| */ |
| 9 |
|
| |
| 10 |
|
| #include "mds_footnote.h" |
| 11 |
|
| #include <string.h> |
| 12 |
|
| #include <stdlib.h> |
| 13 |
|
| #include <ctype.h> |
| 14 |
|
| |
| 15 |
1656466 |
| static size_t fn_normalise(const char* s, size_t n, char* out) { |
| 16 |
1656466 |
| size_t a = 0, b = n; |
| 17 |
1656466 |
| size_t j = 0; |
| 18 |
|
| size_t i; |
| 19 |
1656466 |
| int in_ws = 0; |
| 20 |
1656466 |
▶ | while (a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\n')) a++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\n')) a++; |
1656466 |
0 |
1656466 |
| while (a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\n')) a++; |
0 |
1656466 |
1656466 |
| while (a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\n')) a++; |
0 |
1656466 |
1656466 |
| while (a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\n')) a++; |
0 |
1656466 |
1656466 |
|
| 21 |
1656466 |
▶ | while (b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\n')) b--; |
Branch: 4 branches
| Branch | True | False | Total |
| while (b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\n')) b--; |
1656466 |
0 |
1656466 |
| while (b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\n')) b--; |
0 |
1656466 |
1656466 |
| while (b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\n')) b--; |
0 |
1656466 |
1656466 |
| while (b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\n')) b--; |
0 |
1656466 |
1656466 |
|
| 22 |
3313586 |
▶ | for (i = a; i < b; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = a; i < b; i++) { |
1657120 |
1656466 |
3313586 |
|
| 23 |
1657120 |
| unsigned char c = (unsigned char)s[i]; |
| 24 |
1657120 |
▶ | if (c == ' ' || c == '\t' || c == '\n') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == ' ' || c == '\t' || c == '\n') { |
1657120 |
0 |
1657120 |
| if (c == ' ' || c == '\t' || c == '\n') { |
1657120 |
0 |
1657120 |
| if (c == ' ' || c == '\t' || c == '\n') { |
0 |
1657120 |
1657120 |
|
| 25 |
0 |
▶ | if (!in_ws) { out[j++] = ' '; in_ws = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in_ws) { out[j++] = ' '; in_ws = 1; } |
0 |
0 |
0 |
|
| 26 |
|
| } else { |
| 27 |
1657120 |
▶ | if (c >= 'A' && c <= 'Z') c = (unsigned char)(c + 0x20); |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'A' && c <= 'Z') c = (unsigned char)(c + 0x20); |
1656988 |
132 |
1657120 |
| if (c >= 'A' && c <= 'Z') c = (unsigned char)(c + 0x20); |
0 |
1656988 |
1656988 |
|
| 28 |
1657120 |
| out[j++] = (char)c; |
| 29 |
1657120 |
| in_ws = 0; |
| 30 |
|
| } |
| 31 |
|
| } |
| 32 |
1656466 |
| return j; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
121263 |
| static char* fn_arena_dup(mds_arena* a, const char* s, size_t n) { |
| 36 |
|
| char* d; |
| 37 |
121263 |
| d = (char*)mds_arena_alloc(a, n + 1); |
| 38 |
121263 |
▶ | if (n) memcpy(d, s, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) memcpy(d, s, n); |
121263 |
0 |
121263 |
|
| 39 |
121263 |
| d[n] = '\0'; |
| 40 |
121263 |
| return d; |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
40409 |
| void mds_footnote_init(struct mds_footnote_tab* t, mds_arena* a) { |
| 44 |
40409 |
| t->entries = NULL; |
| 45 |
40409 |
| t->len = 0; |
| 46 |
40409 |
| t->cap = 0; |
| 47 |
40409 |
| t->arena = a; |
| 48 |
40409 |
| } |
| 49 |
|
| |
| 50 |
|
| /* The entry strings are arena copies and go with the arena, but the entry |
| 51 |
|
| * array itself is grown with realloc off the general heap, so it needs an |
| 52 |
|
| * explicit release. Call this before the arena is reset or freed: the table |
| 53 |
|
| * struct is itself arena-allocated. */ |
| 54 |
164107 |
| void mds_footnote_free(struct mds_footnote_tab* t) { |
| 55 |
164107 |
▶ | if (!t) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) return; |
123698 |
40409 |
164107 |
|
| 56 |
40409 |
| free(t->entries); |
| 57 |
40409 |
| t->entries = NULL; |
| 58 |
40409 |
| t->len = 0; |
| 59 |
40409 |
| t->cap = 0; |
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
848445 |
| const mds_footnote* mds_footnote_get(const struct mds_footnote_tab* t, |
| 63 |
|
| const char* label, size_t llen) { |
| 64 |
|
| char buf[4096]; |
| 65 |
|
| size_t nlen; |
| 66 |
|
| size_t i; |
| 67 |
848445 |
▶ | if (!t || !t->len) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!t || !t->len) return NULL; |
848445 |
0 |
848445 |
| if (!t || !t->len) return NULL; |
0 |
848445 |
848445 |
|
| 68 |
848445 |
▶ | if (llen > sizeof buf) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (llen > sizeof buf) return NULL; |
0 |
848445 |
848445 |
|
| 69 |
848445 |
| nlen = fn_normalise(label, llen, buf); |
| 70 |
848496 |
▶ | for (i = 0; i < t->len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->len; i++) { |
848493 |
3 |
848496 |
|
| 71 |
848493 |
▶ | if (t->entries[i].klen == nlen && |
Branch: 2 branches
| Branch | True | False | Total |
| if (t->entries[i].klen == nlen && |
848442 |
51 |
848493 |
| if (t->entries[i].klen == nlen && |
848442 |
0 |
848442 |
|
| 72 |
848442 |
▶ | (nlen == 0 || memcmp(t->entries[i].key, buf, nlen) == 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| (nlen == 0 || memcmp(t->entries[i].key, buf, nlen) == 0)) |
848442 |
0 |
848442 |
|
| 73 |
848442 |
| return &t->entries[i]; |
| 74 |
|
| } |
| 75 |
3 |
| return NULL; |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
808021 |
| int mds_footnote_add(struct mds_footnote_tab* t, |
| 79 |
|
| const char* label, size_t llen, |
| 80 |
|
| const char* body, size_t blen) { |
| 81 |
|
| char nbuf[4096]; |
| 82 |
|
| size_t nlen; |
| 83 |
|
| size_t i; |
| 84 |
|
| mds_footnote* e; |
| 85 |
808021 |
▶ | if (llen > sizeof nbuf) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (llen > sizeof nbuf) return 0; |
0 |
808021 |
808021 |
|
| 86 |
808021 |
| nlen = fn_normalise(label, llen, nbuf); |
| 87 |
|
| /* duplicate label: first-wins (spec §6.13). */ |
| 88 |
808051 |
▶ | for (i = 0; i < t->len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->len; i++) { |
767630 |
40421 |
808051 |
|
| 89 |
767630 |
▶ | if (t->entries[i].klen == nlen && |
Branch: 2 branches
| Branch | True | False | Total |
| if (t->entries[i].klen == nlen && |
767600 |
30 |
767630 |
| if (t->entries[i].klen == nlen && |
767600 |
0 |
767600 |
|
| 90 |
767600 |
▶ | (nlen == 0 || memcmp(t->entries[i].key, nbuf, nlen) == 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| (nlen == 0 || memcmp(t->entries[i].key, nbuf, nlen) == 0)) |
767600 |
0 |
767600 |
|
| 91 |
767600 |
| return 0; |
| 92 |
|
| } |
| 93 |
40421 |
▶ | if (t->len == t->cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->len == t->cap) { |
40409 |
12 |
40421 |
|
| 94 |
40409 |
▶ | size_t nc = t->cap ? t->cap * 2 : 8; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t nc = t->cap ? t->cap * 2 : 8; |
0 |
40409 |
40409 |
|
| 95 |
40409 |
| t->entries = (mds_footnote*)realloc(t->entries, nc * sizeof(mds_footnote)); |
| 96 |
40409 |
| t->cap = nc; |
| 97 |
|
| } |
| 98 |
40421 |
| e = &t->entries[t->len++]; |
| 99 |
40421 |
| e->label = fn_arena_dup(t->arena, label, llen); e->llen = llen; |
| 100 |
40421 |
| e->key = fn_arena_dup(t->arena, nbuf, nlen); e->klen = nlen; |
| 101 |
40421 |
| e->body = fn_arena_dup(t->arena, body, blen); e->blen = blen; |
| 102 |
40421 |
| return 1; |
| 103 |
|
| } |