Skip to content

Commit 4fc66a7

Browse files
committed
Use BufReader::lines() to parse out the lines
It already imlements the right behaviour, and it saves us implementing the state machine.
1 parent a0dbeea commit 4fc66a7

File tree

2 files changed

+19
-68
lines changed

2 files changed

+19
-68
lines changed

src/librustc_driver/args/mod.rs

Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,38 @@
1-
#![allow(dead_code)]
2-
31
use std::env;
42
use std::error;
53
use std::fmt;
64
use std::fs;
7-
use std::io;
5+
use std::io::{self, BufRead};
86
use std::str;
97

108
#[cfg(test)]
119
mod tests;
1210

13-
/// States for parsing text
14-
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
15-
enum State {
16-
Normal, // within normal text
17-
Cr, // just saw \r
18-
Lf, // just saw \n
19-
}
20-
2111
struct FileArgs {
2212
path: String,
2313
input: Vec<u8>,
24-
offset: usize,
2514
}
2615

2716
impl FileArgs {
28-
pub fn new(path: String, input: Vec<u8>) -> Self {
29-
FileArgs { path, input, offset: 0 }
17+
fn new(path: String, input: Vec<u8>) -> Self {
18+
FileArgs { path, input }
3019
}
31-
}
32-
33-
impl Iterator for FileArgs {
34-
type Item = Result<String, Error>;
35-
36-
fn next(&mut self) -> Option<Self::Item> {
37-
if self.offset >= self.input.len() {
38-
// All done
39-
return None;
40-
}
41-
42-
use State::*;
43-
let mut state = Normal;
44-
let start = self.offset;
45-
let mut end = start;
46-
47-
for (idx, b) in self.input[start..].iter().enumerate() {
48-
let idx = start + idx + 1;
49-
50-
self.offset = idx;
51-
52-
match (b, state) {
53-
(b'\r', Normal) => state = Cr,
54-
(b'\n', Normal) => state = Lf,
55-
56-
(b'\r', Lf) | (b'\n', Cr) => {
57-
// Two-character line break (accept \r\n and \n\r(?)), so consume them both
58-
break;
59-
}
60-
61-
(_, Cr) | (_, Lf) => {
62-
// Peeked at character after single-character line break, so rewind to visit it
63-
// next time around.
64-
self.offset = idx - 1;
65-
break;
66-
}
67-
68-
(_, _) => {
69-
end = idx;
70-
state = Normal;
71-
}
72-
}
73-
}
7420

75-
Some(
76-
String::from_utf8(self.input[start..end].to_vec())
77-
.map_err(|_| Error::Utf8Error(Some(self.path.clone()))),
78-
)
21+
fn lines(self) -> impl Iterator<Item = Result<String, Error>> {
22+
let Self { input, path } = self;
23+
io::Cursor::new(input).lines().map(move |res| {
24+
let path = path.clone();
25+
res.map_err(move |err| match err.kind() {
26+
io::ErrorKind::InvalidData => Error::Utf8Error(Some(path)),
27+
_ => Error::IOError(path, err),
28+
})
29+
})
7930
}
8031
}
8132

8233
pub struct ArgsIter {
8334
base: env::ArgsOs,
84-
file: Option<FileArgs>,
35+
file: Option<Box<dyn Iterator<Item = Result<String, Error>>>>,
8536
}
8637

8738
impl ArgsIter {
@@ -107,13 +58,12 @@ impl Iterator for ArgsIter {
10758
match arg {
10859
Some(Err(err)) => return Some(Err(err)),
10960
Some(Ok(ref arg)) if arg.starts_with("@") => {
110-
// can't not be utf-8 now
111-
let path = str::from_utf8(&arg.as_bytes()[1..]).unwrap();
112-
let file = match fs::read(path) {
113-
Ok(file) => file,
61+
let path = &arg[1..];
62+
let lines = match fs::read(path) {
63+
Ok(file) => FileArgs::new(path.to_string(), file).lines(),
11464
Err(err) => return Some(Err(Error::IOError(path.to_string(), err))),
11565
};
116-
self.file = Some(FileArgs::new(path.to_string(), file));
66+
self.file = Some(Box::new(lines));
11767
}
11868
Some(Ok(arg)) => return Some(Ok(arg)),
11969
None => return None,

src/librustc_driver/args/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn want_args(v: impl IntoIterator<Item = &'static str>) -> Vec<String> {
55
}
66

77
fn got_args(file: &[u8]) -> Result<Vec<String>, Error> {
8-
FileArgs::new(String::new(), file.to_vec()).collect()
8+
FileArgs::new(String::new(), file.to_vec()).lines().collect()
99
}
1010

1111
#[test]
@@ -78,6 +78,7 @@ fn multi_empty_end() {
7878
assert_eq!(got_args(file).unwrap(), want_args(vec!["foo", "bar", ""]));
7979
}
8080

81+
#[test]
8182
fn simple_eol_crlf() {
8283
let file = b"foo\r\n";
8384

0 commit comments

Comments
 (0)