@@ -5,7 +5,7 @@ extern crate memmap;
5
5
extern crate memchr;
6
6
7
7
use std:: path:: Path ;
8
- use std:: cmp;
8
+ use std:: cmp:: { min , max } ;
9
9
use std:: fmt;
10
10
11
11
use memmap:: { Mmap , Protection } ;
@@ -40,9 +40,7 @@ pub enum LineConfig<'a> {
40
40
multi_start : & ' a str ,
41
41
multi_end : & ' a str ,
42
42
} ,
43
- SingleOnly {
44
- single_start : & ' a str ,
45
- } ,
43
+ SingleOnly { single_start : & ' a str } ,
46
44
MultiOnly {
47
45
multi_start : & ' a str ,
48
46
multi_end : & ' a str ,
@@ -584,6 +582,7 @@ pub fn count_single(filepath: &str, single_start: &str) -> Count {
584
582
c
585
583
}
586
584
585
+ // TODO(cgag): don't forget to update this when fixing the lua bug
587
586
pub fn count_multi ( filepath : & str , multi_start : & str , multi_end : & str ) -> Count {
588
587
// this is a duplicate of count_single_multi without the check for single comment.
589
588
// Basically removes one branch. Probably pointless: benchmark.
@@ -630,7 +629,7 @@ pub fn count_multi(filepath: &str, multi_start: &str, multi_end: &str) -> Count
630
629
let mut found_code = false ;
631
630
' outer: while pos < trimmed. len ( ) {
632
631
// TODO(cgag): must be a less stupid way to do this
633
- for i in pos..( pos + cmp :: max ( start_len, end_len) + 1 ) {
632
+ for i in pos..pos + min ( max ( start_len, end_len) + 1 , trimmed . len ( ) - pos ) {
634
633
if !trimmed. is_char_boundary ( i) {
635
634
pos += 1 ;
636
635
continue ' outer;
@@ -642,7 +641,7 @@ pub fn count_multi(filepath: &str, multi_start: &str, multi_end: &str) -> Count
642
641
pos += start_len;
643
642
in_comment = true ;
644
643
} else if in_comment && pos + end_len <= trimmed. len ( ) &&
645
- & trimmed[ pos..( pos + end_len) ] == multi_end {
644
+ & trimmed[ pos..( pos + end_len) ] == multi_end {
646
645
pos += end_len;
647
646
in_comment = false ;
648
647
// TODO(cgag): should we bother handling whitespace here?
@@ -718,13 +717,17 @@ pub fn count_single_multi(filepath: &str,
718
717
} ;
719
718
c. lines += 1 ;
720
719
720
+
721
721
let trimmed = line. trim_left ( ) ;
722
722
if trimmed. is_empty ( ) {
723
723
c. blank += 1 ;
724
724
continue ;
725
725
} ;
726
726
727
- if !in_comment && trimmed. starts_with ( single_start) {
727
+ // TODO(cgag): Could be more efficient by only doing this third check when
728
+ // multi_start starts with the same chars as single_start, such as with lua (--, --[, ]]).
729
+ // Not sure it's necessary
730
+ if !in_comment && trimmed. starts_with ( single_start) && !trimmed. starts_with ( multi_start) {
728
731
c. comment += 1 ;
729
732
continue ;
730
733
}
@@ -748,7 +751,7 @@ pub fn count_single_multi(filepath: &str,
748
751
// TODO(cgag): must be a less stupid way to do this. At the
749
752
// very least don't recalculate max over and over. LLVM probably
750
753
// optimizes this but it seems dumb to depend on it?
751
- for i in pos..( pos + cmp :: max ( start_len, end_len) + 1 ) {
754
+ for i in pos..pos + min ( max ( start_len, end_len) + 1 , trimmed . len ( ) - pos ) {
752
755
if !trimmed. is_char_boundary ( i) {
753
756
pos += 1 ;
754
757
continue ' outer;
@@ -760,7 +763,7 @@ pub fn count_single_multi(filepath: &str,
760
763
pos += start_len;
761
764
in_comment = true ;
762
765
} else if in_comment && pos + end_len <= trimmed_len &&
763
- & trimmed[ pos..( pos + end_len) ] == multi_end {
766
+ & trimmed[ pos..( pos + end_len) ] == multi_end {
764
767
pos += end_len;
765
768
in_comment = false ;
766
769
// TODO(cgag): should we bother handling whitespace here?
0 commit comments