Skip to content

Commit a3867a5

Browse files
committed
add find_last_uncommented
1 parent 68ffa8d commit a3867a5

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/comment.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,7 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a s
976976

977977
pub(crate) trait FindUncommented {
978978
fn find_uncommented(&self, pat: &str) -> Option<usize>;
979+
fn find_last_uncommented(&self, pat: &str) -> Option<usize>;
979980
}
980981

981982
impl FindUncommented for str {
@@ -1001,6 +1002,19 @@ impl FindUncommented for str {
10011002
None => Some(self.len() - pat.len()),
10021003
}
10031004
}
1005+
1006+
fn find_last_uncommented(&self, pat: &str) -> Option<usize> {
1007+
if let Some(left) = self.find_uncommented(pat) {
1008+
let mut result = left;
1009+
// add 1 to use find_last_uncommented for &str after pat
1010+
while let Some(next) = self[(result + 1)..].find_last_uncommented(pat) {
1011+
result += next + 1;
1012+
}
1013+
Some(result)
1014+
} else {
1015+
None
1016+
}
1017+
}
10041018
}
10051019

10061020
// Returns the first byte position after the first comment. The given string
@@ -1882,6 +1896,16 @@ mod test {
18821896
check("\"/* abc", "abc", Some(4));
18831897
}
18841898

1899+
#[test]
1900+
fn test_find_last_uncommented() {
1901+
fn check(haystack: &str, needle: &str, expected: Option<usize>) {
1902+
assert_eq!(expected, haystack.find_last_uncommented(needle));
1903+
}
1904+
check("foo test bar test", "test", Some(13));
1905+
check("test,", "test", Some(0));
1906+
check("nothing", "test", None);
1907+
}
1908+
18851909
#[test]
18861910
fn test_filter_normal_code() {
18871911
let s = r#"

0 commit comments

Comments
 (0)