@@ -192,25 +192,11 @@ fn stripped_filtered_line<'a>(s: &'a str) -> Option<&'a str> {
192
192
/// Returns a new string with all consecutive whitespace collapsed into
193
193
/// single spaces.
194
194
///
195
- /// The input is assumed to be already trimmed.
195
+ /// Any leading or trailing whitespace be trimmed.
196
196
fn collapse_whitespace ( s : & str ) -> String {
197
- let mut buffer = String :: with_capacity ( s. len ( ) ) ;
198
- let mut previous_char_is_whitespace = false ;
199
-
200
- for c in s. chars ( ) {
201
- if c. is_whitespace ( ) {
202
- if !previous_char_is_whitespace {
203
- buffer. push ( ' ' ) ;
204
- }
205
-
206
- previous_char_is_whitespace = true ;
207
- } else {
208
- buffer. push ( c) ;
209
- previous_char_is_whitespace = false ;
210
- }
211
- }
212
-
213
- buffer
197
+ s. split ( |c : char | c. is_whitespace ( ) ) . filter ( |s| {
198
+ !s. is_empty ( )
199
+ } ) . collect :: < Vec < _ > > ( ) . connect ( " " )
214
200
}
215
201
216
202
thread_local ! ( static USED_HEADER_MAP : RefCell <HashMap <String , usize >> = {
@@ -623,8 +609,9 @@ mod tests {
623
609
}
624
610
625
611
t ( "foo" , "foo" ) ;
626
- t ( "foo bar" , "foo bar" ) ;
627
- t ( "foo bar\n baz" , "foo bar baz" ) ;
628
- t ( "foo bar \n baz\t \t qux" , "foo bar baz qux" ) ;
612
+ t ( "foo bar baz" , "foo bar baz" ) ;
613
+ t ( " foo bar" , "foo bar" ) ;
614
+ t ( "\t foo bar\n baz" , "foo bar baz" ) ;
615
+ t ( "foo bar \n baz\t \t qux\n " , "foo bar baz qux" ) ;
629
616
}
630
617
}
0 commit comments