From ebb57f185a877c1d88ef3fcd09a192451449afb9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 30 Jan 2023 21:41:24 +0800 Subject: [PATCH] feat: codec ffi support unsafe convert --- src/klotski_core/ffi/codec.cc | 28 ++++++++++++++++++++++++++++ src/klotski_core/klotski.h | 9 ++++++++- src/klotski_core/raw_code/convert.cc | 4 ---- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/klotski_core/ffi/codec.cc b/src/klotski_core/ffi/codec.cc index 9a4a33e..7bc467c 100644 --- a/src/klotski_core/ffi/codec.cc +++ b/src/klotski_core/ffi/codec.cc @@ -106,6 +106,34 @@ bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code) { //////////////////////////////////////////////////////////////////////////////// +uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code) { + return RawCode::unsafe_create(raw_code) + .to_common_code().to_short_code().unwrap(); +} + +uint64_t short_code_to_raw_code_unsafe(uint32_t short_code) { + return ShortCode::unsafe_create(short_code) + .to_common_code().to_raw_code().unwrap(); +} + +uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code) { + return RawCode::unsafe_create(raw_code).to_common_code().unwrap(); +} + +uint64_t common_code_to_raw_code_unsafe(uint64_t common_code) { + return CommonCode::unsafe_create(common_code).to_raw_code().unwrap(); +} + +uint64_t short_code_to_common_code_unsafe(uint32_t short_code) { + return ShortCode::unsafe_create(short_code).to_common_code().unwrap(); +} + +uint32_t common_code_to_short_code_unsafe(uint64_t common_code) { + return CommonCode::unsafe_create(common_code).to_short_code().unwrap(); +} + +//////////////////////////////////////////////////////////////////////////////// + const uint32_t SHORT_CODE_STR_SIZE = 6; bool short_code_to_string(uint32_t short_code, char short_code_str[]) { diff --git a/src/klotski_core/klotski.h b/src/klotski_core/klotski.h index ae48a71..a935714 100644 --- a/src/klotski_core/klotski.h +++ b/src/klotski_core/klotski.h @@ -74,7 +74,6 @@ extern "C" { extern bool short_code_check(uint32_t short_code); extern bool common_code_check(uint64_t common_code); - // TODO: allow unsafe convert extern bool raw_code_to_short_code(uint64_t raw_code, uint32_t *short_code); extern bool short_code_to_raw_code(uint32_t short_code, uint64_t *raw_code); extern bool raw_code_to_common_code(uint64_t raw_code, uint64_t *common_code); @@ -82,6 +81,14 @@ extern "C" { extern bool short_code_to_common_code(uint32_t short_code, uint64_t *common_code); extern bool common_code_to_short_code(uint64_t common_code, uint32_t *short_code); + // TODO: allow unsafe convert + extern uint32_t raw_code_to_short_code_unsafe(uint64_t raw_code); + extern uint64_t short_code_to_raw_code_unsafe(uint32_t short_code); + extern uint64_t raw_code_to_common_code_unsafe(uint64_t raw_code); + extern uint64_t common_code_to_raw_code_unsafe(uint64_t common_code); + extern uint64_t short_code_to_common_code_unsafe(uint32_t short_code); + extern uint32_t common_code_to_short_code_unsafe(uint64_t common_code); + extern const uint32_t SHORT_CODE_STR_SIZE; extern bool short_code_to_string(uint32_t short_code, char short_code_str[]); extern bool short_code_from_string(const char short_code_str[], uint32_t *short_code); diff --git a/src/klotski_core/raw_code/convert.cc b/src/klotski_core/raw_code/convert.cc index 9f86417..d99a452 100644 --- a/src/klotski_core/raw_code/convert.cc +++ b/src/klotski_core/raw_code/convert.cc @@ -8,10 +8,6 @@ using klotski::RawCodeException; /// RawCode to CommonCode CommonCode RawCode::to_common_code() const { - if (!RawCode::check(code)) { - throw RawCodeException("raw code invalid"); - } - /// pass raw code checker -> common code must valid return CommonCode::unsafe_create(RawCode::compact(code)); }