The inline state for a doc text whose inline lines are lines[0..n): arrays * with one slot per '[' and per backtick of the whole text, so no paragraph can * outgrow them. NULL when there is no inline text or memory ran out; *buf_out * is the paragraph buffer. */
| 2053 | * outgrow them. NULL when there is no inline text or memory ran out; *buf_out |
| 2054 | * is the paragraph buffer. */ |
| 2055 | static rs_inline_t *rs_inline_new(CBMArena *tmp, const rs_line_t *lines, int n, char **buf_out) { |
| 2056 | size_t text_bytes = 0; |
| 2057 | size_t brackets = 0; |
| 2058 | size_t backticks = 0; |
| 2059 | for (int i = 0; i < n; i++) { |
| 2060 | if (lines[i].kind != RS_L_TEXT) { |
| 2061 | continue; |
| 2062 | } |
| 2063 | text_bytes += (size_t)lines[i].len + SKIP_ONE; |
| 2064 | for (uint32_t k = 0; k < lines[i].len; k++) { |
| 2065 | brackets += lines[i].p[k] == '['; |
| 2066 | backticks += lines[i].p[k] == '`'; |
| 2067 | } |
| 2068 | } |
| 2069 | if (text_bytes == 0 || brackets == 0) { |
| 2070 | return NULL; /* no bracket, no link */ |
| 2071 | } |
| 2072 | rs_inline_t *in = (rs_inline_t *)cbm_arena_alloc(tmp, sizeof(*in)); |
| 2073 | char *buf = (char *)cbm_arena_alloc(tmp, text_bytes + SKIP_ONE); |
| 2074 | if (!in || !buf) { |
| 2075 | return NULL; |
| 2076 | } |
| 2077 | memset(in, 0, sizeof(*in)); |
| 2078 | in->open = (rs_opener_t *)cbm_arena_alloc(tmp, brackets * sizeof(rs_opener_t)); |
| 2079 | in->runs = (rs_run_t *)cbm_arena_alloc(tmp, (backticks + SKIP_ONE) * sizeof(rs_run_t)); |
| 2080 | in->by_len = (rs_run_t *)cbm_arena_alloc(tmp, (backticks + SKIP_ONE) * sizeof(rs_run_t)); |
| 2081 | if (!in->open || !in->runs || !in->by_len) { |
| 2082 | return NULL; |
| 2083 | } |
| 2084 | *buf_out = buf; |
| 2085 | return in; |
| 2086 | } |
| 2087 | |
| 2088 | /* The references of one doc text, pushed as tokens of the node `source_qn`. |
| 2089 | * `file_doc`: it is the file's own doc, whose lines need not be contiguous. */ |
no test coverage detected