Let's use encode_utf8

This commit is contained in:
Joonas Javanainen 2017-05-12 23:35:43 +03:00
parent 4dfdf0ae5e
commit ace021b94a
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
2 changed files with 7 additions and 7 deletions

View File

@ -228,14 +228,10 @@ impl ImGui {
io.key_map[key as usize] = mapping as i32;
}
pub fn add_input_character(&mut self, character: char) {
// TODO: This is slightly better. We should use char::encode_utf8 when it stabilizes
// to allow us to skip the string intermediate since we can then go directly
// to bytes
let mut string = String::new();
string.push(character);
string.push('\0');
let mut buf = [0; 5];
character.encode_utf8(&mut buf);
unsafe {
imgui_sys::ImGuiIO_AddInputCharactersUTF8(string.as_ptr() as *const _);
imgui_sys::ImGuiIO_AddInputCharactersUTF8(buf.as_ptr() as *const _);
}
}
pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } }

View File

@ -29,6 +29,10 @@ impl ImString {
self.0.clear();
self.0.push(b'\0');
}
pub fn push(&mut self, ch: char) {
let mut buf = [0; 4];
self.push_str(ch.encode_utf8(&mut buf));
}
pub fn push_str(&mut self, string: &str) {
self.refresh_len();
self.0.extend_from_slice(string.as_bytes());