Code spans: a run of N backticks up to the next run of exactly N. An * escaped first backtick (an odd run of backslashes before it) does not open * a span. Closers come from the runs sorted by (length, start). */
| 1223 | * escaped first backtick (an odd run of backslashes before it) does not open |
| 1224 | * a span. Closers come from the runs sorted by (length, start). */ |
| 1225 | static void md_code_spans(md_scan_t *s) { |
| 1226 | s->runs.n = 0; |
| 1227 | s->by_len.n = 0; |
| 1228 | s->ranges.n = 0; |
| 1229 | for (int i = 0; i < s->len;) { |
| 1230 | if (s->buf[i] != '`') { |
| 1231 | i++; |
| 1232 | continue; |
| 1233 | } |
| 1234 | int j = i; |
| 1235 | while (j < s->len && s->buf[j] == '`') { |
| 1236 | j++; |
| 1237 | } |
| 1238 | if (!md_push_pair(&s->runs, i, j - i) || !md_push_pair(&s->by_len, j - i, i)) { |
| 1239 | md_fail(s); |
| 1240 | return; |
| 1241 | } |
| 1242 | i = j; |
| 1243 | } |
| 1244 | int nruns = s->runs.n / PAIR_LEN; |
| 1245 | if (nruns < PAIR_LEN) { |
| 1246 | return; |
| 1247 | } |
| 1248 | qsort(s->by_len.v, (size_t)nruns, sizeof(int32_t) * PAIR_LEN, md_pair_cmp); |
| 1249 | int pos = 0; |
| 1250 | for (int r = 0; r < nruns; r++) { |
| 1251 | int start = s->runs.v[r * PAIR_LEN]; |
| 1252 | int len = s->runs.v[(r * PAIR_LEN) + SKIP_ONE]; |
| 1253 | if (start < pos) { |
| 1254 | continue; |
| 1255 | } |
| 1256 | int bs = 0; |
| 1257 | while (start - bs > 0 && s->buf[start - bs - SKIP_ONE] == '\\') { |
| 1258 | bs++; |
| 1259 | } |
| 1260 | if (bs % PAIR_LEN) { |
| 1261 | start++; |
| 1262 | len--; |
| 1263 | if (len == 0) { |
| 1264 | continue; |
| 1265 | } |
| 1266 | } |
| 1267 | /* the first run of exactly `len` starting at or after start + len */ |
| 1268 | int lo = 0; |
| 1269 | int hi = nruns; |
| 1270 | int32_t key[PAIR_LEN] = {len, start + len}; |
| 1271 | while (lo < hi) { |
| 1272 | int mid = lo + ((hi - lo) / PAIR_LEN); |
| 1273 | if (md_pair_cmp(&s->by_len.v[mid * PAIR_LEN], key) < 0) { |
| 1274 | lo = mid + SKIP_ONE; |
| 1275 | } else { |
| 1276 | hi = mid; |
| 1277 | } |
| 1278 | } |
| 1279 | if (lo < nruns && s->by_len.v[lo * PAIR_LEN] == len) { |
| 1280 | int close = s->by_len.v[(lo * PAIR_LEN) + SKIP_ONE]; |
| 1281 | int a = start + len; |
| 1282 | int b = close; |
no test coverage detected