Skip to content

Commit

Permalink
Reduce allocation in comment finding
Browse files Browse the repository at this point in the history
  • Loading branch information
WGUNDERWOOD committed Oct 7, 2024
1 parent c716d4d commit f885d31
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// Find the location where a comment begins in a line
pub fn find_comment_index(line: &str) -> Option<usize> {
let mut prev_c = ' ';
for (i, c) in line.chars().enumerate() {
for (i, c) in line.char_indices() {
if c == '%' && prev_c != '\\' {
return Some(i);
}
Expand All @@ -13,11 +13,11 @@ pub fn find_comment_index(line: &str) -> Option<usize> {
}

/// Remove a comment from the end of a line
pub fn remove_comment(line: &str, comment: Option<usize>) -> String {
comment.map_or_else(|| line.to_string(), |c| line.chars().take(c).collect())
pub fn remove_comment(line: &str, comment: Option<usize>) -> &str {
comment.map_or_else(|| line, |c| &line[0..c])
}

/// Extract a comment from the end of a line
pub fn get_comment(line: &str, comment: Option<usize>) -> String {
comment.map_or_else(String::new, |c| line.chars().skip(c).collect())
pub fn get_comment(line: &str, comment: Option<usize>) -> &str {
comment.map_or_else(|| "", |c| &line[c..])
}
4 changes: 2 additions & 2 deletions src/subs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ pub fn put_env_new_line(
);
}
let comment_index = find_comment_index(line);
let comment = &get_comment(line, comment_index);
let mut text = &remove_comment(line, comment_index);
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();
Expand Down

0 comments on commit f885d31

Please sign in to comment.