Skip to content

Commit 4449eb0

Browse files
stepanchegUser
authored and
User
committed
---
yaml --- r: 143342 b: refs/heads/try2 c: b92d1ea h: refs/heads/master v: v3
1 parent 5b42838 commit 4449eb0

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 5842ab36b81f0a3e8d6dd48a200bc405ad19ca96
8+
refs/heads/try2: b92d1ea723034c3f9adc10a6da11b124a20c57b1
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/io.rs

Lines changed: 38 additions & 4 deletions
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)