From ace021b94a235ee44ad3a305eee7e525bba132a3 Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Fri, 12 May 2017 23:35:43 +0300 Subject: [PATCH] Let's use encode_utf8 --- src/lib.rs | 10 +++------- src/string.rs | 4 ++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6a141a8..639cf93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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() } } diff --git a/src/string.rs b/src/string.rs index e6d8dee..f713eea 100644 --- a/src/string.rs +++ b/src/string.rs @@ -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());