@@ -976,6 +976,7 @@ fn left_trim_comment_line<'a>(line: &'a str, style: &CommentStyle<'_>) -> (&'a s
976
976
977
977
pub ( crate ) trait FindUncommented {
978
978
fn find_uncommented ( & self , pat : & str ) -> Option < usize > ;
979
+ fn find_last_uncommented ( & self , pat : & str ) -> Option < usize > ;
979
980
}
980
981
981
982
impl FindUncommented for str {
@@ -1001,6 +1002,19 @@ impl FindUncommented for str {
1001
1002
None => Some ( self . len ( ) - pat. len ( ) ) ,
1002
1003
}
1003
1004
}
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
+ }
1004
1018
}
1005
1019
1006
1020
// Returns the first byte position after the first comment. The given string
@@ -1882,6 +1896,16 @@ mod test {
1882
1896
check ( "\" /* abc" , "abc" , Some ( 4 ) ) ;
1883
1897
}
1884
1898
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
+
1885
1909
#[ test]
1886
1910
fn test_filter_normal_code ( ) {
1887
1911
let s = r#"
0 commit comments