From 8ec34e07010a9e69f16003cc66a990751e5d1bbd Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Sun, 18 Oct 2015 12:34:00 +0300 Subject: [PATCH] Fix input character passing escape_default is actually not the right function here, because it is meant for escaping strings with C-like rules. For example, character " becomes the string \" Since Strings are UTF-8, we can just directly allocate a String as an intermediate buffer. --- src/lib.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8539235..aa2f4b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -231,17 +231,14 @@ impl ImGui { io.key_map[key as usize] = mapping as i32; } pub fn add_input_character(&mut self, character: char) { - if !character.is_control() { - // 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 utf8_str: String = character.escape_default().collect(); - let mut bytes = utf8_str.into_bytes(); - // into_bytes does not produce a c-string, we must append the null terminator - bytes.push(0); - unsafe { - imgui_sys::ImGuiIO_AddInputCharactersUTF8(bytes.as_ptr() as *const i8); - } + // 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'); + unsafe { + imgui_sys::ImGuiIO_AddInputCharactersUTF8(string.as_ptr() as *const i8); } } pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } }