diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2014-01-07 08:48:44 -0800 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2014-01-08 13:25:19 -0800 |
| commit | 75165f78de69379bd765a85ab01e20ac9eef7893 (patch) | |
| tree | a5a0005937c1e94396db16454c42d3ea9206ac0b /src/libstd | |
| parent | 97005c0068f22032cbc30205a25dc88851eba424 (diff) | |
| download | rust-75165f78de69379bd765a85ab01e20ac9eef7893.tar.gz rust-75165f78de69379bd765a85ab01e20ac9eef7893.zip | |
Robustly read remaining bytes in a character
Closes #11372
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/io/buffered.rs | 8 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 12 |
2 files changed, 16 insertions, 4 deletions
diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index fa85f286af7..47213e39434 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -322,7 +322,7 @@ mod test { use io; use prelude::*; use super::*; - use super::super::mem::{MemReader, MemWriter}; + use super::super::mem::{MemReader, MemWriter, BufReader}; use Harness = extra::test::BenchHarness; /// A type, free to create, primarily intended for benchmarking creation of wrappers that, just @@ -526,6 +526,12 @@ mod test { assert_eq!(reader.read(buf), None); } + #[test] + fn read_char_buffered() { + let buf = [195u8, 159u8]; + let mut reader = BufferedReader::with_capacity(1, BufReader::new(buf)); + assert_eq!(reader.read_char(), Some('ß')); + } #[bench] fn bench_buffered_reader(bh: &mut Harness) { diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 823ca80a8a1..5f597847057 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -1212,9 +1212,15 @@ pub trait Buffer: Reader { }; if width == 0 { return None } // not uf8 let mut buf = [0, ..4]; - match self.read(buf.mut_slice_to(width)) { - Some(n) if n == width => {} - Some(..) | None => return None // read error + { + let mut start = 0; + loop { + match self.read(buf.mut_slice(start, width)) { + Some(n) if n == width - start => break, + Some(n) if n < width - start => { start += n; } + Some(..) | None => return None // read error + } + } } match str::from_utf8_opt(buf.slice_to(width)) { Some(s) => Some(s.char_at(0)), |
