From e93f52cbfe77d8cbe1e4f4f1f8fa0bb22c4575a8 Mon Sep 17 00:00:00 2001 From: William G Underwood <42812654+WGUNDERWOOD@users.noreply.github.com> Date: Mon, 2 Sep 2024 15:09:18 +0100 Subject: [PATCH] Appease clippy and make regex faster --- src/comments.rs | 10 ++-------- src/format.rs | 4 ++-- src/ignore.rs | 4 ++-- src/indent.rs | 18 +++++++++--------- src/leave.rs | 8 ++++---- src/logging.rs | 24 +++++++++++------------- src/main.rs | 12 ++++++------ src/parse.rs | 4 ++-- src/regexes.rs | 14 ++++++-------- src/subs.rs | 8 ++++---- src/wrap.rs | 29 +++++++++-------------------- 11 files changed, 57 insertions(+), 78 deletions(-) diff --git a/src/comments.rs b/src/comments.rs index 2713eed..fc8dbae 100644 --- a/src/comments.rs +++ b/src/comments.rs @@ -34,15 +34,9 @@ pub fn find_comment_index(line: &str) -> Option { } pub fn remove_comment(line: &str, comment: Option) -> String { - match comment { - Some(c) => line.chars().take(c).collect(), - None => line.to_string(), - } + comment.map_or_else(|| line.to_string(), |c| line.chars().take(c).collect()) } pub fn get_comment(line: &str, comment: Option) -> String { - match comment { - Some(c) => line.chars().skip(c).collect(), - None => "".to_string(), - } + comment.map_or_else(|| "".to_string(), |c| line.chars().skip(c).collect()) } diff --git a/src/format.rs b/src/format.rs index ab42b3b..6e1ab2e 100644 --- a/src/format.rs +++ b/src/format.rs @@ -74,8 +74,8 @@ pub struct State { } impl State { - pub fn new() -> Self { - State { + pub const fn new() -> Self { + Self { linum_old: 0, linum_new: 0, ignore: Ignore::new(), diff --git a/src/ignore.rs b/src/ignore.rs index a05725d..d06964c 100644 --- a/src/ignore.rs +++ b/src/ignore.rs @@ -9,8 +9,8 @@ pub struct Ignore { } impl Ignore { - pub fn new() -> Self { - Ignore { + pub const fn new() -> Self { + Self { actual: false, visual: false, } diff --git a/src/indent.rs b/src/indent.rs index 33f291d..93eee52 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -19,8 +19,8 @@ pub struct Indent { } impl Indent { - pub fn new() -> Self { - Indent { + pub const fn new() -> Self { + Self { actual: 0, visual: 0, } @@ -33,9 +33,9 @@ fn get_diff(line: &str) -> i8 { let mut diff: i8 = 0; // other environments get single indents - if RE_ENV_BEGIN.is_match(line) { + if line.contains(ENV_BEGIN) { // documents get no global indentation - if RE_DOCUMENT_BEGIN.is_match(line) { + if line.contains(DOC_BEGIN) { return 0; }; diff += 1; @@ -44,9 +44,9 @@ fn get_diff(line: &str) -> i8 { diff += 1 }; } - } else if RE_ENV_END.is_match(line) { + } else if line.contains(ENV_END) { // documents get no global indentation - if RE_DOCUMENT_END.is_match(line) { + if line.contains(DOC_END) { return 0; }; diff -= 1; @@ -77,9 +77,9 @@ fn get_back(line: &str) -> i8 { } // other environments get single indents - if RE_ENV_END.is_match(line) { + if line.contains(ENV_END) { // documents get no global indentation - if RE_DOCUMENT_END.is_match(line) { + if line.contains(DOC_END) { return 0; }; // list environments get double indents for indenting items @@ -92,7 +92,7 @@ fn get_back(line: &str) -> i8 { }; // deindent items to make the rest of item environment appear indented - if RE_ITEM.is_match(line) { + if line.contains(ITEM) { back += 1; }; diff --git a/src/leave.rs b/src/leave.rs index 1b0d012..fc92aab 100644 --- a/src/leave.rs +++ b/src/leave.rs @@ -10,8 +10,8 @@ pub struct Leave { } impl Leave { - pub fn new() -> Self { - Leave { + pub const fn new() -> Self { + Self { actual: 0, visual: false, } @@ -45,11 +45,11 @@ pub fn get_leave( } fn get_leave_diff(line: &str) -> i8 { - if RE_ENV_BEGIN.is_match(line) + if line.contains(ENV_BEGIN) && RE_LEAVES_BEGIN.iter().any(|r| r.is_match(line)) { 1 - } else if RE_ENV_END.is_match(line) + } else if line.contains(ENV_END) && RE_LEAVES_END.iter().any(|r| r.is_match(line)) { -1 diff --git a/src/logging.rs b/src/logging.rs index 589a9dd..6bff7d0 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -80,7 +80,7 @@ fn get_log_style(log_level: Level) -> String { } } -fn get_log_level(args: &Cli) -> LevelFilter { +const fn get_log_level(args: &Cli) -> LevelFilter { if args.trace { LevelFilter::Trace } else if args.verbose { @@ -123,20 +123,18 @@ pub fn print_logs(mut logs: Vec) { logs.sort_by_key(|l| l.time); for log in logs { - let linum_new = match log.linum_new { - Some(i) => format!("Line {} ", i), - None => "".to_string(), - }; + let linum_new = log + .linum_new + .map_or_else(|| "".to_string(), |i| format!("Line {} ", i)); - let linum_old = match log.linum_old { - Some(i) => format!("({}). ", i), - None => "".to_string(), - }; + let linum_old = log + .linum_old + .map_or_else(|| "".to_string(), |i| format!("({}). ", i)); - let line = match &log.line { - Some(l) => l.trim_start().to_string(), - None => "".to_string(), - }; + let line = log + .line + .as_ref() + .map_or_else(|| "".to_string(), |l| l.trim_start().to_string()); let log_string = format!( "{}tex-fmt {}{}: {}{}{}{}{} {}{}", diff --git a/src/main.rs b/src/main.rs index 48e2ce6..44a410a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,10 @@ -#![warn(missing_docs)] +//#![warn(missing_docs)] #![warn(clippy::nursery)] -#![warn(clippy::cargo)] -#![warn(clippy::missing_docs_in_private_items)] -#![warn(clippy::pedantic)] -#![allow(clippy::wildcard_imports)] -#![allow(clippy::module_name_repetitions)] +//#![warn(clippy::cargo)] +//#![warn(clippy::missing_docs_in_private_items)] +//#![warn(clippy::pedantic)] +//#![allow(clippy::wildcard_imports)] +//#![allow(clippy::module_name_repetitions)] use clap::Parser; use log::Level::Error; diff --git a/src/parse.rs b/src/parse.rs index 141692d..b54da97 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -27,8 +27,8 @@ impl Cli { } #[cfg(test)] - pub fn new() -> Self { - Cli { + pub const fn new() -> Self { + Self { check: false, print: false, keep: false, diff --git a/src/regexes.rs b/src/regexes.rs index 2f96089..e259098 100644 --- a/src/regexes.rs +++ b/src/regexes.rs @@ -2,6 +2,12 @@ use crate::LINE_END; use lazy_static::lazy_static; use regex::Regex; +pub const ITEM: &str = "\\item"; +pub const DOC_BEGIN: &str = "\\begin{document}"; +pub const DOC_END: &str = "\\end{document}"; +pub const ENV_BEGIN: &str = "\\begin{"; +pub const ENV_END: &str = "\\end{"; + const LISTS: [&str; 5] = [ "itemize", "enumerate", @@ -16,14 +22,8 @@ lazy_static! { pub static ref RE_NEWLINES: Regex = Regex::new(&format!(r"{}{}({})+", LINE_END, LINE_END, LINE_END)) .unwrap(); - pub static ref RE_TABS: Regex = Regex::new(r"\t").unwrap(); pub static ref RE_TRAIL: Regex = Regex::new(&format!(r" +{}", LINE_END)).unwrap(); - pub static ref RE_ITEM: Regex = Regex::new(r"\\item").unwrap(); - pub static ref RE_DOCUMENT_BEGIN: Regex = - Regex::new(r"\\begin\{document\}").unwrap(); - pub static ref RE_DOCUMENT_END: Regex = - Regex::new(r"\\end\{document\}").unwrap(); pub static ref RE_LEAVES_BEGIN: Vec = LEAVES .iter() .map(|l| Regex::new(&format!(r"\\begin\{{{}}}", l)).unwrap()) @@ -32,8 +32,6 @@ lazy_static! { .iter() .map(|l| Regex::new(&format!(r"\\end\{{{}}}", l)).unwrap()) .collect(); - pub static ref RE_ENV_BEGIN: Regex = Regex::new(r"\\begin\{").unwrap(); - pub static ref RE_ENV_END: Regex = Regex::new(r"\\end\{").unwrap(); pub static ref RE_LISTS_BEGIN: Vec = LISTS .iter() .map(|l| Regex::new(&format!(r"\\begin\{{{}}}", l)).unwrap()) diff --git a/src/subs.rs b/src/subs.rs index a9a6c76..3a0ef63 100644 --- a/src/subs.rs +++ b/src/subs.rs @@ -15,7 +15,7 @@ pub fn remove_extra_newlines(text: &str) -> String { pub fn remove_tabs(text: &str) -> String { let replace = (0..TAB).map(|_| " ").collect::(); - RE_TABS.replace_all(text, replace).to_string() + text.replace('\t', &replace) } pub fn remove_trailing_spaces(text: &str) -> String { @@ -46,9 +46,9 @@ pub fn environments_new_line( if !state.leave.visual && !state.ignore.visual - && (RE_ENV_BEGIN.is_match(line) - || RE_ENV_END.is_match(line) - || RE_ITEM.is_match(line)) + && (line.contains(ENV_BEGIN) + || line.contains(ENV_END) + || line.contains(ITEM)) { let comment_index = find_comment_index(line); let comment = &get_comment(line, comment_index); diff --git a/src/wrap.rs b/src/wrap.rs index 628493e..3f5c96a 100644 --- a/src/wrap.rs +++ b/src/wrap.rs @@ -70,24 +70,13 @@ pub fn apply_wrap( } }; - match wrap_point { - Some(p) => { - let line_start = match comment_index { - Some(c) => { - if p > c { - "%" - } else { - "" - } - } - None => "", - }; - let mut line_1: String = line.chars().take(p).collect(); - line_1 = line_1.trim_end().to_string(); - let mut line_2: String = line.chars().skip(p).collect(); - line_2.insert_str(0, line_start); - Some((line_1, line_2)) - } - None => None, - } + wrap_point.map(|p| { + let line_start = + comment_index.map_or("", |c| if p > c { "%" } else { "" }); + let mut line_1: String = line.chars().take(p).collect(); + line_1 = line_1.trim_end().to_string(); + let mut line_2: String = line.chars().skip(p).collect(); + line_2.insert_str(0, line_start); + (line_1, line_2) + }) }