auto merge of #11310 : Dretch/rust/write_char, r=alexcrichton

This commit is contained in:
bors 2014-01-04 20:16:44 -08:00
commit bf9a9afc7c
2 changed files with 18 additions and 0 deletions

View File

@ -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];

View File

@ -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))