Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::io: Add Chars iterator for Buffer. #12120

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,16 @@ mod test {
assert_eq!(reader.read_char(), Ok('ß'));
}

#[test]
fn test_chars() {
let buf = [195u8, 159u8, 'a' as u8];
let mut reader = BufferedReader::with_capacity(1, BufReader::new(buf));
let mut it = reader.chars();
assert_eq!(it.next(), Some('ß'));
assert_eq!(it.next(), Some('a'));
assert_eq!(it.next(), None);
}

#[bench]
fn bench_buffered_reader(bh: &mut Harness) {
bh.iter(|| {
Expand Down
35 changes: 35 additions & 0 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,30 @@ impl<'r, T: Buffer> Iterator<~str> for Lines<'r, T> {
}
}

/// An iterator that reads a utf8-encoded character on each iteration,
/// until `.read_char()` returns `None`.
///
/// # Notes about the Iteration Protocol
///
/// The `Chars` may yield `None` and thus terminate
/// an iteration, but continue to yield elements if iteration
/// is attempted again.
///
/// # Error
///
/// This iterator will swallow all I/O errors, transforming `Err` values to
/// `None`. If errors need to be handled, it is recommended to use the
/// `read_char` method directly.
pub struct Chars<'r, T> {
priv buffer: &'r mut T
}

impl<'r, T: Buffer> Iterator<char> for Chars<'r, T> {
fn next(&mut self) -> Option<char> {
self.buffer.read_char().ok()
}
}

/// A Buffer is a type of reader which has some form of internal buffering to
/// allow certain kinds of reading operations to be more optimized than others.
/// This type extends the `Reader` trait with a few methods that are not
Expand Down Expand Up @@ -1146,6 +1170,17 @@ pub trait Buffer: Reader {
None => Err(standard_error(InvalidInput))
}
}

/// Create an iterator that reads a utf8-encoded character on each iteration until EOF.
///
/// # Error
///
/// This iterator will transform all error values to `None`, discarding the
/// cause of the error. If this is undesirable, it is recommended to call
/// `read_char` directly.
fn chars<'r>(&'r mut self) -> Chars<'r, Self> {
Chars { buffer: self }
}
}

pub enum SeekStyle {
Expand Down