mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-22 08:04:19 +02:00
move find_marker outside of highlight_code
This commit is contained in:
parent
4e8f6661d3
commit
793e064c9e
1 changed files with 25 additions and 25 deletions
|
|
@ -114,31 +114,6 @@ fn highlight_code<E>(
|
|||
mut text: String,
|
||||
process_snippet: &mut impl FnMut(PageSnippet<&str>) -> Result<(), E>,
|
||||
) -> Result<(), E> {
|
||||
fn find_marker(s: &str, marker: &str, forbidden_prefix: &str) -> Option<usize> {
|
||||
assert_eq!(
|
||||
marker.as_bytes()[0],
|
||||
forbidden_prefix.as_bytes()[forbidden_prefix.len() - 1]
|
||||
);
|
||||
|
||||
let mut search_start = 0;
|
||||
loop {
|
||||
let marker_index = s.find_from(marker, search_start)?;
|
||||
|
||||
// Avoid matching the "{{" at the end of "\{\{{" (where "{{" is the marker, and "\{\{{"
|
||||
// is the forbidden prefix)
|
||||
let overlaps_with_prefix = (forbidden_prefix.len() <= marker_index + 1) && {
|
||||
let prefix_start = marker_index + 1 - forbidden_prefix.len();
|
||||
&s[prefix_start..=marker_index] == forbidden_prefix
|
||||
};
|
||||
if !overlaps_with_prefix {
|
||||
return Some(marker_index);
|
||||
}
|
||||
|
||||
// The next valid marker cannot include the first character of the current match
|
||||
search_start = marker_index + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE: This is not optimal, as it allocates one String for each `replace`
|
||||
let replace_escaped = |s: &str| s.replace(r"\{\{", "{{").replace(r"\}\}", "}}");
|
||||
|
||||
|
|
@ -174,6 +149,31 @@ fn highlight_code<E>(
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn find_marker(s: &str, marker: &str, forbidden_prefix: &str) -> Option<usize> {
|
||||
assert_eq!(
|
||||
marker.as_bytes()[0],
|
||||
forbidden_prefix.as_bytes()[forbidden_prefix.len() - 1]
|
||||
);
|
||||
|
||||
let mut search_start = 0;
|
||||
loop {
|
||||
let marker_index = s.find_from(marker, search_start)?;
|
||||
|
||||
// Avoid matching the "{{" at the end of "\{\{{" (where "{{" is the marker, and "\{\{{"
|
||||
// is the forbidden prefix)
|
||||
let overlaps_with_prefix = (forbidden_prefix.len() <= marker_index + 1) && {
|
||||
let prefix_start = marker_index + 1 - forbidden_prefix.len();
|
||||
&s[prefix_start..=marker_index] == forbidden_prefix
|
||||
};
|
||||
if !overlaps_with_prefix {
|
||||
return Some(marker_index);
|
||||
}
|
||||
|
||||
// The next valid marker cannot include the first character of the current match
|
||||
search_start = marker_index + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// Yields `NormalCode` and `CommandName` in alternating order according to the occurrences of
|
||||
/// `command_name` in `segment`. Variables are not detected here, see `highlight_code`
|
||||
/// instead.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue