Skip to content

Commit

Permalink
Split environment and item lines without allocating
Browse files Browse the repository at this point in the history
This does not compile because the `format` function cannot yet deal with the new return type.
  • Loading branch information
cdesaintguilhem committed Oct 13, 2024
1 parent c49a08c commit 0f48547
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 29 deletions.
3 changes: 3 additions & 0 deletions src/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ lazy_static! {
Regex::new(r"(?P<prev>\S.*?)(?P<env>\\end\{)").unwrap();
pub static ref RE_ITEM_SHARED_LINE: Regex =
Regex::new(r"(?P<prev>\S.*?)(?P<env>\\item)").unwrap();
pub static ref RE_ENV_ITEM_SHARED_LINE: Regex =
Regex::new(r"(?P<prev>\S.*?)(?P<env>(\\begin|\\end|\\item)\{.*)")
.unwrap();
}
44 changes: 16 additions & 28 deletions src/subs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ pub fn needs_env_new_line(
}

/// Ensure LaTeX environments begin on new lines
pub fn put_env_new_line(
line: &str,
pub fn put_env_new_line<'a>(
line: &'a str,
state: &State,
file: &str,
args: &Cli,
logs: &mut Vec<Log>,
) -> Option<(String, String)> {
) -> (&'a str, Option<&'a str>) {
if args.trace {
record_line_log(
logs,
Expand All @@ -60,31 +60,19 @@ pub fn put_env_new_line(
"Placing environment on new line.",
);
}

// If there is one, find the index of the start of the comment and split the line into its comment and text parts.
let comment_index = find_comment_index(line);
let comment = get_comment(line, comment_index);
let mut text = remove_comment(line, comment_index);
let mut temp = RE_ENV_BEGIN_SHARED_LINE
.replace(text, format!("$prev{LINE_END}$env"))
.to_string();
text = &temp;
if !text.contains(LINE_END) {
temp = RE_ENV_END_SHARED_LINE
.replace(text, format!("$prev{LINE_END}$env"))
.to_string();
text = &temp;
}
if !text.contains(LINE_END) {
temp = RE_ITEM_SHARED_LINE
.replace(text, format!("$prev{LINE_END}$env"))
.to_string();
text = &temp;
}
if text.contains(LINE_END) {
let split = text.split_once(LINE_END).unwrap();
let split_0 = split.0.to_string();
let mut split_1 = split.1.to_string();
split_1.push_str(comment);
return Some((split_0, split_1));

let captures = RE_ENV_ITEM_SHARED_LINE
.captures(line)
.expect("This captures because the pattern says so.");

let (line, [prev, rest, _]) = captures.extract();

if comment_index.is_some() && captures.get(2).unwrap().start() > comment_index.unwrap() {
(line, None)
} else {
(prev, Some(rest))
}
None
}
2 changes: 1 addition & 1 deletion tests/source/environment_lines.tex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
\end{env2} \end{env1}

% environments all on same line
\begin{env1}\begin{env2}\end{env2}\end{env1} % with a comment
\begin{env1}\begin{env2}\end{env2}\end{env1} % with a comment \begin{env1}

% environments with extra brackets
\begin{env1}(a)(b \begin{env2}[c{d}e] \end{env2}[f]g)\end{env1}
Expand Down

0 comments on commit 0f48547

Please sign in to comment.