@@ -244,87 +244,86 @@ impl Comments {
244244 let mut start_line = 1u64 ;
245245 let mut start_col = 1u64 ;
246246
247- let mut line = 1u64 ;
247+ let mut line_num = 1u64 ;
248248 let mut col = 1u64 ;
249249
250250 let mut in_single = false ;
251251 let mut in_multi = false ;
252252
253253 let mut buf = String :: new ( ) ;
254254
255- let mut chars = src. chars ( ) . peekable ( ) ;
256-
257- while let Some ( c) = chars. next ( ) {
258- match ( in_single, in_multi, c) {
259- ( false , false , '-' ) => {
260- if chars. peek ( ) . copied ( ) == Some ( '-' ) {
261- chars. next ( ) ;
262- in_single = true ;
263- start_line = line;
264- start_col = col;
265- buf. clear ( ) ;
266- col += 1 ;
255+ for line in src. lines ( ) {
256+ col = 1 ;
257+ let mut chars = line. chars ( ) . peekable ( ) ;
258+ while let Some ( c) = chars. next ( ) {
259+ match ( in_single, in_multi, c) {
260+ ( false , false , '-' ) => {
261+ if chars. peek ( ) . copied ( ) == Some ( '-' ) {
262+ chars. next ( ) ;
263+ in_single = true ;
264+ start_line = line_num;
265+ start_col = col;
266+ buf. clear ( ) ;
267+ col += 1 ;
268+ }
267269 }
268- }
269- ( false , false , '/' ) => {
270- if chars. peek ( ) . copied ( ) == Some ( '*' ) {
271- chars . next ( ) ;
272- in_multi = true ;
273- start_line = line ;
274- start_col = col ;
275- buf . clear ( ) ;
276- col += 1 ;
270+ ( false , false , '/' ) => {
271+ if chars . peek ( ) . copied ( ) == Some ( '*' ) {
272+ chars. next ( ) ;
273+ in_multi = true ;
274+ start_line = line_num ;
275+ start_col = col ;
276+ buf . clear ( ) ;
277+ col += 1 ;
278+ }
277279 }
278- }
279- ( false , false , '*' ) => {
280- if chars. peek ( ) . copied ( ) == Some ( '*' ) {
281- let loc = Location :: new ( line, col) ;
282- return Err ( CommentError :: UnmatchedMultilineCommentStart { location : loc } ) ;
280+ ( false , false , '*' ) => {
281+ if chars. peek ( ) . copied ( ) == Some ( '/' ) {
282+ let loc = Location :: new ( line_num, col) ;
283+ return Err ( CommentError :: UnmatchedMultilineCommentStart {
284+ location : loc,
285+ } ) ;
286+ }
283287 }
284- }
285- ( true , false , '\n' ) => {
286- let end_loc = Location :: new ( line, col) ;
287- comments. push ( Comment :: new (
288- CommentKind :: SingleLine ( buf. trim ( ) . to_string ( ) ) ,
289- Span :: new ( Location { line : start_line, column : start_col } , end_loc) ,
290- ) ) ;
291- in_single = false ;
292- buf. clear ( ) ;
293- }
294- ( false , true , '*' ) => {
295- if chars. peek ( ) . copied ( ) == Some ( '/' ) {
296- chars. next ( ) ;
297- let end_loc = Location :: new ( line, col + 1 ) ;
298- comments. push ( Comment :: new (
299- CommentKind :: MultiLine ( buf. trim ( ) . to_string ( ) ) ,
300- Span :: new ( Location { line : start_line, column : start_col } , end_loc) ,
301- ) ) ;
302- in_multi = false ;
303- buf. clear ( ) ;
304- col += 1 ;
305- } else {
306- buf. push ( '*' ) ;
288+ ( false , true , '*' ) => {
289+ if chars. peek ( ) . copied ( ) == Some ( '/' ) {
290+ chars. next ( ) ;
291+ let end_loc = Location :: new ( line_num, col + 1 ) ;
292+ comments. push ( Comment :: new (
293+ CommentKind :: MultiLine ( buf. trim ( ) . to_string ( ) ) ,
294+ Span :: new (
295+ Location { line : start_line, column : start_col } ,
296+ end_loc,
297+ ) ,
298+ ) ) ;
299+ in_multi = false ;
300+ buf. clear ( ) ;
301+ col += 1 ;
302+ } else {
303+ buf. push ( '*' ) ;
304+ }
305+ }
306+ ( false , true , ch) | ( true , false , ch) => {
307+ buf. push ( ch) ;
308+ }
309+ ( false , false , _) => { }
310+ ( true , true , _) => {
311+ unreachable ! ( "should not be possible to be in multiline and single line" )
307312 }
308313 }
309- ( false , true , '\n' ) => {
310- buf. push ( '\n' ) ;
311- }
312- ( false , true , ch) | ( true , false , ch) => {
313- buf. push ( ch) ;
314- }
315- ( false , false , _) => { }
316- ( true , true , _) => {
317- unreachable ! ( "should not be possible to be in multiline and single line" )
318- }
319- }
320- if c == '\n' {
321- line += 1 ;
322- col = 0 ;
323- } else {
324314 col += 1 ;
325315 }
316+ if in_single {
317+ in_single = false ;
318+ let end_loc = Location :: new ( line_num, col) ;
319+ comments. push ( Comment :: new (
320+ CommentKind :: SingleLine ( buf. trim ( ) . to_string ( ) ) ,
321+ Span :: new ( Location { line : start_line, column : start_col } , end_loc) ,
322+ ) ) ;
323+ buf. clear ( ) ;
324+ }
325+ line_num += 1 ;
326326 }
327-
328327 // EOF: close any open comments
329328 if in_multi {
330329 return Err ( CommentError :: UnterminatedMultiLineComment {
@@ -333,7 +332,7 @@ impl Comments {
333332 }
334333
335334 if in_single && !buf. is_empty ( ) {
336- let end_loc = Location :: new ( line , col) ;
335+ let end_loc = Location :: new ( line_num , col) ;
337336 comments. push ( Comment :: new (
338337 CommentKind :: SingleLine ( buf. trim_end ( ) . to_string ( ) ) ,
339338 Span :: new ( Location { line : start_line, column : start_col } , end_loc) ,
@@ -410,7 +409,7 @@ mod tests {
410409
411410 #[ test]
412411 fn multiline_comment_span ( ) {
413- let kind = CommentKind :: MultiLine ( "/* hello\n world */" . to_string ( ) ) ;
412+ let kind = CommentKind :: MultiLine ( "/* hello world */" . to_string ( ) ) ;
414413 let span = Span :: new ( Location { line : 1 , column : 1 } , Location { line : 2 , column : 9 } ) ;
415414
416415 let comment = Comment :: new ( kind. clone ( ) , span) ;
@@ -495,25 +494,25 @@ mod tests {
495494
496495 fn expected_multiline_comments ( ) -> & ' static [ & ' static str ] {
497496 & [
498- "Users table stores user account information \n multiline " ,
499- "Primary key \n multiline" ,
500- "Username for login \n multiline" ,
501- "Email address \n multiline" ,
502- "When the user registered \n multiline" ,
503- "Posts table stores blog posts \n multiline " ,
504- "Primary key \n multiline" ,
505- "Post title \n multiline" ,
506- "Foreign key linking to users \n multiline" ,
507- "Main body text \n multiline" ,
508- "When the post was created \n multiline" ,
497+ "Users table stores user account information multiline " ,
498+ "Primary key multiline" ,
499+ "Username for login multiline" ,
500+ "Email address multiline" ,
501+ "When the user registered multiline" ,
502+ "Posts table stores blog posts multiline " ,
503+ "Primary key multiline" ,
504+ "Post title multiline" ,
505+ "Foreign key linking to users multiline" ,
506+ "Main body text multiline" ,
507+ "When the post was created multiline" ,
509508 ]
510509 }
511510
512511 fn expected_mixed_comments ( ) -> & ' static [ & ' static str ] {
513512 & [
514513 "interstitial Comment above statements (should be ignored)" ,
515514 "Users table stores user account information" ,
516- "users interstitial comment \n (should be ignored)" ,
515+ "users interstitial comment (should be ignored)" ,
517516 "Primary key" ,
518517 "Id comment that is interstitial (should be ignored)" ,
519518 "Username for login" ,
@@ -556,8 +555,8 @@ mod tests {
556555 assert_eq ! ( first. span( ) . end( ) , & Location :: new( 1 , 47 ) ) ;
557556 let primary_key = & comments[ 1 ] ;
558557 assert_eq ! ( primary_key. kind( ) . comment( ) , "Primary key" ) ;
559- assert_eq ! ( primary_key. span( ) . start( ) , & Location :: new( 3 , 4 ) ) ;
560- assert_eq ! ( primary_key. span( ) . end( ) , & Location :: new( 3 , 18 ) ) ;
558+ assert_eq ! ( primary_key. span( ) . start( ) , & Location :: new( 3 , 5 ) ) ;
559+ assert_eq ! ( primary_key. span( ) . end( ) , & Location :: new( 3 , 19 ) ) ;
561560 assert ! (
562561 primary_key. span( ) . end( ) . column( ) > primary_key. span( ) . start( ) . column( ) ,
563562 "end column should be after start column" ,
@@ -588,19 +587,16 @@ mod tests {
588587 let comments = comments. comments ( ) ;
589588 assert_eq ! ( comments. len( ) , 11 ) ;
590589 let first = & comments[ 0 ] ;
591- assert_eq ! (
592- first. kind( ) . comment( ) ,
593- "Users table stores user account information \n multiline"
594- ) ;
590+ assert_eq ! ( first. kind( ) . comment( ) , "Users table stores user account information multiline" ) ;
595591 assert_eq ! ( first. span( ) . start( ) , & Location :: new( 1 , 1 ) ) ;
596592 assert_eq ! ( first. span( ) . end( ) . line( ) , 2 ) ;
597593 assert ! (
598594 first. span( ) . end( ) . column( ) > first. span( ) . start( ) . column( ) ,
599595 "end column should be after start column for first multiline comment" ,
600596 ) ;
601597 let primary_key = & comments[ 1 ] ;
602- assert_eq ! ( primary_key. kind( ) . comment( ) , "Primary key \n multiline" ) ;
603- assert_eq ! ( primary_key. span( ) . start( ) , & Location :: new( 4 , 4 ) ) ;
598+ assert_eq ! ( primary_key. kind( ) . comment( ) , "Primary key multiline" ) ;
599+ assert_eq ! ( primary_key. span( ) . start( ) , & Location :: new( 4 , 5 ) ) ;
604600 assert_eq ! ( primary_key. span( ) . end( ) . line( ) , 5 ) ;
605601 assert ! (
606602 primary_key. span( ) . end( ) . column( ) > primary_key. span( ) . start( ) . column( ) ,
0 commit comments