src/mds_slug.h

filter: Statementstmt 100% 58 / 58 Branchbran 87.1% 61 / 70 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.9% 119 / 128 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/mds.c ↑ Summary src/simd/mds_simd_avx2.c »
1 /* mds_slug.h - heading text to anchor id.
2 *
3 * Header-only, in the style of the Eshu headers this engine already pulls in.
4 * The rule is GitHub's, because that is what readers' muscle memory and every
5 * existing cross-document link already assume: lowercase, ASCII punctuation
6 * dropped, runs of whitespace collapsed to a single hyphen, leading and
7 * trailing hyphens trimmed.
8 *
9 * Bytes >= 0x80 pass through untouched. Lowercasing UTF-8 properly would mean
10 * carrying a case-folding table for the whole of Unicode, and getting it half
11 * right (ASCII-folding the lead bytes of a multi-byte sequence) would corrupt
12 * the text outright. Passing them through leaves a heading in Greek or
13 * Japanese with a usable, stable, round-trippable id.
14 *
15 * Slugs must be unique within a document, so the caller keeps a table of what
16 * it has already emitted and mds_slug_uniq appends -1, -2 and so on. The
17 * table is the caller's because the renderer owns the document scope; this
18 * header stays free of state.
19 */
20 #ifndef MDS_SLUG_H
21 #define MDS_SLUG_H
22  
23 #include <stddef.h>
24 #include <string.h>
25 #include <stdlib.h>
26  
27 /* Keep an id from growing without bound on a pathological heading. Long
28 * enough that no realistic heading is truncated. */
29 #define MDS_SLUG_MAX 128
30  
31 /* True for the ASCII bytes a slug keeps verbatim. */
32 11756839 static int mds_slug_keep(unsigned char c) {
33 11756839 ▶ return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
Branch: 4 branches
BranchTrueFalseTotal
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || 8 10504409 10504417
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || 1252420 10 1252430
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || 18 1252402 1252420
return (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || 24 4 28
34 23513678 ▶ c == '-' || c == '_' || c >= 0x80;
Branch: 3 branches
BranchTrueFalseTotal
c == '-' || c == '_' || c >= 0x80; 10504417 1252422 11756839
c == '-' || c == '_' || c >= 0x80; 22 2 24
c == '-' || c == '_' || c >= 0x80; 8 14 22
35 }
36  
37 /* Write the slug for `src` into `out` (at least MDS_SLUG_MAX + 1 bytes).
38 * Returns the length written, excluding the NUL. May be 0: a heading of pure
39 * punctuation has no slug, and the caller decides what to do about that. */
40 808051 static size_t mds_slugify(const char* src, size_t len, char* out) {
41 808051 size_t i, n = 0;
42 808051 int pending_dash = 0;
43  
44 14180948 ▶ for (i = 0; i < len && n < MDS_SLUG_MAX; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < len && n < MDS_SLUG_MAX; i++) { 13372898 808050 14180948
for (i = 0; i < len && n < MDS_SLUG_MAX; i++) { 13372897 1 13372898
45 13372897 unsigned char c = (unsigned char)src[i];
46  
47 13372897 ▶ if (c >= 'A' && c <= 'Z') c = (unsigned char)(c - 'A' + 'a');
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'Z') c = (unsigned char)(c - 'A' + 'a'); 10504419 2868478 13372897
if (c >= 'A' && c <= 'Z') c = (unsigned char)(c - 'A' + 'a'); 1616044 8888375 10504419
48  
49 13372897 ▶ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
Branch: 4 branches
BranchTrueFalseTotal
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { 11756839 1616058 13372897
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { 11756839 0 11756839
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { 11756839 0 11756839
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { 0 11756839 11756839
50 /* Defer: a run of whitespace becomes one hyphen, and trailing
51 * whitespace becomes nothing at all. */
52 1616058 ▶ if (n > 0) pending_dash = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) pending_dash = 1; 1616058 0 1616058
53 1616058 continue;
54 }
55 11756839 ▶ if (!mds_slug_keep(c)) continue; /* ASCII punctuation */
Branch: 1 branch
BranchTrueFalseTotal
if (!mds_slug_keep(c)) continue; /* ASCII punctuation */ 14 11756825 11756839
56  
57 11756825 ▶ if (pending_dash) {
Branch: 1 branch
BranchTrueFalseTotal
if (pending_dash) { 1616052 10140773 11756825
58 1616052 out[n++] = '-';
59 1616052 pending_dash = 0;
60 1616052 ▶ if (n >= MDS_SLUG_MAX) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= MDS_SLUG_MAX) break; 0 1616052 1616052
61 }
62 11756825 out[n++] = (char)c;
63 }
64  
65 /* Trim hyphens that ended up at either edge. Leading ones can only come
66 * from the source text, since pending_dash never fires at n == 0. */
67 808054 ▶ while (n > 0 && out[n - 1] == '-') n--;
Branch: 2 branches
BranchTrueFalseTotal
while (n > 0 && out[n - 1] == '-') n--; 808051 3 808054
while (n > 0 && out[n - 1] == '-') n--; 3 808048 808051
68 {
69 808051 size_t lead = 0;
70 808054 ▶ while (lead < n && out[lead] == '-') lead++;
Branch: 2 branches
BranchTrueFalseTotal
while (lead < n && out[lead] == '-') lead++; 808051 3 808054
while (lead < n && out[lead] == '-') lead++; 3 808048 808051
71 808051 ▶ if (lead) { memmove(out, out + lead, n - lead); n -= lead; }
Branch: 1 branch
BranchTrueFalseTotal
if (lead) { memmove(out, out + lead, n - lead); n -= lead; } 1 808050 808051
72 }
73  
74 808051 out[n] = '\0';
75 808051 return n;
76 }
77  
78 /* A table of the slugs already emitted for this document. Grown off the
79 * general heap; the caller frees it with mds_slug_table_free. */
80 typedef struct mds_slug_table {
81 char** slugs;
82 size_t len;
83 size_t cap;
84 } mds_slug_table;
85  
86 164109 static void mds_slug_table_free(mds_slug_table* t) {
87 size_t i;
88 164109 ▶ if (!t) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return; 0 164109 164109
89 972160 ▶ for (i = 0; i < t->len; i++) free(t->slugs[i]);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->len; i++) free(t->slugs[i]); 808051 164109 972160
90 164109 free(t->slugs);
91 164109 t->slugs = NULL;
92 164109 t->len = t->cap = 0;
93 }
94  
95 /* Make `slug` unique against `t`, extending it in place with -1, -2, ... as
96 * GitHub does, and record the result. `slug` must have room for
97 * MDS_SLUG_MAX + 16 bytes. Returns the final length.
98 *
99 * A linear scan is the right shape here: a page has tens of headings, and a
100 * hash table would cost more to build than it ever saves. */
101 808051 static size_t mds_slug_uniq(mds_slug_table* t, char* slug, size_t len) {
102 size_t i;
103 808051 unsigned suffix = 0;
104 808051 size_t base_len = len;
105  
106 6 for (;;) {
107 808057 int clash = 0;
108 8484091 ▶ for (i = 0; i < t->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->len; i++) { 7676040 808051 8484091
109 7676040 ▶ if (strcmp(t->slugs[i], slug) == 0) { clash = 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(t->slugs[i], slug) == 0) { clash = 1; break; } 6 7676034 7676040
110 }
111 808057 ▶ if (!clash) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!clash) break; 808051 6 808057
112 6 suffix++;
113 /* Rewrite the suffix onto the original stem each time, so the second
114 * "Notes" is notes-1 rather than notes-1-1. */
115 6 len = base_len;
116 {
117 char num[16];
118 6 size_t nl = 0, k;
119 6 unsigned v = suffix;
120 6 ▶ do { num[nl++] = (char)('0' + (v % 10)); v /= 10; } while (v);
Branch: 1 branch
BranchTrueFalseTotal
do { num[nl++] = (char)('0' + (v % 10)); v /= 10; } while (v); 0 6 6
121 6 slug[len++] = '-';
122 12 ▶ for (k = 0; k < nl; k++) slug[len + k] = num[nl - 1 - k];
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nl; k++) slug[len + k] = num[nl - 1 - k]; 6 6 12
123 6 len += nl;
124 6 slug[len] = '\0';
125 }
126 }
127  
128 808051 ▶ if (t->len == t->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (t->len == t->cap) { 80833 727218 808051
129 80833 ▶ size_t nc = t->cap ? t->cap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
size_t nc = t->cap ? t->cap * 2 : 16; 40400 40433 80833
130 80833 char** ns = (char**)realloc(t->slugs, nc * sizeof(char*));
131 80833 ▶ if (!ns) return len; /* OOM: skip recording */
Branch: 1 branch
BranchTrueFalseTotal
if (!ns) return len; /* OOM: skip recording */ 0 80833 80833
132 80833 t->slugs = ns;
133 80833 t->cap = nc;
134 }
135 {
136 808051 char* copy = (char*)malloc(len + 1);
137 808051 ▶ if (copy) {
Branch: 1 branch
BranchTrueFalseTotal
if (copy) { 808051 0 808051
138 808051 memcpy(copy, slug, len + 1);
139 808051 t->slugs[t->len++] = copy;
140 }
141 }
142 808051 return len;
143 }
144  
145 #endif /* MDS_SLUG_H */
« src/mds.c ↑ Summary src/simd/mds_simd_avx2.c »