Skip to content

Commit bf9a9af

Browse files
committed
auto merge of #11310 : Dretch/rust/write_char, r=alexcrichton
2 parents 57db916 + 195b23b commit bf9a9af

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/libstd/io/mem.rs

+10
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,16 @@ mod test {
419419
assert_eq!(r.read_to_str(), ~"testingtesting\ntesting");
420420
}
421421
422+
#[test]
423+
fn test_write_char() {
424+
let mut writer = MemWriter::new();
425+
writer.write_char('a');
426+
writer.write_char('\n');
427+
writer.write_char('ệ');
428+
let mut r = BufReader::new(*writer.inner_ref());
429+
assert_eq!(r.read_to_str(), ~"a\nệ");
430+
}
431+
422432
#[test]
423433
fn test_read_whole_string_bad() {
424434
let buf = [0xff];

src/libstd/io/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ Out of scope
290290
#[allow(missing_doc)];
291291

292292
use cast;
293+
use char::Char;
293294
use condition::Guard;
294295
use container::Container;
295296
use int;
@@ -914,6 +915,13 @@ pub trait Writer {
914915
self.write(['\n' as u8]);
915916
}
916917

918+
/// Write a single char, encoded as UTF-8.
919+
fn write_char(&mut self, c: char) {
920+
let mut buf = [0u8, ..4];
921+
let n = c.encode_utf8(buf.as_mut_slice());
922+
self.write(buf.slice_to(n));
923+
}
924+
917925
/// Write the result of passing n through `int::to_str_bytes`.
918926
fn write_int(&mut self, n: int) {
919927
int::to_str_bytes(n, 10u, |bytes| self.write(bytes))

0 commit comments

Comments
 (0)