Skip to content

Commit

Permalink
ReaderUtil::each_byte shouldn't include EOF byte -- Issue #5056
Browse files Browse the repository at this point in the history
  • Loading branch information
stepancheg authored and User committed Jul 28, 2013
1 parent 5842ab3 commit b92d1ea
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions src/libstd/io.rs
Expand Up @@ -148,6 +148,9 @@ pub trait Reader {
/**
* Returns a boolean value: are we currently at EOF?
*
* Note that stream position may be already at the end-of-file point,
* but `eof` returns false until an attempt to read at that position.
*
* `eof` is conceptually similar to C's `feof` function.
*
* # Examples
Expand Down Expand Up @@ -724,15 +727,21 @@ impl<T:Reader> ReaderUtil for T {
}

fn each_byte(&self, it: &fn(int) -> bool) -> bool {
while !self.eof() {
if !it(self.read_byte()) { return false; }
loop {
match self.read_byte() {
-1 => break,
ch => if !it(ch) { return false; }
}
}
return true;
}

fn each_char(&self, it: &fn(char) -> bool) -> bool {
while !self.eof() {
if !it(self.read_char()) { return false; }
loop {
match self.read_char() {
eof if eof == (-1 as char) => break,
ch => if !it(ch) { return false; }
}
}
return true;
}
Expand Down Expand Up @@ -1858,6 +1867,31 @@ mod tests {
assert_eq!(frood, frood2);
}

#[test]
fn test_each_byte_each_char_file() {
// Issue #5056 -- shouldn't include trailing EOF.
let path = Path("tmp/lib-io-test-each-byte-each-char-file.tmp");

{
// create empty, enough to reproduce a problem
io::file_writer(&path, [io::Create]).unwrap();
}

{
let file = io::file_reader(&path).unwrap();
for file.each_byte() |_| {
fail!("must be empty");
}
}

{
let file = io::file_reader(&path).unwrap();
for file.each_char() |_| {
fail!("must be empty");
}
}
}

#[test]
fn test_readchars_empty() {
do io::with_str_reader("") |inp| {
Expand Down

5 comments on commit b92d1ea

@bors
Copy link
Contributor

@bors bors commented on b92d1ea Jul 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from huonw
at stepancheg@b92d1ea

@bors
Copy link
Contributor

@bors bors commented on b92d1ea Jul 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging stepancheg/rust/each_byte = b92d1ea into auto

@bors
Copy link
Contributor

@bors bors commented on b92d1ea Jul 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stepancheg/rust/each_byte = b92d1ea merged ok, testing candidate = 293ec2c

@bors
Copy link
Contributor

@bors bors commented on b92d1ea Jul 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 293ec2c

Please sign in to comment.