From 0f485479646fff15c155cc632221bba4df821e2f Mon Sep 17 00:00:00 2001 From: Cyprien de Saint Guilhem Date: Thu, 10 Oct 2024 18:10:00 +0200 Subject: [PATCH] Split environment and item lines without allocating This does not compile because the `format` function cannot yet deal with the new return type. --- src/regexes.rs | 3 ++ src/subs.rs | 44 +++++++++++------------------- tests/source/environment_lines.tex | 2 +- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/src/regexes.rs b/src/regexes.rs index 346a1f1..0f4906d 100644 --- a/src/regexes.rs +++ b/src/regexes.rs @@ -51,4 +51,7 @@ lazy_static! { Regex::new(r"(?P\S.*?)(?P\\end\{)").unwrap(); pub static ref RE_ITEM_SHARED_LINE: Regex = Regex::new(r"(?P\S.*?)(?P\\item)").unwrap(); + pub static ref RE_ENV_ITEM_SHARED_LINE: Regex = + Regex::new(r"(?P\S.*?)(?P(\\begin|\\end|\\item)\{.*)") + .unwrap(); } diff --git a/src/subs.rs b/src/subs.rs index 30b9894..3198f02 100644 --- a/src/subs.rs +++ b/src/subs.rs @@ -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, -) -> Option<(String, String)> { +) -> (&'a str, Option<&'a str>) { if args.trace { record_line_log( logs, @@ -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 } diff --git a/tests/source/environment_lines.tex b/tests/source/environment_lines.tex index 70890ac..969f697 100644 --- a/tests/source/environment_lines.tex +++ b/tests/source/environment_lines.tex @@ -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}