@@ -26,38 +26,35 @@ fn lines_escaped<R: BufRead>(reader: R) -> impl Iterator<Item = io::Result<(Line
26
26
let mut processed: Option < String > = None ;
27
27
28
28
let escaped_line = loop {
29
- raw_line_num += 1 ; // Fine to increment first ; line numbers start at 1.
29
+ raw_line_num += 1 ; // Fine to increment before grabbing from `raw_lines` iterator ; line numbers start at 1.
30
30
31
31
let raw_line = match raw_lines. next ( ) {
32
+ // Next line in file is just regular text.
32
33
Some ( Ok ( text) ) => text,
33
- // If we hit an error, short-circuit this function. Can be caught by the consumer.
34
+ // Failed to read next line; throw the error up to the consumer.
34
35
Some ( Err ( err) ) => return Some ( Err ( err) ) ,
35
- None => {
36
- // If the last line of the file has a backslash on it, we might run out of lines while we're
37
- // currently holding onto processed line(s). We
38
- match processed {
39
- Some ( text) => break text,
40
- None => return None ,
41
- } ;
42
- } ,
36
+ // There are no more lines in the file. If `processed` is still Some, that means that the previous line
37
+ // ended with a backslash, and so we _should_ have found another line here. Otherwise, we can just
38
+ // return `None` and stop iteration.
39
+ None if processed. is_some ( ) => return Some ( Err ( io:: ErrorKind :: UnexpectedEof . into ( ) ) ) ,
40
+ None => return None ,
43
41
} ;
44
42
45
- // If we have any processed text currently pending, append this line. Otherwise, our new pending text is
46
- // just the freshly read value on its own.
43
+ // If we have any processed text currently pending, append this line to it . Otherwise, our new pending text
44
+ // is just the freshly read value on its own.
47
45
let mut pending = match processed {
48
46
Some ( prev) => prev + & raw_line,
49
47
None => raw_line,
50
48
} ;
51
49
52
- // Then, check if the newly appended-to
50
+ // If our collection of lines still ends with a backslash, trim it off and keep looping.
53
51
if pending. ends_with ( '\\' ) {
54
- // If our escaped text still ends with a backslash, trim off that backslash and keep looping. We are
55
- // safe to truncate by -1 because we now the last character is a plain ASCII backslash.
52
+ // Safe to truncate by 1 byte since we know the last character is a plain ASCII backslash
56
53
pending. truncate ( pending. len ( ) - 1 ) ;
57
54
processed = Some ( pending) ;
58
55
num_escaped += 1 ;
59
56
} else {
60
- // Otherwise, what we've got is the current line.
57
+ // Otherwise, what we've got is the final line.
61
58
break pending;
62
59
}
63
60
} ;
@@ -70,6 +67,15 @@ fn lines_escaped<R: BufRead>(reader: R) -> impl Iterator<Item = io::Result<(Line
70
67
}
71
68
72
69
70
+ /// Grab everything in a line up to the first line-comment character(s). Also trims the start and end of the string.
71
+ fn trim_line_comment < ' a > ( line : & ' a str , comment : & str ) -> & ' a str {
72
+ match line. find ( comment) {
73
+ Some ( i) => line[ 0 ..i] . trim ( ) ,
74
+ None => line[ 0 ..] . trim ( ) ,
75
+ }
76
+ }
77
+
78
+
73
79
/// Formats a range of line numbers as either `line N` or `lines N to M` if the range is longer than one line.
74
80
fn fmt_line_range ( range : & LineRange ) -> String {
75
81
// Range is exclusive, hence - 1
0 commit comments