Skip to content

Commit

Permalink
Fix rdr.done() (again).
Browse files Browse the repository at this point in the history
It is much more convenient for rdr.done() to return true if and only if
no more records will be emitted on subsequent calls to next_bytes/next_str.
  • Loading branch information
BurntSushi committed Jul 31, 2016
1 parent 6c34691 commit 77547b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
18 changes: 14 additions & 4 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,8 @@ impl<R: io::Read> Reader<R> {
return NextField::Error(Error::Io(err));
}
if self.buf.len() == 0 {
self.eof = true;
if let StartRecord = self.state {
self.eof = true;
return self.next_eoc();
} else if let EndRecord = self.state {
self.state = StartRecord;
Expand All @@ -648,9 +648,6 @@ impl<R: io::Read> Reader<R> {
}
}
EndRecord => {
if self.record_term.is_crlf() && c == b'\n' {
self.bump();
}
self.state = StartRecord;
return self.next_eor();
}
Expand All @@ -661,6 +658,7 @@ impl<R: io::Read> Reader<R> {
} else if c == self.delimiter {
return self.next_data();
} else if self.is_record_term(c) {
self.bump_eor(c);
self.state = EndRecord;
return self.next_data();
} else {
Expand All @@ -674,6 +672,7 @@ impl<R: io::Read> Reader<R> {
self.state = StartField;
return self.next_data();
} else if self.is_record_term(c) {
self.bump_eor(c);
self.state = EndRecord;
return self.next_data();
} else {
Expand Down Expand Up @@ -704,6 +703,7 @@ impl<R: io::Read> Reader<R> {
self.state = StartField;
return self.next_data();
} else if self.is_record_term(c) {
self.bump_eor(c);
self.state = EndRecord;
return self.next_data();
} else {
Expand Down Expand Up @@ -814,6 +814,16 @@ impl<R: io::Read> Reader<R> {
self.byte_offset += 1;
}

#[inline]
fn bump_eor(&mut self, c: u8) {
if !self.record_term.is_crlf() || c != b'\r' {
return;
}
if self.buf.get(self.bufi) == Some(&b'\n') {
self.bump();
}
}

#[inline]
fn add(&mut self, c: u8) {
self.fieldbuf.push(c);
Expand Down
6 changes: 4 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,21 @@ fn extra_record_crlf() {
assert_eq!("foo", d.next_str().into_iter_result().unwrap().unwrap());
assert!(d.next_str().into_iter_result().is_none());
assert_eq!("1", d.next_str().into_iter_result().unwrap().unwrap());
assert!(d.next_str().into_iter_result().is_none());
assert!(!d.done());
assert!(d.next_str().into_iter_result().is_none());
assert!(d.done());
assert!(d.next_str().into_iter_result().is_none());
assert!(d.done());

let mut d = Reader::from_string("foo\r\n1\r\n").has_headers(false);
assert_eq!("foo", d.next_str().into_iter_result().unwrap().unwrap());
assert!(d.next_str().into_iter_result().is_none());
assert_eq!("1", d.next_str().into_iter_result().unwrap().unwrap());
assert!(d.next_str().into_iter_result().is_none());
assert!(!d.done());
assert!(d.next_str().into_iter_result().is_none());
assert!(d.done());
assert!(d.next_str().into_iter_result().is_none());
assert!(d.done());
}

#[test]
Expand Down

0 comments on commit 77547b5

Please sign in to comment.