Skip to content

Commit 293ec2c

Browse files
committed
auto merge of #8081 : stepancheg/rust/each_byte, r=huonw
Took a patch by @martine, and fixed a tests: replaced with_str_reader with file_reader, because with_str_reader seems not to reveal the problem.
2 parents 5842ab3 + b92d1ea commit 293ec2c

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/libstd/io.rs

+38-4
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ pub trait Reader {
148148
/**
149149
* Returns a boolean value: are we currently at EOF?
150150
*
151+
* Note that stream position may be already at the end-of-file point,
152+
* but `eof` returns false until an attempt to read at that position.
153+
*
151154
* `eof` is conceptually similar to C's `feof` function.
152155
*
153156
* # Examples
@@ -724,15 +727,21 @@ impl<T:Reader> ReaderUtil for T {
724727
}
725728

726729
fn each_byte(&self, it: &fn(int) -> bool) -> bool {
727-
while !self.eof() {
728-
if !it(self.read_byte()) { return false; }
730+
loop {
731+
match self.read_byte() {
732+
-1 => break,
733+
ch => if !it(ch) { return false; }
734+
}
729735
}
730736
return true;
731737
}
732738

733739
fn each_char(&self, it: &fn(char) -> bool) -> bool {
734-
while !self.eof() {
735-
if !it(self.read_char()) { return false; }
740+
loop {
741+
match self.read_char() {
742+
eof if eof == (-1 as char) => break,
743+
ch => if !it(ch) { return false; }
744+
}
736745
}
737746
return true;
738747
}
@@ -1858,6 +1867,31 @@ mod tests {
18581867
assert_eq!(frood, frood2);
18591868
}
18601869

1870+
#[test]
1871+
fn test_each_byte_each_char_file() {
1872+
// Issue #5056 -- shouldn't include trailing EOF.
1873+
let path = Path("tmp/lib-io-test-each-byte-each-char-file.tmp");
1874+
1875+
{
1876+
// create empty, enough to reproduce a problem
1877+
io::file_writer(&path, [io::Create]).unwrap();
1878+
}
1879+
1880+
{
1881+
let file = io::file_reader(&path).unwrap();
1882+
for file.each_byte() |_| {
1883+
fail!("must be empty");
1884+
}
1885+
}
1886+
1887+
{
1888+
let file = io::file_reader(&path).unwrap();
1889+
for file.each_char() |_| {
1890+
fail!("must be empty");
1891+
}
1892+
}
1893+
}
1894+
18611895
#[test]
18621896
fn test_readchars_empty() {
18631897
do io::with_str_reader("") |inp| {

0 commit comments

Comments
 (0)