1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use memchr::{memchr, memchr2};
use ox_content_allocator::Vec;
use ox_content_ast::{Image, Link, Node, Span, Text};
use super::Parser;
use crate::error::ParseResult;
impl<'a> Parser<'a> {
pub(super) fn parse_link(
&self,
content: &'a str,
offset: usize,
children: &mut Vec<'a, Node<'a>>,
pos: &mut usize,
) -> ParseResult<()> {
let bytes = content.as_bytes();
let link_start = *pos;
*pos += 1;
let text_start = *pos;
*pos = Self::scan_balanced(bytes, *pos, b'[', b']');
if *pos < content.len()
&& bytes[*pos] == b']'
&& *pos + 1 < content.len()
&& bytes[*pos + 1] == b'('
{
let link_text = &content[text_start..*pos];
*pos += 2;
let url_start = *pos;
*pos = Self::scan_balanced(bytes, *pos, b'(', b')');
if *pos < content.len() && bytes[*pos] == b')' {
let url = &content[url_start..*pos];
*pos += 1;
let children_nodes = self.parse_inline(link_text, offset + text_start)?;
children.push(Node::Link(Link {
url,
title: None,
children: children_nodes,
span: Span::new((offset + link_start) as u32, (offset + *pos) as u32),
}));
} else {
Self::push_text(
children,
&content[link_start..*pos],
offset + link_start,
offset + *pos,
);
}
} else {
Self::push_text(children, "[", offset + link_start, offset + link_start + 1);
*pos = link_start + 1;
}
Ok(())
}
pub(super) fn parse_image(
content: &'a str,
offset: usize,
children: &mut Vec<'a, Node<'a>>,
pos: &mut usize,
) {
let bytes = content.as_bytes();
if *pos + 1 >= content.len() || bytes[*pos + 1] != b'[' {
Self::push_text(children, "!", offset + *pos, offset + *pos + 1);
*pos += 1;
return;
}
let image_start = *pos;
*pos += 2;
let alt_start = *pos;
*pos = Self::scan_balanced(bytes, *pos, b'[', b']');
if *pos < content.len()
&& bytes[*pos] == b']'
&& *pos + 1 < content.len()
&& bytes[*pos + 1] == b'('
{
let alt = &content[alt_start..*pos];
*pos += 2;
let url_start = *pos;
*pos = Self::scan_balanced(bytes, *pos, b'(', b')');
if *pos < content.len() && bytes[*pos] == b')' {
let url = &content[url_start..*pos];
*pos += 1;
children.push(Node::Image(Image {
url,
alt,
title: None,
span: Span::new((offset + image_start) as u32, (offset + *pos) as u32),
}));
} else {
Self::push_text(
children,
&content[image_start..*pos],
offset + image_start,
offset + *pos,
);
}
} else {
Self::push_text(children, "![", offset + image_start, offset + image_start + 2);
*pos = image_start + 2;
}
}
pub(super) fn push_text(
children: &mut Vec<'a, Node<'a>>,
value: &'a str,
start: usize,
end: usize,
) {
children.push(Node::Text(Text { value, span: Span::new(start as u32, end as u32) }));
}
pub(super) fn marker_run_len(bytes: &[u8], start: usize, marker: u8) -> usize {
let mut count = 1;
while start + count < bytes.len() && bytes[start + count] == marker {
count += 1;
}
count
}
/// Finds the next emphasis/strong delimiter run of at least `min_count`.
///
/// Only occurrences of `marker` can change the result. Using `memchr` to
/// jump between marker runs preserves the delimiter positions visited by
/// the old byte-by-byte loop while making long non-marker spans cheap.
pub(super) fn find_marker_run(
bytes: &[u8],
mut cursor: usize,
marker: u8,
min_count: usize,
) -> Option<usize> {
while cursor < bytes.len() {
// Skip directly to the next marker byte instead of inspecting every
// intervening byte; the original `else { cursor += 1 }` arm was a
// pure no-op skip, so the marker positions visited are identical.
let off = memchr(marker, &bytes[cursor..])?;
cursor += off;
let count = Self::marker_run_len(bytes, cursor, marker);
if count >= min_count {
return Some(cursor);
}
cursor += count;
}
None
}
/// Scans a balanced delimiter region and returns the matching close byte.
///
/// Link labels and destinations only care about nested `open`/`close`
/// delimiters; every other byte is inert. `memchr2` moves directly to the
/// next byte that can affect depth, which keeps deeply textual labels from
/// paying a branch for each character.
fn scan_balanced(bytes: &[u8], mut cursor: usize, open: u8, close: u8) -> usize {
let mut depth = 1;
while cursor < bytes.len() {
// Only `open`/`close` change depth, so jump straight to the next one
// via memchr2; the skipped bytes were a no-op in the original loop.
let Some(off) = memchr2(open, close, &bytes[cursor..]) else {
return bytes.len();
};
cursor += off;
if bytes[cursor] == open {
depth += 1;
} else {
depth -= 1;
// Stop AT the closing delimiter (matching the original, which
// skipped its trailing `cursor += 1` once depth hit 0).
if depth == 0 {
return cursor;
}
}
cursor += 1;
}
cursor
}
}