@@ -901,6 +901,45 @@ where
901
901
}
902
902
}
903
903
904
+ /// An iterator over the lines of a string, paired with the char kind at the
905
+ /// end of the line.
906
+ pub struct LineClasses < ' a > {
907
+ base : iter:: Peekable < CharClasses < std:: str:: Chars < ' a > > > ,
908
+ kind : FullCodeCharKind ,
909
+ }
910
+
911
+ impl < ' a > LineClasses < ' a > {
912
+ pub fn new ( s : & ' a str ) -> Self {
913
+ LineClasses {
914
+ base : CharClasses :: new ( s. chars ( ) ) . peekable ( ) ,
915
+ kind : FullCodeCharKind :: Normal ,
916
+ }
917
+ }
918
+ }
919
+
920
+ impl < ' a > Iterator for LineClasses < ' a > {
921
+ type Item = ( FullCodeCharKind , String ) ;
922
+
923
+ fn next ( & mut self ) -> Option < Self :: Item > {
924
+ if self . base . peek ( ) . is_none ( ) {
925
+ return None ;
926
+ }
927
+
928
+ let mut line = String :: new ( ) ;
929
+
930
+ while let Some ( ( kind, c) ) = self . base . next ( ) {
931
+ self . kind = kind;
932
+ if c == '\n' {
933
+ break ;
934
+ } else {
935
+ line. push ( c) ;
936
+ }
937
+ }
938
+
939
+ Some ( ( self . kind , line) )
940
+ }
941
+ }
942
+
904
943
/// Iterator over functional and commented parts of a string. Any part of a string is either
905
944
/// functional code, either *one* block comment, either *one* line comment. Whitespace between
906
945
/// comments is functional code. Line comments contain their ending newlines.
@@ -1141,8 +1180,7 @@ fn remove_comment_header(comment: &str) -> &str {
1141
1180
1142
1181
#[ cfg( test) ]
1143
1182
mod test {
1144
- use super :: { contains_comment, rewrite_comment, CharClasses , CodeCharKind , CommentCodeSlices ,
1145
- FindUncommented , FullCodeCharKind } ;
1183
+ use super :: * ;
1146
1184
use shape:: { Indent , Shape } ;
1147
1185
1148
1186
#[ test]
@@ -1298,4 +1336,10 @@ mod test {
1298
1336
check ( "\" /* abc */\" " , "abc" , Some ( 4 ) ) ;
1299
1337
check ( "\" /* abc" , "abc" , Some ( 4 ) ) ;
1300
1338
}
1339
+
1340
+ #[ test]
1341
+ fn test_remove_trailing_white_spaces ( ) {
1342
+ let s = format ! ( " r#\" \n test\n \" #" ) ;
1343
+ assert_eq ! ( remove_trailing_white_spaces( & s) , s) ;
1344
+ }
1301
1345
}
0 commit comments