Skip to content
Merged
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/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,16 @@ mod test {
assert_eq!(r.read_to_str(), ~"testingtesting\ntesting");
}

#[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];
Expand Down
8 changes: 8 additions & 0 deletions src/libstd/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ Out of scope
#[allow(missing_doc)];

use cast;
use char::Char;
use condition::Guard;
use container::Container;
use int;
Expand Down Expand Up @@ -902,6 +903,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))
Expand Down