diff --git a/src/comments.rs b/src/comments.rs index c93ee9a..e95e6ce 100644 --- a/src/comments.rs +++ b/src/comments.rs @@ -3,7 +3,7 @@ /// Find the location where a comment begins in a line pub fn find_comment_index(line: &str) -> Option { 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); } @@ -13,11 +13,11 @@ pub fn find_comment_index(line: &str) -> Option { } /// Remove a comment from the end of a line -pub fn remove_comment(line: &str, comment: Option) -> String { - comment.map_or_else(|| line.to_string(), |c| line.chars().take(c).collect()) +pub fn remove_comment(line: &str, comment: Option) -> &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) -> String { - comment.map_or_else(String::new, |c| line.chars().skip(c).collect()) +pub fn get_comment(line: &str, comment: Option) -> &str { + comment.map_or_else(|| "", |c| &line[c..]) } diff --git a/src/subs.rs b/src/subs.rs index ac66e3a..30b9894 100644 --- a/src/subs.rs +++ b/src/subs.rs @@ -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();