From 4575948ed2e599915afcc8991b9c679672d9a117 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 26 Feb 2023 21:02:25 +0800 Subject: [PATCH] update: rust klotski codec ffi --- src/cli/src/main.rs | 12 +++- src/rust_ffi/src/codec/ffi.rs | 106 ++++++++++++++++++++++++++++++---- 2 files changed, 105 insertions(+), 13 deletions(-) diff --git a/src/cli/src/main.rs b/src/cli/src/main.rs index b06897f..f39b6e8 100644 --- a/src/cli/src/main.rs +++ b/src/cli/src/main.rs @@ -19,6 +19,16 @@ fn main() { // let raw_code = klotski_ffi::common_code_to_raw_code(0x1A9BF0C00).unwrap(); // println!("raw code = {}", raw_code); - println!("short code: {}", klotski_ffi::short_code_to_string(12345).unwrap()); + // println!("short code: {}", klotski_ffi::short_code_to_string(12345).unwrap()); + // println!("short code: {}", klotski_ffi::short_code_to_string_unsafe(12345)); + // println!("{}", klotski_ffi::short_code_from_string("12345").unwrap()); + + // println!("common code: {}", klotski_ffi::common_code_to_string(0x1A9BF0C00).unwrap()); + // println!("common code: {}", klotski_ffi::common_code_to_string_unsafe(0x1A9BF0C00)); + // println!("common code: {}", klotski_ffi::common_code_to_string_shorten(0x1A9BF0C00).unwrap()); + // println!("common code: {}", klotski_ffi::common_code_to_string_shorten_unsafe(0x1A9BF0C00)); + + // println!("{}", klotski_ffi::common_code_from_string("1A9BF0C").unwrap()); + // println!("{}", klotski_ffi::common_code_from_string("1A9BF0C00").unwrap()); } diff --git a/src/rust_ffi/src/codec/ffi.rs b/src/rust_ffi/src/codec/ffi.rs index 2a7a03d..fdb634f 100644 --- a/src/rust_ffi/src/codec/ffi.rs +++ b/src/rust_ffi/src/codec/ffi.rs @@ -1,4 +1,3 @@ -use std::f32::consts::E; use std::ffi::{c_char, CString}; use super::Core; @@ -204,11 +203,7 @@ fn to_horizontal_mirror_unsafe(raw_code: u64) -> u64 { } } -// extern const uint32_t SHORT_CODE_STR_SIZE; -// EXTERN_FUNC bool short_code_to_string(uint32_t short_code, char short_code_str[]) NOEXCEPT; -// EXTERN_FUNC bool short_code_from_string(const char short_code_str[], uint32_t *short_code) NOEXCEPT; - -pub fn short_code_to_string(short_code: u32) -> Result { +fn short_code_to_string(short_code: u32) -> Result { unsafe { let mut buffer: Vec = vec![0; Core::SHORT_CODE_STR_SIZE as usize]; match Core::short_code_to_string(short_code, buffer.as_mut_ptr()) { @@ -224,11 +219,98 @@ pub fn short_code_to_string(short_code: u32) -> Result { } } -// TODO: add unsafe version +fn short_code_to_string_unsafe(short_code: u32) -> String { + unsafe { + let mut buffer: Vec = vec![0; Core::SHORT_CODE_STR_SIZE as usize]; + Core::short_code_to_string(short_code, buffer.as_mut_ptr()); + let mut result = String::new(); + for c in &buffer[..buffer.len() - 1] { + result.push(*c as u8 as char); + } + result + } +} + +fn short_code_from_string(short_code: &str) -> Result { + unsafe { + let mut result: u32 = 0; + match Core::short_code_from_string( + CString::new(short_code).unwrap().into_raw(), + &mut result + ) { + true => Ok(result), + _ => Err("invalid short code text"), + } + } +} + +fn common_code_to_string(common_code: u64) -> Result { + unsafe { + let mut buffer: Vec = vec![0; Core::COMMON_CODE_STR_SIZE as usize]; + match Core::common_code_to_string(common_code, buffer.as_mut_ptr()) { + true => { + let mut result = String::new(); + for c in &buffer[..buffer.len() - 1] { + result.push(*c as u8 as char); + } + Ok(result) + }, + _ => Err("invalid common code"), + } + } +} + +fn common_code_to_string_unsafe(common_code: u64) -> String { + unsafe { + let mut buffer: Vec = vec![0; Core::COMMON_CODE_STR_SIZE as usize]; + Core::common_code_to_string_unsafe(common_code, buffer.as_mut_ptr()); + let mut result = String::new(); + for c in &buffer[..buffer.len() - 1] { + result.push(*c as u8 as char); + } + result + } +} + +fn common_code_to_string_shorten(common_code: u64) -> Result { + unsafe { + let mut buffer: Vec = vec![0; Core::COMMON_CODE_STR_SIZE as usize]; + match Core::common_code_to_string_shorten(common_code, buffer.as_mut_ptr()) { + true => { + let mut result = String::new(); + for c in &buffer[..buffer.len() - 1] { + if *c == 0 { break; } + result.push(*c as u8 as char); + } + Ok(result) + }, + _ => Err("invalid common code"), + } + } +} -// extern const uint32_t COMMON_CODE_STR_SIZE; -// EXTERN_FUNC bool common_code_to_string(uint64_t common_code, char common_code_str[]) NOEXCEPT; -// EXTERN_FUNC bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) NOEXCEPT; -// EXTERN_FUNC bool common_code_from_string(const char common_code_str[], uint64_t *common_code) NOEXCEPT; +fn common_code_to_string_shorten_unsafe(common_code: u64) -> String { + unsafe { + let mut buffer: Vec = vec![0; Core::COMMON_CODE_STR_SIZE as usize]; + Core::common_code_to_string_shorten(common_code, buffer.as_mut_ptr()); + let mut result = String::new(); + for c in &buffer[..buffer.len() - 1] { + if *c == 0 { break; } + result.push(*c as u8 as char); + } + result + } +} -// TODO: add unsafe version +fn common_code_from_string(common_code: &str) -> Result { + unsafe { + let mut result: u64 = 0; + match Core::common_code_from_string( + CString::new(common_code).unwrap().into_raw(), + &mut result + ) { + true => Ok(result), + _ => Err("invalid common code text"), + } + } +}