diff options
| author | bors <bors@rust-lang.org> | 2014-01-04 20:16:44 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-01-04 20:16:44 -0800 |
| commit | bf9a9afc7ca086f7ef2fafa7eb791c2ca92e0ddf (patch) | |
| tree | 2f9c3465cfa7ad7a9a3751b03a90ae2da3c89c76 /src/libstd/io | |
| parent | 57db916e86e36a703264b2aeba051b91a0fb03b3 (diff) | |
| parent | 195b23b34fe10bac94043650bdf806b51f949023 (diff) | |
| download | rust-bf9a9afc7ca086f7ef2fafa7eb791c2ca92e0ddf.tar.gz rust-bf9a9afc7ca086f7ef2fafa7eb791c2ca92e0ddf.zip | |
auto merge of #11310 : Dretch/rust/write_char, r=alexcrichton
Diffstat (limited to 'src/libstd/io')
| -rw-r--r-- | src/libstd/io/mem.rs | 10 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 8 |
2 files changed, 18 insertions, 0 deletions
diff --git a/src/libstd/io/mem.rs b/src/libstd/io/mem.rs index 5096e9237e3..aaba3232aa0 100644 --- a/src/libstd/io/mem.rs +++ b/src/libstd/io/mem.rs @@ -420,6 +420,16 @@ mod test { } #[test] + fn test_write_char() { + let mut writer = MemWriter::new(); + writer.write_char('a'); + writer.write_char('\n'); + writer.write_char('ệ'); + let mut r = BufReader::new(*writer.inner_ref()); + assert_eq!(r.read_to_str(), ~"a\nệ"); + } + + #[test] fn test_read_whole_string_bad() { let buf = [0xff]; let mut r = BufReader::new(buf); diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 9c181aa56c2..b7e185f8ed5 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -290,6 +290,7 @@ Out of scope #[allow(missing_doc)]; use cast; +use char::Char; use condition::Guard; use container::Container; use int; @@ -914,6 +915,13 @@ pub trait Writer { self.write(['\n' as u8]); } + /// Write a single char, encoded as UTF-8. + fn write_char(&mut self, c: char) { + let mut buf = [0u8, ..4]; + let n = c.encode_utf8(buf.as_mut_slice()); + self.write(buf.slice_to(n)); + } + /// Write the result of passing n through `int::to_str_bytes`. fn write_int(&mut self, n: int) { int::to_str_bytes(n, 10u, |bytes| self.write(bytes)) |
