| 1 |
|
| /* mds_inline.c — scalar CommonMark §6 inline tokenizer. |
| 2 |
|
| * |
| 3 |
|
| * Algorithm follows the cmark reference implementation and the |
| 4 |
|
| * CommonMark spec appendix ("An algorithm for parsing nested emphasis |
| 5 |
|
| * and links"). |
| 6 |
|
| * |
| 7 |
|
| * Single forward pass builds a doubly-linked list of nodes: |
| 8 |
|
| * TEXT, CODE, AUTOLINK, HTMLINLINE, SOFTBREAK, LINEBREAK |
| 9 |
|
| * DELIM (* / _ runs) |
| 10 |
|
| * OPEN_BRACKET ([), OPEN_BANG_BRACKET (![) |
| 11 |
|
| * |
| 12 |
|
| * Then process_emphasis() folds DELIM nodes into EMPH/STRONG using the |
| 13 |
|
| * delimiter-run stack algorithm. process_links_and_images() is folded |
| 14 |
|
| * inline during the forward pass at ']' time (cmark does it that way too). |
| 15 |
|
| * |
| 16 |
|
| * Finally emit() walks the linked list and dispatches SAX events. |
| 17 |
|
| */ |
| 18 |
|
| |
| 19 |
|
| #include "mds_inline.h" |
| 20 |
|
| #include "mds_ir.h" |
| 21 |
|
| #include "mds_linkref.h" |
| 22 |
|
| #include "mds_footnote.h" |
| 23 |
|
| #include "mds_entity.h" |
| 24 |
|
| #include "mds_arena.h" |
| 25 |
|
| #include "mds.h" |
| 26 |
|
| #if defined(__ARM_NEON) || defined(__aarch64__) |
| 27 |
|
| # include <arm_neon.h> |
| 28 |
|
| # define MDS_INLINE_HAVE_NEON 1 |
| 29 |
|
| #endif |
| 30 |
|
| |
| 31 |
|
| #include <string.h> |
| 32 |
|
| #include <stdlib.h> |
| 33 |
|
| #include <ctype.h> |
| 34 |
|
| #include <stdio.h> |
| 35 |
|
| |
| 36 |
|
| /* ---------------- byte class table ---------------- */ |
| 37 |
|
| |
| 38 |
|
| enum { |
| 39 |
|
| BC_PUNCT = 1 << 0, /* ASCII punctuation per CommonMark §2.1 */ |
| 40 |
|
| BC_WS = 1 << 1, /* ASCII whitespace: space tab \n \v \f \r */ |
| 41 |
|
| BC_ALNUM = 1 << 2 |
| 42 |
|
| }; |
| 43 |
|
| |
| 44 |
|
| static unsigned char g_byteclass[256]; |
| 45 |
|
| static int g_byteclass_inited = 0; |
| 46 |
|
| |
| 47 |
2445917 |
| static void byteclass_init(void) { |
| 48 |
|
| int c; |
| 49 |
2445917 |
▶ | if (g_byteclass_inited) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (g_byteclass_inited) return; |
2445891 |
26 |
2445917 |
|
| 50 |
6682 |
▶ | for (c = 0; c < 256; c++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < 256; c++) { |
6656 |
26 |
6682 |
|
| 51 |
6656 |
| unsigned f = 0; |
| 52 |
6656 |
▶ | if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || |
Branch: 5 branches
| Branch | True | False | Total |
| if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || |
6630 |
26 |
6656 |
| if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || |
6604 |
26 |
6630 |
| if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || |
6578 |
26 |
6604 |
| if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || |
6552 |
26 |
6578 |
| if (c == ' ' || c == '\t' || c == '\n' || c == '\v' || |
6526 |
26 |
6552 |
|
| 53 |
6526 |
▶ | c == '\f' || c == '\r') |
Branch: 1 branch
| Branch | True | False | Total |
| c == '\f' || c == '\r') |
26 |
6500 |
6526 |
|
| 54 |
156 |
| f |= BC_WS; |
| 55 |
6656 |
▶ | if ((c >= '0' && c <= '9') || |
Branch: 3 branches
| Branch | True | False | Total |
| if ((c >= '0' && c <= '9') || |
5408 |
1248 |
6656 |
| if ((c >= '0' && c <= '9') || |
5148 |
260 |
5408 |
| if ((c >= '0' && c <= '9') || |
4966 |
1430 |
6396 |
|
| 56 |
6396 |
▶ | (c >= 'A' && c <= 'Z') || |
Branch: 2 branches
| Branch | True | False | Total |
| (c >= 'A' && c <= 'Z') || |
4290 |
676 |
4966 |
| (c >= 'A' && c <= 'Z') || |
4134 |
1586 |
5720 |
|
| 57 |
4134 |
▶ | (c >= 'a' && c <= 'z')) |
Branch: 1 branch
| Branch | True | False | Total |
| (c >= 'a' && c <= 'z')) |
676 |
3458 |
4134 |
|
| 58 |
1612 |
| f |= BC_ALNUM; |
| 59 |
|
| /* CommonMark "ASCII punctuation": !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~ */ |
| 60 |
6656 |
▶ | if ((c >= 33 && c <= 47) || (c >= 58 && c <= 64) || |
Branch: 5 branches
| Branch | True | False | Total |
| if ((c >= 33 && c <= 47) || (c >= 58 && c <= 64) || |
5798 |
858 |
6656 |
| if ((c >= 33 && c <= 47) || (c >= 58 && c <= 64) || |
5408 |
390 |
5798 |
| if ((c >= 33 && c <= 47) || (c >= 58 && c <= 64) || |
5148 |
1118 |
6266 |
| if ((c >= 33 && c <= 47) || (c >= 58 && c <= 64) || |
4966 |
182 |
5148 |
| if ((c >= 33 && c <= 47) || (c >= 58 && c <= 64) || |
4290 |
1794 |
6084 |
|
| 61 |
6084 |
▶ | (c >= 91 && c <= 96) || (c >= 123 && c <= 126)) |
Branch: 3 branches
| Branch | True | False | Total |
| (c >= 91 && c <= 96) || (c >= 123 && c <= 126)) |
4134 |
156 |
4290 |
| (c >= 91 && c <= 96) || (c >= 123 && c <= 126)) |
3458 |
2470 |
5928 |
| (c >= 91 && c <= 96) || (c >= 123 && c <= 126)) |
104 |
3354 |
3458 |
|
| 62 |
832 |
| f |= BC_PUNCT; |
| 63 |
6656 |
| g_byteclass[c] = (unsigned char)f; |
| 64 |
|
| } |
| 65 |
26 |
| g_byteclass_inited = 1; |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
314 |
| static inline int is_ascii_punct(unsigned char c) { return g_byteclass[c] & BC_PUNCT; } |
| 69 |
848 |
| static inline int is_unicode_ws(unsigned char c) { return g_byteclass[c] & BC_WS; } |
| 70 |
|
| |
| 71 |
|
| /* Decode a single UTF-8 codepoint at s[i] (i < n). Returns codepoint; |
| 72 |
|
| * sets *adv to bytes consumed. Lenient on invalid bytes. */ |
| 73 |
54 |
| static unsigned mds_utf8_decode(const char* s, size_t n, size_t i, int* adv) { |
| 74 |
54 |
| unsigned char c = (unsigned char)s[i]; |
| 75 |
54 |
▶ | if (c < 0x80) { *adv = 1; return c; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0x80) { *adv = 1; return c; } |
0 |
54 |
54 |
|
| 76 |
54 |
▶ | if ((c & 0xE0) == 0xC0 && i + 1 < n && |
Branch: 2 branches
| Branch | True | False | Total |
| if ((c & 0xE0) == 0xC0 && i + 1 < n && |
48 |
6 |
54 |
| if ((c & 0xE0) == 0xC0 && i + 1 < n && |
48 |
0 |
48 |
|
| 77 |
48 |
▶ | ((unsigned char)s[i+1] & 0xC0) == 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+1] & 0xC0) == 0x80) { |
48 |
0 |
48 |
|
| 78 |
48 |
| *adv = 2; |
| 79 |
48 |
| return ((unsigned)(c & 0x1F) << 6) | ((unsigned char)s[i+1] & 0x3F); |
| 80 |
|
| } |
| 81 |
6 |
▶ | if ((c & 0xF0) == 0xE0 && i + 2 < n && |
Branch: 2 branches
| Branch | True | False | Total |
| if ((c & 0xF0) == 0xE0 && i + 2 < n && |
6 |
0 |
6 |
| if ((c & 0xF0) == 0xE0 && i + 2 < n && |
6 |
0 |
6 |
|
| 82 |
6 |
▶ | ((unsigned char)s[i+1] & 0xC0) == 0x80 && |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+1] & 0xC0) == 0x80 && |
6 |
0 |
6 |
|
| 83 |
6 |
▶ | ((unsigned char)s[i+2] & 0xC0) == 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+2] & 0xC0) == 0x80) { |
6 |
0 |
6 |
|
| 84 |
6 |
| *adv = 3; |
| 85 |
6 |
| return ((unsigned)(c & 0x0F) << 12) |
| 86 |
6 |
| | (((unsigned char)s[i+1] & 0x3F) << 6) |
| 87 |
6 |
| | ((unsigned char)s[i+2] & 0x3F); |
| 88 |
|
| } |
| 89 |
0 |
▶ | if ((c & 0xF8) == 0xF0 && i + 3 < n && |
Branch: 2 branches
| Branch | True | False | Total |
| if ((c & 0xF8) == 0xF0 && i + 3 < n && |
0 |
0 |
0 |
| if ((c & 0xF8) == 0xF0 && i + 3 < n && |
0 |
0 |
0 |
|
| 90 |
0 |
▶ | ((unsigned char)s[i+1] & 0xC0) == 0x80 && |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+1] & 0xC0) == 0x80 && |
0 |
0 |
0 |
|
| 91 |
0 |
▶ | ((unsigned char)s[i+2] & 0xC0) == 0x80 && |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+2] & 0xC0) == 0x80 && |
0 |
0 |
0 |
|
| 92 |
0 |
▶ | ((unsigned char)s[i+3] & 0xC0) == 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+3] & 0xC0) == 0x80) { |
0 |
0 |
0 |
|
| 93 |
0 |
| *adv = 4; |
| 94 |
0 |
| return ((unsigned)(c & 0x07) << 18) |
| 95 |
0 |
| | (((unsigned char)s[i+1] & 0x3F) << 12) |
| 96 |
0 |
| | (((unsigned char)s[i+2] & 0x3F) << 6) |
| 97 |
0 |
| | ((unsigned char)s[i+3] & 0x3F); |
| 98 |
|
| } |
| 99 |
0 |
| *adv = 1; |
| 100 |
0 |
| return c; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| /* Decode the codepoint ending just before s[pos]; pos > 0 required. |
| 104 |
|
| * Walks backward over continuation bytes (max 3). Returns codepoint; |
| 105 |
|
| * sets *cp_start to start offset. */ |
| 106 |
27 |
| static unsigned mds_utf8_decode_prev(const char* s, size_t n, size_t pos, |
| 107 |
|
| size_t* cp_start) { |
| 108 |
27 |
| size_t i = pos - 1; |
| 109 |
27 |
| int back = 0; |
| 110 |
|
| int adv; |
| 111 |
57 |
▶ | while (i > 0 && back < 3 && |
Branch: 2 branches
| Branch | True | False | Total |
| while (i > 0 && back < 3 && |
57 |
0 |
57 |
| while (i > 0 && back < 3 && |
57 |
0 |
57 |
|
| 112 |
57 |
▶ | ((unsigned char)s[i] & 0xC0) == 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i] & 0xC0) == 0x80) { |
30 |
27 |
57 |
|
| 113 |
30 |
| i--; back++; |
| 114 |
|
| } |
| 115 |
27 |
| *cp_start = i; |
| 116 |
27 |
| return mds_utf8_decode(s, n, i, &adv); |
| 117 |
|
| } |
| 118 |
|
| |
| 119 |
|
| /* Is codepoint Unicode whitespace per CommonMark spec (General_Category |
| 120 |
|
| * Zs, plus tab/CR/LF/FF). */ |
| 121 |
3052 |
| static int cp_is_ws(unsigned cp) { |
| 122 |
3052 |
▶ | if (cp == 0x09 || cp == 0x0A || cp == 0x0B || cp == 0x0C || cp == 0x0D || |
Branch: 6 branches
| Branch | True | False | Total |
| if (cp == 0x09 || cp == 0x0A || cp == 0x0B || cp == 0x0C || cp == 0x0D || |
3052 |
0 |
3052 |
| if (cp == 0x09 || cp == 0x0A || cp == 0x0B || cp == 0x0C || cp == 0x0D || |
2197 |
855 |
3052 |
| if (cp == 0x09 || cp == 0x0A || cp == 0x0B || cp == 0x0C || cp == 0x0D || |
2197 |
0 |
2197 |
| if (cp == 0x09 || cp == 0x0A || cp == 0x0B || cp == 0x0C || cp == 0x0D || |
2197 |
0 |
2197 |
| if (cp == 0x09 || cp == 0x0A || cp == 0x0B || cp == 0x0C || cp == 0x0D || |
2197 |
0 |
2197 |
| if (cp == 0x09 || cp == 0x0A || cp == 0x0B || cp == 0x0C || cp == 0x0D || |
403 |
1794 |
2197 |
|
| 123 |
1258 |
| cp == 0x20) return 1; |
| 124 |
1794 |
▶ | if (cp == 0xA0) return 1; /* NBSP */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp == 0xA0) return 1; /* NBSP */ |
6 |
1788 |
1794 |
|
| 125 |
1788 |
▶ | if (cp == 0x1680) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp == 0x1680) return 1; |
0 |
1788 |
1788 |
|
| 126 |
1788 |
▶ | if (cp >= 0x2000 && cp <= 0x200A) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0x2000 && cp <= 0x200A) return 1; |
6 |
1782 |
1788 |
| if (cp >= 0x2000 && cp <= 0x200A) return 1; |
0 |
6 |
6 |
|
| 127 |
1788 |
▶ | if (cp == 0x2028 || cp == 0x2029) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp == 0x2028 || cp == 0x2029) return 1; |
1788 |
0 |
1788 |
| if (cp == 0x2028 || cp == 0x2029) return 1; |
0 |
1788 |
1788 |
|
| 128 |
1788 |
▶ | if (cp == 0x202F || cp == 0x205F) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp == 0x202F || cp == 0x205F) return 1; |
1788 |
0 |
1788 |
| if (cp == 0x202F || cp == 0x205F) return 1; |
0 |
1788 |
1788 |
|
| 129 |
1788 |
▶ | if (cp == 0x3000) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp == 0x3000) return 1; |
0 |
1788 |
1788 |
|
| 130 |
1788 |
| return 0; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
|
| /* Is codepoint Unicode punctuation per CommonMark 0.31 spec |
| 134 |
|
| * (General_Category P* or S*). For non-ASCII we approximate with the |
| 135 |
|
| * ranges most likely to appear in spec examples: Latin-1 punctuation |
| 136 |
|
| * and symbols (¡¢£¤¥¦§¨©ª«¬®¯°±²³´¶·¸¹º»¼½¾¿×÷), General Punctuation |
| 137 |
|
| * (U+2000-206F), Currency Symbols (U+20A0-U+20CF), Letterlike (some), |
| 138 |
|
| * Arrows (U+2190-U+21FF), Mathematical (U+2200-U+22FF), Misc Tech |
| 139 |
|
| * (U+2300-U+23FF), Box Drawing/Block (U+2500-U+259F), Geometric |
| 140 |
|
| * (U+25A0-U+25FF), Misc Symbols (U+2600-U+26FF), Dingbats (U+2700-U+27BF), |
| 141 |
|
| * CJK Symbols (U+3000-U+303F), Halfwidth (U+FF00-U+FFEF symbols subset). */ |
| 142 |
3052 |
| static int cp_is_punct(unsigned cp) { |
| 143 |
3052 |
▶ | if (cp < 0x80) return g_byteclass[cp] & BC_PUNCT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp < 0x80) return g_byteclass[cp] & BC_PUNCT; |
2998 |
54 |
3052 |
|
| 144 |
|
| /* Latin-1 Supplement P/S categories */ |
| 145 |
54 |
▶ | if (cp >= 0xA1 && cp <= 0xBF) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0xA1 && cp <= 0xBF) return 1; |
48 |
6 |
54 |
| if (cp >= 0xA1 && cp <= 0xBF) return 1; |
6 |
42 |
48 |
|
| 146 |
48 |
▶ | if (cp == 0xD7 || cp == 0xF7) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp == 0xD7 || cp == 0xF7) return 1; |
48 |
0 |
48 |
| if (cp == 0xD7 || cp == 0xF7) return 1; |
0 |
48 |
48 |
|
| 147 |
|
| /* General Punctuation block */ |
| 148 |
48 |
▶ | if (cp >= 0x2000 && cp <= 0x206F) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0x2000 && cp <= 0x206F) return 1; |
6 |
42 |
48 |
| if (cp >= 0x2000 && cp <= 0x206F) return 1; |
0 |
6 |
6 |
|
| 149 |
|
| /* Superscripts/Subscripts (Sm subset) */ |
| 150 |
48 |
▶ | if (cp >= 0x2070 && cp <= 0x209F) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0x2070 && cp <= 0x209F) return 1; |
6 |
42 |
48 |
| if (cp >= 0x2070 && cp <= 0x209F) return 1; |
0 |
6 |
6 |
|
| 151 |
|
| /* Currency Symbols */ |
| 152 |
48 |
▶ | if (cp >= 0x20A0 && cp <= 0x20CF) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0x20A0 && cp <= 0x20CF) return 1; |
6 |
42 |
48 |
| if (cp >= 0x20A0 && cp <= 0x20CF) return 1; |
6 |
0 |
6 |
|
| 153 |
|
| /* Letterlike symbols (S subset) */ |
| 154 |
42 |
▶ | if (cp >= 0x2100 && cp <= 0x214F) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0x2100 && cp <= 0x214F) return 1; |
0 |
42 |
42 |
| if (cp >= 0x2100 && cp <= 0x214F) return 1; |
0 |
0 |
0 |
|
| 155 |
|
| /* Arrows / Math / Misc Tech / Box / Geometric / Misc / Dingbats */ |
| 156 |
42 |
▶ | if (cp >= 0x2190 && cp <= 0x27BF) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0x2190 && cp <= 0x27BF) return 1; |
0 |
42 |
42 |
| if (cp >= 0x2190 && cp <= 0x27BF) return 1; |
0 |
0 |
0 |
|
| 157 |
|
| /* CJK Symbols and Punctuation */ |
| 158 |
42 |
▶ | if (cp >= 0x3000 && cp <= 0x303F) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0x3000 && cp <= 0x303F) return 1; |
0 |
42 |
42 |
| if (cp >= 0x3000 && cp <= 0x303F) return 1; |
0 |
0 |
0 |
|
| 159 |
|
| /* Halfwidth / Fullwidth punctuation (subset) */ |
| 160 |
42 |
▶ | if (cp >= 0xFF00 && cp <= 0xFF0F) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0xFF00 && cp <= 0xFF0F) return 1; |
0 |
42 |
42 |
| if (cp >= 0xFF00 && cp <= 0xFF0F) return 1; |
0 |
0 |
0 |
|
| 161 |
42 |
▶ | if (cp >= 0xFF1A && cp <= 0xFF20) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0xFF1A && cp <= 0xFF20) return 1; |
0 |
42 |
42 |
| if (cp >= 0xFF1A && cp <= 0xFF20) return 1; |
0 |
0 |
0 |
|
| 162 |
42 |
▶ | if (cp >= 0xFF3B && cp <= 0xFF40) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0xFF3B && cp <= 0xFF40) return 1; |
0 |
42 |
42 |
| if (cp >= 0xFF3B && cp <= 0xFF40) return 1; |
0 |
0 |
0 |
|
| 163 |
42 |
▶ | if (cp >= 0xFF5B && cp <= 0xFF65) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 0xFF5B && cp <= 0xFF65) return 1; |
0 |
42 |
42 |
| if (cp >= 0xFF5B && cp <= 0xFF65) return 1; |
0 |
0 |
0 |
|
| 164 |
42 |
| return 0; |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
|
| /* ---------------- inline node ---------------- */ |
| 168 |
|
| |
| 169 |
|
| typedef enum { |
| 170 |
|
| N_TEXT, |
| 171 |
|
| N_CODE, |
| 172 |
|
| N_AUTOLINK, |
| 173 |
|
| N_HTMLINLINE, |
| 174 |
|
| N_SOFTBREAK, |
| 175 |
|
| N_LINEBREAK, |
| 176 |
|
| N_DELIM, /* * or _ run */ |
| 177 |
|
| N_OPEN_BRACKET, /* '[' */ |
| 178 |
|
| N_OPEN_BANG, /* '![' */ |
| 179 |
|
| N_EMPH, /* after process_emphasis */ |
| 180 |
|
| N_STRONG, |
| 181 |
|
| N_STRIKE, |
| 182 |
|
| N_LINK, |
| 183 |
|
| N_IMAGE, |
| 184 |
|
| N_FOOTNOTE_REF /* GFM §6.13 */ |
| 185 |
|
| } ntype; |
| 186 |
|
| |
| 187 |
|
| typedef struct inode { |
| 188 |
|
| ntype type; |
| 189 |
|
| struct inode* prev; |
| 190 |
|
| struct inode* next; |
| 191 |
|
| const char* s; |
| 192 |
|
| size_t n; |
| 193 |
|
| int is_email; |
| 194 |
|
| unsigned char delim_char; |
| 195 |
|
| int count; |
| 196 |
|
| int can_open; |
| 197 |
|
| int can_close; |
| 198 |
|
| int active; |
| 199 |
|
| int bracket_after_emph; |
| 200 |
|
| struct inode* children; |
| 201 |
|
| struct inode* children_tail; |
| 202 |
|
| const char* href; size_t hlen; |
| 203 |
|
| const char* title; size_t tlen; |
| 204 |
|
| } inode; |
| 205 |
|
| /* Bitfield packing of the flag ints was attempted but |
| 206 |
|
| * produced no measurable speedup (commonmark-spec / synth-prose within |
| 207 |
|
| * +/-2%, synth-tables drifted -5%) so the original layout is kept. The |
| 208 |
|
| * 32-byte aspirational target requires splitting href/title into a side |
| 209 |
|
| * allocation keyed off type == N_LINK|N_IMAGE; deferred until the inline |
| 210 |
|
| * parser is rewritten around tagged unions. */ |
| 211 |
|
| |
| 212 |
|
| /* ---------------- scanner state ---------------- */ |
| 213 |
|
| |
| 214 |
|
| typedef struct { |
| 215 |
|
| mds_ctx* ctx; |
| 216 |
|
| const char* s; |
| 217 |
|
| size_t n; |
| 218 |
|
| size_t pos; |
| 219 |
|
| inode* head; |
| 220 |
|
| inode* tail; |
| 221 |
|
| } scn; |
| 222 |
|
| |
| 223 |
11299042 |
| static inode* node_new(scn* z, ntype t) { |
| 224 |
11299042 |
| inode* x = (inode*)mds_arena_alloc(&z->ctx->arena, sizeof(inode)); |
| 225 |
11299042 |
| memset(x, 0, sizeof *x); |
| 226 |
11299042 |
| x->type = t; |
| 227 |
11299042 |
| return x; |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
11298433 |
| static void append(scn* z, inode* x) { |
| 231 |
11298433 |
| x->prev = z->tail; |
| 232 |
11298433 |
| x->next = NULL; |
| 233 |
11298433 |
▶ | if (z->tail) z->tail->next = x; |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->tail) z->tail->next = x; |
8044119 |
3254314 |
11298433 |
|
| 234 |
3254314 |
| else z->head = x; |
| 235 |
11298433 |
| z->tail = x; |
| 236 |
11298433 |
| } |
| 237 |
|
| |
| 238 |
0 |
| static void append_to(inode* parent, inode* x) { |
| 239 |
0 |
| x->prev = parent->children_tail; |
| 240 |
0 |
| x->next = NULL; |
| 241 |
0 |
▶ | if (parent->children_tail) parent->children_tail->next = x; |
Branch: 1 branch
| Branch | True | False | Total |
| if (parent->children_tail) parent->children_tail->next = x; |
0 |
0 |
0 |
|
| 242 |
0 |
| else parent->children = x; |
| 243 |
0 |
| parent->children_tail = x; |
| 244 |
0 |
| } |
| 245 |
|
| |
| 246 |
|
| /* Append literal text bytes; coalesces with previous TEXT node if possible |
| 247 |
|
| * (only if contiguous in source). */ |
| 248 |
6467334 |
| static void append_text(scn* z, const char* p, size_t k) { |
| 249 |
|
| inode* x; |
| 250 |
6467334 |
▶ | if (k == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (k == 0) return; |
0 |
6467334 |
6467334 |
|
| 251 |
6467334 |
▶ | if (z->tail && z->tail->type == N_TEXT && |
Branch: 2 branches
| Branch | True | False | Total |
| if (z->tail && z->tail->type == N_TEXT && |
4830566 |
1636768 |
6467334 |
| if (z->tail && z->tail->type == N_TEXT && |
736 |
4829830 |
4830566 |
|
| 252 |
736 |
▶ | z->tail->s + z->tail->n == p) { |
Branch: 1 branch
| Branch | True | False | Total |
| z->tail->s + z->tail->n == p) { |
579 |
157 |
736 |
|
| 253 |
579 |
| z->tail->n += k; |
| 254 |
579 |
| return; |
| 255 |
|
| } |
| 256 |
6466755 |
| x = node_new(z, N_TEXT); |
| 257 |
6466755 |
| x->s = p; x->n = k; |
| 258 |
6466755 |
| append(z, x); |
| 259 |
|
| } |
| 260 |
|
| |
| 261 |
|
| /* Allocate a fresh text node referring to arena-stored bytes (e.g. an |
| 262 |
|
| * entity expansion). */ |
| 263 |
333 |
| static void append_text_dup(scn* z, const char* p, size_t k) { |
| 264 |
|
| char* d; |
| 265 |
|
| inode* x; |
| 266 |
333 |
▶ | if (k == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (k == 0) return; |
0 |
333 |
333 |
|
| 267 |
333 |
| d = (char*)mds_arena_alloc(&z->ctx->arena, k); |
| 268 |
333 |
| memcpy(d, p, k); |
| 269 |
333 |
| x = node_new(z, N_TEXT); |
| 270 |
333 |
| x->s = d; x->n = k; |
| 271 |
333 |
| append(z, x); |
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
|
| /* ---------------- flanking rules (§6.4) ---------------- */ |
| 275 |
|
| /* |
| 276 |
|
| * preceded_by_ws / followed_by_ws : Unicode whitespace at run boundary |
| 277 |
|
| * preceded_by_punct / followed_by_punct : ASCII punct (Unicode punct is |
| 278 |
|
| * approximated as the high-bit set + any non-alnum byte; full UTF-8 |
| 279 |
|
| * Unicode-punct lookup is deferred). |
| 280 |
|
| * |
| 281 |
|
| * left-flanking iff: NOT followed by Unicode-WS AND |
| 282 |
|
| * (NOT followed by punct OR |
| 283 |
|
| * preceded by Unicode-WS or punct) |
| 284 |
|
| * |
| 285 |
|
| * right-flanking iff: NOT preceded by Unicode-WS AND |
| 286 |
|
| * (NOT preceded by punct OR |
| 287 |
|
| * followed by Unicode-WS or punct) |
| 288 |
|
| */ |
| 289 |
1526 |
| static int classify_run(const char* s, size_t n, size_t pos, size_t runlen, |
| 290 |
|
| int* can_open_out, int* can_close_out, |
| 291 |
|
| unsigned char ch) { |
| 292 |
|
| unsigned cp_before, cp_after; |
| 293 |
|
| size_t after_pos; |
| 294 |
|
| int before_ws, after_ws, before_punct, after_punct; |
| 295 |
|
| int left, right; |
| 296 |
|
| int can_open, can_close; |
| 297 |
|
| |
| 298 |
|
| /* Decode the codepoint immediately before pos and the one starting |
| 299 |
|
| * at pos+runlen. Treat document edges as line feeds (whitespace). |
| 300 |
|
| * Decoding multi-byte codepoints is essential for non-ASCII spec |
| 301 |
|
| * cases (NBSP as WS, currency / arrows / etc. as Unicode punct). */ |
| 302 |
1526 |
▶ | if (pos == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos == 0) { |
405 |
1121 |
1526 |
|
| 303 |
405 |
| cp_before = '\n'; |
| 304 |
|
| } else { |
| 305 |
1121 |
| unsigned char b = (unsigned char)s[pos - 1]; |
| 306 |
1121 |
▶ | if (b < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (b < 0x80) { |
1094 |
27 |
1121 |
|
| 307 |
1094 |
| cp_before = b; |
| 308 |
|
| } else { |
| 309 |
|
| size_t st; |
| 310 |
27 |
| cp_before = mds_utf8_decode_prev(s, n, pos, &st); |
| 311 |
|
| } |
| 312 |
|
| } |
| 313 |
1526 |
| after_pos = pos + runlen; |
| 314 |
1526 |
▶ | if (after_pos >= n) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (after_pos >= n) { |
416 |
1110 |
1526 |
|
| 315 |
416 |
| cp_after = '\n'; |
| 316 |
|
| } else { |
| 317 |
1110 |
| unsigned char a = (unsigned char)s[after_pos]; |
| 318 |
1110 |
▶ | if (a < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a < 0x80) { |
1083 |
27 |
1110 |
|
| 319 |
1083 |
| cp_after = a; |
| 320 |
|
| } else { |
| 321 |
|
| int adv; |
| 322 |
27 |
| cp_after = mds_utf8_decode(s, n, after_pos, &adv); |
| 323 |
|
| } |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
1526 |
| before_ws = cp_is_ws(cp_before); |
| 327 |
1526 |
| after_ws = cp_is_ws(cp_after); |
| 328 |
1526 |
| before_punct = cp_is_punct(cp_before); |
| 329 |
1526 |
| after_punct = cp_is_punct(cp_after); |
| 330 |
|
| |
| 331 |
1526 |
▶ | left = !after_ws && (!after_punct || before_ws || before_punct); |
Branch: 4 branches
| Branch | True | False | Total |
| left = !after_ws && (!after_punct || before_ws || before_punct); |
931 |
595 |
1526 |
| left = !after_ws && (!after_punct || before_ws || before_punct); |
263 |
668 |
931 |
| left = !after_ws && (!after_punct || before_ws || before_punct); |
152 |
111 |
263 |
| left = !after_ws && (!after_punct || before_ws || before_punct); |
54 |
98 |
152 |
|
| 332 |
1526 |
▶ | right = !before_ws && (!before_punct || after_ws || after_punct); |
Branch: 4 branches
| Branch | True | False | Total |
| right = !before_ws && (!before_punct || after_ws || after_punct); |
857 |
669 |
1526 |
| right = !before_ws && (!before_punct || after_ws || after_punct); |
243 |
614 |
857 |
| right = !before_ws && (!before_punct || after_ws || after_punct); |
147 |
96 |
243 |
| right = !before_ws && (!before_punct || after_ws || after_punct); |
54 |
93 |
147 |
|
| 333 |
|
| |
| 334 |
1526 |
▶ | if (ch == '_') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ch == '_') { |
535 |
991 |
1526 |
|
| 335 |
|
| /* §6.4: _ delimiters with intra-word restrictions */ |
| 336 |
535 |
▶ | can_open = left && (!right || before_punct); |
Branch: 3 branches
| Branch | True | False | Total |
| can_open = left && (!right || before_punct); |
297 |
238 |
535 |
| can_open = left && (!right || before_punct); |
86 |
211 |
297 |
| can_open = left && (!right || before_punct); |
30 |
56 |
86 |
|
| 337 |
535 |
▶ | can_close = right && (!left || after_punct); |
Branch: 3 branches
| Branch | True | False | Total |
| can_close = right && (!left || after_punct); |
282 |
253 |
535 |
| can_close = right && (!left || after_punct); |
86 |
196 |
282 |
| can_close = right && (!left || after_punct); |
30 |
56 |
86 |
|
| 338 |
|
| } else { |
| 339 |
|
| /* * (and ~ for strikethrough) */ |
| 340 |
991 |
| can_open = left; |
| 341 |
991 |
| can_close = right; |
| 342 |
|
| } |
| 343 |
1526 |
| *can_open_out = can_open; |
| 344 |
1526 |
| *can_close_out = can_close; |
| 345 |
1526 |
| return 1; |
| 346 |
|
| } |
| 347 |
|
| |
| 348 |
|
| /* ---------------- code span (§6.3) ---------------- */ |
| 349 |
|
| |
| 350 |
|
| /* Try to match a code span starting at pos (first byte = '`'). |
| 351 |
|
| * On success returns new pos past the closing fence; emits one node. |
| 352 |
|
| * On failure returns 0 (caller consumes one backtick as text). */ |
| 353 |
382 |
| static size_t try_code_span(scn* z, size_t pos) { |
| 354 |
382 |
| const char* s = z->s; |
| 355 |
382 |
| size_t n = z->n; |
| 356 |
382 |
| size_t open_start = pos; |
| 357 |
|
| size_t open_len; |
| 358 |
|
| size_t content_start; |
| 359 |
|
| size_t scan; |
| 360 |
856 |
▶ | while (pos < n && s[pos] == '`') pos++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (pos < n && s[pos] == '`') pos++; |
802 |
54 |
856 |
| while (pos < n && s[pos] == '`') pos++; |
474 |
328 |
802 |
|
| 361 |
382 |
| open_len = pos - open_start; |
| 362 |
382 |
| content_start = pos; |
| 363 |
382 |
| scan = pos; |
| 364 |
436 |
▶ | while (scan < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (scan < n) { |
370 |
66 |
436 |
|
| 365 |
|
| /* find next run of backticks */ |
| 366 |
370 |
| const char* p = (const char*)memchr(s + scan, '`', n - scan); |
| 367 |
|
| size_t bs; |
| 368 |
|
| size_t be; |
| 369 |
370 |
▶ | if (!p) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return 0; |
24 |
346 |
370 |
|
| 370 |
346 |
| bs = (size_t)(p - s); |
| 371 |
346 |
| be = bs; |
| 372 |
802 |
▶ | while (be < n && s[be] == '`') be++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (be < n && s[be] == '`') be++; |
598 |
204 |
802 |
| while (be < n && s[be] == '`') be++; |
456 |
142 |
598 |
|
| 373 |
346 |
▶ | if (be - bs == open_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (be - bs == open_len) { |
292 |
54 |
346 |
|
| 374 |
|
| /* matched */ |
| 375 |
292 |
| size_t cs = content_start; |
| 376 |
292 |
| size_t ce = bs; |
| 377 |
292 |
| int has_nonspace = 0; |
| 378 |
292 |
| int needs_replace = 0; |
| 379 |
|
| inode* x; |
| 380 |
|
| size_t i; |
| 381 |
|
| /* normalisation: if first and last are space, and content is |
| 382 |
|
| * not all spaces, strip one leading and trailing space. */ |
| 383 |
378 |
▶ | for (i = cs; i < ce; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = cs; i < ce; i++) { |
366 |
12 |
378 |
|
| 384 |
366 |
▶ | if (s[i] != ' ' && s[i] != '\n') { has_nonspace = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[i] != ' ' && s[i] != '\n') { has_nonspace = 1; break; } |
300 |
66 |
366 |
| if (s[i] != ' ' && s[i] != '\n') { has_nonspace = 1; break; } |
280 |
20 |
300 |
|
| 385 |
|
| } |
| 386 |
292 |
▶ | if (has_nonspace && ce - cs >= 2 && |
Branch: 2 branches
| Branch | True | False | Total |
| if (has_nonspace && ce - cs >= 2 && |
280 |
12 |
292 |
| if (has_nonspace && ce - cs >= 2 && |
236 |
44 |
280 |
|
| 387 |
236 |
▶ | (s[cs] == ' ' || s[cs] == '\n') && |
Branch: 2 branches
| Branch | True | False | Total |
| (s[cs] == ' ' || s[cs] == '\n') && |
194 |
42 |
236 |
| (s[cs] == ' ' || s[cs] == '\n') && |
20 |
174 |
194 |
|
| 388 |
62 |
▶ | (s[ce - 1] == ' ' || s[ce - 1] == '\n')) { |
Branch: 2 branches
| Branch | True | False | Total |
| (s[ce - 1] == ' ' || s[ce - 1] == '\n')) { |
26 |
36 |
62 |
| (s[ce - 1] == ' ' || s[ce - 1] == '\n')) { |
20 |
6 |
26 |
|
| 389 |
56 |
| cs++; ce--; |
| 390 |
|
| } |
| 391 |
|
| /* replace newlines with spaces */ |
| 392 |
1544 |
▶ | for (i = cs; i < ce; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = cs; i < ce; i++) { |
1276 |
268 |
1544 |
|
| 393 |
1276 |
▶ | if (s[i] == '\n') { needs_replace = 1; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[i] == '\n') { needs_replace = 1; break; } |
24 |
1252 |
1276 |
|
| 394 |
|
| } |
| 395 |
292 |
| x = node_new(z, N_CODE); |
| 396 |
292 |
▶ | if (needs_replace) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_replace) { |
24 |
268 |
292 |
|
| 397 |
24 |
| char* d = (char*)mds_arena_alloc(&z->ctx->arena, ce - cs); |
| 398 |
312 |
▶ | for (i = cs; i < ce; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = cs; i < ce; i++) |
288 |
24 |
312 |
|
| 399 |
288 |
▶ | d[i - cs] = (s[i] == '\n') ? ' ' : s[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| d[i - cs] = (s[i] == '\n') ? ' ' : s[i]; |
258 |
30 |
288 |
|
| 400 |
24 |
| x->s = d; x->n = ce - cs; |
| 401 |
|
| } else { |
| 402 |
268 |
| x->s = s + cs; x->n = ce - cs; |
| 403 |
|
| } |
| 404 |
292 |
| append(z, x); |
| 405 |
292 |
| return be; |
| 406 |
|
| } |
| 407 |
54 |
| scan = be; |
| 408 |
|
| } |
| 409 |
66 |
| return 0; |
| 410 |
|
| } |
| 411 |
|
| |
| 412 |
|
| /* ---------------- entity (§6.2) ---------------- */ |
| 413 |
|
| |
| 414 |
|
| /* Try to decode entity starting at pos (s[pos] == '&'). |
| 415 |
|
| * Returns chars consumed (including & and ;) on success, 0 otherwise. */ |
| 416 |
112 |
| static size_t try_entity(scn* z, size_t pos) { |
| 417 |
112 |
| const char* s = z->s; size_t n = z->n; |
| 418 |
|
| size_t q; |
| 419 |
|
| size_t name_start; |
| 420 |
|
| const mds_entity* e; |
| 421 |
|
| mds_entity ent_scratch; |
| 422 |
112 |
▶ | if (pos + 1 >= n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos + 1 >= n) return 0; |
0 |
112 |
112 |
|
| 423 |
112 |
| q = pos + 1; |
| 424 |
112 |
▶ | if (s[q] == '#') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[q] == '#') { |
53 |
59 |
112 |
|
| 425 |
53 |
| unsigned long cp = 0; |
| 426 |
53 |
| size_t digits = 0; |
| 427 |
|
| char buf[5]; |
| 428 |
|
| size_t blen; |
| 429 |
53 |
| q++; |
| 430 |
53 |
▶ | if (q < n && (s[q] == 'x' || s[q] == 'X')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (q < n && (s[q] == 'x' || s[q] == 'X')) { |
53 |
0 |
53 |
| if (q < n && (s[q] == 'x' || s[q] == 'X')) { |
45 |
8 |
53 |
| if (q < n && (s[q] == 'x' || s[q] == 'X')) { |
6 |
39 |
45 |
|
| 431 |
14 |
| q++; |
| 432 |
42 |
▶ | while (q < n && digits < 6 && isxdigit((unsigned char)s[q])) { |
Branch: 3 branches
| Branch | True | False | Total |
| while (q < n && digits < 6 && isxdigit((unsigned char)s[q])) { |
42 |
0 |
42 |
| while (q < n && digits < 6 && isxdigit((unsigned char)s[q])) { |
42 |
0 |
42 |
| while (q < n && digits < 6 && isxdigit((unsigned char)s[q])) { |
28 |
14 |
42 |
|
| 433 |
28 |
| char c = s[q]; |
| 434 |
40 |
▶ | cp = cp * 16 + (c <= '9' ? c - '0' : |
Branch: 2 branches
| Branch | True | False | Total |
| cp = cp * 16 + (c <= '9' ? c - '0' : |
16 |
12 |
28 |
| cp = cp * 16 + (c <= '9' ? c - '0' : |
3 |
9 |
12 |
|
| 435 |
12 |
| (c <= 'F' ? c - 'A' + 10 : c - 'a' + 10)); |
| 436 |
28 |
| q++; digits++; |
| 437 |
|
| } |
| 438 |
|
| } else { |
| 439 |
123 |
▶ | while (q < n && digits < 7 && s[q] >= '0' && s[q] <= '9') { |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && digits < 7 && s[q] >= '0' && s[q] <= '9') { |
123 |
0 |
123 |
| while (q < n && digits < 7 && s[q] >= '0' && s[q] <= '9') { |
120 |
3 |
123 |
| while (q < n && digits < 7 && s[q] >= '0' && s[q] <= '9') { |
120 |
0 |
120 |
| while (q < n && digits < 7 && s[q] >= '0' && s[q] <= '9') { |
84 |
36 |
120 |
|
| 440 |
84 |
| cp = cp * 10 + (unsigned long)(s[q] - '0'); |
| 441 |
84 |
| q++; digits++; |
| 442 |
|
| } |
| 443 |
|
| } |
| 444 |
53 |
▶ | if (!digits || q >= n || s[q] != ';') return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!digits || q >= n || s[q] != ';') return 0; |
44 |
9 |
53 |
| if (!digits || q >= n || s[q] != ';') return 0; |
44 |
0 |
44 |
| if (!digits || q >= n || s[q] != ';') return 0; |
3 |
41 |
44 |
|
| 445 |
41 |
| q++; |
| 446 |
|
| /* Encode codepoint as UTF-8. NUL → U+FFFD. */ |
| 447 |
41 |
▶ | if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) |
38 |
3 |
41 |
| if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) |
38 |
0 |
38 |
| if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) |
0 |
38 |
38 |
| if (cp == 0 || cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) |
0 |
0 |
0 |
|
| 448 |
3 |
| cp = 0xFFFD; |
| 449 |
41 |
▶ | if (cp < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp < 0x80) { |
26 |
15 |
41 |
|
| 450 |
26 |
| buf[0] = (char)cp; blen = 1; |
| 451 |
15 |
▶ | } else if (cp < 0x800) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cp < 0x800) { |
6 |
9 |
15 |
|
| 452 |
6 |
| buf[0] = (char)(0xC0 | (cp >> 6)); |
| 453 |
6 |
| buf[1] = (char)(0x80 | (cp & 0x3F)); blen = 2; |
| 454 |
9 |
▶ | } else if (cp < 0x10000) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cp < 0x10000) { |
9 |
0 |
9 |
|
| 455 |
9 |
| buf[0] = (char)(0xE0 | (cp >> 12)); |
| 456 |
9 |
| buf[1] = (char)(0x80 | ((cp >> 6) & 0x3F)); |
| 457 |
9 |
| buf[2] = (char)(0x80 | (cp & 0x3F)); blen = 3; |
| 458 |
|
| } else { |
| 459 |
0 |
| buf[0] = (char)(0xF0 | (cp >> 18)); |
| 460 |
0 |
| buf[1] = (char)(0x80 | ((cp >> 12) & 0x3F)); |
| 461 |
0 |
| buf[2] = (char)(0x80 | ((cp >> 6) & 0x3F)); |
| 462 |
0 |
| buf[3] = (char)(0x80 | (cp & 0x3F)); blen = 4; |
| 463 |
|
| } |
| 464 |
41 |
| append_text_dup(z, buf, blen); |
| 465 |
41 |
| return q - pos; |
| 466 |
|
| } |
| 467 |
|
| /* named entity */ |
| 468 |
59 |
| name_start = q; |
| 469 |
446 |
▶ | while (q < n && isalnum((unsigned char)s[q])) q++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (q < n && isalnum((unsigned char)s[q])) q++; |
443 |
3 |
446 |
| while (q < n && isalnum((unsigned char)s[q])) q++; |
387 |
56 |
443 |
|
| 470 |
59 |
▶ | if (q == name_start || q >= n || s[q] != ';') return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (q == name_start || q >= n || s[q] != ';') return 0; |
56 |
3 |
59 |
| if (q == name_start || q >= n || s[q] != ';') return 0; |
53 |
3 |
56 |
| if (q == name_start || q >= n || s[q] != ';') return 0; |
6 |
47 |
53 |
|
| 471 |
47 |
| e = mds_entity_lookup(s + name_start, q - name_start, &ent_scratch); |
| 472 |
47 |
▶ | if (!e) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) return 0; |
9 |
38 |
47 |
|
| 473 |
38 |
| q++; |
| 474 |
38 |
| append_text_dup(z, e->utf8, e->ulen); |
| 475 |
38 |
| return q - pos; |
| 476 |
|
| } |
| 477 |
|
| |
| 478 |
|
| /* ---------------- autolink (§6.7) ---------------- */ |
| 479 |
|
| |
| 480 |
|
| /* Returns total chars (including <>) on success, 0 otherwise. */ |
| 481 |
279 |
| static size_t try_autolink(scn* z, size_t pos) { |
| 482 |
279 |
| const char* s = z->s; size_t n = z->n; |
| 483 |
|
| size_t q; |
| 484 |
|
| size_t scheme_start; |
| 485 |
|
| size_t scheme_len; |
| 486 |
|
| size_t body_start; |
| 487 |
|
| size_t r; |
| 488 |
|
| size_t e_start; |
| 489 |
279 |
| int dot_ok = 0; |
| 490 |
279 |
| int label_len = 0; |
| 491 |
|
| inode* x; |
| 492 |
279 |
▶ | if (pos >= n || s[pos] != '<') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (pos >= n || s[pos] != '<') return 0; |
279 |
0 |
279 |
| if (pos >= n || s[pos] != '<') return 0; |
0 |
279 |
279 |
|
| 493 |
279 |
| q = pos + 1; |
| 494 |
|
| /* URI autolink: scheme = [A-Za-z][A-Za-z0-9+.-]{1,31}: */ |
| 495 |
279 |
| scheme_start = q; |
| 496 |
279 |
▶ | if (q >= n || !isalpha((unsigned char)s[q])) goto try_email; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q >= n || !isalpha((unsigned char)s[q])) goto try_email; |
279 |
0 |
279 |
| if (q >= n || !isalpha((unsigned char)s[q])) goto try_email; |
78 |
201 |
279 |
|
| 497 |
201 |
| q++; |
| 498 |
742 |
▶ | while (q < n && (isalnum((unsigned char)s[q]) || s[q] == '+' || |
Branch: 3 branches
| Branch | True | False | Total |
| while (q < n && (isalnum((unsigned char)s[q]) || s[q] == '+' || |
742 |
0 |
742 |
| while (q < n && (isalnum((unsigned char)s[q]) || s[q] == '+' || |
517 |
225 |
742 |
| while (q < n && (isalnum((unsigned char)s[q]) || s[q] == '+' || |
9 |
216 |
225 |
|
| 499 |
216 |
▶ | s[q] == '.' || s[q] == '-')) |
Branch: 2 branches
| Branch | True | False | Total |
| s[q] == '.' || s[q] == '-')) |
6 |
210 |
216 |
| s[q] == '.' || s[q] == '-')) |
9 |
201 |
210 |
|
| 500 |
541 |
| q++; |
| 501 |
201 |
| scheme_len = q - scheme_start; |
| 502 |
201 |
▶ | if (scheme_len < 2 || scheme_len > 32) goto try_email; |
Branch: 2 branches
| Branch | True | False | Total |
| if (scheme_len < 2 || scheme_len > 32) goto try_email; |
135 |
66 |
201 |
| if (scheme_len < 2 || scheme_len > 32) goto try_email; |
0 |
135 |
135 |
|
| 503 |
135 |
▶ | if (q >= n || s[q] != ':') goto try_email; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q >= n || s[q] != ':') goto try_email; |
135 |
0 |
135 |
| if (q >= n || s[q] != ':') goto try_email; |
82 |
53 |
135 |
|
| 504 |
|
| /* body: any non-WS, non-< non-> */ |
| 505 |
53 |
| body_start = q + 1; |
| 506 |
53 |
| r = body_start; |
| 507 |
1799 |
▶ | while (r < n && s[r] != '>' && s[r] != '<' && |
Branch: 1 branch
| Branch | True | False | Total |
| while (r < n && s[r] != '>' && s[r] != '<' && |
848 |
50 |
898 |
|
| 508 |
1746 |
▶ | !is_unicode_ws((unsigned char)s[r]) && |
Branch: 1 branch
| Branch | True | False | Total |
| !is_unicode_ws((unsigned char)s[r]) && |
898 |
0 |
898 |
|
| 509 |
845 |
▶ | (unsigned char)s[r] >= 0x20) |
Branch: 1 branch
| Branch | True | False | Total |
| (unsigned char)s[r] >= 0x20) |
845 |
0 |
845 |
|
| 510 |
845 |
| r++; |
| 511 |
53 |
▶ | if (r < n && s[r] == '>') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (r < n && s[r] == '>') { |
53 |
0 |
53 |
| if (r < n && s[r] == '>') { |
3 |
50 |
53 |
|
| 512 |
50 |
| x = node_new(z, N_AUTOLINK); |
| 513 |
50 |
| x->s = s + pos + 1; x->n = r - (pos + 1); |
| 514 |
50 |
| x->is_email = 0; |
| 515 |
50 |
| append(z, x); |
| 516 |
50 |
| return r - pos + 1; |
| 517 |
|
| } |
| 518 |
3 |
| try_email: |
| 519 |
|
| /* email autolink: simple validation */ |
| 520 |
229 |
| q = pos + 1; |
| 521 |
229 |
| e_start = q; |
| 522 |
953 |
▶ | while (q < n && (isalnum((unsigned char)s[q]) || |
Branch: 2 branches
| Branch | True | False | Total |
| while (q < n && (isalnum((unsigned char)s[q]) || |
953 |
0 |
953 |
| while (q < n && (isalnum((unsigned char)s[q]) || |
616 |
337 |
953 |
|
| 523 |
337 |
▶ | strchr(".!#$%&'*+/=?^_`{|}~-", s[q]))) |
Branch: 1 branch
| Branch | True | False | Total |
| strchr(".!#$%&'*+/=?^_`{|}~-", s[q]))) |
108 |
229 |
337 |
|
| 524 |
724 |
| q++; |
| 525 |
229 |
▶ | if (q == e_start || q >= n || s[q] != '@') return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (q == e_start || q >= n || s[q] != '@') return 0; |
214 |
15 |
229 |
| if (q == e_start || q >= n || s[q] != '@') return 0; |
214 |
0 |
214 |
| if (q == e_start || q >= n || s[q] != '@') return 0; |
207 |
7 |
214 |
|
| 526 |
7 |
| q++; |
| 527 |
111 |
▶ | while (q < n && s[q] != '>') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (q < n && s[q] != '>') { |
111 |
0 |
111 |
| while (q < n && s[q] != '>') { |
104 |
7 |
111 |
|
| 528 |
104 |
| char c = s[q]; |
| 529 |
104 |
▶ | if (isalnum((unsigned char)c)) { label_len++; q++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (isalnum((unsigned char)c)) { label_len++; q++; } |
88 |
16 |
104 |
|
| 530 |
16 |
▶ | else if (c == '-') { if (!label_len) return 0; label_len++; q++; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c == '-') { if (!label_len) return 0; label_len++; q++; } |
3 |
13 |
16 |
| else if (c == '-') { if (!label_len) return 0; label_len++; q++; } |
0 |
3 |
3 |
|
| 531 |
13 |
▶ | else if (c == '.') { if (!label_len) return 0; dot_ok = 1; label_len = 0; q++; } |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c == '.') { if (!label_len) return 0; dot_ok = 1; label_len = 0; q++; } |
13 |
0 |
13 |
| else if (c == '.') { if (!label_len) return 0; dot_ok = 1; label_len = 0; q++; } |
0 |
13 |
13 |
|
| 532 |
0 |
| else return 0; |
| 533 |
104 |
▶ | if (label_len > 63) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (label_len > 63) return 0; |
0 |
104 |
104 |
|
| 534 |
|
| } |
| 535 |
|
| (void)dot_ok; |
| 536 |
7 |
▶ | if (q >= n || s[q] != '>' || label_len == 0) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (q >= n || s[q] != '>' || label_len == 0) return 0; |
7 |
0 |
7 |
| if (q >= n || s[q] != '>' || label_len == 0) return 0; |
7 |
0 |
7 |
| if (q >= n || s[q] != '>' || label_len == 0) return 0; |
0 |
7 |
7 |
|
| 537 |
7 |
| x = node_new(z, N_AUTOLINK); |
| 538 |
7 |
| x->s = s + pos + 1; x->n = q - (pos + 1); |
| 539 |
7 |
| x->is_email = 1; |
| 540 |
7 |
| append(z, x); |
| 541 |
7 |
| return q - pos + 1; |
| 542 |
|
| } |
| 543 |
|
| |
| 544 |
|
| /* ---------------- raw HTML inline (§6.8) ---------------- */ |
| 545 |
|
| |
| 546 |
402 |
| static int html_attr_name_char(char c, int first) { |
| 547 |
402 |
▶ | if (first) return isalpha((unsigned char)c) || c == '_' || c == ':'; |
Branch: 4 branches
| Branch | True | False | Total |
| if (first) return isalpha((unsigned char)c) || c == '_' || c == ':'; |
81 |
321 |
402 |
| if (first) return isalpha((unsigned char)c) || c == '_' || c == ':'; |
3 |
78 |
81 |
| if (first) return isalpha((unsigned char)c) || c == '_' || c == ':'; |
0 |
3 |
3 |
| if (first) return isalpha((unsigned char)c) || c == '_' || c == ':'; |
0 |
0 |
0 |
|
| 548 |
321 |
▶ | return isalnum((unsigned char)c) || c == '_' || c == ':' || c == '.' || c == '-'; |
Branch: 5 branches
| Branch | True | False | Total |
| return isalnum((unsigned char)c) || c == '_' || c == ':' || c == '.' || c == '-'; |
84 |
237 |
321 |
| return isalnum((unsigned char)c) || c == '_' || c == ':' || c == '.' || c == '-'; |
84 |
0 |
84 |
| return isalnum((unsigned char)c) || c == '_' || c == ':' || c == '.' || c == '-'; |
81 |
3 |
84 |
| return isalnum((unsigned char)c) || c == '_' || c == ':' || c == '.' || c == '-'; |
81 |
0 |
81 |
| return isalnum((unsigned char)c) || c == '_' || c == ':' || c == '.' || c == '-'; |
0 |
81 |
81 |
|
| 549 |
|
| } |
| 550 |
|
| |
| 551 |
220 |
| static size_t try_html_inline(scn* z, size_t pos) { |
| 552 |
220 |
| const char* s = z->s; size_t n = z->n; |
| 553 |
|
| size_t q; |
| 554 |
|
| inode* x; |
| 555 |
|
| int closing; |
| 556 |
220 |
▶ | if (pos >= n || s[pos] != '<') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (pos >= n || s[pos] != '<') return 0; |
220 |
0 |
220 |
| if (pos >= n || s[pos] != '<') return 0; |
0 |
220 |
220 |
|
| 557 |
220 |
▶ | if (pos + 1 >= n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pos + 1 >= n) return 0; |
0 |
220 |
220 |
|
| 558 |
220 |
| q = pos + 1; |
| 559 |
|
| /* comment (CommonMark 0.30+): |
| 560 |
|
| * <!--> | <!---> | <!-- ...not containing -->... --> */ |
| 561 |
220 |
▶ | if (q + 2 < n && s[q] == '!' && s[q+1] == '-' && s[q+2] == '-') { |
Branch: 4 branches
| Branch | True | False | Total |
| if (q + 2 < n && s[q] == '!' && s[q+1] == '-' && s[q+2] == '-') { |
217 |
3 |
220 |
| if (q + 2 < n && s[q] == '!' && s[q+1] == '-' && s[q+2] == '-') { |
21 |
196 |
217 |
| if (q + 2 < n && s[q] == '!' && s[q+1] == '-' && s[q+2] == '-') { |
12 |
9 |
21 |
| if (q + 2 < n && s[q] == '!' && s[q+1] == '-' && s[q+2] == '-') { |
9 |
3 |
12 |
|
| 562 |
9 |
| size_t r = q + 3; |
| 563 |
|
| /* short forms */ |
| 564 |
9 |
▶ | if (r < n && s[r] == '>') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (r < n && s[r] == '>') { |
9 |
0 |
9 |
| if (r < n && s[r] == '>') { |
3 |
6 |
9 |
|
| 565 |
3 |
| r += 1; |
| 566 |
3 |
| x = node_new(z, N_HTMLINLINE); |
| 567 |
3 |
| x->s = s + pos; x->n = r - pos; |
| 568 |
3 |
| append(z, x); |
| 569 |
3 |
| return r - pos; |
| 570 |
|
| } |
| 571 |
6 |
▶ | if (r + 1 < n && s[r] == '-' && s[r+1] == '>') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (r + 1 < n && s[r] == '-' && s[r+1] == '>') { |
6 |
0 |
6 |
| if (r + 1 < n && s[r] == '-' && s[r+1] == '>') { |
3 |
3 |
6 |
| if (r + 1 < n && s[r] == '-' && s[r+1] == '>') { |
3 |
0 |
3 |
|
| 572 |
3 |
| r += 2; |
| 573 |
3 |
| x = node_new(z, N_HTMLINLINE); |
| 574 |
3 |
| x->s = s + pos; x->n = r - pos; |
| 575 |
3 |
| append(z, x); |
| 576 |
3 |
| return r - pos; |
| 577 |
|
| } |
| 578 |
|
| /* general form: scan for "-->" with no constraint on inner '--'. */ |
| 579 |
114 |
▶ | while (r + 2 < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (r + 2 < n) { |
114 |
0 |
114 |
|
| 580 |
114 |
▶ | if (s[r] == '-' && s[r+1] == '-' && s[r+2] == '>') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (s[r] == '-' && s[r+1] == '-' && s[r+2] == '>') { |
12 |
102 |
114 |
| if (s[r] == '-' && s[r+1] == '-' && s[r+2] == '>') { |
6 |
6 |
12 |
| if (s[r] == '-' && s[r+1] == '-' && s[r+2] == '>') { |
3 |
3 |
6 |
|
| 581 |
3 |
| r += 3; |
| 582 |
3 |
| x = node_new(z, N_HTMLINLINE); |
| 583 |
3 |
| x->s = s + pos; x->n = r - pos; |
| 584 |
3 |
| append(z, x); |
| 585 |
3 |
| return r - pos; |
| 586 |
|
| } |
| 587 |
111 |
| r++; |
| 588 |
|
| } |
| 589 |
0 |
| return 0; |
| 590 |
|
| } |
| 591 |
|
| /* PI */ |
| 592 |
211 |
▶ | if (q < n && s[q] == '?') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < n && s[q] == '?') { |
211 |
0 |
211 |
| if (q < n && s[q] == '?') { |
3 |
208 |
211 |
|
| 593 |
3 |
| q++; |
| 594 |
42 |
▶ | while (q + 1 < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (q + 1 < n) { |
42 |
0 |
42 |
|
| 595 |
42 |
▶ | if (s[q] == '?' && s[q+1] == '>') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[q] == '?' && s[q+1] == '>') { |
3 |
39 |
42 |
| if (s[q] == '?' && s[q+1] == '>') { |
3 |
0 |
3 |
|
| 596 |
3 |
| q += 2; |
| 597 |
3 |
| x = node_new(z, N_HTMLINLINE); |
| 598 |
3 |
| x->s = s + pos; x->n = q - pos; |
| 599 |
3 |
| append(z, x); |
| 600 |
3 |
| return q - pos; |
| 601 |
|
| } |
| 602 |
39 |
| q++; |
| 603 |
|
| } |
| 604 |
0 |
| return 0; |
| 605 |
|
| } |
| 606 |
|
| /* CDATA */ |
| 607 |
208 |
▶ | if (q + 7 < n && memcmp(s + q, "![CDATA[", 8) == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q + 7 < n && memcmp(s + q, "![CDATA[", 8) == 0) { |
175 |
33 |
208 |
| if (q + 7 < n && memcmp(s + q, "![CDATA[", 8) == 0) { |
3 |
172 |
175 |
|
| 608 |
3 |
| q += 8; |
| 609 |
12 |
▶ | while (q + 2 < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (q + 2 < n) { |
12 |
0 |
12 |
|
| 610 |
12 |
▶ | if (s[q] == ']' && s[q+1] == ']' && s[q+2] == '>') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (s[q] == ']' && s[q+1] == ']' && s[q+2] == '>') { |
3 |
9 |
12 |
| if (s[q] == ']' && s[q+1] == ']' && s[q+2] == '>') { |
3 |
0 |
3 |
| if (s[q] == ']' && s[q+1] == ']' && s[q+2] == '>') { |
3 |
0 |
3 |
|
| 611 |
3 |
| q += 3; |
| 612 |
3 |
| x = node_new(z, N_HTMLINLINE); |
| 613 |
3 |
| x->s = s + pos; x->n = q - pos; |
| 614 |
3 |
| append(z, x); |
| 615 |
3 |
| return q - pos; |
| 616 |
|
| } |
| 617 |
9 |
| q++; |
| 618 |
|
| } |
| 619 |
0 |
| return 0; |
| 620 |
|
| } |
| 621 |
|
| /* declaration <!A...> */ |
| 622 |
205 |
▶ | if (q < n && s[q] == '!' && q + 1 < n && isalpha((unsigned char)s[q+1])) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (q < n && s[q] == '!' && q + 1 < n && isalpha((unsigned char)s[q+1])) { |
205 |
0 |
205 |
| if (q < n && s[q] == '!' && q + 1 < n && isalpha((unsigned char)s[q+1])) { |
9 |
196 |
205 |
| if (q < n && s[q] == '!' && q + 1 < n && isalpha((unsigned char)s[q+1])) { |
9 |
0 |
9 |
| if (q < n && s[q] == '!' && q + 1 < n && isalpha((unsigned char)s[q+1])) { |
3 |
6 |
9 |
|
| 623 |
3 |
| q += 2; |
| 624 |
48 |
▶ | while (q < n && s[q] != '>') q++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (q < n && s[q] != '>') q++; |
48 |
0 |
48 |
| while (q < n && s[q] != '>') q++; |
45 |
3 |
48 |
|
| 625 |
3 |
▶ | if (q >= n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (q >= n) return 0; |
0 |
3 |
3 |
|
| 626 |
3 |
| q++; |
| 627 |
3 |
| x = node_new(z, N_HTMLINLINE); |
| 628 |
3 |
| x->s = s + pos; x->n = q - pos; |
| 629 |
3 |
| append(z, x); |
| 630 |
3 |
| return q - pos; |
| 631 |
|
| } |
| 632 |
|
| /* closing tag */ |
| 633 |
202 |
| closing = 0; |
| 634 |
202 |
▶ | if (q < n && s[q] == '/') { closing = 1; q++; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < n && s[q] == '/') { closing = 1; q++; } |
202 |
0 |
202 |
| if (q < n && s[q] == '/') { closing = 1; q++; } |
32 |
170 |
202 |
|
| 635 |
|
| /* tag name */ |
| 636 |
202 |
▶ | if (q >= n || !isalpha((unsigned char)s[q])) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q >= n || !isalpha((unsigned char)s[q])) return 0; |
202 |
0 |
202 |
| if (q >= n || !isalpha((unsigned char)s[q])) return 0; |
27 |
175 |
202 |
|
| 637 |
175 |
| q++; |
| 638 |
516 |
▶ | while (q < n && (isalnum((unsigned char)s[q]) || s[q] == '-')) q++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (q < n && (isalnum((unsigned char)s[q]) || s[q] == '-')) q++; |
516 |
0 |
516 |
| while (q < n && (isalnum((unsigned char)s[q]) || s[q] == '-')) q++; |
338 |
178 |
516 |
| while (q < n && (isalnum((unsigned char)s[q]) || s[q] == '-')) q++; |
3 |
175 |
178 |
|
| 639 |
175 |
▶ | if (closing) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (closing) { |
32 |
143 |
175 |
|
| 640 |
38 |
▶ | while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
38 |
0 |
38 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
6 |
32 |
38 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
32 |
32 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
32 |
32 |
|
| 641 |
32 |
▶ | if (q >= n || s[q] != '>') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q >= n || s[q] != '>') return 0; |
32 |
0 |
32 |
| if (q >= n || s[q] != '>') return 0; |
3 |
29 |
32 |
|
| 642 |
29 |
| q++; |
| 643 |
29 |
| x = node_new(z, N_HTMLINLINE); |
| 644 |
29 |
| x->s = s + pos; x->n = q - pos; |
| 645 |
29 |
| append(z, x); |
| 646 |
29 |
| return q - pos; |
| 647 |
|
| } |
| 648 |
|
| /* attributes */ |
| 649 |
218 |
▶ | while (q < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (q < n) { |
218 |
0 |
218 |
|
| 650 |
218 |
| size_t pre_attr = q; |
| 651 |
218 |
| int saw_ws = 0; |
| 652 |
|
| size_t save; |
| 653 |
314 |
▶ | while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) { |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) { |
314 |
0 |
314 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) { |
84 |
230 |
314 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) { |
0 |
230 |
230 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) { |
12 |
218 |
230 |
|
| 654 |
96 |
| saw_ws = 1; q++; |
| 655 |
|
| } |
| 656 |
218 |
▶ | if (q >= n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (q >= n) return 0; |
0 |
218 |
218 |
|
| 657 |
218 |
▶ | if (s[q] == '>' || s[q] == '/') break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[q] == '>' || s[q] == '/') break; |
138 |
80 |
218 |
| if (s[q] == '>' || s[q] == '/') break; |
117 |
21 |
138 |
|
| 658 |
117 |
▶ | if (!saw_ws) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!saw_ws) return 0; |
36 |
81 |
117 |
|
| 659 |
81 |
▶ | if (!html_attr_name_char(s[q], 1)) { q = pre_attr; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!html_attr_name_char(s[q], 1)) { q = pre_attr; break; } |
0 |
81 |
81 |
|
| 660 |
81 |
| q++; |
| 661 |
321 |
▶ | while (q < n && html_attr_name_char(s[q], 0)) q++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (q < n && html_attr_name_char(s[q], 0)) q++; |
321 |
0 |
321 |
| while (q < n && html_attr_name_char(s[q], 0)) q++; |
240 |
81 |
321 |
|
| 662 |
|
| /* optional value */ |
| 663 |
81 |
| save = q; |
| 664 |
87 |
▶ | while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
87 |
0 |
87 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
6 |
81 |
87 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
81 |
81 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
81 |
81 |
|
| 665 |
81 |
▶ | if (q < n && s[q] == '=') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < n && s[q] == '=') { |
81 |
0 |
81 |
| if (q < n && s[q] == '=') { |
69 |
12 |
81 |
|
| 666 |
69 |
| q++; |
| 667 |
72 |
▶ | while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
72 |
0 |
72 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
3 |
69 |
72 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
69 |
69 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
69 |
69 |
|
| 668 |
69 |
▶ | if (q < n && (s[q] == '"' || s[q] == '\'')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (q < n && (s[q] == '"' || s[q] == '\'')) { |
69 |
0 |
69 |
| if (q < n && (s[q] == '"' || s[q] == '\'')) { |
15 |
54 |
69 |
| if (q < n && (s[q] == '"' || s[q] == '\'')) { |
6 |
9 |
15 |
|
| 669 |
60 |
| char qc = s[q]; q++; |
| 670 |
366 |
▶ | while (q < n && s[q] != qc) q++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (q < n && s[q] != qc) q++; |
360 |
6 |
366 |
| while (q < n && s[q] != qc) q++; |
306 |
54 |
360 |
|
| 671 |
60 |
▶ | if (q >= n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (q >= n) return 0; |
6 |
54 |
60 |
|
| 672 |
54 |
| q++; |
| 673 |
|
| } else { |
| 674 |
|
| /* unquoted */ |
| 675 |
9 |
| size_t vs = q; |
| 676 |
54 |
▶ | while (q < n && s[q] != ' ' && s[q] != '\t' && s[q] != '\n' && |
Branch: 3 branches
| Branch | True | False | Total |
| while (q < n && s[q] != ' ' && s[q] != '\t' && s[q] != '\n' && |
42 |
3 |
45 |
| while (q < n && s[q] != ' ' && s[q] != '\t' && s[q] != '\n' && |
42 |
0 |
42 |
| while (q < n && s[q] != ' ' && s[q] != '\t' && s[q] != '\n' && |
39 |
3 |
42 |
|
| 677 |
39 |
▶ | s[q] != '"' && s[q] != '\'' && s[q] != '=' && |
Branch: 3 branches
| Branch | True | False | Total |
| s[q] != '"' && s[q] != '\'' && s[q] != '=' && |
39 |
0 |
39 |
| s[q] != '"' && s[q] != '\'' && s[q] != '=' && |
36 |
3 |
39 |
| s[q] != '"' && s[q] != '\'' && s[q] != '=' && |
36 |
0 |
36 |
|
| 678 |
45 |
▶ | s[q] != '<' && s[q] != '>' && s[q] != '`') q++; |
Branch: 4 branches
| Branch | True | False | Total |
| s[q] != '<' && s[q] != '>' && s[q] != '`') q++; |
45 |
0 |
45 |
| s[q] != '<' && s[q] != '>' && s[q] != '`') q++; |
36 |
0 |
36 |
| s[q] != '<' && s[q] != '>' && s[q] != '`') q++; |
36 |
0 |
36 |
| s[q] != '<' && s[q] != '>' && s[q] != '`') q++; |
36 |
0 |
36 |
|
| 679 |
9 |
▶ | if (q == vs) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (q == vs) return 0; |
0 |
9 |
9 |
|
| 680 |
|
| } |
| 681 |
|
| } else { |
| 682 |
12 |
| q = save; |
| 683 |
|
| } |
| 684 |
|
| } |
| 685 |
101 |
▶ | while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
101 |
0 |
101 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
101 |
101 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
101 |
101 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
101 |
101 |
|
| 686 |
101 |
▶ | if (q < n && s[q] == '/') q++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < n && s[q] == '/') q++; |
101 |
0 |
101 |
| if (q < n && s[q] == '/') q++; |
21 |
80 |
101 |
|
| 687 |
101 |
▶ | if (q >= n || s[q] != '>') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (q >= n || s[q] != '>') return 0; |
101 |
0 |
101 |
| if (q >= n || s[q] != '>') return 0; |
3 |
98 |
101 |
|
| 688 |
98 |
| q++; |
| 689 |
98 |
| x = node_new(z, N_HTMLINLINE); |
| 690 |
98 |
| x->s = s + pos; x->n = q - pos; |
| 691 |
98 |
| append(z, x); |
| 692 |
98 |
| return q - pos; |
| 693 |
|
| } |
| 694 |
|
| |
| 695 |
|
| /* ---------------- link parsing helpers ---------------- */ |
| 696 |
|
| |
| 697 |
|
| /* Parse a CommonMark link destination starting at *p. On success advances |
| 698 |
|
| * *p past it and sets [out_s, out_e) to the destination bytes (still in the |
| 699 |
|
| * source buffer; the renderer normalises). */ |
| 700 |
808222 |
| static int parse_link_destination(const char* s, size_t* pp, size_t n, |
| 701 |
|
| const char** out_s, size_t* out_n) { |
| 702 |
808222 |
| size_t p = *pp; |
| 703 |
|
| size_t ds; |
| 704 |
|
| int paren; |
| 705 |
808222 |
▶ | if (p >= n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p >= n) return 0; |
0 |
808222 |
808222 |
|
| 706 |
808222 |
▶ | if (s[p] == '<') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[p] == '<') { |
30 |
808192 |
808222 |
|
| 707 |
30 |
| p++; |
| 708 |
30 |
| ds = p; |
| 709 |
150 |
▶ | while (p < n && s[p] != '>' && s[p] != '<' && s[p] != '\n') { |
Branch: 4 branches
| Branch | True | False | Total |
| while (p < n && s[p] != '>' && s[p] != '<' && s[p] != '\n') { |
147 |
3 |
150 |
| while (p < n && s[p] != '>' && s[p] != '<' && s[p] != '\n') { |
126 |
21 |
147 |
| while (p < n && s[p] != '>' && s[p] != '<' && s[p] != '\n') { |
126 |
0 |
126 |
| while (p < n && s[p] != '>' && s[p] != '<' && s[p] != '\n') { |
120 |
6 |
126 |
|
| 710 |
120 |
▶ | if (s[p] == '\\' && p + 1 < n) p++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[p] == '\\' && p + 1 < n) p++; |
3 |
117 |
120 |
| if (s[p] == '\\' && p + 1 < n) p++; |
3 |
0 |
3 |
|
| 711 |
120 |
| p++; |
| 712 |
|
| } |
| 713 |
30 |
▶ | if (p >= n || s[p] != '>') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p >= n || s[p] != '>') return 0; |
27 |
3 |
30 |
| if (p >= n || s[p] != '>') return 0; |
6 |
21 |
27 |
|
| 714 |
21 |
| *out_s = s + ds; |
| 715 |
21 |
| *out_n = p - ds; |
| 716 |
21 |
| *pp = p + 1; |
| 717 |
21 |
| return 1; |
| 718 |
|
| } |
| 719 |
808192 |
| paren = 0; |
| 720 |
808192 |
| ds = p; |
| 721 |
23433579 |
▶ | while (p < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < n) { |
23433576 |
3 |
23433579 |
|
| 722 |
23433576 |
| unsigned char c = (unsigned char)s[p]; |
| 723 |
23433576 |
▶ | if (c < 0x20 || c == 0x7f) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c < 0x20 || c == 0x7f) break; |
23433570 |
6 |
23433576 |
| if (c < 0x20 || c == 0x7f) break; |
23433570 |
0 |
23433570 |
|
| 724 |
23433570 |
▶ | if (c == ' ' || c == '\t' || c == '\n') break; |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == ' ' || c == '\t' || c == '\n') break; |
23433523 |
47 |
23433570 |
| if (c == ' ' || c == '\t' || c == '\n') break; |
23433523 |
0 |
23433523 |
| if (c == ' ' || c == '\t' || c == '\n') break; |
23433523 |
0 |
23433523 |
|
| 725 |
23433523 |
▶ | if (c == '\\' && p + 1 < n && is_ascii_punct((unsigned char)s[p+1])) { p += 2; continue; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == '\\' && p + 1 < n && is_ascii_punct((unsigned char)s[p+1])) { p += 2; continue; } |
27 |
23433496 |
23433523 |
| if (c == '\\' && p + 1 < n && is_ascii_punct((unsigned char)s[p+1])) { p += 2; continue; } |
27 |
0 |
27 |
| if (c == '\\' && p + 1 < n && is_ascii_punct((unsigned char)s[p+1])) { p += 2; continue; } |
24 |
3 |
27 |
|
| 726 |
23433499 |
▶ | if (c == '(') { paren++; p++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '(') { paren++; p++; continue; } |
15 |
23433484 |
23433499 |
|
| 727 |
23433484 |
▶ | if (c == ')') { if (paren == 0) break; paren--; p++; continue; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == ')') { if (paren == 0) break; paren--; p++; continue; } |
808151 |
22625333 |
23433484 |
| if (c == ')') { if (paren == 0) break; paren--; p++; continue; } |
808136 |
15 |
808151 |
|
| 728 |
22625333 |
| p++; |
| 729 |
|
| } |
| 730 |
808192 |
▶ | if (p == ds || paren != 0) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p == ds || paren != 0) return 0; |
808192 |
0 |
808192 |
| if (p == ds || paren != 0) return 0; |
0 |
808192 |
808192 |
|
| 731 |
808192 |
| *out_s = s + ds; |
| 732 |
808192 |
| *out_n = p - ds; |
| 733 |
808192 |
| *pp = p; |
| 734 |
808192 |
| return 1; |
| 735 |
|
| } |
| 736 |
|
| |
| 737 |
41 |
| static int parse_link_title(const char* s, size_t* pp, size_t n, |
| 738 |
|
| const char** out_s, size_t* out_n) { |
| 739 |
41 |
| size_t p = *pp; |
| 740 |
|
| char open, close; |
| 741 |
|
| size_t ts; |
| 742 |
|
| int prev_blank_line; |
| 743 |
41 |
▶ | if (p >= n) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p >= n) return 0; |
0 |
41 |
41 |
|
| 744 |
41 |
| open = s[p]; |
| 745 |
41 |
▶ | if (open == '"' || open == '\'') close = open; |
Branch: 2 branches
| Branch | True | False | Total |
| if (open == '"' || open == '\'') close = open; |
12 |
29 |
41 |
| if (open == '"' || open == '\'') close = open; |
9 |
3 |
12 |
|
| 746 |
3 |
▶ | else if (open == '(') close = ')'; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (open == '(') close = ')'; |
3 |
0 |
3 |
|
| 747 |
0 |
| else return 0; |
| 748 |
41 |
| p++; |
| 749 |
41 |
| ts = p; |
| 750 |
41 |
| prev_blank_line = 0; |
| 751 |
339 |
▶ | while (p < n && s[p] != close) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (p < n && s[p] != close) { |
339 |
0 |
339 |
| while (p < n && s[p] != close) { |
298 |
41 |
339 |
|
| 752 |
298 |
▶ | if (s[p] == '\\' && p + 1 < n && is_ascii_punct((unsigned char)s[p+1])) { p += 2; continue; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (s[p] == '\\' && p + 1 < n && is_ascii_punct((unsigned char)s[p+1])) { p += 2; continue; } |
6 |
292 |
298 |
| if (s[p] == '\\' && p + 1 < n && is_ascii_punct((unsigned char)s[p+1])) { p += 2; continue; } |
6 |
0 |
6 |
| if (s[p] == '\\' && p + 1 < n && is_ascii_punct((unsigned char)s[p+1])) { p += 2; continue; } |
6 |
0 |
6 |
|
| 753 |
292 |
▶ | if (open == '(' && s[p] == '(') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (open == '(' && s[p] == '(') return 0; |
15 |
277 |
292 |
| if (open == '(' && s[p] == '(') return 0; |
0 |
15 |
15 |
|
| 754 |
292 |
▶ | if (s[p] == '\n') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s[p] == '\n') { |
0 |
292 |
292 |
|
| 755 |
|
| /* check for blank line */ |
| 756 |
0 |
| size_t r = p + 1; |
| 757 |
0 |
▶ | while (r < n && (s[r] == ' ' || s[r] == '\t')) r++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (r < n && (s[r] == ' ' || s[r] == '\t')) r++; |
0 |
0 |
0 |
| while (r < n && (s[r] == ' ' || s[r] == '\t')) r++; |
0 |
0 |
0 |
| while (r < n && (s[r] == ' ' || s[r] == '\t')) r++; |
0 |
0 |
0 |
|
| 758 |
0 |
▶ | if (r >= n || s[r] == '\n') { prev_blank_line = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (r >= n || s[r] == '\n') { prev_blank_line = 1; break; } |
0 |
0 |
0 |
| if (r >= n || s[r] == '\n') { prev_blank_line = 1; break; } |
0 |
0 |
0 |
|
| 759 |
|
| } |
| 760 |
292 |
| p++; |
| 761 |
|
| } |
| 762 |
41 |
▶ | if (prev_blank_line || p >= n || s[p] != close) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (prev_blank_line || p >= n || s[p] != close) return 0; |
41 |
0 |
41 |
| if (prev_blank_line || p >= n || s[p] != close) return 0; |
41 |
0 |
41 |
| if (prev_blank_line || p >= n || s[p] != close) return 0; |
0 |
41 |
41 |
|
| 763 |
41 |
| *out_s = s + ts; |
| 764 |
41 |
| *out_n = p - ts; |
| 765 |
41 |
| *pp = p + 1; |
| 766 |
41 |
| return 1; |
| 767 |
|
| } |
| 768 |
|
| |
| 769 |
|
| /* ---------------- find matching open bracket ---------------- */ |
| 770 |
|
| |
| 771 |
2424725 |
| static inode* find_open_bracket(scn* z, int* is_image) { |
| 772 |
|
| inode* x; |
| 773 |
2424725 |
| *is_image = 0; |
| 774 |
4849855 |
▶ | for (x = z->tail; x; x = x->prev) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = z->tail; x; x = x->prev) { |
4849839 |
16 |
4849855 |
|
| 775 |
4849839 |
▶ | if (x->type == N_OPEN_BRACKET || x->type == N_OPEN_BANG) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (x->type == N_OPEN_BRACKET || x->type == N_OPEN_BANG) { |
3233216 |
1616623 |
4849839 |
| if (x->type == N_OPEN_BRACKET || x->type == N_OPEN_BANG) { |
808086 |
2425130 |
3233216 |
|
| 776 |
2424709 |
| *is_image = (x->type == N_OPEN_BANG); |
| 777 |
2424709 |
| return x; /* return topmost — caller checks ->active */ |
| 778 |
|
| } |
| 779 |
|
| } |
| 780 |
16 |
| return NULL; |
| 781 |
|
| } |
| 782 |
|
| |
| 783 |
|
| /* Disable any '[' opener nodes appearing before x (for nested-link rule). */ |
| 784 |
808344 |
| static void deactivate_brackets(scn* z, inode* x) { |
| 785 |
|
| inode* p; |
| 786 |
2424604 |
▶ | for (p = x->prev; p; p = p->prev) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = x->prev; p; p = p->prev) { |
1616260 |
808344 |
2424604 |
|
| 787 |
1616260 |
▶ | if (p->type == N_OPEN_BRACKET) p->active = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->type == N_OPEN_BRACKET) p->active = 0; |
30 |
1616230 |
1616260 |
|
| 788 |
|
| } |
| 789 |
|
| (void)z; |
| 790 |
808344 |
| } |
| 791 |
|
| |
| 792 |
|
| /* Move nodes (open_bracket->next .. end) into a new container of type t, |
| 793 |
|
| * which replaces the open_bracket and everything after. Returns the |
| 794 |
|
| * container. */ |
| 795 |
1616424 |
| static inode* wrap_after(scn* z, inode* open, ntype t) { |
| 796 |
1616424 |
| inode* c = node_new(z, t); |
| 797 |
1616424 |
| c->children = open->next; |
| 798 |
1616424 |
| c->children_tail = z->tail; |
| 799 |
1616424 |
▶ | if (c->children) c->children->prev = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->children) c->children->prev = NULL; |
1616415 |
9 |
1616424 |
|
| 800 |
|
| /* sever tail link */ |
| 801 |
1616424 |
| z->tail = open->prev; |
| 802 |
1616424 |
▶ | if (z->tail) z->tail->next = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->tail) z->tail->next = NULL; |
808127 |
808297 |
1616424 |
|
| 803 |
808297 |
| else z->head = NULL; |
| 804 |
|
| /* remove the open bracket itself */ |
| 805 |
|
| /* (it's now floating; we won't re-link it) */ |
| 806 |
1616424 |
| append(z, c); |
| 807 |
1616424 |
| return c; |
| 808 |
|
| } |
| 809 |
|
| |
| 810 |
|
| /* ---------------- process_emphasis (§6.4) ---------------- */ |
| 811 |
|
| |
| 812 |
|
| /* The CommonMark algorithm operates on the delimiter stack. We use the |
| 813 |
|
| * doubly-linked node list directly; DELIM nodes ARE the stack entries. |
| 814 |
|
| * |
| 815 |
|
| * stack_bottom: only consider delimiters strictly after this node. |
| 816 |
|
| * NULL = whole list (or list head). |
| 817 |
|
| */ |
| 818 |
4062341 |
| static void process_emphasis(scn* z, inode* stack_bottom) { |
| 819 |
|
| /* openers_bottom[delim_idx][closer_count_mod3][can_open(0|1)] */ |
| 820 |
|
| inode* openers_bottom[3][3][2]; |
| 821 |
|
| inode* closer; |
| 822 |
|
| int a, b, c; |
| 823 |
|
| int use2; |
| 824 |
|
| ntype tt; |
| 825 |
|
| unsigned _ifl; |
| 826 |
|
| inode* container; |
| 827 |
|
| inode* first; |
| 828 |
|
| inode* last; |
| 829 |
|
| inode* before; |
| 830 |
|
| inode* after; |
| 831 |
|
| inode* new_open; |
| 832 |
|
| inode* new_close; |
| 833 |
|
| inode* prev_link; |
| 834 |
|
| inode* start; |
| 835 |
|
| inode* p; |
| 836 |
16249364 |
▶ | for (a = 0; a < 3; a++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (a = 0; a < 3; a++) |
12187023 |
4062341 |
16249364 |
|
| 837 |
48748092 |
▶ | for (b = 0; b < 3; b++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (b = 0; b < 3; b++) |
36561069 |
12187023 |
48748092 |
|
| 838 |
109683207 |
▶ | for (c = 0; c < 2; c++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < 2; c++) |
73122138 |
36561069 |
109683207 |
|
| 839 |
73122138 |
| openers_bottom[a][b][c] = stack_bottom; |
| 840 |
|
| |
| 841 |
4062341 |
▶ | closer = stack_bottom ? stack_bottom->next : z->head; |
Branch: 1 branch
| Branch | True | False | Total |
| closer = stack_bottom ? stack_bottom->next : z->head; |
1616424 |
2445917 |
4062341 |
|
| 842 |
|
| /* find first potential closer */ |
| 843 |
12934985 |
▶ | while (closer) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (closer) { |
8873165 |
4061820 |
12934985 |
|
| 844 |
8873165 |
▶ | if (closer->type == N_DELIM && closer->can_close && |
Branch: 2 branches
| Branch | True | False | Total |
| if (closer->type == N_DELIM && closer->can_close && |
1305 |
8871860 |
8873165 |
| if (closer->type == N_DELIM && closer->can_close && |
521 |
784 |
1305 |
|
| 845 |
521 |
▶ | (closer->delim_char == '*' || closer->delim_char == '_' || |
Branch: 2 branches
| Branch | True | False | Total |
| (closer->delim_char == '*' || closer->delim_char == '_' || |
182 |
339 |
521 |
| (closer->delim_char == '*' || closer->delim_char == '_' || |
26 |
156 |
182 |
|
| 846 |
26 |
▶ | closer->delim_char == '~')) |
Branch: 1 branch
| Branch | True | False | Total |
| closer->delim_char == '~')) |
0 |
26 |
26 |
|
| 847 |
|
| break; |
| 848 |
8872644 |
| closer = closer->next; |
| 849 |
|
| } |
| 850 |
4063120 |
▶ | while (closer) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (closer) { |
779 |
4062341 |
4063120 |
|
| 851 |
779 |
| unsigned char ch = closer->delim_char; |
| 852 |
779 |
▶ | int didx = (ch == '*') ? 0 : (ch == '_') ? 1 : 2; |
Branch: 2 branches
| Branch | True | False | Total |
| int didx = (ch == '*') ? 0 : (ch == '_') ? 1 : 2; |
276 |
503 |
779 |
| int didx = (ch == '*') ? 0 : (ch == '_') ? 1 : 2; |
244 |
32 |
276 |
|
| 853 |
779 |
| int co_mod = closer->count % 3; |
| 854 |
779 |
| int co_op = closer->can_open ? 1 : 0; |
| 855 |
779 |
| inode* bot = openers_bottom[didx][co_mod][co_op]; |
| 856 |
|
| |
| 857 |
|
| /* walk back for matching opener */ |
| 858 |
779 |
| inode* opener = closer->prev; |
| 859 |
779 |
| int found = 0; |
| 860 |
1957 |
▶ | while (opener && opener != bot && opener != stack_bottom) { |
Branch: 3 branches
| Branch | True | False | Total |
| while (opener && opener != bot && opener != stack_bottom) { |
1825 |
132 |
1957 |
| while (opener && opener != bot && opener != stack_bottom) { |
1792 |
33 |
1825 |
| while (opener && opener != bot && opener != stack_bottom) { |
1792 |
0 |
1792 |
|
| 861 |
1792 |
▶ | if (opener->type == N_DELIM && opener->can_open && |
Branch: 2 branches
| Branch | True | False | Total |
| if (opener->type == N_DELIM && opener->can_open && |
719 |
1073 |
1792 |
| if (opener->type == N_DELIM && opener->can_open && |
665 |
54 |
719 |
|
| 862 |
665 |
▶ | opener->delim_char == ch) { |
Branch: 1 branch
| Branch | True | False | Total |
| opener->delim_char == ch) { |
629 |
36 |
665 |
|
| 863 |
|
| /* rule of three */ |
| 864 |
560 |
▶ | int odd_match = (closer->can_open || opener->can_close) && |
Branch: 1 branch
| Branch | True | False | Total |
| int odd_match = (closer->can_open || opener->can_close) && |
18 |
542 |
560 |
|
| 865 |
1210 |
▶ | ((opener->count + closer->count) % 3 == 0) && |
Branch: 2 branches
| Branch | True | False | Total |
| ((opener->count + closer->count) % 3 == 0) && |
560 |
69 |
629 |
| ((opener->count + closer->count) % 3 == 0) && |
21 |
66 |
87 |
|
| 866 |
21 |
▶ | !(opener->count % 3 == 0 && closer->count % 3 == 0); |
Branch: 2 branches
| Branch | True | False | Total |
| !(opener->count % 3 == 0 && closer->count % 3 == 0); |
6 |
15 |
21 |
| !(opener->count % 3 == 0 && closer->count % 3 == 0); |
0 |
6 |
6 |
|
| 867 |
629 |
▶ | if (!odd_match) { found = 1; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!odd_match) { found = 1; break; } |
614 |
15 |
629 |
|
| 868 |
|
| } |
| 869 |
1178 |
| opener = opener->prev; |
| 870 |
|
| } |
| 871 |
779 |
▶ | if (!found) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!found) { |
165 |
614 |
779 |
|
| 872 |
165 |
| openers_bottom[didx][co_mod][co_op] = closer->prev; |
| 873 |
|
| /* If the closer itself can't also open, mark it inert so it |
| 874 |
|
| * becomes literal text in the final sweep. Either way, advance. */ |
| 875 |
165 |
▶ | if (!closer->can_open) closer->can_close = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!closer->can_open) closer->can_close = 0; |
105 |
60 |
165 |
|
| 876 |
165 |
| closer = closer->next; |
| 877 |
315 |
▶ | while (closer) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (closer) { |
210 |
105 |
315 |
|
| 878 |
210 |
▶ | if (closer->type == N_DELIM && closer->can_close && |
Branch: 2 branches
| Branch | True | False | Total |
| if (closer->type == N_DELIM && closer->can_close && |
63 |
147 |
210 |
| if (closer->type == N_DELIM && closer->can_close && |
60 |
3 |
63 |
|
| 879 |
60 |
▶ | (closer->delim_char == '*' || closer->delim_char == '_' || |
Branch: 2 branches
| Branch | True | False | Total |
| (closer->delim_char == '*' || closer->delim_char == '_' || |
18 |
42 |
60 |
| (closer->delim_char == '*' || closer->delim_char == '_' || |
0 |
18 |
18 |
|
| 880 |
0 |
▶ | closer->delim_char == '~')) |
Branch: 1 branch
| Branch | True | False | Total |
| closer->delim_char == '~')) |
0 |
0 |
0 |
|
| 881 |
|
| break; |
| 882 |
150 |
| closer = closer->next; |
| 883 |
|
| } |
| 884 |
165 |
| continue; |
| 885 |
|
| } |
| 886 |
|
| |
| 887 |
614 |
| use2 = (ch == '~') |
| 888 |
|
| ? opener->count /* matched count for tildes */ |
| 889 |
614 |
▶ | : ((opener->count >= 2 && closer->count >= 2) ? 2 : 1); |
Branch: 3 branches
| Branch | True | False | Total |
| : ((opener->count >= 2 && closer->count >= 2) ? 2 : 1); |
29 |
585 |
614 |
| : ((opener->count >= 2 && closer->count >= 2) ? 2 : 1); |
259 |
326 |
585 |
| : ((opener->count >= 2 && closer->count >= 2) ? 2 : 1); |
241 |
18 |
259 |
|
| 890 |
|
| |
| 891 |
|
| /* GFM strike: counts must match and be 1 or 2 (no triple+). */ |
| 892 |
614 |
▶ | if (ch == '~' && (opener->count != closer->count || |
Branch: 2 branches
| Branch | True | False | Total |
| if (ch == '~' && (opener->count != closer->count || |
29 |
585 |
614 |
| if (ch == '~' && (opener->count != closer->count || |
26 |
3 |
29 |
|
| 893 |
26 |
▶ | (opener->count != 1 && opener->count != 2))) { |
Branch: 2 branches
| Branch | True | False | Total |
| (opener->count != 1 && opener->count != 2))) { |
17 |
9 |
26 |
| (opener->count != 1 && opener->count != 2))) { |
0 |
17 |
17 |
|
| 894 |
|
| /* skip — leave as text */ |
| 895 |
3 |
| openers_bottom[didx][co_mod][co_op] = opener; |
| 896 |
3 |
| closer = closer->next; |
| 897 |
3 |
▶ | while (closer) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (closer) { |
0 |
3 |
3 |
|
| 898 |
0 |
▶ | if (closer->type == N_DELIM && closer->can_close && |
Branch: 2 branches
| Branch | True | False | Total |
| if (closer->type == N_DELIM && closer->can_close && |
0 |
0 |
0 |
| if (closer->type == N_DELIM && closer->can_close && |
0 |
0 |
0 |
|
| 899 |
0 |
▶ | (closer->delim_char == '*' || closer->delim_char == '_' || |
Branch: 2 branches
| Branch | True | False | Total |
| (closer->delim_char == '*' || closer->delim_char == '_' || |
0 |
0 |
0 |
| (closer->delim_char == '*' || closer->delim_char == '_' || |
0 |
0 |
0 |
|
| 900 |
0 |
▶ | closer->delim_char == '~')) |
Branch: 1 branch
| Branch | True | False | Total |
| closer->delim_char == '~')) |
0 |
0 |
0 |
|
| 901 |
|
| break; |
| 902 |
0 |
| closer = closer->next; |
| 903 |
|
| } |
| 904 |
3 |
| continue; |
| 905 |
|
| } |
| 906 |
|
| |
| 907 |
611 |
▶ | tt = (ch == '~') ? N_STRIKE : (use2 == 2 ? N_STRONG : N_EMPH); |
Branch: 2 branches
| Branch | True | False | Total |
| tt = (ch == '~') ? N_STRIKE : (use2 == 2 ? N_STRONG : N_EMPH); |
585 |
26 |
611 |
| tt = (ch == '~') ? N_STRIKE : (use2 == 2 ? N_STRONG : N_EMPH); |
241 |
344 |
585 |
|
| 908 |
611 |
| _ifl = z->ctx->flags; |
| 909 |
611 |
▶ | if ((tt == N_EMPH && (_ifl & MDS_FLAG_NO_EMPH)) || |
Branch: 3 branches
| Branch | True | False | Total |
| if ((tt == N_EMPH && (_ifl & MDS_FLAG_NO_EMPH)) || |
344 |
267 |
611 |
| if ((tt == N_EMPH && (_ifl & MDS_FLAG_NO_EMPH)) || |
343 |
1 |
344 |
| if ((tt == N_EMPH && (_ifl & MDS_FLAG_NO_EMPH)) || |
241 |
369 |
610 |
|
| 910 |
241 |
▶ | (tt == N_STRONG && (_ifl & MDS_FLAG_NO_STRONG))) { |
Branch: 1 branch
| Branch | True | False | Total |
| (tt == N_STRONG && (_ifl & MDS_FLAG_NO_STRONG))) { |
1 |
240 |
241 |
|
| 911 |
|
| /* Skip: leave delim run as-is, advance closer (becomes text) */ |
| 912 |
2 |
| openers_bottom[didx][co_mod][co_op] = opener; |
| 913 |
2 |
▶ | if (!closer->can_open) closer->can_close = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!closer->can_open) closer->can_close = 0; |
2 |
0 |
2 |
|
| 914 |
2 |
| closer = closer->next; |
| 915 |
2 |
▶ | while (closer) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (closer) { |
0 |
2 |
2 |
|
| 916 |
0 |
▶ | if (closer->type == N_DELIM && closer->can_close && |
Branch: 2 branches
| Branch | True | False | Total |
| if (closer->type == N_DELIM && closer->can_close && |
0 |
0 |
0 |
| if (closer->type == N_DELIM && closer->can_close && |
0 |
0 |
0 |
|
| 917 |
0 |
▶ | (closer->delim_char == '*' || closer->delim_char == '_' || |
Branch: 2 branches
| Branch | True | False | Total |
| (closer->delim_char == '*' || closer->delim_char == '_' || |
0 |
0 |
0 |
| (closer->delim_char == '*' || closer->delim_char == '_' || |
0 |
0 |
0 |
|
| 918 |
0 |
▶ | closer->delim_char == '~')) |
Branch: 1 branch
| Branch | True | False | Total |
| closer->delim_char == '~')) |
0 |
0 |
0 |
|
| 919 |
|
| break; |
| 920 |
0 |
| closer = closer->next; |
| 921 |
|
| } |
| 922 |
2 |
| continue; |
| 923 |
|
| } |
| 924 |
609 |
| container = node_new(z, tt); |
| 925 |
|
| /* children = (opener->next .. closer->prev) */ |
| 926 |
609 |
| first = opener->next; |
| 927 |
609 |
| last = closer->prev; |
| 928 |
609 |
▶ | if (first != closer) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (first != closer) { |
609 |
0 |
609 |
|
| 929 |
609 |
| container->children = first; |
| 930 |
609 |
| container->children_tail = last; |
| 931 |
609 |
| first->prev = NULL; |
| 932 |
609 |
| last->next = NULL; |
| 933 |
|
| } |
| 934 |
|
| /* shrink/remove delimiters */ |
| 935 |
609 |
| opener->count -= use2; |
| 936 |
609 |
| closer->count -= use2; |
| 937 |
|
| |
| 938 |
|
| /* relink: replace [opener? closer?] block with container */ |
| 939 |
609 |
| before = opener->prev; |
| 940 |
609 |
| after = closer->next; |
| 941 |
|
| |
| 942 |
609 |
▶ | new_open = (opener->count > 0) ? opener : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| new_open = (opener->count > 0) ? opener : NULL; |
65 |
544 |
609 |
|
| 943 |
609 |
▶ | new_close = (closer->count > 0) ? closer : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| new_close = (closer->count > 0) ? closer : NULL; |
71 |
538 |
609 |
|
| 944 |
|
| |
| 945 |
|
| /* shrink opener s/n bytes for proper future text emission? Not |
| 946 |
|
| * needed — opener bytes are never emitted directly; only the count |
| 947 |
|
| * matters when shrunken to >0 and treated as remaining delim. */ |
| 948 |
609 |
▶ | if (new_open) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_open) { |
65 |
544 |
609 |
|
| 949 |
|
| /* truncate opener's literal length so leftover delim chars |
| 950 |
|
| * remain rendered if not consumed later */ |
| 951 |
65 |
| new_open->n = (size_t)new_open->count; |
| 952 |
|
| } |
| 953 |
609 |
▶ | if (new_close) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_close) { |
71 |
538 |
609 |
|
| 954 |
71 |
| new_close->n = (size_t)new_close->count; |
| 955 |
71 |
| new_close->s = new_close->s; /* keep pointer, length adjusted */ |
| 956 |
|
| } |
| 957 |
|
| |
| 958 |
|
| /* build list: before, new_open?, container, new_close?, after */ |
| 959 |
609 |
| prev_link = before; |
| 960 |
609 |
▶ | if (new_open) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_open) { |
65 |
544 |
609 |
|
| 961 |
65 |
▶ | if (prev_link) prev_link->next = new_open; |
Branch: 1 branch
| Branch | True | False | Total |
| if (prev_link) prev_link->next = new_open; |
11 |
54 |
65 |
|
| 962 |
54 |
| else z->head = new_open; |
| 963 |
65 |
| new_open->prev = prev_link; |
| 964 |
65 |
| prev_link = new_open; |
| 965 |
|
| } |
| 966 |
609 |
▶ | if (prev_link) prev_link->next = container; |
Branch: 1 branch
| Branch | True | False | Total |
| if (prev_link) prev_link->next = container; |
344 |
265 |
609 |
|
| 967 |
265 |
| else z->head = container; |
| 968 |
609 |
| container->prev = prev_link; |
| 969 |
609 |
| prev_link = container; |
| 970 |
609 |
▶ | if (new_close) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_close) { |
71 |
538 |
609 |
|
| 971 |
71 |
| prev_link->next = new_close; |
| 972 |
71 |
| new_close->prev = prev_link; |
| 973 |
71 |
| prev_link = new_close; |
| 974 |
|
| } |
| 975 |
609 |
| prev_link->next = after; |
| 976 |
609 |
▶ | if (after) after->prev = prev_link; |
Branch: 1 branch
| Branch | True | False | Total |
| if (after) after->prev = prev_link; |
228 |
381 |
609 |
|
| 977 |
381 |
| else z->tail = prev_link; |
| 978 |
|
| |
| 979 |
|
| /* continue: if closer still has count, use it as closer again; |
| 980 |
|
| * otherwise resume from after */ |
| 981 |
609 |
▶ | if (new_close) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (new_close) { |
71 |
538 |
609 |
|
| 982 |
71 |
| closer = new_close; |
| 983 |
|
| } else { |
| 984 |
538 |
| closer = after; |
| 985 |
860 |
▶ | while (closer) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (closer) { |
449 |
411 |
860 |
|
| 986 |
449 |
▶ | if (closer->type == N_DELIM && closer->can_close && |
Branch: 2 branches
| Branch | True | False | Total |
| if (closer->type == N_DELIM && closer->can_close && |
158 |
291 |
449 |
| if (closer->type == N_DELIM && closer->can_close && |
127 |
31 |
158 |
|
| 987 |
127 |
▶ | (closer->delim_char == '*' || closer->delim_char == '_' || |
Branch: 2 branches
| Branch | True | False | Total |
| (closer->delim_char == '*' || closer->delim_char == '_' || |
58 |
69 |
127 |
| (closer->delim_char == '*' || closer->delim_char == '_' || |
6 |
52 |
58 |
|
| 988 |
6 |
▶ | closer->delim_char == '~')) |
Branch: 1 branch
| Branch | True | False | Total |
| closer->delim_char == '~')) |
0 |
6 |
6 |
|
| 989 |
|
| break; |
| 990 |
322 |
| closer = closer->next; |
| 991 |
|
| } |
| 992 |
|
| } |
| 993 |
|
| } |
| 994 |
|
| /* clear remaining DELIMs to TEXT */ |
| 995 |
4062341 |
▶ | start = stack_bottom ? stack_bottom->next : z->head; |
Branch: 1 branch
| Branch | True | False | Total |
| start = stack_bottom ? stack_bottom->next : z->head; |
1616424 |
2445917 |
4062341 |
|
| 996 |
12934780 |
▶ | for (p = start; p; p = p->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = start; p; p = p->next) { |
8872439 |
4062341 |
12934780 |
|
| 997 |
8872439 |
▶ | if (p->type == N_DELIM) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->type == N_DELIM) { |
408 |
8872031 |
8872439 |
|
| 998 |
408 |
| p->type = N_TEXT; |
| 999 |
|
| } |
| 1000 |
|
| } |
| 1001 |
4062341 |
| } |
| 1002 |
|
| |
| 1003 |
|
| /* ---------------- process ']' ---------------- */ |
| 1004 |
|
| |
| 1005 |
2424725 |
| static int try_close_bracket(scn* z, size_t* pos_io) { |
| 1006 |
2424725 |
| const char* s = z->s; size_t n = z->n; |
| 1007 |
2424725 |
| size_t pos = *pos_io; |
| 1008 |
2424725 |
| int is_image = 0; |
| 1009 |
|
| inode* opener; |
| 1010 |
|
| size_t p; |
| 1011 |
2424725 |
| int matched = 0; |
| 1012 |
2424725 |
| const char *href_s = NULL, *title_s = NULL; |
| 1013 |
2424725 |
| size_t hlen = 0, tlen = 0; |
| 1014 |
2424725 |
| int is_ref = 0; |
| 1015 |
2424725 |
| const mds_linkref* refent = NULL; |
| 1016 |
|
| ntype t; |
| 1017 |
|
| inode* container; |
| 1018 |
2424725 |
| opener = find_open_bracket(z, &is_image); |
| 1019 |
2424725 |
▶ | if (!opener) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!opener) { |
16 |
2424709 |
2424725 |
|
| 1020 |
16 |
| return 0; /* no opener — caller emits literal ']' */ |
| 1021 |
|
| } |
| 1022 |
|
| /* CommonMark "look for link or image" step 3: if the opener exists |
| 1023 |
|
| * but is inactive, remove it from the stack (convert to literal `[`) |
| 1024 |
|
| * and treat this `]` as literal text. Do NOT keep searching for an |
| 1025 |
|
| * earlier active opener — the inactive opener blocks it. This is |
| 1026 |
|
| * what makes the alt text of an image with a nested link come out |
| 1027 |
|
| * as `[foo](uri2)` literally (CM example 520). */ |
| 1028 |
2424709 |
▶ | if (!opener->active) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!opener->active) { |
21 |
2424688 |
2424709 |
|
| 1029 |
21 |
| opener->type = N_TEXT; |
| 1030 |
21 |
| opener->active = 0; |
| 1031 |
21 |
| return 0; |
| 1032 |
|
| } |
| 1033 |
2424688 |
| p = pos + 1; |
| 1034 |
|
| |
| 1035 |
|
| /* GFM footnote reference [^label] — checked first so it wins over |
| 1036 |
|
| * inline link/ref interpretations. Requires the bracket content to |
| 1037 |
|
| * begin with `^` and the label (everything after) to be present in |
| 1038 |
|
| * ctx->footnotes. Unresolved [^label] falls through to normal |
| 1039 |
|
| * processing (becomes literal text). */ |
| 1040 |
2424688 |
▶ | if ((z->ctx->flags & MDS_FLAG_FOOTNOTES) && z->ctx->footnotes) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((z->ctx->flags & MDS_FLAG_FOOTNOTES) && z->ctx->footnotes) { |
2424120 |
568 |
2424688 |
| if ((z->ctx->flags & MDS_FLAG_FOOTNOTES) && z->ctx->footnotes) { |
1616027 |
808093 |
2424120 |
|
| 1041 |
1616027 |
| size_t txt_s0 = (size_t)((opener->s + opener->n) - s); |
| 1042 |
1616027 |
| size_t txt_e0 = pos; |
| 1043 |
1616027 |
▶ | if (txt_e0 > txt_s0 && s[txt_s0] == '^') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (txt_e0 > txt_s0 && s[txt_s0] == '^') { |
1616027 |
0 |
1616027 |
| if (txt_e0 > txt_s0 && s[txt_s0] == '^') { |
808027 |
808000 |
1616027 |
|
| 1044 |
808027 |
| const char* lab_s = s + txt_s0 + 1; |
| 1045 |
808027 |
| size_t lab_n = txt_e0 - txt_s0 - 1; |
| 1046 |
808027 |
| const mds_footnote* fn = mds_footnote_get(z->ctx->footnotes, |
| 1047 |
|
| lab_s, lab_n); |
| 1048 |
808027 |
▶ | if (fn) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fn) { |
808024 |
3 |
808027 |
|
| 1049 |
|
| /* Discard any children between opener and the `]` (the |
| 1050 |
|
| * `^label` text/delim nodes); we don't render them. */ |
| 1051 |
808024 |
| opener->children = NULL; |
| 1052 |
808024 |
| opener->children_tail = NULL; |
| 1053 |
|
| /* Drop everything after opener up to but not including pos. */ |
| 1054 |
808024 |
| opener->next = NULL; |
| 1055 |
808024 |
| z->tail = opener; |
| 1056 |
808024 |
▶ | if (opener->type == N_OPEN_BANG) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opener->type == N_OPEN_BANG) { |
3 |
808021 |
808024 |
|
| 1057 |
|
| inode* fnref; |
| 1058 |
|
| /* Salvage the literal `!` byte that the bang opener |
| 1059 |
|
| * absorbed — emit it as a sibling TEXT node BEFORE |
| 1060 |
|
| * the footnote ref. Without this, inputs like |
| 1061 |
|
| * `text![^1]` lose the `!`. */ |
| 1062 |
3 |
| opener->type = N_TEXT; |
| 1063 |
3 |
| opener->n = 1; /* s already points at '!' */ |
| 1064 |
3 |
| fnref = node_new(z, N_FOOTNOTE_REF); |
| 1065 |
3 |
| fnref->href = fn->label; |
| 1066 |
3 |
| fnref->hlen = fn->llen; |
| 1067 |
3 |
| fnref->active = 0; |
| 1068 |
3 |
| append(z, fnref); /* updates z->tail */ |
| 1069 |
|
| } else { |
| 1070 |
|
| /* Convert opener into the FOOTNOTE_REF node itself. */ |
| 1071 |
808021 |
| opener->type = N_FOOTNOTE_REF; |
| 1072 |
808021 |
| opener->href = fn->label; |
| 1073 |
808021 |
| opener->hlen = fn->llen; |
| 1074 |
808021 |
| opener->active = 0; |
| 1075 |
|
| } |
| 1076 |
808024 |
| *pos_io = pos + 1; |
| 1077 |
808024 |
| return 1; |
| 1078 |
|
| } |
| 1079 |
|
| } |
| 1080 |
|
| } |
| 1081 |
|
| |
| 1082 |
|
| /* (a) inline link [text](url "title") */ |
| 1083 |
1616664 |
▶ | if (p < n && s[p] == '(') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < n && s[p] == '(') { |
1616507 |
157 |
1616664 |
| if (p < n && s[p] == '(') { |
808231 |
808276 |
1616507 |
|
| 1084 |
808231 |
| size_t q = p + 1; |
| 1085 |
808240 |
▶ | while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
808240 |
0 |
808240 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
9 |
808231 |
808240 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
808231 |
808231 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
808231 |
808231 |
|
| 1086 |
808231 |
▶ | if (q < n && s[q] != ')') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < n && s[q] != ')') { |
808231 |
0 |
808231 |
| if (q < n && s[q] != ')') { |
808222 |
9 |
808231 |
|
| 1087 |
1616435 |
▶ | if (parse_link_destination(s, &q, n, &href_s, &hlen)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (parse_link_destination(s, &q, n, &href_s, &hlen)) { |
808213 |
9 |
808222 |
|
| 1088 |
808213 |
| size_t after_dest = q; |
| 1089 |
808272 |
▶ | while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
808269 |
3 |
808272 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
50 |
808219 |
808269 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
808219 |
808219 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
9 |
808210 |
808219 |
|
| 1090 |
808213 |
▶ | if (q < n && (s[q] == '"' || s[q] == '\'' || s[q] == '(')) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (q < n && (s[q] == '"' || s[q] == '\'' || s[q] == '(')) { |
808210 |
3 |
808213 |
| if (q < n && (s[q] == '"' || s[q] == '\'' || s[q] == '(')) { |
808181 |
29 |
808210 |
| if (q < n && (s[q] == '"' || s[q] == '\'' || s[q] == '(')) { |
808172 |
9 |
808181 |
| if (q < n && (s[q] == '"' || s[q] == '\'' || s[q] == '(')) { |
3 |
808169 |
808172 |
|
| 1091 |
41 |
▶ | if (!parse_link_title(s, &q, n, &title_s, &tlen)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!parse_link_title(s, &q, n, &title_s, &tlen)) { |
0 |
41 |
41 |
|
| 1092 |
0 |
| q = after_dest; |
| 1093 |
0 |
| title_s = NULL; tlen = 0; |
| 1094 |
|
| } else { |
| 1095 |
56 |
▶ | while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
56 |
0 |
56 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
15 |
41 |
56 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
41 |
41 |
| while (q < n && (s[q] == ' ' || s[q] == '\t' || s[q] == '\n')) q++; |
0 |
41 |
41 |
|
| 1096 |
|
| } |
| 1097 |
|
| } |
| 1098 |
808213 |
▶ | if (q < n && s[q] == ')') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (q < n && s[q] == ')') { |
808210 |
3 |
808213 |
| if (q < n && s[q] == ')') { |
808189 |
21 |
808210 |
|
| 1099 |
808189 |
| p = q + 1; |
| 1100 |
808189 |
| matched = 1; |
| 1101 |
|
| } |
| 1102 |
|
| } |
| 1103 |
9 |
▶ | } else if (q < n && s[q] == ')') { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (q < n && s[q] == ')') { |
9 |
0 |
9 |
| } else if (q < n && s[q] == ')') { |
9 |
0 |
9 |
|
| 1104 |
|
| /* empty destination */ |
| 1105 |
9 |
| href_s = s; hlen = 0; |
| 1106 |
9 |
| p = q + 1; |
| 1107 |
9 |
| matched = 1; |
| 1108 |
|
| } |
| 1109 |
|
| } |
| 1110 |
|
| |
| 1111 |
|
| /* (b) ref link forms */ |
| 1112 |
1616664 |
▶ | if (!matched && z->ctx->refs) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!matched && z->ctx->refs) { |
808466 |
808198 |
1616664 |
| if (!matched && z->ctx->refs) { |
808256 |
210 |
808466 |
|
| 1113 |
|
| /* label text bytes are between opener and current ] */ |
| 1114 |
808256 |
| size_t lbl_start_off = (size_t)(opener->s - s) + opener->n; /* after [ */ |
| 1115 |
|
| /* Actually opener->s points at '[' itself; opener->n == 1 (or 2 for ![) */ |
| 1116 |
|
| /* simpler: text content range is open_text..pos */ |
| 1117 |
808256 |
| size_t txt_s = (size_t)((opener->s + opener->n) - s); |
| 1118 |
808256 |
| size_t txt_e = pos; |
| 1119 |
808256 |
| int tried_full = 0; |
| 1120 |
|
| (void)lbl_start_off; |
| 1121 |
|
| |
| 1122 |
|
| /* full ref: [text][label] */ |
| 1123 |
808256 |
▶ | if (p < n && s[p] == '[') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p < n && s[p] == '[') { |
808139 |
117 |
808256 |
| if (p < n && s[p] == '[') { |
808105 |
34 |
808139 |
|
| 1124 |
808105 |
| size_t q = p + 1; |
| 1125 |
808105 |
| size_t lbl_s = q; |
| 1126 |
1616337 |
▶ | while (q < n && s[q] != ']' && s[q] != '[' && q - lbl_s < 1000) { |
Branch: 4 branches
| Branch | True | False | Total |
| while (q < n && s[q] != ']' && s[q] != '[' && q - lbl_s < 1000) { |
1616337 |
0 |
1616337 |
| while (q < n && s[q] != ']' && s[q] != '[' && q - lbl_s < 1000) { |
808232 |
808105 |
1616337 |
| while (q < n && s[q] != ']' && s[q] != '[' && q - lbl_s < 1000) { |
808232 |
0 |
808232 |
| while (q < n && s[q] != ']' && s[q] != '[' && q - lbl_s < 1000) { |
808232 |
0 |
808232 |
|
| 1127 |
808232 |
▶ | if (s[q] == '\\' && q + 1 < n) q++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[q] == '\\' && q + 1 < n) q++; |
6 |
808226 |
808232 |
| if (s[q] == '\\' && q + 1 < n) q++; |
6 |
0 |
6 |
|
| 1128 |
808232 |
| q++; |
| 1129 |
|
| } |
| 1130 |
808105 |
▶ | if (q < n && s[q] == ']' && q > lbl_s) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (q < n && s[q] == ']' && q > lbl_s) { |
808105 |
0 |
808105 |
| if (q < n && s[q] == ']' && q > lbl_s) { |
808105 |
0 |
808105 |
| if (q < n && s[q] == ']' && q > lbl_s) { |
808071 |
34 |
808105 |
|
| 1131 |
808071 |
| tried_full = 1; |
| 1132 |
808071 |
| refent = mds_linkref_get(z->ctx->refs, s + lbl_s, q - lbl_s); |
| 1133 |
808071 |
▶ | if (refent) { p = q + 1; is_ref = 1; matched = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (refent) { p = q + 1; is_ref = 1; matched = 1; } |
808062 |
9 |
808071 |
|
| 1134 |
34 |
▶ | } else if (q < n && s[q] == ']' && q == lbl_s) { |
Branch: 3 branches
| Branch | True | False | Total |
| } else if (q < n && s[q] == ']' && q == lbl_s) { |
34 |
0 |
34 |
| } else if (q < n && s[q] == ']' && q == lbl_s) { |
34 |
0 |
34 |
| } else if (q < n && s[q] == ']' && q == lbl_s) { |
34 |
0 |
34 |
|
| 1135 |
|
| /* collapsed [text][] */ |
| 1136 |
34 |
| refent = mds_linkref_get(z->ctx->refs, s + txt_s, txt_e - txt_s); |
| 1137 |
34 |
▶ | if (refent) { p = q + 1; is_ref = 1; matched = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (refent) { p = q + 1; is_ref = 1; matched = 1; } |
34 |
0 |
34 |
|
| 1138 |
|
| } |
| 1139 |
|
| } |
| 1140 |
808256 |
▶ | if (!matched && !tried_full) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!matched && !tried_full) { |
160 |
808096 |
808256 |
| if (!matched && !tried_full) { |
151 |
9 |
160 |
|
| 1141 |
|
| /* shortcut [text] — only when there is no [label] following */ |
| 1142 |
151 |
| refent = mds_linkref_get(z->ctx->refs, s + txt_s, txt_e - txt_s); |
| 1143 |
151 |
▶ | if (refent) { is_ref = 1; matched = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (refent) { is_ref = 1; matched = 1; } |
130 |
21 |
151 |
|
| 1144 |
|
| } |
| 1145 |
808256 |
▶ | if (matched && refent) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (matched && refent) { |
808226 |
30 |
808256 |
| if (matched && refent) { |
808226 |
0 |
808226 |
|
| 1146 |
808226 |
| href_s = refent->url; hlen = refent->ulen; |
| 1147 |
808226 |
| title_s = refent->title; tlen = refent->tlen; |
| 1148 |
|
| } |
| 1149 |
|
| } |
| 1150 |
|
| |
| 1151 |
1616664 |
▶ | if (!matched) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!matched) { |
240 |
1616424 |
1616664 |
|
| 1152 |
|
| /* no link: drop opener from stack — convert to plain TEXT so it |
| 1153 |
|
| * doesn't block outer brackets from matching this ']'. (CM spec |
| 1154 |
|
| * "look for link or image" step: remove opener on failure.) */ |
| 1155 |
240 |
| opener->type = N_TEXT; |
| 1156 |
240 |
| opener->active = 0; |
| 1157 |
240 |
| return 0; |
| 1158 |
|
| } |
| 1159 |
|
| |
| 1160 |
|
| /* run process_emphasis on the children range (opener->next .. tail) */ |
| 1161 |
1616424 |
| process_emphasis(z, opener); |
| 1162 |
|
| |
| 1163 |
|
| /* wrap into LINK or IMAGE container */ |
| 1164 |
1616424 |
▶ | t = is_image ? N_IMAGE : N_LINK; |
Branch: 1 branch
| Branch | True | False | Total |
| t = is_image ? N_IMAGE : N_LINK; |
808080 |
808344 |
1616424 |
|
| 1165 |
1616424 |
| container = wrap_after(z, opener, t); |
| 1166 |
1616424 |
| container->href = href_s; container->hlen = hlen; |
| 1167 |
1616424 |
| container->title = title_s; container->tlen = tlen; |
| 1168 |
|
| |
| 1169 |
|
| /* unlink opener from list (it became the boundary; wrap_after kept it |
| 1170 |
|
| * outside the new container — we need to remove it now). */ |
| 1171 |
1616424 |
▶ | if (opener->prev) opener->prev->next = container; |
Branch: 1 branch
| Branch | True | False | Total |
| if (opener->prev) opener->prev->next = container; |
808127 |
808297 |
1616424 |
|
| 1172 |
808297 |
| else z->head = container; |
| 1173 |
1616424 |
| container->prev = opener->prev; |
| 1174 |
|
| |
| 1175 |
|
| /* deactivate any earlier '[' if this is a link (not image) */ |
| 1176 |
1616424 |
▶ | if (!is_image) deactivate_brackets(z, container); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_image) deactivate_brackets(z, container); |
808344 |
808080 |
1616424 |
|
| 1177 |
|
| |
| 1178 |
1616424 |
| *pos_io = p; |
| 1179 |
|
| (void)is_ref; |
| 1180 |
1616424 |
| return 1; |
| 1181 |
|
| } |
| 1182 |
|
| |
| 1183 |
|
| /* ---------------- forward pass ---------------- */ |
| 1184 |
|
| |
| 1185 |
|
| /* SWAR / NEON fast-skip over runs of plain prose. The inline |
| 1186 |
|
| * scanner's outer switch fires on exactly these 11 bytes: |
| 1187 |
|
| * '\\' '`' '<' '&' '*' '_' '~' '[' '!' ']' '\n' |
| 1188 |
|
| * Everything else falls through to `default: pos++;`. We replace that |
| 1189 |
|
| * single-byte advance with a 16-byte (NEON) or 8-byte (SWAR) scan that |
| 1190 |
|
| * returns the offset to the next interesting byte (or the chunk size if |
| 1191 |
|
| * none). On prose corpora ~99% of bytes match the fast path. */ |
| 1192 |
|
| |
| 1193 |
|
| static const unsigned char mds_inline_interest[256] = { |
| 1194 |
|
| ['\\']=1, ['`']=1, ['<']=1, ['&']=1, ['*']=1, ['_']=1, |
| 1195 |
|
| ['~']=1, ['[']=1, ['!']=1, [']']=1, ['\n']=1, |
| 1196 |
|
| }; |
| 1197 |
|
| |
| 1198 |
|
| #if MDS_INLINE_HAVE_NEON |
| 1199 |
|
| static inline size_t mds_inline_skip16(const char* p) { |
| 1200 |
|
| /* Returns 0..16: bytes safe to skip before the first interesting one. */ |
| 1201 |
|
| uint8x16_t v = vld1q_u8((const uint8_t*)p); |
| 1202 |
|
| uint8x16_t bs = vceqq_u8(v, vdupq_n_u8('\\')); |
| 1203 |
|
| uint8x16_t bt = vceqq_u8(v, vdupq_n_u8('`')); |
| 1204 |
|
| uint8x16_t lt = vceqq_u8(v, vdupq_n_u8('<')); |
| 1205 |
|
| uint8x16_t amp = vceqq_u8(v, vdupq_n_u8('&')); |
| 1206 |
|
| uint8x16_t st = vceqq_u8(v, vdupq_n_u8('*')); |
| 1207 |
|
| uint8x16_t us = vceqq_u8(v, vdupq_n_u8('_')); |
| 1208 |
|
| uint8x16_t ti = vceqq_u8(v, vdupq_n_u8('~')); |
| 1209 |
|
| uint8x16_t lb = vceqq_u8(v, vdupq_n_u8('[')); |
| 1210 |
|
| uint8x16_t bg = vceqq_u8(v, vdupq_n_u8('!')); |
| 1211 |
|
| uint8x16_t rb = vceqq_u8(v, vdupq_n_u8(']')); |
| 1212 |
|
| uint8x16_t nl = vceqq_u8(v, vdupq_n_u8('\n')); |
| 1213 |
|
| uint8x16_t any = vorrq_u8(vorrq_u8(vorrq_u8(bs, bt), vorrq_u8(lt, amp)), |
| 1214 |
|
| vorrq_u8(vorrq_u8(vorrq_u8(st, us), vorrq_u8(ti, lb)), |
| 1215 |
|
| vorrq_u8(vorrq_u8(bg, rb), nl))); |
| 1216 |
|
| uint8x8_t lo, hi; |
| 1217 |
|
| uint8x8_t packed_lo; |
| 1218 |
|
| uint64_t m; |
| 1219 |
|
| if (vmaxvq_u8(any) == 0) return 16; |
| 1220 |
|
| /* Reduce to 64-bit then ctz to find first match. */ |
| 1221 |
|
| lo = vget_low_u8(any); |
| 1222 |
|
| hi = vget_high_u8(any); |
| 1223 |
|
| /* Pack each byte's high bit into a 16-bit mask via shrn trick. */ |
| 1224 |
|
| packed_lo = vshrn_n_u16(vreinterpretq_u16_u8(any), 4); |
| 1225 |
|
| m = vget_lane_u64(vreinterpret_u64_u8(packed_lo), 0); |
| 1226 |
|
| (void)lo; (void)hi; |
| 1227 |
|
| return (size_t)(__builtin_ctzll(m) >> 2); |
| 1228 |
|
| } |
| 1229 |
|
| #endif |
| 1230 |
|
| |
| 1231 |
8870041 |
| static inline size_t mds_inline_skip8(const char* p) { |
| 1232 |
|
| /* Portable SWAR fallback: 8-byte stride. */ |
| 1233 |
|
| uint64_t w; |
| 1234 |
|
| uint64_t m; |
| 1235 |
8870041 |
| memcpy(&w, p, 8); |
| 1236 |
|
| #define MDS_HASZ(x) (((x) - 0x0101010101010101ULL) & ~(x) & 0x8080808080808080ULL) |
| 1237 |
|
| #define MDS_BC(b) ((uint64_t)(b) * 0x0101010101010101ULL) |
| 1238 |
8870041 |
| m = MDS_HASZ(w ^ MDS_BC('\\')) |
| 1239 |
8870041 |
| | MDS_HASZ(w ^ MDS_BC('`')) |
| 1240 |
8870041 |
| | MDS_HASZ(w ^ MDS_BC('<')) |
| 1241 |
8870041 |
| | MDS_HASZ(w ^ MDS_BC('&')) |
| 1242 |
8870041 |
| | MDS_HASZ(w ^ MDS_BC('*')) |
| 1243 |
8870041 |
| | MDS_HASZ(w ^ MDS_BC('_')) |
| 1244 |
8870041 |
| | MDS_HASZ(w ^ MDS_BC('~')) |
| 1245 |
8870041 |
| | MDS_HASZ(w ^ MDS_BC('[')) |
| 1246 |
8870041 |
| | MDS_HASZ(w ^ MDS_BC('!')) |
| 1247 |
8870041 |
| | MDS_HASZ(w ^ MDS_BC(']')) |
| 1248 |
8870041 |
| | MDS_HASZ(w ^ MDS_BC('\n')); |
| 1249 |
|
| #undef MDS_HASZ |
| 1250 |
|
| #undef MDS_BC |
| 1251 |
8870041 |
▶ | if (!m) return 8; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) return 8; |
4848768 |
4021273 |
8870041 |
|
| 1252 |
|
| /* m has high bit set in each matching lane (little-endian byte order). */ |
| 1253 |
4021273 |
| return (size_t)(__builtin_ctzll(m) >> 3); |
| 1254 |
|
| } |
| 1255 |
|
| |
| 1256 |
2445917 |
| static void scan_forward(scn* z) { |
| 1257 |
2445917 |
| const char* s = z->s; |
| 1258 |
2445917 |
| size_t n = z->n; |
| 1259 |
2445917 |
| size_t pos = 0; |
| 1260 |
2445917 |
| size_t text_start = 0; |
| 1261 |
|
| |
| 1262 |
|
| #define FLUSH_TEXT() do { \ |
| 1263 |
|
| if (pos > text_start) append_text(z, s + text_start, pos - text_start); \ |
| 1264 |
|
| text_start = pos; \ |
| 1265 |
|
| } while (0) |
| 1266 |
|
| |
| 1267 |
14552937 |
▶ | while (pos < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (pos < n) { |
12107020 |
2445917 |
14552937 |
|
| 1268 |
12107020 |
| unsigned char c = (unsigned char)s[pos]; |
| 1269 |
12107020 |
| switch (c) { |
| 1270 |
339 |
| case '\\': { |
| 1271 |
|
| inode* x; |
| 1272 |
303 |
▶ | if (pos + 1 < n && s[pos+1] == '\n') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pos + 1 < n && s[pos+1] == '\n') { |
294 |
9 |
303 |
| if (pos + 1 < n && s[pos+1] == '\n') { |
13 |
281 |
294 |
|
| 1273 |
|
| /* hard break */ |
| 1274 |
13 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
13 |
0 |
13 |
|
| 1275 |
13 |
| x = node_new(z, N_LINEBREAK); |
| 1276 |
13 |
| append(z, x); |
| 1277 |
13 |
| pos += 2; |
| 1278 |
|
| /* skip leading spaces on next line */ |
| 1279 |
19 |
▶ | while (pos < n && (s[pos] == ' ' || s[pos] == '\t')) pos++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (pos < n && (s[pos] == ' ' || s[pos] == '\t')) pos++; |
19 |
0 |
19 |
| while (pos < n && (s[pos] == ' ' || s[pos] == '\t')) pos++; |
6 |
13 |
19 |
| while (pos < n && (s[pos] == ' ' || s[pos] == '\t')) pos++; |
0 |
13 |
13 |
|
| 1280 |
13 |
| text_start = pos; |
| 1281 |
13 |
| continue; |
| 1282 |
|
| } |
| 1283 |
290 |
▶ | if (pos + 1 < n && is_ascii_punct((unsigned char)s[pos+1])) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pos + 1 < n && is_ascii_punct((unsigned char)s[pos+1])) { |
281 |
9 |
290 |
| if (pos + 1 < n && is_ascii_punct((unsigned char)s[pos+1])) { |
254 |
27 |
281 |
|
| 1284 |
254 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
67 |
187 |
254 |
|
| 1285 |
254 |
| append_text_dup(z, s + pos + 1, 1); |
| 1286 |
254 |
| pos += 2; |
| 1287 |
254 |
| text_start = pos; |
| 1288 |
254 |
| continue; |
| 1289 |
|
| } |
| 1290 |
36 |
| pos++; |
| 1291 |
|
| (void)x; |
| 1292 |
36 |
| continue; |
| 1293 |
|
| } |
| 1294 |
193 |
| case '`': { |
| 1295 |
|
| size_t end; |
| 1296 |
193 |
▶ | if (z->ctx->flags & MDS_FLAG_NO_CODE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->ctx->flags & MDS_FLAG_NO_CODE) { |
2 |
191 |
193 |
|
| 1297 |
|
| /* emit literal backtick(s) as text */ |
| 1298 |
2 |
| size_t r = pos; |
| 1299 |
4 |
▶ | while (r < n && s[r] == '`') r++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (r < n && s[r] == '`') r++; |
4 |
0 |
4 |
| while (r < n && s[r] == '`') r++; |
2 |
2 |
4 |
|
| 1300 |
2 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
2 |
0 |
2 |
|
| 1301 |
2 |
| append_text(z, s + pos, r - pos); |
| 1302 |
2 |
| pos = r; text_start = pos; continue; |
| 1303 |
|
| } |
| 1304 |
191 |
| end = try_code_span(z, pos); |
| 1305 |
|
| if (end) { |
| 1306 |
|
| /* flush bytes before pos */ |
| 1307 |
|
| if (pos > text_start) { |
| 1308 |
|
| /* append_text already; but z->tail is the new CODE node. |
| 1309 |
|
| * We need to insert text BEFORE it. Re-do manually. */ |
| 1310 |
|
| } |
| 1311 |
|
| /* Actually: try_code_span already appended a CODE node, |
| 1312 |
|
| * so the prior bytes weren't flushed. Need to flush first. */ |
| 1313 |
|
| /* To keep things simple, flush BEFORE attempting span. */ |
| 1314 |
|
| /* Implementation note: re-do as flush-then-attempt below. */ |
| 1315 |
|
| (void)end; |
| 1316 |
|
| } |
| 1317 |
|
| /* re-attempt with flush */ |
| 1318 |
45 |
| { |
| 1319 |
191 |
| size_t saved_pos = pos; |
| 1320 |
|
| size_t end2; |
| 1321 |
|
| size_t r; |
| 1322 |
|
| /* remove the CODE node just appended (we did it above) */ |
| 1323 |
191 |
▶ | if (end && z->tail && z->tail->type == N_CODE) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (end && z->tail && z->tail->type == N_CODE) { |
146 |
45 |
191 |
| if (end && z->tail && z->tail->type == N_CODE) { |
146 |
0 |
146 |
| if (end && z->tail && z->tail->type == N_CODE) { |
146 |
0 |
146 |
|
| 1324 |
146 |
| inode* dead = z->tail; |
| 1325 |
146 |
| z->tail = dead->prev; |
| 1326 |
146 |
▶ | if (z->tail) z->tail->next = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->tail) z->tail->next = NULL; |
49 |
97 |
146 |
|
| 1327 |
97 |
| else z->head = NULL; |
| 1328 |
|
| } |
| 1329 |
|
| /* flush text */ |
| 1330 |
191 |
▶ | if (saved_pos > text_start) |
Branch: 1 branch
| Branch | True | False | Total |
| if (saved_pos > text_start) |
73 |
118 |
191 |
|
| 1331 |
73 |
| append_text(z, s + text_start, saved_pos - text_start); |
| 1332 |
191 |
| text_start = saved_pos; |
| 1333 |
|
| /* re-attempt cleanly */ |
| 1334 |
191 |
| end2 = try_code_span(z, saved_pos); |
| 1335 |
191 |
▶ | if (end2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (end2) { |
146 |
45 |
191 |
|
| 1336 |
146 |
| pos = end2; text_start = pos; continue; |
| 1337 |
|
| } |
| 1338 |
|
| /* failed: emit literal backticks */ |
| 1339 |
45 |
| r = pos; |
| 1340 |
99 |
▶ | while (r < n && s[r] == '`') r++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (r < n && s[r] == '`') r++; |
72 |
27 |
99 |
| while (r < n && s[r] == '`') r++; |
54 |
18 |
72 |
|
| 1341 |
45 |
| append_text(z, s + pos, r - pos); |
| 1342 |
45 |
| pos = r; text_start = pos; continue; |
| 1343 |
|
| } |
| 1344 |
|
| } |
| 1345 |
356 |
| case '<': { |
| 1346 |
|
| size_t end; |
| 1347 |
279 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
117 |
162 |
279 |
|
| 1348 |
279 |
| end = try_autolink(z, pos); |
| 1349 |
279 |
▶ | if (end) { pos += end; text_start = pos; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (end) { pos += end; text_start = pos; continue; } |
57 |
222 |
279 |
|
| 1350 |
222 |
▶ | if (!(z->ctx->flags & MDS_FLAG_NO_HTML)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(z->ctx->flags & MDS_FLAG_NO_HTML)) { |
220 |
2 |
222 |
|
| 1351 |
220 |
| end = try_html_inline(z, pos); |
| 1352 |
220 |
▶ | if (end) { pos += end; text_start = pos; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (end) { pos += end; text_start = pos; continue; } |
145 |
75 |
220 |
|
| 1353 |
|
| } |
| 1354 |
77 |
| append_text(z, s + pos, 1); |
| 1355 |
77 |
| pos++; text_start = pos; |
| 1356 |
77 |
| continue; |
| 1357 |
|
| } |
| 1358 |
145 |
| case '&': { |
| 1359 |
112 |
| size_t consumed = try_entity(z, pos); |
| 1360 |
112 |
▶ | if (consumed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (consumed) { |
79 |
33 |
112 |
|
| 1361 |
|
| /* flush prior text first */ |
| 1362 |
79 |
| size_t before = pos; |
| 1363 |
|
| /* try_entity already appended the entity TEXT; we need to |
| 1364 |
|
| * insert prior bytes before it. */ |
| 1365 |
79 |
▶ | if (z->tail && before > text_start) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (z->tail && before > text_start) { |
79 |
0 |
79 |
| if (z->tail && before > text_start) { |
52 |
27 |
79 |
|
| 1366 |
52 |
| inode* added = z->tail; |
| 1367 |
|
| /* detach */ |
| 1368 |
52 |
| z->tail = added->prev; |
| 1369 |
52 |
▶ | if (z->tail) z->tail->next = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->tail) z->tail->next = NULL; |
49 |
3 |
52 |
|
| 1370 |
3 |
| else z->head = NULL; |
| 1371 |
52 |
| append_text(z, s + text_start, before - text_start); |
| 1372 |
|
| /* re-append */ |
| 1373 |
52 |
| added->prev = z->tail; |
| 1374 |
52 |
| added->next = NULL; |
| 1375 |
52 |
▶ | if (z->tail) z->tail->next = added; |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->tail) z->tail->next = added; |
52 |
0 |
52 |
|
| 1376 |
0 |
| else z->head = added; |
| 1377 |
52 |
| z->tail = added; |
| 1378 |
|
| } |
| 1379 |
79 |
| pos += consumed; text_start = pos; |
| 1380 |
79 |
| continue; |
| 1381 |
|
| } |
| 1382 |
33 |
| pos++; continue; |
| 1383 |
|
| } |
| 1384 |
1548 |
| case '*': |
| 1385 |
|
| case '_': |
| 1386 |
1526 |
| case '~': { |
| 1387 |
|
| size_t start; |
| 1388 |
|
| size_t runlen; |
| 1389 |
|
| int co, cc; |
| 1390 |
|
| inode* x; |
| 1391 |
1548 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
987 |
561 |
1548 |
|
| 1392 |
1548 |
| start = pos; |
| 1393 |
4006 |
▶ | while (pos < n && (unsigned char)s[pos] == c) pos++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (pos < n && (unsigned char)s[pos] == c) pos++; |
3584 |
422 |
4006 |
| while (pos < n && (unsigned char)s[pos] == c) pos++; |
2458 |
1126 |
3584 |
|
| 1394 |
1548 |
| runlen = pos - start; |
| 1395 |
1548 |
▶ | if (c == '~' && ((runlen != 1 && runlen != 2) || !(z->ctx->flags & MDS_FLAG_STRIKE))) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == '~' && ((runlen != 1 && runlen != 2) || !(z->ctx->flags & MDS_FLAG_STRIKE))) { |
92 |
1456 |
1548 |
| if (c == '~' && ((runlen != 1 && runlen != 2) || !(z->ctx->flags & MDS_FLAG_STRIKE))) { |
55 |
37 |
92 |
| if (c == '~' && ((runlen != 1 && runlen != 2) || !(z->ctx->flags & MDS_FLAG_STRIKE))) { |
43 |
12 |
55 |
| if (c == '~' && ((runlen != 1 && runlen != 2) || !(z->ctx->flags & MDS_FLAG_STRIKE))) { |
10 |
70 |
80 |
|
| 1396 |
|
| /* not strike candidate (or strikethrough disabled); emit as text */ |
| 1397 |
22 |
| append_text(z, s + start, runlen); |
| 1398 |
22 |
| text_start = pos; |
| 1399 |
22 |
| continue; |
| 1400 |
|
| } |
| 1401 |
1526 |
| classify_run(s, n, start, runlen, &co, &cc, c); |
| 1402 |
1526 |
| x = node_new(z, N_DELIM); |
| 1403 |
1526 |
| x->delim_char = c; |
| 1404 |
1526 |
| x->count = (int)runlen; |
| 1405 |
1526 |
| x->can_open = co; |
| 1406 |
1526 |
| x->can_close = cc; |
| 1407 |
1526 |
| x->s = s + start; x->n = runlen; |
| 1408 |
1526 |
| append(z, x); |
| 1409 |
1526 |
| text_start = pos; |
| 1410 |
1526 |
| continue; |
| 1411 |
|
| } |
| 1412 |
3233319 |
| case '[': { |
| 1413 |
|
| inode* x; |
| 1414 |
1616660 |
▶ | if (z->ctx->flags & MDS_FLAG_NO_LINKS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->ctx->flags & MDS_FLAG_NO_LINKS) { |
1 |
1616659 |
1616660 |
|
| 1415 |
1 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
0 |
1 |
1 |
|
| 1416 |
1 |
| append_text(z, s + pos, 1); |
| 1417 |
1 |
| pos++; text_start = pos; continue; |
| 1418 |
|
| } |
| 1419 |
1616659 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
1616108 |
551 |
1616659 |
|
| 1420 |
1616659 |
| x = node_new(z, N_OPEN_BRACKET); |
| 1421 |
1616659 |
| x->s = s + pos; x->n = 1; |
| 1422 |
1616659 |
| x->active = 1; |
| 1423 |
1616659 |
| append(z, x); |
| 1424 |
1616659 |
| pos++; text_start = pos; |
| 1425 |
1616659 |
| continue; |
| 1426 |
|
| } |
| 1427 |
808119 |
| case '!': { |
| 1428 |
808119 |
▶ | if (pos + 1 < n && s[pos+1] == '[' && |
Branch: 2 branches
| Branch | True | False | Total |
| if (pos + 1 < n && s[pos+1] == '[' && |
808101 |
18 |
808119 |
| if (pos + 1 < n && s[pos+1] == '[' && |
808087 |
14 |
808101 |
|
| 1429 |
808087 |
▶ | !(z->ctx->flags & MDS_FLAG_NO_IMAGES)) { |
Branch: 1 branch
| Branch | True | False | Total |
| !(z->ctx->flags & MDS_FLAG_NO_IMAGES)) { |
808086 |
1 |
808087 |
|
| 1430 |
|
| inode* x; |
| 1431 |
808086 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
13 |
808073 |
808086 |
|
| 1432 |
808086 |
| x = node_new(z, N_OPEN_BANG); |
| 1433 |
808086 |
| x->s = s + pos; x->n = 2; |
| 1434 |
808086 |
| x->active = 1; |
| 1435 |
808086 |
| append(z, x); |
| 1436 |
808086 |
| pos += 2; text_start = pos; |
| 1437 |
808086 |
| continue; |
| 1438 |
|
| } |
| 1439 |
33 |
| pos++; continue; |
| 1440 |
|
| } |
| 1441 |
2425002 |
| case ']': { |
| 1442 |
|
| size_t p2; |
| 1443 |
2424725 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
2424575 |
150 |
2424725 |
|
| 1444 |
2424725 |
| p2 = pos; |
| 1445 |
2424725 |
▶ | if (try_close_bracket(z, &p2)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (try_close_bracket(z, &p2)) { |
2424448 |
277 |
2424725 |
|
| 1446 |
2424448 |
| pos = p2; text_start = pos; |
| 1447 |
2424448 |
| continue; |
| 1448 |
|
| } |
| 1449 |
|
| /* literal ] */ |
| 1450 |
277 |
| append_text(z, s + pos, 1); |
| 1451 |
277 |
| pos++; text_start = pos; |
| 1452 |
277 |
| continue; |
| 1453 |
|
| } |
| 1454 |
1576280 |
| case '\n': { |
| 1455 |
|
| int hard; |
| 1456 |
|
| inode* br; |
| 1457 |
788140 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
788084 |
56 |
788140 |
|
| 1458 |
|
| /* hard break iff prev TEXT ended with two-or-more spaces */ |
| 1459 |
788140 |
| hard = 0; |
| 1460 |
788140 |
▶ | if (z->tail && z->tail->type == N_TEXT) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (z->tail && z->tail->type == N_TEXT) { |
788140 |
0 |
788140 |
| if (z->tail && z->tail->type == N_TEXT) { |
788102 |
38 |
788140 |
|
| 1461 |
788102 |
| inode* t = z->tail; |
| 1462 |
788102 |
▶ | if (t->n >= 2 && t->s[t->n - 1] == ' ' && t->s[t->n - 2] == ' ') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (t->n >= 2 && t->s[t->n - 1] == ' ' && t->s[t->n - 2] == ' ') { |
788080 |
22 |
788102 |
| if (t->n >= 2 && t->s[t->n - 1] == ' ' && t->s[t->n - 2] == ' ') { |
19 |
788061 |
788080 |
| if (t->n >= 2 && t->s[t->n - 1] == ' ' && t->s[t->n - 2] == ' ') { |
16 |
3 |
19 |
|
| 1463 |
|
| /* trim trailing spaces */ |
| 1464 |
72 |
▶ | while (t->n > 0 && t->s[t->n - 1] == ' ') t->n--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (t->n > 0 && t->s[t->n - 1] == ' ') t->n--; |
72 |
0 |
72 |
| while (t->n > 0 && t->s[t->n - 1] == ' ') t->n--; |
56 |
16 |
72 |
|
| 1465 |
16 |
▶ | if (t->n == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->n == 0) { |
0 |
16 |
16 |
|
| 1466 |
|
| /* remove empty text */ |
| 1467 |
0 |
| z->tail = t->prev; |
| 1468 |
0 |
▶ | if (z->tail) z->tail->next = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->tail) z->tail->next = NULL; |
0 |
0 |
0 |
|
| 1469 |
0 |
| else z->head = NULL; |
| 1470 |
|
| } |
| 1471 |
16 |
| hard = 1; |
| 1472 |
788086 |
▶ | } else if (t->n >= 1 && t->s[t->n - 1] == ' ') { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (t->n >= 1 && t->s[t->n - 1] == ' ') { |
788086 |
0 |
788086 |
| } else if (t->n >= 1 && t->s[t->n - 1] == ' ') { |
9 |
788077 |
788086 |
|
| 1473 |
|
| /* single space trailing — strip */ |
| 1474 |
9 |
| t->n--; |
| 1475 |
9 |
▶ | if (t->n == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->n == 0) { |
6 |
3 |
9 |
|
| 1476 |
6 |
| z->tail = t->prev; |
| 1477 |
6 |
▶ | if (z->tail) z->tail->next = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->tail) z->tail->next = NULL; |
6 |
0 |
6 |
|
| 1478 |
0 |
| else z->head = NULL; |
| 1479 |
|
| } |
| 1480 |
|
| } |
| 1481 |
|
| } |
| 1482 |
788140 |
▶ | br = node_new(z, hard ? N_LINEBREAK : N_SOFTBREAK); |
Branch: 1 branch
| Branch | True | False | Total |
| br = node_new(z, hard ? N_LINEBREAK : N_SOFTBREAK); |
16 |
788124 |
788140 |
|
| 1483 |
788140 |
| append(z, br); |
| 1484 |
788140 |
| pos++; |
| 1485 |
|
| /* skip leading spaces on next line */ |
| 1486 |
788305 |
▶ | while (pos < n && (s[pos] == ' ' || s[pos] == '\t')) pos++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (pos < n && (s[pos] == ' ' || s[pos] == '\t')) pos++; |
788305 |
0 |
788305 |
| while (pos < n && (s[pos] == ' ' || s[pos] == '\t')) pos++; |
165 |
788140 |
788305 |
| while (pos < n && (s[pos] == ' ' || s[pos] == '\t')) pos++; |
0 |
788140 |
788140 |
|
| 1487 |
788140 |
| text_start = pos; |
| 1488 |
788140 |
| continue; |
| 1489 |
|
| } |
| 1490 |
6466941 |
| default: |
| 1491 |
|
| { |
| 1492 |
|
| /* Fast skip over plain prose. The chunked stride keeps the |
| 1493 |
|
| * text run intact (no FLUSH_TEXT needed) — we just advance |
| 1494 |
|
| * `pos` past bytes the outer switch would have ignored. */ |
| 1495 |
|
| #if MDS_INLINE_HAVE_NEON |
| 1496 |
|
| while (pos + 16 <= n) { |
| 1497 |
|
| size_t k = mds_inline_skip16(s + pos); |
| 1498 |
|
| pos += k; |
| 1499 |
|
| if (k < 16) goto next_iter; |
| 1500 |
|
| } |
| 1501 |
|
| #endif |
| 1502 |
11315709 |
▶ | while (pos + 8 <= n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (pos + 8 <= n) { |
8870041 |
2445668 |
11315709 |
|
| 1503 |
8870041 |
| size_t k = mds_inline_skip8(s + pos); |
| 1504 |
8870041 |
| pos += k; |
| 1505 |
8870041 |
▶ | if (k < 8) goto next_iter; |
Branch: 1 branch
| Branch | True | False | Total |
| if (k < 8) goto next_iter; |
4021273 |
4848768 |
8870041 |
|
| 1506 |
|
| } |
| 1507 |
|
| /* Tail: 1-byte at a time. The interest table makes the |
| 1508 |
|
| * predicate branch-free. */ |
| 1509 |
9843132 |
▶ | while (pos < n && !mds_inline_interest[(unsigned char)s[pos]]) |
Branch: 2 branches
| Branch | True | False | Total |
| while (pos < n && !mds_inline_interest[(unsigned char)s[pos]]) |
1636792 |
8206340 |
9843132 |
| while (pos < n && !mds_inline_interest[(unsigned char)s[pos]]) |
7397464 |
808876 |
8206340 |
|
| 1510 |
7397464 |
| pos++; |
| 1511 |
2445668 |
| next_iter: |
| 1512 |
6466941 |
| continue; |
| 1513 |
|
| } |
| 1514 |
|
| } |
| 1515 |
|
| } |
| 1516 |
2445917 |
▶ | FLUSH_TEXT(); |
Branch: 1 branch
| Branch | True | False | Total |
| FLUSH_TEXT(); |
1636819 |
809098 |
2445917 |
|
| 1517 |
|
| #undef FLUSH_TEXT |
| 1518 |
2445917 |
| } |
| 1519 |
|
| |
| 1520 |
|
| /* ---------------- emit pass ---------------- */ |
| 1521 |
|
| |
| 1522 |
|
| static void emit_children(scn* z, inode* head); |
| 1523 |
|
| |
| 1524 |
|
| /* HTML-escape NOT done here; renderer cb_text does the escaping. */ |
| 1525 |
5659945 |
| static void emit_text(scn* z, const char* s, size_t n) { |
| 1526 |
5659945 |
▶ | if (n == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return; |
0 |
5659945 |
5659945 |
|
| 1527 |
5659945 |
▶ | if (z->ctx->cb.text) z->ctx->cb.text(z->ctx->ud, s, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->ctx->cb.text) z->ctx->cb.text(z->ctx->ud, s, n); |
5659945 |
0 |
5659945 |
|
| 1528 |
|
| } |
| 1529 |
139 |
| static void emit_raw(scn* z, const char* s, size_t n) { |
| 1530 |
139 |
▶ | if (n == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return; |
0 |
139 |
139 |
|
| 1531 |
139 |
▶ | if (z->ctx->cb.raw) z->ctx->cb.raw(z->ctx->ud, s, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (z->ctx->cb.raw) z->ctx->cb.raw(z->ctx->ud, s, n); |
139 |
0 |
139 |
|
| 1532 |
|
| } |
| 1533 |
|
| |
| 1534 |
8873351 |
| static void emit_node(scn* z, inode* x) { |
| 1535 |
8873351 |
| mds_callbacks* cb = &z->ctx->cb; |
| 1536 |
|
| mds_inline_detail d; |
| 1537 |
8873351 |
| memset(&d, 0, sizeof d); |
| 1538 |
8873351 |
| switch (x->type) { |
| 1539 |
5659727 |
| case N_TEXT: |
| 1540 |
5659727 |
| emit_text(z, x->s, x->n); |
| 1541 |
5659727 |
| break; |
| 1542 |
788124 |
| case N_SOFTBREAK: |
| 1543 |
788124 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_SOFTBREAK, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_SOFTBREAK, &d); |
788124 |
0 |
788124 |
|
| 1544 |
788124 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_SOFTBREAK); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_SOFTBREAK); |
788124 |
0 |
788124 |
|
| 1545 |
788124 |
| break; |
| 1546 |
29 |
| case N_LINEBREAK: |
| 1547 |
29 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_LINEBREAK, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_LINEBREAK, &d); |
29 |
0 |
29 |
|
| 1548 |
29 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_LINEBREAK); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_LINEBREAK); |
29 |
0 |
29 |
|
| 1549 |
29 |
| break; |
| 1550 |
146 |
| case N_CODE: |
| 1551 |
146 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_CODE, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_CODE, &d); |
146 |
0 |
146 |
|
| 1552 |
146 |
| emit_text(z, x->s, x->n); |
| 1553 |
146 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_CODE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_CODE); |
146 |
0 |
146 |
|
| 1554 |
146 |
| break; |
| 1555 |
57 |
| case N_AUTOLINK: |
| 1556 |
57 |
| d.u.autolink.uri = x->s; |
| 1557 |
57 |
| d.u.autolink.uri_len = x->n; |
| 1558 |
57 |
| d.u.autolink.is_email = x->is_email; |
| 1559 |
57 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_AUTOLINK, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_AUTOLINK, &d); |
57 |
0 |
57 |
|
| 1560 |
57 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_AUTOLINK); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_AUTOLINK); |
57 |
0 |
57 |
|
| 1561 |
57 |
| break; |
| 1562 |
139 |
| case N_HTMLINLINE: |
| 1563 |
139 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_HTML_INLINE, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_HTML_INLINE, &d); |
139 |
0 |
139 |
|
| 1564 |
139 |
| emit_raw(z, x->s, x->n); |
| 1565 |
139 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_HTML_INLINE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_HTML_INLINE); |
139 |
0 |
139 |
|
| 1566 |
139 |
| break; |
| 1567 |
343 |
| case N_EMPH: |
| 1568 |
343 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_EMPH, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_EMPH, &d); |
343 |
0 |
343 |
|
| 1569 |
343 |
| emit_children(z, x->children); |
| 1570 |
343 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_EMPH); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_EMPH); |
343 |
0 |
343 |
|
| 1571 |
343 |
| break; |
| 1572 |
240 |
| case N_STRONG: |
| 1573 |
240 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_STRONG, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_STRONG, &d); |
240 |
0 |
240 |
|
| 1574 |
240 |
| emit_children(z, x->children); |
| 1575 |
240 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_STRONG); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_STRONG); |
240 |
0 |
240 |
|
| 1576 |
240 |
| break; |
| 1577 |
26 |
| case N_STRIKE: |
| 1578 |
26 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_STRIKE, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_STRIKE, &d); |
26 |
0 |
26 |
|
| 1579 |
26 |
| emit_children(z, x->children); |
| 1580 |
26 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_STRIKE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_STRIKE); |
26 |
0 |
26 |
|
| 1581 |
26 |
| break; |
| 1582 |
808344 |
| case N_LINK: |
| 1583 |
808344 |
| d.u.link.href = x->href; d.u.link.href_len = x->hlen; |
| 1584 |
808344 |
| d.u.link.title = x->title; d.u.link.title_len = x->tlen; |
| 1585 |
808344 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_LINK, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_LINK, &d); |
808344 |
0 |
808344 |
|
| 1586 |
808344 |
| emit_children(z, x->children); |
| 1587 |
808344 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_LINK); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_LINK); |
808344 |
0 |
808344 |
|
| 1588 |
808344 |
| break; |
| 1589 |
808080 |
| case N_IMAGE: |
| 1590 |
808080 |
| d.u.image.href = x->href; d.u.image.href_len = x->hlen; |
| 1591 |
808080 |
| d.u.image.title = x->title; d.u.image.title_len = x->tlen; |
| 1592 |
808080 |
| d.u.image.alt = NULL; d.u.image.alt_len = 0; /* renderer derives alt from children */ |
| 1593 |
808080 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_IMAGE, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_IMAGE, &d); |
808080 |
0 |
808080 |
|
| 1594 |
808080 |
| emit_children(z, x->children); |
| 1595 |
808080 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_IMAGE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_IMAGE); |
808080 |
0 |
808080 |
|
| 1596 |
808080 |
| break; |
| 1597 |
808024 |
| case N_FOOTNOTE_REF: |
| 1598 |
|
| /* opener->href / hlen carry the raw label captured at match |
| 1599 |
|
| * time; we stuffed them there because inode has no dedicated |
| 1600 |
|
| * label slot. The renderer manages numbering. */ |
| 1601 |
808024 |
| d.u.footnote_ref.label = x->href; |
| 1602 |
808024 |
| d.u.footnote_ref.label_len = x->hlen; |
| 1603 |
808024 |
▶ | if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_FOOTNOTE_REF, &d); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->enter_inline) cb->enter_inline(z->ctx->ud, MDS_INL_FOOTNOTE_REF, &d); |
808024 |
0 |
808024 |
|
| 1604 |
808024 |
▶ | if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_FOOTNOTE_REF); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cb->leave_inline) cb->leave_inline(z->ctx->ud, MDS_INL_FOOTNOTE_REF); |
808024 |
0 |
808024 |
|
| 1605 |
808024 |
| break; |
| 1606 |
72 |
| case N_DELIM: |
| 1607 |
|
| case N_OPEN_BRACKET: |
| 1608 |
|
| case N_OPEN_BANG: |
| 1609 |
|
| /* leftover unmatched delimiter/bracket — emit as literal text */ |
| 1610 |
72 |
| emit_text(z, x->s, x->n); |
| 1611 |
72 |
| break; |
| 1612 |
|
| } |
| 1613 |
8873351 |
| } |
| 1614 |
|
| |
| 1615 |
4062950 |
| static void emit_children(scn* z, inode* head) { |
| 1616 |
|
| inode* p; |
| 1617 |
12936301 |
▶ | for (p = head; p; p = p->next) emit_node(z, p); |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = head; p; p = p->next) emit_node(z, p); |
8873351 |
4062950 |
12936301 |
|
| 1618 |
4062950 |
| } |
| 1619 |
|
| |
| 1620 |
|
| /* ---------------- public entry ---------------- */ |
| 1621 |
|
| |
| 1622 |
4103837 |
| MDS_HOT void mds_inline_scan(mds_ctx* ctx, const char* s, size_t n) { |
| 1623 |
|
| scn z; |
| 1624 |
5761757 |
▶ | if (n == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return; |
0 |
4103837 |
4103837 |
|
| 1625 |
|
| |
| 1626 |
|
| /* Fast path for table cells and trivial paragraphs: if no byte in |
| 1627 |
|
| * the run can possibly trigger an inline construct, we can skip |
| 1628 |
|
| * the entire inode-list build / process_emphasis / emit_children |
| 1629 |
|
| * pipeline and just call cb.text directly. Inline triggers are: |
| 1630 |
|
| * `* _ ~ ` [ ] ! < & \\` plus the line-break candidates `\n` and |
| 1631 |
|
| * the trailing-spaces hard-break case. The classifier dispatch |
| 1632 |
|
| * table (src/simd/mds_dispatch.h) is the authoritative list; we |
| 1633 |
|
| * use a small per-call SWAR-style scalar scan rather than the SIMD |
| 1634 |
|
| * classifier so this stays cheap for short cell-sized runs. |
| 1635 |
|
| * |
| 1636 |
|
| * Tables hit this constantly (cells are typically a single word), |
| 1637 |
|
| * and ordinary prose paragraphs hit it for runs between inline |
| 1638 |
|
| * markers. The slow path is bit-identical to the original code. */ |
| 1639 |
|
| { |
| 1640 |
4103837 |
| const unsigned char* p = (const unsigned char*)s; |
| 1641 |
4103837 |
| const unsigned char* end = p + n; |
| 1642 |
56516243 |
▶ | for (; p < end; p++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; p < end; p++) { |
54858323 |
1657920 |
56516243 |
|
| 1643 |
54858323 |
| unsigned char c = *p; |
| 1644 |
|
| /* Bucket the trigger set with a small bitmap-style check. |
| 1645 |
|
| * The compiler turns this into a branchless OR-chain. */ |
| 1646 |
54858323 |
▶ | if (c == '*' || c == '_' || c == '~' || c == '`' || |
Branch: 5 branches
| Branch | True | False | Total |
| if (c == '*' || c == '_' || c == '~' || c == '`' || |
54858010 |
313 |
54858323 |
| if (c == '*' || c == '_' || c == '~' || c == '`' || |
54857781 |
229 |
54858010 |
| if (c == '*' || c == '_' || c == '~' || c == '`' || |
54857748 |
33 |
54857781 |
| if (c == '*' || c == '_' || c == '~' || c == '`' || |
54857632 |
116 |
54857748 |
| if (c == '*' || c == '_' || c == '~' || c == '`' || |
53645155 |
1212477 |
54857632 |
|
| 1647 |
53645155 |
▶ | c == '[' || c == ']' || c == '!' || c == '<' || |
Branch: 4 branches
| Branch | True | False | Total |
| c == '[' || c == ']' || c == '!' || c == '<' || |
53645155 |
0 |
53645155 |
| c == '[' || c == ']' || c == '!' || c == '<' || |
52837060 |
808095 |
53645155 |
| c == '[' || c == ']' || c == '!' || c == '<' || |
52836896 |
164 |
52837060 |
| c == '[' || c == ']' || c == '!' || c == '<' || |
52836863 |
33 |
52836896 |
|
| 1648 |
52836863 |
▶ | c == '&' || c == '\\' || c == '\n') break; |
Branch: 2 branches
| Branch | True | False | Total |
| c == '&' || c == '\\' || c == '\n') break; |
52836777 |
86 |
52836863 |
| c == '&' || c == '\\' || c == '\n') break; |
52412406 |
424371 |
52836777 |
|
| 1649 |
|
| } |
| 1650 |
4103837 |
▶ | if (MDS_LIKELY(p == end)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (MDS_LIKELY(p == end)) { |
1657920 |
2445917 |
4103837 |
|
| 1651 |
|
| /* Also bail out on trailing spaces, which CommonMark would |
| 1652 |
|
| * otherwise turn into a hard-break candidate. Table cells |
| 1653 |
|
| * never have them (the splitter trims) and most paragraph |
| 1654 |
|
| * runs don't either. */ |
| 1655 |
1657920 |
▶ | if (n < 2 || !(s[n-1] == ' ' && s[n-2] == ' ')) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (n < 2 || !(s[n-1] == ' ' && s[n-2] == ' ')) { |
1657752 |
168 |
1657920 |
| if (n < 2 || !(s[n-1] == ' ' && s[n-2] == ' ')) { |
0 |
1657752 |
1657752 |
| if (n < 2 || !(s[n-1] == ' ' && s[n-2] == ' ')) { |
0 |
0 |
0 |
|
| 1656 |
1657920 |
▶ | if (ctx->cb.text) ctx->cb.text(ctx->ud, s, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->cb.text) ctx->cb.text(ctx->ud, s, n); |
1657920 |
0 |
1657920 |
|
| 1657 |
1657920 |
| return; |
| 1658 |
|
| } |
| 1659 |
|
| } |
| 1660 |
|
| } |
| 1661 |
|
| |
| 1662 |
2445917 |
| byteclass_init(); |
| 1663 |
2445917 |
| memset(&z, 0, sizeof z); |
| 1664 |
2445917 |
| z.ctx = ctx; |
| 1665 |
2445917 |
| z.s = s; |
| 1666 |
2445917 |
| z.n = n; |
| 1667 |
|
| |
| 1668 |
2445917 |
| scan_forward(&z); |
| 1669 |
2445917 |
| process_emphasis(&z, NULL); |
| 1670 |
2445917 |
| emit_children(&z, z.head); |
| 1671 |
|
| } |
| 1672 |
|
| |