small performance improvements

This commit is contained in:
LordMZTE 2020-08-25 17:27:35 +02:00
parent ac61409ef4
commit 4fd3ec98cc

View file

@ -1,6 +1,6 @@
use winapi::um::winuser::GetKeyNameTextW;
use std::ffi::OsString;
use wio::wide::FromWide;
use wio::wide::{FromWide, ToWide};
pub mod logger;
@ -9,6 +9,8 @@ pub fn scan_code_to_key_name(scan_code: u32) -> String {
unsafe {
let mut out = [0u16; 128];
GetKeyNameTextW((scan_code << 16) as i32, (&mut out).as_mut_ptr(), 128);
OsString::from_wide(&out).to_str().unwrap().replace('\0', "")
let null_pos = out.iter().position(|x| *x == b'\0' as u16).unwrap_or_else(|| out.len());
//use to_string_lossy to avoid unicode checks for better performance. if the windows api screws up thats not my fault :P
OsString::from_wide(&out[..null_pos]).to_string_lossy().into()
}
}