| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| /* mPUSHs landed after 5.10.0 (the mortal-push macros arrived piecemeal), |
| 6 |
|
| * so on 5.10.0 the .so fails to load with "undefined symbol: mPUSHs" - the |
| 7 |
|
| * compiler took the undefined macro for an implicit external call. Every use |
| 8 |
|
| * here is already guarded by an EXTEND, so this literal expansion is exact. */ |
| 9 |
|
| #ifndef mPUSHs |
| 10 |
|
| # define mPUSHs(s) PUSHs(sv_2mortal(s)) |
| 11 |
|
| #endif |
| 12 |
|
| |
| 13 |
|
| /* ------------------------------------------------------------------ */ |
| 14 |
|
| /* Unity build: the parser sources live in src/ and are pulled in below |
| 15 |
|
| * so the per-xs Makefile doesn't need extra OBJECT entries. Order |
| 16 |
|
| * matters: headers first, then .c bodies. All symbols here are static |
| 17 |
|
| * to the compilation unit except mds_render_html_to_sv, which the XS |
| 18 |
|
| * glue at the bottom of this file calls. |
| 19 |
|
| */ |
| 20 |
|
| #define MDS_UNITY_BUILD 1 |
| 21 |
|
| #include "src/mds_arena.c" |
| 22 |
|
| #include "src/mds_buf.c" |
| 23 |
|
| #include "src/mds_linkref.c" |
| 24 |
|
| #include "src/mds_footnote.c" |
| 25 |
|
| #include "src/mds_block.c" |
| 26 |
|
| #include "src/mds_inline.c" |
| 27 |
|
| #include "src/mds_render_html.c" |
| 28 |
|
| #include "src/mds_gfm.c" |
| 29 |
|
| #include "src/mds.c" |
| 30 |
|
| |
| 31 |
|
| /* SIMD foundation: always build the scalar + dispatch units. The |
| 32 |
|
| * AVX2/SSE2/NEON files compile only when their feature macro is set. */ |
| 33 |
|
| #include "src/simd/mds_simd_scalar.c" |
| 34 |
|
| #ifdef MDS_HAVE_SSE2 |
| 35 |
|
| # include "src/simd/mds_simd_sse2.c" |
| 36 |
|
| #endif |
| 37 |
|
| #ifdef MDS_HAVE_AVX2 |
| 38 |
|
| # include "src/simd/mds_simd_avx2.c" |
| 39 |
|
| #endif |
| 40 |
|
| #ifdef MDS_HAVE_NEON |
| 41 |
|
| # include "src/simd/mds_simd_neon.c" |
| 42 |
|
| #endif |
| 43 |
|
| #include "src/simd/mds_simd_dispatch.c" |
| 44 |
|
| #include "src/simd/mds_dispatch.c" |
| 45 |
|
| /* ------------------------------------------------------------------ */ |
| 46 |
|
| |
| 47 |
|
| /* Special-byte table used by strip_markdown_except_lists_tables to skip |
| 48 |
|
| * over runs of ordinary text quickly. Bytes here are the ones that can |
| 49 |
|
| * START a markdown construct in that pass. */ |
| 50 |
|
| static unsigned char mds_special[256]; |
| 51 |
|
| static int mds_special_init = 0; |
| 52 |
10 |
| static void mds_build_special(void) { |
| 53 |
10 |
▶ | if (mds_special_init) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (mds_special_init) return; |
7 |
3 |
10 |
|
| 54 |
3 |
| mds_special[(unsigned char)'#'] = 1; |
| 55 |
3 |
| mds_special[(unsigned char)'`'] = 1; |
| 56 |
3 |
| mds_special[(unsigned char)'*'] = 1; |
| 57 |
3 |
| mds_special[(unsigned char)'_'] = 1; |
| 58 |
3 |
| mds_special[(unsigned char)'~'] = 1; |
| 59 |
3 |
| mds_special[(unsigned char)'['] = 1; |
| 60 |
3 |
| mds_special[(unsigned char)'!'] = 1; |
| 61 |
3 |
| mds_special[(unsigned char)'|'] = 1; |
| 62 |
3 |
| mds_special[(unsigned char)'\n'] = 1; |
| 63 |
3 |
| mds_special[(unsigned char)'\r'] = 1; |
| 64 |
3 |
| mds_special_init = 1; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
10 |
| static SV* strip_markdown_except_lists_tables(const char* input) { |
| 68 |
10 |
| SV* out = newSVpv("", 0); |
| 69 |
10 |
| const char* p = input; |
| 70 |
|
| |
| 71 |
10 |
| mds_build_special(); |
| 72 |
|
| |
| 73 |
131 |
▶ | while (*p) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*p) { |
121 |
10 |
131 |
|
| 74 |
|
| // Unordered lists: keep marker, remove space |
| 75 |
121 |
▶ | if ((p == input || *(p-1) == '\n') && (*p == '-' || *p == '*' || *p == '+') && *(p+1) == ' ') { |
Branch: 6 branches
| Branch | True | False | Total |
| if ((p == input || *(p-1) == '\n') && (*p == '-' || *p == '*' || *p == '+') && *(p+1) == ' ') { |
111 |
10 |
121 |
| if ((p == input || *(p-1) == '\n') && (*p == '-' || *p == '*' || *p == '+') && *(p+1) == ' ') { |
22 |
89 |
111 |
| if ((p == input || *(p-1) == '\n') && (*p == '-' || *p == '*' || *p == '+') && *(p+1) == ' ') { |
30 |
2 |
32 |
| if ((p == input || *(p-1) == '\n') && (*p == '-' || *p == '*' || *p == '+') && *(p+1) == ' ') { |
27 |
3 |
30 |
| if ((p == input || *(p-1) == '\n') && (*p == '-' || *p == '*' || *p == '+') && *(p+1) == ' ') { |
0 |
27 |
27 |
| if ((p == input || *(p-1) == '\n') && (*p == '-' || *p == '*' || *p == '+') && *(p+1) == ' ') { |
4 |
1 |
5 |
|
| 76 |
4 |
| sv_catpvn(out, p, 1); |
| 77 |
4 |
| sv_catpvn(out, " ", 1); // Keep space after marker |
| 78 |
4 |
| p += 2; |
| 79 |
|
| // Task list [ ] or [x] immediately after bullet — strip the box. |
| 80 |
4 |
▶ | if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
Branch: 5 branches
| Branch | True | False | Total |
| if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
2 |
2 |
4 |
| if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
1 |
1 |
2 |
| if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
0 |
1 |
1 |
| if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
0 |
0 |
0 |
| if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
2 |
0 |
2 |
|
| 81 |
2 |
| p += 3; |
| 82 |
2 |
▶ | if (*p == ' ') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ' ') p++; |
2 |
0 |
2 |
|
| 83 |
|
| } |
| 84 |
4 |
| continue; |
| 85 |
|
| } |
| 86 |
|
| // Tables: just copy everything (including pipes and dashes) |
| 87 |
117 |
▶ | if (*p == '|') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '|') { |
12 |
105 |
117 |
|
| 88 |
12 |
| sv_catpvn(out, p, 1); |
| 89 |
12 |
| p++; |
| 90 |
12 |
| continue; |
| 91 |
|
| } |
| 92 |
|
| // Table separator row (---): just copy dashes and pipes |
| 93 |
105 |
▶ | if (*p == '-' && ((p > input && *(p-1) == '|') || (p == input))) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (*p == '-' && ((p > input && *(p-1) == '|') || (p == input))) { |
15 |
90 |
105 |
| if (*p == '-' && ((p > input && *(p-1) == '|') || (p == input))) { |
15 |
0 |
15 |
| if (*p == '-' && ((p > input && *(p-1) == '|') || (p == input))) { |
12 |
3 |
15 |
| if (*p == '-' && ((p > input && *(p-1) == '|') || (p == input))) { |
0 |
12 |
12 |
|
| 94 |
3 |
| sv_catpvn(out, p, 1); |
| 95 |
3 |
| p++; |
| 96 |
3 |
| continue; |
| 97 |
|
| } |
| 98 |
|
| // Remove bold (** or __) |
| 99 |
102 |
▶ | if ((*p == '*' && *(p+1) == '*') || (*p == '_' && *(p+1) == '_')) { |
Branch: 4 branches
| Branch | True | False | Total |
| if ((*p == '*' && *(p+1) == '*') || (*p == '_' && *(p+1) == '_')) { |
6 |
96 |
102 |
| if ((*p == '*' && *(p+1) == '*') || (*p == '_' && *(p+1) == '_')) { |
0 |
6 |
6 |
| if ((*p == '*' && *(p+1) == '*') || (*p == '_' && *(p+1) == '_')) { |
10 |
86 |
96 |
| if ((*p == '*' && *(p+1) == '*') || (*p == '_' && *(p+1) == '_')) { |
6 |
4 |
10 |
|
| 100 |
12 |
| p += 2; |
| 101 |
12 |
| continue; |
| 102 |
|
| } |
| 103 |
|
| // Remove italic (* or _) |
| 104 |
90 |
▶ | if (*p == '*' || *p == '_') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '*' || *p == '_') { |
90 |
0 |
90 |
| if (*p == '*' || *p == '_') { |
4 |
86 |
90 |
|
| 105 |
4 |
| p++; |
| 106 |
4 |
| continue; |
| 107 |
|
| } |
| 108 |
|
| // Remove strikethrough (~~) |
| 109 |
86 |
▶ | if (*p == '~' && *(p+1) == '~') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '~' && *(p+1) == '~') { |
0 |
86 |
86 |
| if (*p == '~' && *(p+1) == '~') { |
0 |
0 |
0 |
|
| 110 |
0 |
| p += 2; |
| 111 |
0 |
| continue; |
| 112 |
|
| } |
| 113 |
|
| // Remove inline code (`) and fenced code (```) — keep content, drop fences |
| 114 |
86 |
▶ | if (*p == '`') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '`') { |
2 |
84 |
86 |
|
| 115 |
3 |
▶ | if (*(p+1) == '`' && *(p+2) == '`') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*(p+1) == '`' && *(p+2) == '`') { |
1 |
1 |
2 |
| if (*(p+1) == '`' && *(p+2) == '`') { |
1 |
0 |
1 |
|
| 116 |
|
| // Fenced block: drop the opening fence line entirely |
| 117 |
|
| // (but preserve one newline so surrounding text stays |
| 118 |
|
| // on its own line). |
| 119 |
|
| const char* fence; |
| 120 |
|
| const char* body_end; |
| 121 |
1 |
| p += 3; |
| 122 |
1 |
▶ | while (*p && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p && *p != '\n') p++; |
1 |
0 |
1 |
| while (*p && *p != '\n') p++; |
0 |
1 |
1 |
|
| 123 |
|
| // keep the '\n' that terminated the fence line in output |
| 124 |
1 |
▶ | if (*p == '\n') { sv_catpvn(out, "\n", 1); p++; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '\n') { sv_catpvn(out, "\n", 1); p++; } |
1 |
0 |
1 |
|
| 125 |
1 |
| fence = strstr(p, "```"); |
| 126 |
1 |
▶ | body_end = fence ? fence : p + strlen(p); |
Branch: 1 branch
| Branch | True | False | Total |
| body_end = fence ? fence : p + strlen(p); |
0 |
1 |
1 |
|
| 127 |
1 |
▶ | if (body_end > p) |
Branch: 1 branch
| Branch | True | False | Total |
| if (body_end > p) |
1 |
0 |
1 |
|
| 128 |
1 |
| sv_catpvn(out, p, (STRLEN)(body_end - p)); |
| 129 |
1 |
▶ | if (fence) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fence) { |
1 |
0 |
1 |
|
| 130 |
1 |
| p = fence + 3; |
| 131 |
|
| // drop the rest of the closing fence line |
| 132 |
1 |
▶ | while (*p && *p != '\n') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p && *p != '\n') p++; |
1 |
0 |
1 |
| while (*p && *p != '\n') p++; |
0 |
1 |
1 |
|
| 133 |
|
| } else { |
| 134 |
0 |
| p = body_end; |
| 135 |
|
| } |
| 136 |
|
| } else { |
| 137 |
|
| const char* code_start; |
| 138 |
1 |
| p++; |
| 139 |
1 |
| code_start = p; |
| 140 |
6 |
▶ | while (*p && *p != '`') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p && *p != '`') p++; |
6 |
0 |
6 |
| while (*p && *p != '`') p++; |
5 |
1 |
6 |
|
| 141 |
1 |
▶ | if (p > code_start) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p > code_start) |
1 |
0 |
1 |
|
| 142 |
1 |
| sv_catpvn(out, code_start, (STRLEN)(p - code_start)); |
| 143 |
1 |
▶ | if (*p == '`') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '`') p++; |
1 |
0 |
1 |
|
| 144 |
|
| } |
| 145 |
2 |
| continue; |
| 146 |
|
| } |
| 147 |
|
| // Remove images  |
| 148 |
84 |
▶ | if (*p == '!' && *(p+1) == '[') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*p == '!' && *(p+1) == '[') { |
1 |
83 |
84 |
| if (*p == '!' && *(p+1) == '[') { |
1 |
0 |
1 |
|
| 149 |
1 |
| p += 2; |
| 150 |
2 |
▶ | while (*p && *p != ']') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p && *p != ']') p++; |
2 |
0 |
2 |
| while (*p && *p != ']') p++; |
1 |
1 |
2 |
|
| 151 |
1 |
▶ | if (*p == ']') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ']') p++; |
1 |
0 |
1 |
|
| 152 |
1 |
▶ | if (*p == '(') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '(') { |
1 |
0 |
1 |
|
| 153 |
1 |
| p++; |
| 154 |
6 |
▶ | while (*p && *p != ')') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p && *p != ')') p++; |
6 |
0 |
6 |
| while (*p && *p != ')') p++; |
5 |
1 |
6 |
|
| 155 |
1 |
▶ | if (*p == ')') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ')') p++; |
1 |
0 |
1 |
|
| 156 |
|
| } |
| 157 |
1 |
| continue; |
| 158 |
|
| } |
| 159 |
|
| // Remove links [text](url), keep text |
| 160 |
83 |
▶ | if (*p == '[') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '[') { |
1 |
82 |
83 |
|
| 161 |
|
| const char* text_start; |
| 162 |
|
| int text_len; |
| 163 |
1 |
| p++; |
| 164 |
1 |
| text_start = p; |
| 165 |
11 |
▶ | while (*p && *p != ']') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p && *p != ']') p++; |
11 |
0 |
11 |
| while (*p && *p != ']') p++; |
10 |
1 |
11 |
|
| 166 |
1 |
| text_len = (int)(p - text_start); |
| 167 |
1 |
▶ | if (text_len > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (text_len > 0) |
1 |
0 |
1 |
|
| 168 |
1 |
| sv_catpvn(out, text_start, text_len); |
| 169 |
1 |
▶ | if (*p == ']') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ']') p++; |
1 |
0 |
1 |
|
| 170 |
1 |
▶ | if (*p == '(') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '(') { |
1 |
0 |
1 |
|
| 171 |
1 |
| p++; |
| 172 |
9 |
▶ | while (*p && *p != ')') p++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (*p && *p != ')') p++; |
9 |
0 |
9 |
| while (*p && *p != ')') p++; |
8 |
1 |
9 |
|
| 173 |
1 |
▶ | if (*p == ')') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ')') p++; |
1 |
0 |
1 |
|
| 174 |
|
| } |
| 175 |
1 |
| continue; |
| 176 |
|
| } |
| 177 |
|
| // Remove headers (#) |
| 178 |
82 |
▶ | if (*p == '#') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == '#') { |
6 |
76 |
82 |
|
| 179 |
13 |
▶ | while (*p == '#') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| while (*p == '#') p++; |
7 |
6 |
13 |
|
| 180 |
6 |
▶ | if (*p == ' ') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ' ') p++; |
6 |
0 |
6 |
|
| 181 |
6 |
| continue; |
| 182 |
|
| } |
| 183 |
|
| // Remove task list [ ] or [x] |
| 184 |
76 |
▶ | if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
Branch: 5 branches
| Branch | True | False | Total |
| if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
0 |
76 |
76 |
| if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
0 |
0 |
0 |
| if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
0 |
0 |
0 |
| if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
0 |
0 |
0 |
| if (*p == '[' && (*(p+1) == ' ' || *(p+1) == 'x' || *(p+1) == 'X') && *(p+2) == ']') { |
0 |
0 |
0 |
|
| 185 |
0 |
| p += 3; |
| 186 |
0 |
▶ | if (*p == ' ') p++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == ' ') p++; |
0 |
0 |
0 |
|
| 187 |
0 |
| continue; |
| 188 |
|
| } |
| 189 |
|
| // Default: emit a single special byte, OR batch a run of non-special. |
| 190 |
|
| /* Special set for strip is the markdown special set PLUS '|', '-', '+', |
| 191 |
|
| * and ASCII digits (all line-start-conditional in this function). */ |
| 192 |
76 |
▶ | if (mds_special[(unsigned char)*p] |
Branch: 1 branch
| Branch | True | False | Total |
| if (mds_special[(unsigned char)*p] |
50 |
26 |
76 |
|
| 193 |
50 |
▶ | || *p == '|' || *p == '-' || *p == '+' |
Branch: 3 branches
| Branch | True | False | Total |
| || *p == '|' || *p == '-' || *p == '+' |
50 |
0 |
50 |
| || *p == '|' || *p == '-' || *p == '+' |
38 |
12 |
50 |
| || *p == '|' || *p == '-' || *p == '+' |
38 |
0 |
38 |
|
| 194 |
38 |
▶ | || (*p >= '1' && *p <= '9')) { |
Branch: 2 branches
| Branch | True | False | Total |
| || (*p >= '1' && *p <= '9')) { |
30 |
8 |
38 |
| || (*p >= '1' && *p <= '9')) { |
4 |
26 |
30 |
|
| 195 |
42 |
| sv_catpvn(out, p, 1); |
| 196 |
42 |
| p++; |
| 197 |
|
| } else { |
| 198 |
34 |
| const char* run = p; |
| 199 |
160 |
▶ | do { p++; } while (*p && !mds_special[(unsigned char)*p] |
Branch: 1 branch
| Branch | True | False | Total |
| do { p++; } while (*p && !mds_special[(unsigned char)*p] |
134 |
26 |
160 |
|
| 200 |
134 |
▶ | && *p != '|' && *p != '-' && *p != '+' |
Branch: 3 branches
| Branch | True | False | Total |
| && *p != '|' && *p != '-' && *p != '+' |
134 |
0 |
134 |
| && *p != '|' && *p != '-' && *p != '+' |
134 |
0 |
134 |
| && *p != '|' && *p != '-' && *p != '+' |
134 |
0 |
134 |
|
| 201 |
300 |
▶ | && !(*p >= '1' && *p <= '9')); |
Branch: 3 branches
| Branch | True | False | Total |
| && !(*p >= '1' && *p <= '9')); |
160 |
6 |
166 |
| && !(*p >= '1' && *p <= '9')); |
22 |
112 |
134 |
| && !(*p >= '1' && *p <= '9')); |
110 |
2 |
112 |
|
| 202 |
34 |
| sv_catpvn(out, run, (STRLEN)(p - run)); |
| 203 |
|
| } |
| 204 |
|
| } |
| 205 |
10 |
| return out; |
| 206 |
|
| } |
| 207 |
|
| |
| 208 |
|
| /* Shared options-hash decoder used by both the procedural |
| 209 |
|
| * markdown_to_html entry point and the persistent session render path. |
| 210 |
|
| * GFM is the default; an `hv` of NULL returns the GFM preset unchanged. */ |
| 211 |
22495 |
| static unsigned mds_flags_from_hv(pTHX_ HV* h) { |
| 212 |
22495 |
| unsigned flags = MDS_FLAGS_GFM; |
| 213 |
|
| SV** v; |
| 214 |
22495 |
▶ | if (!h) return flags; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!h) return flags; |
172 |
22323 |
22495 |
|
| 215 |
22323 |
▶ | if ((v = hv_fetch(h, "gfm", 3, 0)) && !SvTRUE(*v)) flags = MDS_FLAGS_COMMONMARK; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "gfm", 3, 0)) && !SvTRUE(*v)) flags = MDS_FLAGS_COMMONMARK; |
2045 |
20278 |
22323 |
| if ((v = hv_fetch(h, "gfm", 3, 0)) && !SvTRUE(*v)) flags = MDS_FLAGS_COMMONMARK; |
1985 |
60 |
2045 |
|
| 216 |
22323 |
▶ | if ((v = hv_fetch(h, "tables", 6, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_TABLES) : (flags & ~MDS_FLAG_TABLES); |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "tables", 6, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_TABLES) : (flags & ~MDS_FLAG_TABLES); |
2 |
22321 |
22323 |
| if ((v = hv_fetch(h, "tables", 6, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_TABLES) : (flags & ~MDS_FLAG_TABLES); |
0 |
2 |
2 |
|
| 217 |
22323 |
▶ | if ((v = hv_fetch(h, "strikethrough", 13, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_STRIKE) : (flags & ~MDS_FLAG_STRIKE); |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "strikethrough", 13, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_STRIKE) : (flags & ~MDS_FLAG_STRIKE); |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "strikethrough", 13, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_STRIKE) : (flags & ~MDS_FLAG_STRIKE); |
0 |
1 |
1 |
|
| 218 |
22323 |
▶ | if ((v = hv_fetch(h, "tasklist", 8, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_TASKLIST) : (flags & ~MDS_FLAG_TASKLIST); |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "tasklist", 8, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_TASKLIST) : (flags & ~MDS_FLAG_TASKLIST); |
0 |
22323 |
22323 |
| if ((v = hv_fetch(h, "tasklist", 8, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_TASKLIST) : (flags & ~MDS_FLAG_TASKLIST); |
0 |
0 |
0 |
|
| 219 |
22323 |
▶ | if ((v = hv_fetch(h, "autolink", 8, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_AUTOLINK) : (flags & ~MDS_FLAG_AUTOLINK); |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "autolink", 8, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_AUTOLINK) : (flags & ~MDS_FLAG_AUTOLINK); |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "autolink", 8, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_AUTOLINK) : (flags & ~MDS_FLAG_AUTOLINK); |
0 |
1 |
1 |
|
| 220 |
22323 |
▶ | if ((v = hv_fetch(h, "disallow_raw_html", 17, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_DISALLOW_RAW_HTML) : (flags & ~MDS_FLAG_DISALLOW_RAW_HTML); |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "disallow_raw_html", 17, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_DISALLOW_RAW_HTML) : (flags & ~MDS_FLAG_DISALLOW_RAW_HTML); |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "disallow_raw_html", 17, 0))) flags = SvTRUE(*v) ? (flags | MDS_FLAG_DISALLOW_RAW_HTML) : (flags & ~MDS_FLAG_DISALLOW_RAW_HTML); |
0 |
1 |
1 |
|
| 221 |
22323 |
▶ | if ((v = hv_fetch(h, "hard_breaks", 11, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_HARD_BREAKS; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "hard_breaks", 11, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_HARD_BREAKS; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "hard_breaks", 11, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_HARD_BREAKS; |
1 |
0 |
1 |
|
| 222 |
22323 |
▶ | if ((v = hv_fetch(h, "unsafe", 6, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_UNSAFE; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "unsafe", 6, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_UNSAFE; |
2 |
22321 |
22323 |
| if ((v = hv_fetch(h, "unsafe", 6, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_UNSAFE; |
2 |
0 |
2 |
|
| 223 |
22323 |
▶ | if ((v = hv_fetch(h, "no_simd", 7, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_NO_SIMD; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "no_simd", 7, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_NO_SIMD; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "no_simd", 7, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_NO_SIMD; |
1 |
0 |
1 |
|
| 224 |
22323 |
▶ | if ((v = hv_fetch(h, "strict_utf8", 11, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_STRICT_UTF8; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "strict_utf8", 11, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_STRICT_UTF8; |
3 |
22320 |
22323 |
| if ((v = hv_fetch(h, "strict_utf8", 11, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_STRICT_UTF8; |
3 |
0 |
3 |
|
| 225 |
22323 |
▶ | if ((v = hv_fetch(h, "headers", 7, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_HEADINGS; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "headers", 7, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_HEADINGS; |
3 |
22320 |
22323 |
| if ((v = hv_fetch(h, "headers", 7, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_HEADINGS; |
3 |
0 |
3 |
|
| 226 |
22323 |
▶ | if ((v = hv_fetch(h, "italic", 6, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_EMPH; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "italic", 6, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_EMPH; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "italic", 6, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_EMPH; |
1 |
0 |
1 |
|
| 227 |
22323 |
▶ | if ((v = hv_fetch(h, "bold", 4, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_STRONG; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "bold", 4, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_STRONG; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "bold", 4, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_STRONG; |
1 |
0 |
1 |
|
| 228 |
22323 |
▶ | if ((v = hv_fetch(h, "code", 4, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_CODE; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "code", 4, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_CODE; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "code", 4, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_CODE; |
1 |
0 |
1 |
|
| 229 |
22323 |
▶ | if ((v = hv_fetch(h, "links", 5, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_LINKS; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "links", 5, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_LINKS; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "links", 5, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_LINKS; |
1 |
0 |
1 |
|
| 230 |
22323 |
▶ | if ((v = hv_fetch(h, "images", 6, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_IMAGES; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "images", 6, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_IMAGES; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "images", 6, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_IMAGES; |
1 |
0 |
1 |
|
| 231 |
22323 |
▶ | if ((v = hv_fetch(h, "ordered_lists", 13, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_ORDERED_LISTS; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "ordered_lists", 13, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_ORDERED_LISTS; |
2 |
22321 |
22323 |
| if ((v = hv_fetch(h, "ordered_lists", 13, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_ORDERED_LISTS; |
2 |
0 |
2 |
|
| 232 |
22323 |
▶ | if ((v = hv_fetch(h, "unordered_lists", 15, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_UNORDERED_LISTS; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "unordered_lists", 15, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_UNORDERED_LISTS; |
2 |
22321 |
22323 |
| if ((v = hv_fetch(h, "unordered_lists", 15, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_UNORDERED_LISTS; |
2 |
0 |
2 |
|
| 233 |
22323 |
▶ | if ((v = hv_fetch(h, "blockquote", 10, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_QUOTES; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "blockquote", 10, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_QUOTES; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "blockquote", 10, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_QUOTES; |
1 |
0 |
1 |
|
| 234 |
22323 |
▶ | if ((v = hv_fetch(h, "thematic_break", 14, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_THEMATIC_BREAK; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "thematic_break", 14, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_THEMATIC_BREAK; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "thematic_break", 14, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_THEMATIC_BREAK; |
1 |
0 |
1 |
|
| 235 |
22323 |
▶ | if ((v = hv_fetch(h, "fenced_code", 11, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_FENCED_CODE; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "fenced_code", 11, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_FENCED_CODE; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "fenced_code", 11, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_FENCED_CODE; |
1 |
0 |
1 |
|
| 236 |
22323 |
▶ | if ((v = hv_fetch(h, "indented_code", 13, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_INDENTED_CODE; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "indented_code", 13, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_INDENTED_CODE; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "indented_code", 13, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_INDENTED_CODE; |
1 |
0 |
1 |
|
| 237 |
22323 |
▶ | if ((v = hv_fetch(h, "html", 4, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_HTML; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "html", 4, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_HTML; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "html", 4, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_HTML; |
1 |
0 |
1 |
|
| 238 |
22323 |
▶ | if ((v = hv_fetch(h, "references", 10, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_REFERENCES; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "references", 10, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_REFERENCES; |
1 |
22322 |
22323 |
| if ((v = hv_fetch(h, "references", 10, 0)) && !SvTRUE(*v)) flags |= MDS_FLAG_NO_REFERENCES; |
1 |
0 |
1 |
|
| 239 |
22323 |
▶ | if ((v = hv_fetch(h, "highlight", 9, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_HIGHLIGHT; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "highlight", 9, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_HIGHLIGHT; |
20227 |
2096 |
22323 |
| if ((v = hv_fetch(h, "highlight", 9, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_HIGHLIGHT; |
20227 |
0 |
20227 |
|
| 240 |
22323 |
▶ | if ((v = hv_fetch(h, "heading_ids", 11, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_HEADING_IDS; |
Branch: 2 branches
| Branch | True | False | Total |
| if ((v = hv_fetch(h, "heading_ids", 11, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_HEADING_IDS; |
24 |
22299 |
22323 |
| if ((v = hv_fetch(h, "heading_ids", 11, 0)) && SvTRUE(*v)) flags |= MDS_FLAG_HEADING_IDS; |
24 |
0 |
24 |
|
| 241 |
22323 |
| return flags; |
| 242 |
|
| } |
| 243 |
|
| |
| 244 |
|
| /* Persistent session struct. The arena's head page is kept warm between |
| 245 |
|
| * render() calls by mds_arena_reset, eliminating the per-parse malloc |
| 246 |
|
| * for sub-page workloads, and the block-scanner scratch buffers are |
| 247 |
|
| * persisted alongside so realloc traffic is amortised. */ |
| 248 |
|
| typedef struct mds_session { |
| 249 |
|
| mds_arena arena; |
| 250 |
|
| mds_block_scratch scratch; |
| 251 |
|
| unsigned flags; |
| 252 |
|
| } mds_session; |
| 253 |
|
| |
| 254 |
|
| /* Magic glue: a Markdown::Simple object is a blessed SVRV whose IV slot |
| 255 |
|
| * holds an mds_session* pointer. The pointer is owned by an |
| 256 |
|
| * ext-magic record attached to that IV, so the session is released |
| 257 |
|
| * automatically when the SV is destroyed (covering scope exit, undef, |
| 258 |
|
| * and global destruction without a Perl-level DESTROY method). |
| 259 |
|
| */ |
| 260 |
25 |
| static int mds_session_mg_free(pTHX_ SV* sv, MAGIC* mg) { |
| 261 |
|
| mds_session* s; |
| 262 |
|
| PERL_UNUSED_ARG(sv); |
| 263 |
25 |
| s = (mds_session*)mg->mg_ptr; |
| 264 |
25 |
▶ | if (s) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s) { |
25 |
0 |
25 |
|
| 265 |
25 |
| mds_arena_free(&s->arena); |
| 266 |
25 |
| mds_block_scratch_free(&s->scratch); |
| 267 |
25 |
| free(s); |
| 268 |
25 |
| mg->mg_ptr = NULL; |
| 269 |
|
| } |
| 270 |
25 |
| return 0; |
| 271 |
|
| } |
| 272 |
|
| |
| 273 |
|
| static MGVTBL mds_session_mg_vtbl = { |
| 274 |
|
| NULL, /* get */ |
| 275 |
|
| NULL, /* set */ |
| 276 |
|
| NULL, /* len */ |
| 277 |
|
| NULL, /* clear */ |
| 278 |
|
| mds_session_mg_free, /* free */ |
| 279 |
|
| NULL, /* copy */ |
| 280 |
|
| NULL, /* dup */ |
| 281 |
|
| NULL /* local */ |
| 282 |
|
| }; |
| 283 |
|
| |
| 284 |
|
| /* Look up the session pointer from a blessed SVRV ($self). Croaks on |
| 285 |
|
| * mismatch so misuse fails loudly rather than dereferencing garbage. */ |
| 286 |
141636 |
| static mds_session* mds_session_from_self(pTHX_ SV* self, const char* who) { |
| 287 |
|
| SV* iv; |
| 288 |
|
| MAGIC* mg; |
| 289 |
141636 |
▶ | if (!self || !SvROK(self)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!self || !SvROK(self)) |
141636 |
0 |
141636 |
| if (!self || !SvROK(self)) |
0 |
141636 |
141636 |
|
| 290 |
0 |
| croak("%s: invalid invocant (expected a Markdown::Simple object)", who); |
| 291 |
141636 |
| iv = SvRV(self); |
| 292 |
141636 |
▶ | for (mg = SvMAGIC(iv); mg; mg = mg->mg_moremagic) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(iv); mg; mg = mg->mg_moremagic) { |
141636 |
0 |
141636 |
|
| 293 |
141636 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &mds_session_mg_vtbl) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &mds_session_mg_vtbl) |
141636 |
0 |
141636 |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &mds_session_mg_vtbl) |
141636 |
0 |
141636 |
|
| 294 |
141636 |
| return (mds_session*)mg->mg_ptr; |
| 295 |
|
| } |
| 296 |
0 |
| croak("%s: invocant has no Markdown::Simple session attached", who); |
| 297 |
|
| return NULL; /* not reached */ |
| 298 |
|
| } |
| 299 |
|
| |
| 300 |
|
| /* The shared C ABI. Must come after mds_session_mg_vtbl, mds_flags_from_hv |
| 301 |
|
| * and strip_markdown_except_lists_tables, which its wrappers reach directly. */ |
| 302 |
|
| #include "mds_abi_impl.h" |
| 303 |
|
| |
| 304 |
|
| MODULE = Markdown::Simple PACKAGE = Markdown::Simple |
| 305 |
|
| |
| 306 |
|
| SV* |
| 307 |
|
| strip_markdown(input) |
| 308 |
|
| const char* input |
| 309 |
|
| CODE: |
| 310 |
8 |
| RETVAL = strip_markdown_except_lists_tables(input); |
| 311 |
|
| OUTPUT: |
| 312 |
|
| RETVAL |
| 313 |
|
| |
| 314 |
|
| SV* |
| 315 |
|
| markdown_to_html(input, ...) |
| 316 |
|
| SV* input; |
| 317 |
|
| CODE: |
| 318 |
|
| { |
| 319 |
|
| STRLEN n; |
| 320 |
22465 |
| const char* s = SvPV(input, n); |
| 321 |
22465 |
| SV* out = newSVpv("", 0); |
| 322 |
22308 |
▶ | HV* h = (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| HV* h = (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) |
22308 |
0 |
22308 |
| HV* h = (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) |
22308 |
0 |
22308 |
| HV* h = (items > 1 && SvOK(ST(1)) && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) |
22308 |
0 |
22308 |
|
| 323 |
44773 |
▶ | ? (HV*)SvRV(ST(1)) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV*)SvRV(ST(1)) : NULL; |
22308 |
157 |
22465 |
|
| 324 |
22465 |
| unsigned flags = mds_flags_from_hv(aTHX_ h); |
| 325 |
22465 |
| mds_render_html_to_sv(aTHX_ s, n, flags, out); |
| 326 |
22465 |
▶ | if (SvCUR(out) == 0 && (flags & MDS_FLAG_STRICT_UTF8) && n) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvCUR(out) == 0 && (flags & MDS_FLAG_STRICT_UTF8) && n) { |
5 |
22460 |
22465 |
| if (SvCUR(out) == 0 && (flags & MDS_FLAG_STRICT_UTF8) && n) { |
1 |
4 |
5 |
| if (SvCUR(out) == 0 && (flags & MDS_FLAG_STRICT_UTF8) && n) { |
1 |
0 |
1 |
|
| 327 |
|
| /* Distinguish "empty input" from "rejected as malformed UTF-8". */ |
| 328 |
2 |
| const mds_simd_ops* ops = (flags & MDS_FLAG_NO_SIMD) |
| 329 |
1 |
▶ | ? mds_simd_ops_scalar() : mds_simd_get(); |
Branch: 1 branch
| Branch | True | False | Total |
| ? mds_simd_ops_scalar() : mds_simd_get(); |
0 |
1 |
1 |
|
| 330 |
1 |
▶ | if (!ops->validate_utf8(s, n)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ops->validate_utf8(s, n)) |
1 |
0 |
1 |
|
| 331 |
1 |
| croak("markdown_to_html: input is not valid UTF-8"); |
| 332 |
|
| } |
| 333 |
22464 |
| RETVAL = out; |
| 334 |
22464 |
▶ | if (SvUTF8(input)) SvUTF8_on(RETVAL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8(input)) SvUTF8_on(RETVAL); |
51 |
22413 |
22464 |
|
| 335 |
|
| } |
| 336 |
|
| OUTPUT: |
| 337 |
|
| RETVAL |
| 338 |
|
| |
| 339 |
|
| # ---- Persistent session (reusable arena) ------------------------------- |
| 340 |
|
| # Markdown::Simple->new(\%opts) -> blessed object owning a warm parser |
| 341 |
|
| # $self->render($markdown) -> SV with rendered HTML |
| 342 |
|
| # $self->flags -> integer flag mask (read-only) |
| 343 |
|
| # |
| 344 |
|
| # The session is released by SV magic when the object goes out of scope; |
| 345 |
|
| # no explicit DESTROY method is required. |
| 346 |
|
| |
| 347 |
|
| SV* |
| 348 |
|
| new(class, opts = NULL) |
| 349 |
|
| SV* class; |
| 350 |
|
| SV* opts; |
| 351 |
|
| PREINIT: |
| 352 |
25 |
▶ | HV* h = NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| HV* h = NULL; |
13 |
12 |
25 |
|
| 353 |
|
| HV* stash; |
| 354 |
|
| mds_session* s; |
| 355 |
|
| SV* iv; |
| 356 |
|
| SV* rv; |
| 357 |
|
| const char* klass; |
| 358 |
|
| CODE: |
| 359 |
|
| { |
| 360 |
25 |
▶ | if (opts && SvOK(opts)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (opts && SvOK(opts)) { |
12 |
13 |
25 |
| if (opts && SvOK(opts)) { |
12 |
0 |
12 |
|
| 361 |
12 |
▶ | if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) |
12 |
0 |
12 |
| if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) |
0 |
12 |
12 |
|
| 362 |
0 |
| croak("Markdown::Simple::new: options must be a HASH reference"); |
| 363 |
12 |
| h = (HV*)SvRV(opts); |
| 364 |
|
| } |
| 365 |
25 |
▶ | if (!SvOK(class)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(class)) |
0 |
25 |
25 |
|
| 366 |
0 |
| croak("Markdown::Simple::new: missing class name"); |
| 367 |
50 |
▶ | klass = SvROK(class) ? sv_reftype(SvRV(class), 1) : SvPV_nolen(class); |
Branch: 1 branch
| Branch | True | False | Total |
| klass = SvROK(class) ? sv_reftype(SvRV(class), 1) : SvPV_nolen(class); |
0 |
25 |
25 |
|
| 368 |
25 |
| stash = gv_stashpv(klass, GV_ADD); |
| 369 |
|
| |
| 370 |
25 |
| s = (mds_session*)malloc(sizeof(mds_session)); |
| 371 |
25 |
▶ | if (!s) croak("Markdown::Simple::new: out of memory"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) croak("Markdown::Simple::new: out of memory"); |
0 |
25 |
25 |
|
| 372 |
25 |
| mds_arena_init(&s->arena); |
| 373 |
25 |
| memset(&s->scratch, 0, sizeof s->scratch); |
| 374 |
25 |
| s->flags = mds_flags_from_hv(aTHX_ h); |
| 375 |
|
| |
| 376 |
25 |
| iv = newSViv(0); |
| 377 |
25 |
| sv_magicext(iv, NULL, PERL_MAGIC_ext, &mds_session_mg_vtbl, (const char*)s, 0); |
| 378 |
25 |
| rv = newRV_noinc(iv); |
| 379 |
25 |
| sv_bless(rv, stash); |
| 380 |
25 |
| RETVAL = rv; |
| 381 |
|
| } |
| 382 |
|
| OUTPUT: |
| 383 |
|
| RETVAL |
| 384 |
|
| |
| 385 |
|
| SV* |
| 386 |
|
| render(self, input) |
| 387 |
|
| SV* self; |
| 388 |
|
| SV* input; |
| 389 |
|
| PREINIT: |
| 390 |
|
| mds_session* s; |
| 391 |
|
| STRLEN n; |
| 392 |
|
| const char* in; |
| 393 |
|
| SV* out; |
| 394 |
|
| CODE: |
| 395 |
|
| { |
| 396 |
121425 |
| s = mds_session_from_self(aTHX_ self, "Markdown::Simple::render"); |
| 397 |
242850 |
▶ | in = SvOK(input) ? SvPV(input, n) : (n = 0, ""); |
Branch: 1 branch
| Branch | True | False | Total |
| in = SvOK(input) ? SvPV(input, n) : (n = 0, ""); |
121425 |
0 |
121425 |
|
| 398 |
121425 |
| out = newSVpv("", 0); |
| 399 |
121425 |
| mds_render_html_to_sv_ex(aTHX_ in, n, s->flags, out, &s->arena, &s->scratch); |
| 400 |
121425 |
▶ | if (SvCUR(out) == 0 && (s->flags & MDS_FLAG_STRICT_UTF8) && n) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvCUR(out) == 0 && (s->flags & MDS_FLAG_STRICT_UTF8) && n) { |
1 |
121424 |
121425 |
| if (SvCUR(out) == 0 && (s->flags & MDS_FLAG_STRICT_UTF8) && n) { |
0 |
1 |
1 |
| if (SvCUR(out) == 0 && (s->flags & MDS_FLAG_STRICT_UTF8) && n) { |
0 |
0 |
0 |
|
| 401 |
0 |
| const mds_simd_ops* ops = (s->flags & MDS_FLAG_NO_SIMD) |
| 402 |
0 |
▶ | ? mds_simd_ops_scalar() : mds_simd_get(); |
Branch: 1 branch
| Branch | True | False | Total |
| ? mds_simd_ops_scalar() : mds_simd_get(); |
0 |
0 |
0 |
|
| 403 |
0 |
▶ | if (!ops->validate_utf8(in, n)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ops->validate_utf8(in, n)) |
0 |
0 |
0 |
|
| 404 |
0 |
| croak("render: input is not valid UTF-8"); |
| 405 |
|
| } |
| 406 |
121425 |
| RETVAL = out; |
| 407 |
121425 |
▶ | if (SvUTF8(input)) SvUTF8_on(RETVAL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8(input)) SvUTF8_on(RETVAL); |
0 |
121425 |
121425 |
|
| 408 |
|
| } |
| 409 |
|
| OUTPUT: |
| 410 |
|
| RETVAL |
| 411 |
|
| |
| 412 |
|
| # Render, and hand back the heading list alongside the HTML. Returns |
| 413 |
|
| # ($html, \@headings) where each heading is { level, text, id }; the ids are |
| 414 |
|
| # the ones actually emitted on the <hN> tags, so a caller can build a table |
| 415 |
|
| # of contents that links into the document without re-deriving anything. |
| 416 |
|
| # |
| 417 |
|
| # Asking for the list turns heading ids on for this render regardless of the |
| 418 |
|
| # session's own flags. |
| 419 |
|
| void |
| 420 |
|
| render_with_toc(self, input) |
| 421 |
|
| SV* self; |
| 422 |
|
| SV* input; |
| 423 |
|
| PREINIT: |
| 424 |
|
| mds_session* s; |
| 425 |
|
| STRLEN n; |
| 426 |
|
| const char* in; |
| 427 |
|
| SV* out; |
| 428 |
|
| AV* toc; |
| 429 |
|
| PPCODE: |
| 430 |
|
| { |
| 431 |
20211 |
| s = mds_session_from_self(aTHX_ self, "Markdown::Simple::render_with_toc"); |
| 432 |
40422 |
▶ | in = SvOK(input) ? SvPV(input, n) : (n = 0, ""); |
Branch: 1 branch
| Branch | True | False | Total |
| in = SvOK(input) ? SvPV(input, n) : (n = 0, ""); |
20211 |
0 |
20211 |
|
| 433 |
20211 |
| out = newSVpv("", 0); |
| 434 |
20211 |
| toc = newAV(); |
| 435 |
20211 |
| mds_render_html_to_sv_toc(aTHX_ in, n, s->flags, out, |
| 436 |
|
| &s->arena, &s->scratch, toc); |
| 437 |
20211 |
▶ | if (SvCUR(out) == 0 && (s->flags & MDS_FLAG_STRICT_UTF8) && n) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvCUR(out) == 0 && (s->flags & MDS_FLAG_STRICT_UTF8) && n) { |
1 |
20210 |
20211 |
| if (SvCUR(out) == 0 && (s->flags & MDS_FLAG_STRICT_UTF8) && n) { |
0 |
1 |
1 |
| if (SvCUR(out) == 0 && (s->flags & MDS_FLAG_STRICT_UTF8) && n) { |
0 |
0 |
0 |
|
| 438 |
0 |
| const mds_simd_ops* ops = (s->flags & MDS_FLAG_NO_SIMD) |
| 439 |
0 |
▶ | ? mds_simd_ops_scalar() : mds_simd_get(); |
Branch: 1 branch
| Branch | True | False | Total |
| ? mds_simd_ops_scalar() : mds_simd_get(); |
0 |
0 |
0 |
|
| 440 |
0 |
▶ | if (!ops->validate_utf8(in, n)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ops->validate_utf8(in, n)) { |
0 |
0 |
0 |
|
| 441 |
0 |
| SvREFCNT_dec(out); |
| 442 |
0 |
| SvREFCNT_dec((SV*)toc); |
| 443 |
0 |
| croak("render_with_toc: input is not valid UTF-8"); |
| 444 |
|
| } |
| 445 |
|
| } |
| 446 |
|
| /* The renderer emits raw bytes throughout and leaves the character |
| 447 |
|
| * question to us, so propagate the input's flag to everything we |
| 448 |
|
| * hand back rather than letting the HTML and the heading text |
| 449 |
|
| * disagree about their encoding. */ |
| 450 |
20211 |
▶ | if (SvUTF8(input)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8(input)) { |
1 |
20210 |
20211 |
|
| 451 |
1 |
| SSize_t i, top = av_len(toc); |
| 452 |
1 |
| SvUTF8_on(out); |
| 453 |
2 |
▶ | for (i = 0; i <= top; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= top; i++) { |
1 |
1 |
2 |
|
| 454 |
1 |
| SV** e = av_fetch(toc, i, 0); |
| 455 |
|
| HV* h; |
| 456 |
|
| SV** f; |
| 457 |
1 |
▶ | if (!e || !*e || !SvROK(*e)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!e || !*e || !SvROK(*e)) continue; |
1 |
0 |
1 |
| if (!e || !*e || !SvROK(*e)) continue; |
1 |
0 |
1 |
| if (!e || !*e || !SvROK(*e)) continue; |
0 |
1 |
1 |
|
| 458 |
1 |
| h = (HV*)SvRV(*e); |
| 459 |
1 |
▶ | if ((f = hv_fetchs(h, "text", 0)) && *f) SvUTF8_on(*f); |
Branch: 2 branches
| Branch | True | False | Total |
| if ((f = hv_fetchs(h, "text", 0)) && *f) SvUTF8_on(*f); |
1 |
0 |
1 |
| if ((f = hv_fetchs(h, "text", 0)) && *f) SvUTF8_on(*f); |
1 |
0 |
1 |
|
| 460 |
1 |
▶ | if ((f = hv_fetchs(h, "id", 0)) && *f) SvUTF8_on(*f); |
Branch: 2 branches
| Branch | True | False | Total |
| if ((f = hv_fetchs(h, "id", 0)) && *f) SvUTF8_on(*f); |
1 |
0 |
1 |
| if ((f = hv_fetchs(h, "id", 0)) && *f) SvUTF8_on(*f); |
1 |
0 |
1 |
|
| 461 |
|
| } |
| 462 |
|
| } |
| 463 |
20211 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
20211 |
20211 |
|
| 464 |
20211 |
| PUSHs(sv_2mortal(out)); |
| 465 |
20211 |
| PUSHs(sv_2mortal(newRV_noinc((SV*)toc))); |
| 466 |
|
| } |
| 467 |
|
| |
| 468 |
|
| UV |
| 469 |
|
| flags(self) |
| 470 |
|
| SV* self; |
| 471 |
|
| CODE: |
| 472 |
|
| { |
| 473 |
0 |
| mds_session* s = mds_session_from_self(aTHX_ self, "Markdown::Simple::flags"); |
| 474 |
0 |
▶ | RETVAL = (UV)s->flags; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)s->flags; |
0 |
0 |
0 |
|
| 475 |
|
| } |
| 476 |
|
| OUTPUT: |
| 477 |
|
| RETVAL |
| 478 |
|
| |
| 479 |
|
| # ---- shared C ABI -------------------------------------------------------- |
| 480 |
|
| |
| 481 |
|
| # Address of Markdown::Simple's own C ABI table (mds_abi.h). A consumer XS |
| 482 |
|
| # module (Punk's markdown mount) fetches this once at boot, INT2PTRs it to a |
| 483 |
|
| # `const mds_abi *`, and checks ->abi_version before using it. Not part of the |
| 484 |
|
| # public Perl API. |
| 485 |
|
| # An address is unsigned: PTR2IV would hand back a NEGATIVE integer wherever |
| 486 |
|
| # the shared object maps with the top bit set (Solaris/illumos amd64 loads |
| 487 |
|
| # libraries high, a 32-bit address in the upper half does the same), and the |
| 488 |
|
| # `> 0` sanity check downstream would fail on a pointer that is otherwise |
| 489 |
|
| # perfectly usable. PTR2UV keeps it a large positive integer everywhere. |
| 490 |
|
| UV |
| 491 |
|
| _abi_ptr() |
| 492 |
|
| CODE: |
| 493 |
13 |
▶ | RETVAL = PTR2UV(&MDS_ABI); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PTR2UV(&MDS_ABI); |
0 |
13 |
13 |
|
| 494 |
|
| OUTPUT: |
| 495 |
|
| RETVAL |
| 496 |
|
| |
| 497 |
|
| # Exercise the whole mds_abi table the way a C consumer would: resolve it from |
| 498 |
|
| # the IV _abi_ptr hands back, gate on abi_version, then drive the entries |
| 499 |
|
| # through the function pointers rather than calling the C directly. This is |
| 500 |
|
| # how the ABI gets tested without a second distribution to consume it. |
| 501 |
|
| # |
| 502 |
|
| # Returns ($html, \@toc, $plain) on success; (undef, $error) when a render |
| 503 |
|
| # fails; and an empty list when the gate rejects the table or the invocant is |
| 504 |
|
| # not a session, which is where a consumer would fall back to the methods. |
| 505 |
|
| void |
| 506 |
|
| _abi_selftest(self, input, opts = &PL_sv_undef) |
| 507 |
|
| SV* self |
| 508 |
|
| SV* input |
| 509 |
|
| SV* opts |
| 510 |
|
| PPCODE: |
| 511 |
|
| { |
| 512 |
6 |
| const mds_abi* abi = NULL; |
| 513 |
|
| void* session; |
| 514 |
6 |
| SV* err = NULL; |
| 515 |
|
| SV* html; |
| 516 |
|
| SV* plain; |
| 517 |
|
| AV* toc; |
| 518 |
|
| STRLEN n; |
| 519 |
|
| const char* in; |
| 520 |
|
| { |
| 521 |
6 |
| dSP; |
| 522 |
6 |
| UV ptr = 0; |
| 523 |
|
| int count; |
| 524 |
6 |
| ENTER; SAVETMPS; |
| 525 |
6 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
6 |
6 |
|
| 526 |
6 |
| PUTBACK; |
| 527 |
6 |
| count = call_pv("Markdown::Simple::_abi_ptr", G_SCALAR | G_EVAL); |
| 528 |
6 |
| SPAGAIN; |
| 529 |
6 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
6 |
0 |
6 |
|
| 530 |
|
| /* Pop into a local first: before 5.30 SvUV was a macro that |
| 531 |
|
| * evaluated its argument twice, so SvUV(POPs) popped twice |
| 532 |
|
| * and read the UV off whatever sat one slot lower. */ |
| 533 |
6 |
| SV* rv = POPs; |
| 534 |
6 |
▶ | if (!SvTRUE(ERRSV)) ptr = SvUV(rv); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV)) ptr = SvUV(rv); |
6 |
0 |
6 |
| if (!SvTRUE(ERRSV)) ptr = SvUV(rv); |
6 |
0 |
6 |
|
| 535 |
|
| } |
| 536 |
6 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
6 |
0 |
6 |
|
| 537 |
6 |
▶ | if (ptr) abi = INT2PTR(const mds_abi*, ptr); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr) abi = INT2PTR(const mds_abi*, ptr); |
6 |
0 |
6 |
|
| 538 |
|
| } |
| 539 |
6 |
▶ | if (!abi || abi->abi_version < MDS_ABI_VERSION) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!abi || abi->abi_version < MDS_ABI_VERSION) XSRETURN_EMPTY; |
6 |
0 |
6 |
| if (!abi || abi->abi_version < MDS_ABI_VERSION) XSRETURN_EMPTY; |
0 |
6 |
6 |
|
| 540 |
|
| |
| 541 |
6 |
| session = abi->session_of(aTHX_ self); |
| 542 |
6 |
▶ | if (!session) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!session) XSRETURN_EMPTY; |
3 |
3 |
6 |
|
| 543 |
|
| |
| 544 |
|
| /* Prove flags_from_hv is reachable and agrees with the session by |
| 545 |
|
| * decoding the caller's options, even though session_render uses the |
| 546 |
|
| * flags already bound to the session. */ |
| 547 |
3 |
▶ | if (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
0 |
3 |
3 |
| if (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
0 |
0 |
0 |
| if (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
0 |
0 |
0 |
|
| 548 |
0 |
| (void)abi->flags_from_hv(aTHX_ (HV*)SvRV(opts)); |
| 549 |
|
| |
| 550 |
6 |
▶ | in = SvOK(input) ? SvPV(input, n) : (n = 0, ""); |
Branch: 1 branch
| Branch | True | False | Total |
| in = SvOK(input) ? SvPV(input, n) : (n = 0, ""); |
3 |
0 |
3 |
|
| 551 |
|
| |
| 552 |
3 |
| html = newSVpvs(""); |
| 553 |
3 |
| toc = newAV(); |
| 554 |
3 |
▶ | if (abi->session_render(aTHX_ session, in, n, html, toc, &err) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (abi->session_render(aTHX_ session, in, n, html, toc, &err) != 0) { |
1 |
2 |
3 |
|
| 555 |
1 |
| SvREFCNT_dec(html); |
| 556 |
1 |
| SvREFCNT_dec((SV*)toc); |
| 557 |
1 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
1 |
1 |
|
| 558 |
1 |
| mPUSHs(newSV(0)); |
| 559 |
1 |
▶ | PUSHs(err ? err : sv_2mortal(newSVpvs("unknown render error"))); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(err ? err : sv_2mortal(newSVpvs("unknown render error"))); |
0 |
1 |
1 |
|
| 560 |
1 |
| XSRETURN(2); |
| 561 |
|
| } |
| 562 |
|
| |
| 563 |
2 |
| plain = newSVpvs(""); |
| 564 |
2 |
▶ | if (abi->strip(aTHX_ in, n, plain, &err) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (abi->strip(aTHX_ in, n, plain, &err) != 0) { |
0 |
2 |
2 |
|
| 565 |
0 |
| SvREFCNT_dec(html); |
| 566 |
0 |
| SvREFCNT_dec((SV*)toc); |
| 567 |
0 |
| SvREFCNT_dec(plain); |
| 568 |
0 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
0 |
0 |
|
| 569 |
0 |
| mPUSHs(newSV(0)); |
| 570 |
0 |
▶ | PUSHs(err ? err : sv_2mortal(newSVpvs("unknown strip error"))); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(err ? err : sv_2mortal(newSVpvs("unknown strip error"))); |
0 |
0 |
0 |
|
| 571 |
0 |
| XSRETURN(2); |
| 572 |
|
| } |
| 573 |
|
| |
| 574 |
2 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
2 |
2 |
|
| 575 |
2 |
| mPUSHs(html); |
| 576 |
2 |
| mPUSHs(newRV_noinc((SV*)toc)); |
| 577 |
2 |
| mPUSHs(plain); |
| 578 |
2 |
| XSRETURN(3); |
| 579 |
|
| } |
| 580 |
|
| |
| 581 |
|
| # The sessionless entry, driven the same way. Returns the rendered bytes, or |
| 582 |
|
| # (undef, $error). |
| 583 |
|
| void |
| 584 |
|
| _abi_selftest_render(input, opts = &PL_sv_undef) |
| 585 |
|
| SV* input |
| 586 |
|
| SV* opts |
| 587 |
|
| PPCODE: |
| 588 |
|
| { |
| 589 |
5 |
| const mds_abi* abi = NULL; |
| 590 |
5 |
| SV* err = NULL; |
| 591 |
|
| SV* html; |
| 592 |
|
| unsigned flags; |
| 593 |
|
| STRLEN n; |
| 594 |
|
| const char* in; |
| 595 |
|
| { |
| 596 |
5 |
| dSP; |
| 597 |
5 |
| UV ptr = 0; |
| 598 |
|
| int count; |
| 599 |
5 |
| ENTER; SAVETMPS; |
| 600 |
5 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
5 |
5 |
|
| 601 |
5 |
| PUTBACK; |
| 602 |
5 |
| count = call_pv("Markdown::Simple::_abi_ptr", G_SCALAR | G_EVAL); |
| 603 |
5 |
| SPAGAIN; |
| 604 |
5 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
5 |
0 |
5 |
|
| 605 |
5 |
| SV* rv = POPs; |
| 606 |
5 |
▶ | if (!SvTRUE(ERRSV)) ptr = SvUV(rv); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV)) ptr = SvUV(rv); |
5 |
0 |
5 |
| if (!SvTRUE(ERRSV)) ptr = SvUV(rv); |
5 |
0 |
5 |
|
| 607 |
|
| } |
| 608 |
5 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
5 |
0 |
5 |
|
| 609 |
5 |
▶ | if (ptr) abi = INT2PTR(const mds_abi*, ptr); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr) abi = INT2PTR(const mds_abi*, ptr); |
5 |
0 |
5 |
|
| 610 |
|
| } |
| 611 |
5 |
▶ | if (!abi || abi->abi_version < MDS_ABI_VERSION) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!abi || abi->abi_version < MDS_ABI_VERSION) XSRETURN_EMPTY; |
5 |
0 |
5 |
| if (!abi || abi->abi_version < MDS_ABI_VERSION) XSRETURN_EMPTY; |
0 |
5 |
5 |
|
| 612 |
|
| |
| 613 |
5 |
| flags = abi->flags_from_hv(aTHX_ |
| 614 |
5 |
▶ | (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
3 |
2 |
5 |
| (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
3 |
0 |
3 |
| (SvOK(opts) && SvROK(opts) && SvTYPE(SvRV(opts)) == SVt_PVHV) |
3 |
0 |
3 |
|
| 615 |
|
| ? (HV*)SvRV(opts) : NULL); |
| 616 |
|
| |
| 617 |
10 |
▶ | in = SvOK(input) ? SvPV(input, n) : (n = 0, ""); |
Branch: 1 branch
| Branch | True | False | Total |
| in = SvOK(input) ? SvPV(input, n) : (n = 0, ""); |
5 |
0 |
5 |
|
| 618 |
5 |
| html = newSVpvs(""); |
| 619 |
5 |
▶ | if (abi->render(aTHX_ in, n, flags, html, NULL, &err) != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (abi->render(aTHX_ in, n, flags, html, NULL, &err) != 0) { |
0 |
5 |
5 |
|
| 620 |
0 |
| SvREFCNT_dec(html); |
| 621 |
0 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
0 |
0 |
|
| 622 |
0 |
| mPUSHs(newSV(0)); |
| 623 |
0 |
▶ | PUSHs(err ? err : sv_2mortal(newSVpvs("unknown render error"))); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(err ? err : sv_2mortal(newSVpvs("unknown render error"))); |
0 |
0 |
0 |
|
| 624 |
0 |
| XSRETURN(2); |
| 625 |
|
| } |
| 626 |
5 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
5 |
5 |
|
| 627 |
5 |
| mPUSHs(html); |
| 628 |
5 |
| XSRETURN(1); |
| 629 |
|
| } |
| 630 |
|
| |
| 631 |
|
| # The ABI version this build compiled against, for a consumer's diagnostics. |
| 632 |
|
| IV |
| 633 |
|
| _abi_version() |
| 634 |
|
| CODE: |
| 635 |
1 |
▶ | RETVAL = MDS_ABI_VERSION; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = MDS_ABI_VERSION; |
0 |
1 |
1 |
|
| 636 |
|
| OUTPUT: |
| 637 |
|
| RETVAL |
| 638 |
|
| |
| 639 |
|
| # ---- SIMD backend introspection ---------------------------------------- |
| 640 |
|
| |
| 641 |
|
| SV* |
| 642 |
|
| _simd_backend() |
| 643 |
|
| CODE: |
| 644 |
|
| { |
| 645 |
5 |
| RETVAL = newSVpv(mds_simd_backend(), 0); |
| 646 |
|
| } |
| 647 |
|
| OUTPUT: |
| 648 |
|
| RETVAL |
| 649 |
|
| |
| 650 |
|
| void |
| 651 |
|
| _simd_force_scalar(on) |
| 652 |
|
| int on; |
| 653 |
|
| CODE: |
| 654 |
|
| { |
| 655 |
1394 |
| mds_simd_force_scalar(on); |
| 656 |
|
| } |
| 657 |
|
| |
| 658 |
|
| # ---- Classifier introspection (test-only) ------------------------------ |
| 659 |
|
| # _classify_structural($bytes) -- runs the *active* backend |
| 660 |
|
| # _classify_structural_scalar($bytes) -- runs the scalar reference |
| 661 |
|
| # Both return a byte string of ceil(len/8) bytes; bit i (LSB-first within |
| 662 |
|
| # each byte) is set iff input byte i is structural. |
| 663 |
|
| |
| 664 |
|
| SV* |
| 665 |
|
| _classify_structural(input) |
| 666 |
|
| SV* input; |
| 667 |
|
| CODE: |
| 668 |
|
| { |
| 669 |
|
| STRLEN n; |
| 670 |
|
| const char* s; |
| 671 |
|
| size_t words; |
| 672 |
|
| uint64_t* bm; |
| 673 |
|
| size_t out_bytes; |
| 674 |
599 |
| s = SvPV(input, n); |
| 675 |
599 |
| words = (n + 63) >> 6; |
| 676 |
599 |
▶ | if (!words) words = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!words) words = 1; |
1 |
598 |
599 |
|
| 677 |
599 |
| bm = (uint64_t*)calloc(words, sizeof(uint64_t)); |
| 678 |
599 |
▶ | if (!bm) croak("oom"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bm) croak("oom"); |
0 |
599 |
599 |
|
| 679 |
599 |
| mds_simd_get()->classify_structural(s, n, bm); |
| 680 |
599 |
| out_bytes = (n + 7) >> 3; |
| 681 |
599 |
| RETVAL = newSVpvn((const char*)bm, out_bytes); |
| 682 |
599 |
| free(bm); |
| 683 |
|
| } |
| 684 |
|
| OUTPUT: |
| 685 |
|
| RETVAL |
| 686 |
|
| |
| 687 |
|
| SV* |
| 688 |
|
| _classify_structural_scalar(input) |
| 689 |
|
| SV* input; |
| 690 |
|
| CODE: |
| 691 |
|
| { |
| 692 |
|
| STRLEN n; |
| 693 |
|
| const char* s; |
| 694 |
|
| size_t words; |
| 695 |
|
| uint64_t* bm; |
| 696 |
|
| size_t out_bytes; |
| 697 |
600 |
| s = SvPV(input, n); |
| 698 |
600 |
| words = (n + 63) >> 6; |
| 699 |
600 |
▶ | if (!words) words = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!words) words = 1; |
1 |
599 |
600 |
|
| 700 |
600 |
| bm = (uint64_t*)calloc(words, sizeof(uint64_t)); |
| 701 |
600 |
▶ | if (!bm) croak("oom"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bm) croak("oom"); |
0 |
600 |
600 |
|
| 702 |
600 |
| mds_simd_ops_scalar()->classify_structural(s, n, bm); |
| 703 |
600 |
| out_bytes = (n + 7) >> 3; |
| 704 |
600 |
| RETVAL = newSVpvn((const char*)bm, out_bytes); |
| 705 |
600 |
| free(bm); |
| 706 |
|
| } |
| 707 |
|
| OUTPUT: |
| 708 |
|
| RETVAL |
| 709 |
|
| |
| 710 |
|
| # ---- UTF-8 validator + line scanner (test-only) ------------------------ |
| 711 |
|
| |
| 712 |
|
| int |
| 713 |
|
| _validate_utf8(input) |
| 714 |
|
| SV* input; |
| 715 |
|
| CODE: |
| 716 |
|
| { |
| 717 |
|
| STRLEN n; |
| 718 |
|
| const char* s; |
| 719 |
280 |
| s = SvPV(input, n); |
| 720 |
280 |
| RETVAL = mds_simd_get()->validate_utf8(s, n); |
| 721 |
|
| } |
| 722 |
|
| OUTPUT: |
| 723 |
|
| RETVAL |
| 724 |
|
| |
| 725 |
|
| int |
| 726 |
|
| _validate_utf8_scalar(input) |
| 727 |
|
| SV* input; |
| 728 |
|
| CODE: |
| 729 |
|
| { |
| 730 |
|
| STRLEN n; |
| 731 |
|
| const char* s; |
| 732 |
280 |
| s = SvPV(input, n); |
| 733 |
280 |
| RETVAL = mds_simd_ops_scalar()->validate_utf8(s, n); |
| 734 |
|
| } |
| 735 |
|
| OUTPUT: |
| 736 |
|
| RETVAL |
| 737 |
|
| |
| 738 |
|
| # Both _find_newlines variants return: |
| 739 |
|
| # - undef when the offset table overflows the provided cap, or |
| 740 |
|
| # - a packed string of native-endian uint32_t values (one per '\n'). |
| 741 |
|
| SV* |
| 742 |
|
| _find_newlines(input) |
| 743 |
|
| SV* input; |
| 744 |
|
| CODE: |
| 745 |
|
| { |
| 746 |
|
| STRLEN n; |
| 747 |
|
| const char* s; |
| 748 |
|
| size_t cap; |
| 749 |
|
| uint32_t* offs; |
| 750 |
|
| size_t k; |
| 751 |
306 |
| s = SvPV(input, n); |
| 752 |
306 |
▶ | cap = n ? n : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| cap = n ? n : 1; |
304 |
2 |
306 |
|
| 753 |
306 |
| offs = (uint32_t*)malloc(cap * sizeof(uint32_t)); |
| 754 |
306 |
▶ | if (!offs) croak("oom"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!offs) croak("oom"); |
0 |
306 |
306 |
|
| 755 |
306 |
| k = mds_simd_get()->find_newlines(s, n, offs, cap); |
| 756 |
306 |
▶ | if (k == (size_t)-1) { free(offs); XSRETURN_UNDEF; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (k == (size_t)-1) { free(offs); XSRETURN_UNDEF; } |
0 |
306 |
306 |
|
| 757 |
306 |
| RETVAL = newSVpvn((const char*)offs, k * sizeof(uint32_t)); |
| 758 |
306 |
| free(offs); |
| 759 |
|
| } |
| 760 |
|
| OUTPUT: |
| 761 |
|
| RETVAL |
| 762 |
|
| |
| 763 |
|
| SV* |
| 764 |
|
| _find_newlines_scalar(input) |
| 765 |
|
| SV* input; |
| 766 |
|
| CODE: |
| 767 |
|
| { |
| 768 |
|
| STRLEN n; |
| 769 |
|
| const char* s; |
| 770 |
|
| size_t cap; |
| 771 |
|
| uint32_t* offs; |
| 772 |
|
| size_t k; |
| 773 |
209 |
| s = SvPV(input, n); |
| 774 |
209 |
▶ | cap = n ? n : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| cap = n ? n : 1; |
208 |
1 |
209 |
|
| 775 |
209 |
| offs = (uint32_t*)malloc(cap * sizeof(uint32_t)); |
| 776 |
209 |
▶ | if (!offs) croak("oom"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!offs) croak("oom"); |
0 |
209 |
209 |
|
| 777 |
209 |
| k = mds_simd_ops_scalar()->find_newlines(s, n, offs, cap); |
| 778 |
209 |
▶ | if (k == (size_t)-1) { free(offs); XSRETURN_UNDEF; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (k == (size_t)-1) { free(offs); XSRETURN_UNDEF; } |
0 |
209 |
209 |
|
| 779 |
209 |
| RETVAL = newSVpvn((const char*)offs, k * sizeof(uint32_t)); |
| 780 |
209 |
| free(offs); |
| 781 |
|
| } |
| 782 |
|
| OUTPUT: |
| 783 |
|
| RETVAL |
| 784 |
|
| |
| 785 |
|
| # Bounded-cap variant for testing the overflow sentinel path. |
| 786 |
|
| SV* |
| 787 |
|
| _find_newlines_capped(input, cap) |
| 788 |
|
| SV* input; |
| 789 |
|
| int cap; |
| 790 |
|
| CODE: |
| 791 |
|
| { |
| 792 |
3 |
| STRLEN n; const char* s = SvPV(input, n); |
| 793 |
3 |
| size_t cc = cap < 0 ? 0 : (size_t)cap; |
| 794 |
3 |
▶ | uint32_t* offs = cc ? (uint32_t*)malloc(cc * sizeof(uint32_t)) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| uint32_t* offs = cc ? (uint32_t*)malloc(cc * sizeof(uint32_t)) : NULL; |
3 |
0 |
3 |
|
| 795 |
3 |
| size_t k = mds_simd_get()->find_newlines(s, n, offs, cc); |
| 796 |
3 |
▶ | if (k == (size_t)-1) { free(offs); XSRETURN_UNDEF; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (k == (size_t)-1) { free(offs); XSRETURN_UNDEF; } |
1 |
2 |
3 |
|
| 797 |
2 |
▶ | RETVAL = newSVpvn(offs ? (const char*)offs : "", k * sizeof(uint32_t)); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = newSVpvn(offs ? (const char*)offs : "", k * sizeof(uint32_t)); |
2 |
0 |
2 |
|
| 798 |
2 |
| free(offs); |
| 799 |
|
| } |
| 800 |
|
| OUTPUT: |
| 801 |
|
| RETVAL |
| 802 |
|
| |
| 803 |
|
| # ---- Arena profile from the last parse --------------------------------- |
| 804 |
|
| # Returns a hashref describing arena usage from the most recent call to |
| 805 |
|
| # mds_render_html_to_sv. Intended for bench/profile_arena.pl; not |
| 806 |
|
| # thread-safe (the underlying snapshot is a single static). |
| 807 |
|
| |
| 808 |
|
| SV* |
| 809 |
|
| _last_arena_profile() |
| 810 |
|
| CODE: |
| 811 |
|
| { |
| 812 |
1 |
| HV* h = newHV(); |
| 813 |
1 |
| hv_stores(h, "total_alloc", newSVuv((UV)mds_last_arena_profile.total_alloc)); |
| 814 |
1 |
| hv_stores(h, "page_count", newSVuv((UV)mds_last_arena_profile.page_count)); |
| 815 |
1 |
| hv_stores(h, "big_count", newSVuv((UV)mds_last_arena_profile.big_count)); |
| 816 |
1 |
| hv_stores(h, "big_bytes", newSVuv((UV)mds_last_arena_profile.big_bytes)); |
| 817 |
1 |
| hv_stores(h, "head_used_last", newSVuv((UV)mds_last_arena_profile.head_used_last)); |
| 818 |
1 |
| hv_stores(h, "head_cap_last", newSVuv((UV)mds_last_arena_profile.head_cap_last)); |
| 819 |
1 |
| hv_stores(h, "page_size", newSVuv((UV)MDS_ARENA_PAGE)); |
| 820 |
1 |
| hv_stores(h, "big_threshold", newSVuv((UV)MDS_ARENA_BIG)); |
| 821 |
1 |
| RETVAL = newRV_noinc((SV*)h); |
| 822 |
|
| } |
| 823 |
|
| OUTPUT: |
| 824 |
|
| RETVAL |
| 825 |
|
| |
| 826 |
|
| # ---- Arena + buffer self-tests ----------------------------------------- |
| 827 |
|
| |
| 828 |
|
| SV* |
| 829 |
|
| _arena_test() |
| 830 |
|
| CODE: |
| 831 |
|
| { |
| 832 |
|
| /* Exercise alignment, page chaining, and the big-alloc path. */ |
| 833 |
|
| mds_arena a; |
| 834 |
1 |
| int aligned_ok = 1; |
| 835 |
|
| int chained_ok; |
| 836 |
|
| void* big; |
| 837 |
|
| int big_ok; |
| 838 |
|
| int reset_ok; |
| 839 |
|
| HV* h_a; |
| 840 |
|
| int i_a; |
| 841 |
|
| size_t si_a; |
| 842 |
1 |
| mds_arena_init(&a); |
| 843 |
|
| |
| 844 |
|
| /* 1. Alignment: every returned pointer is MDS_ARENA_ALIGN-aligned. */ |
| 845 |
65 |
▶ | for (i_a = 0; i_a < 64; i_a++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i_a = 0; i_a < 64; i_a++) { |
64 |
1 |
65 |
|
| 846 |
64 |
| { void* p = mds_arena_alloc(&a, 1 + (i_a * 7)); |
| 847 |
64 |
▶ | if (((uintptr_t)p) & (MDS_ARENA_ALIGN - 1)) { aligned_ok = 0; break; } } |
Branch: 1 branch
| Branch | True | False | Total |
| if (((uintptr_t)p) & (MDS_ARENA_ALIGN - 1)) { aligned_ok = 0; break; } } |
0 |
64 |
64 |
|
| 848 |
|
| } |
| 849 |
|
| |
| 850 |
|
| /* 2. Page chaining: allocate enough to force a second page. */ |
| 851 |
201 |
▶ | for (si_a = 0; si_a < 200; si_a++) mds_arena_alloc(&a, 1024); |
Branch: 1 branch
| Branch | True | False | Total |
| for (si_a = 0; si_a < 200; si_a++) mds_arena_alloc(&a, 1024); |
200 |
1 |
201 |
|
| 852 |
1 |
▶ | chained_ok = (a.head && a.head->next != NULL); |
Branch: 2 branches
| Branch | True | False | Total |
| chained_ok = (a.head && a.head->next != NULL); |
1 |
0 |
1 |
| chained_ok = (a.head && a.head->next != NULL); |
1 |
0 |
1 |
|
| 853 |
|
| |
| 854 |
|
| /* 3. Big-alloc: > MDS_ARENA_BIG goes to dedicated page. */ |
| 855 |
1 |
| big = mds_arena_alloc(&a, MDS_ARENA_BIG * 2); |
| 856 |
1 |
▶ | big_ok = (big != NULL && a.big != NULL); |
Branch: 2 branches
| Branch | True | False | Total |
| big_ok = (big != NULL && a.big != NULL); |
1 |
0 |
1 |
| big_ok = (big != NULL && a.big != NULL); |
1 |
0 |
1 |
|
| 857 |
|
| |
| 858 |
|
| /* 4. Reset: walks back to single warm page, no big pages. */ |
| 859 |
1 |
| mds_arena_reset(&a); |
| 860 |
1 |
▶ | reset_ok = (a.head && a.head->next == NULL && a.big == NULL); |
Branch: 3 branches
| Branch | True | False | Total |
| reset_ok = (a.head && a.head->next == NULL && a.big == NULL); |
1 |
0 |
1 |
| reset_ok = (a.head && a.head->next == NULL && a.big == NULL); |
1 |
0 |
1 |
| reset_ok = (a.head && a.head->next == NULL && a.big == NULL); |
1 |
0 |
1 |
|
| 861 |
|
| |
| 862 |
1 |
| mds_arena_free(&a); |
| 863 |
|
| |
| 864 |
1 |
| h_a = newHV(); |
| 865 |
1 |
| hv_stores(h_a, "aligned", newSViv(aligned_ok)); |
| 866 |
1 |
| hv_stores(h_a, "chained", newSViv(chained_ok)); |
| 867 |
1 |
| hv_stores(h_a, "big", newSViv(big_ok)); |
| 868 |
1 |
| hv_stores(h_a, "reset", newSViv(reset_ok)); |
| 869 |
1 |
| RETVAL = newRV_noinc((SV*)h_a); |
| 870 |
|
| } |
| 871 |
|
| OUTPUT: |
| 872 |
|
| RETVAL |
| 873 |
|
| |
| 874 |
|
| SV* |
| 875 |
|
| _buf_test() |
| 876 |
|
| CODE: |
| 877 |
|
| { |
| 878 |
|
| /* Exercise mds_buf: grows, preserves contents, finalises SvCUR. */ |
| 879 |
|
| SV* out; |
| 880 |
|
| mds_buf b; |
| 881 |
|
| int i_b; |
| 882 |
|
| int len_ok; |
| 883 |
|
| const char* p_b; |
| 884 |
|
| int data_ok; |
| 885 |
|
| HV* h_b; |
| 886 |
1 |
| out = newSVpv("", 0); |
| 887 |
1 |
| mds_buf_init(aTHX_ &b, out, 8); /* tiny hint to force growth */ |
| 888 |
1001 |
▶ | for (i_b = 0; i_b < 1000; i_b++) mds_buf_write(aTHX_ &b, "abcdef", 6); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i_b = 0; i_b < 1000; i_b++) mds_buf_write(aTHX_ &b, "abcdef", 6); |
1000 |
1 |
1001 |
|
| 889 |
1 |
| mds_buf_finalize(aTHX_ &b); |
| 890 |
|
| |
| 891 |
1 |
| len_ok = (SvCUR(out) == 6000); |
| 892 |
1 |
| p_b = SvPVX(out); |
| 893 |
2 |
▶ | data_ok = (memcmp(p_b, "abcdef", 6) == 0 && |
Branch: 1 branch
| Branch | True | False | Total |
| data_ok = (memcmp(p_b, "abcdef", 6) == 0 && |
1 |
0 |
1 |
|
| 894 |
1 |
▶ | memcmp(p_b + 5994, "abcdef", 6) == 0); |
Branch: 1 branch
| Branch | True | False | Total |
| memcmp(p_b + 5994, "abcdef", 6) == 0); |
1 |
0 |
1 |
|
| 895 |
|
| |
| 896 |
1 |
| h_b = newHV(); |
| 897 |
1 |
| hv_stores(h_b, "len", newSViv(len_ok)); |
| 898 |
1 |
| hv_stores(h_b, "data", newSViv(data_ok)); |
| 899 |
1 |
| SvREFCNT_dec(out); |
| 900 |
1 |
| RETVAL = newRV_noinc((SV*)h_b); |
| 901 |
|
| } |
| 902 |
|
| OUTPUT: |
| 903 |
|
| RETVAL |
| 904 |
|
| |