Destructively modify string, converting newlines to spaces, then removing a single leading + trailing space, unless the code span consists entirely of space characters.
| 359 | // spaces, then removing a single leading + trailing space, |
| 360 | // unless the code span consists entirely of space characters. |
| 361 | static void S_normalize_code(cmark_strbuf *s) { |
| 362 | bufsize_t r, w; |
| 363 | bool contains_nonspace = false; |
| 364 | |
| 365 | for (r = 0, w = 0; r < s->size; ++r) { |
| 366 | switch (s->ptr[r]) { |
| 367 | case '\r': |
| 368 | if (s->ptr[r + 1] != '\n') { |
| 369 | s->ptr[w++] = ' '; |
| 370 | } |
| 371 | break; |
| 372 | case '\n': |
| 373 | s->ptr[w++] = ' '; |
| 374 | break; |
| 375 | default: |
| 376 | s->ptr[w++] = s->ptr[r]; |
| 377 | } |
| 378 | if (s->ptr[r] != ' ') { |
| 379 | contains_nonspace = true; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // begins and ends with space? |
| 384 | if (contains_nonspace && |
| 385 | s->ptr[0] == ' ' && s->ptr[w - 1] == ' ') { |
| 386 | cmark_strbuf_drop(s, 1); |
| 387 | cmark_strbuf_truncate(s, w - 2); |
| 388 | } else { |
| 389 | cmark_strbuf_truncate(s, w); |
| 390 | } |
| 391 | |
| 392 | } |
| 393 | |
| 394 | |
| 395 | // Parse backtick code section or raw backticks, return an inline. |
no test coverage detected