Skip to content

Commit

Permalink
Appease clippy and make regex faster
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Sep 2, 2024
1 parent 9abddb0 commit e93f52c
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 78 deletions.
10 changes: 2 additions & 8 deletions src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,9 @@ pub fn find_comment_index(line: &str) -> Option<usize> {
}

pub fn remove_comment(line: &str, comment: Option<usize>) -> 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<usize>) -> 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())
}
4 changes: 2 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions src/ignore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub struct Ignore {
}

impl Ignore {
pub fn new() -> Self {
Ignore {
pub const fn new() -> Self {
Self {
actual: false,
visual: false,
}
Expand Down
18 changes: 9 additions & 9 deletions src/indent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub struct Indent {
}

impl Indent {
pub fn new() -> Self {
Indent {
pub const fn new() -> Self {
Self {
actual: 0,
visual: 0,
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
};

Expand Down
8 changes: 4 additions & 4 deletions src/leave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct Leave {
}

impl Leave {
pub fn new() -> Self {
Leave {
pub const fn new() -> Self {
Self {
actual: 0,
visual: false,
}
Expand Down Expand Up @@ -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
Expand Down
24 changes: 11 additions & 13 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -123,20 +123,18 @@ pub fn print_logs(mut logs: Vec<Log>) {
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 {}{}: {}{}{}{}{} {}{}",
Expand Down
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 6 additions & 8 deletions src/regexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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<Regex> = LEAVES
.iter()
.map(|l| Regex::new(&format!(r"\\begin\{{{}}}", l)).unwrap())
Expand All @@ -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<Regex> = LISTS
.iter()
.map(|l| Regex::new(&format!(r"\\begin\{{{}}}", l)).unwrap())
Expand Down
8 changes: 4 additions & 4 deletions src/subs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>();
RE_TABS.replace_all(text, replace).to_string()
text.replace('\t', &replace)
}

pub fn remove_trailing_spaces(text: &str) -> String {
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 9 additions & 20 deletions src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit e93f52c

Please sign in to comment.