Browse Source

feat: unsafe version of `to_string` trait

master
Dnomd343 1 year ago
parent
commit
b4dfdd50dd
  1. 15
      src/klotski_core/ffi/codec.cc
  2. 3
      src/klotski_core/klotski.h
  3. 22
      test/ffi/codec.cc

15
src/klotski_core/ffi/codec.cc

@ -205,6 +205,11 @@ bool short_code_to_string(uint32_t short_code, char short_code_str[]) {
return true;
}
void short_code_to_string_unsafe(uint32_t short_code, char short_code_str[]) {
std::string str = ShortCode::unsafe_create(short_code).to_string();
strcpy(short_code_str, str.c_str());
}
bool short_code_from_string(const char short_code_str[], uint32_t *short_code) {
try {
*short_code = ShortCode::from_string(short_code_str).unwrap();
@ -227,6 +232,11 @@ bool common_code_to_string(uint64_t common_code, char common_code_str[]) {
return true;
}
void common_code_to_string_unsafe(uint64_t common_code, char common_code_str[]) {
std::string str = CommonCode::unsafe_create(common_code).to_string(false);
strcpy(common_code_str, str.c_str());
}
bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) {
if (!CommonCode::check(common_code)) {
return false;
@ -236,6 +246,11 @@ bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[])
return true;
}
void common_code_to_string_shorten_unsafe(uint64_t common_code, char common_code_str[]) {
std::string str = CommonCode::unsafe_create(common_code).to_string(true);
strcpy(common_code_str, str.c_str());
}
bool common_code_from_string(const char common_code_str[], uint64_t *common_code) {
try {
*common_code = CommonCode::from_string(common_code_str).unwrap();

3
src/klotski_core/klotski.h

@ -100,11 +100,14 @@ extern "C" {
extern const uint32_t SHORT_CODE_STR_SIZE;
EXTERN_FUNC bool short_code_to_string(uint32_t short_code, char short_code_str[]);
EXTERN_FUNC void short_code_to_string_unsafe(uint32_t short_code, char short_code_str[]);
EXTERN_FUNC bool short_code_from_string(const char short_code_str[], uint32_t *short_code);
extern const uint32_t COMMON_CODE_STR_SIZE;
EXTERN_FUNC bool common_code_to_string(uint64_t common_code, char common_code_str[]);
EXTERN_FUNC void common_code_to_string_unsafe(uint64_t common_code, char common_code_str[]);
EXTERN_FUNC bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]);
EXTERN_FUNC void common_code_to_string_shorten_unsafe(uint64_t common_code, char common_code_str[]);
EXTERN_FUNC bool common_code_from_string(const char common_code_str[], uint64_t *common_code);
#ifdef __cplusplus
}

22
test/ffi/codec.cc

@ -11,7 +11,10 @@ using klotski::ShortCode;
using klotski::BasicRanges;
const static uint32_t TEST_SHORT_CODE_OK = 4091296;
const static char TEST_SHORT_CODE_STR_OK[] = "4WVE1";
const static uint64_t TEST_COMMON_CODE_OK = 0x1'A9BF'0C00;
const static char TEST_COMMON_CODE_STR_OK[] = "1A9BF0C00";
const static char TEST_COMMON_CODE_STR_SHR_OK[] = "1A9BF0C";
const static uint64_t TEST_RAW_CODE_OK = 0x0603'EDF5'CAFF'F5E2;
const static uint32_t TEST_SHORT_CODE_ERR = 29670987;
@ -65,7 +68,11 @@ TEST(FFI, codec_string) {
EXPECT_EQ(short_code_from_string(TEST_SHORT_CODE_STR_ERR, &short_code), false);
EXPECT_EQ(short_code_to_string(TEST_SHORT_CODE_OK, short_code_buffer), true);
EXPECT_EQ(short_code_from_string(short_code_buffer, &short_code), true);
EXPECT_STREQ(short_code_buffer, TEST_SHORT_CODE_STR_OK);
short_code_to_string_unsafe(TEST_SHORT_CODE_OK, short_code_buffer);
EXPECT_STREQ(short_code_buffer, TEST_SHORT_CODE_STR_OK);
EXPECT_EQ(short_code_from_string(TEST_SHORT_CODE_STR_OK, &short_code), true);
EXPECT_EQ(short_code, TEST_SHORT_CODE_OK);
/// common code string verify
@ -75,11 +82,18 @@ TEST(FFI, codec_string) {
EXPECT_EQ(common_code_from_string(TEST_COMMON_CODE_STR_ERR, &common_code), false);
EXPECT_EQ(common_code_to_string(TEST_COMMON_CODE_OK, common_code_buffer), true);
EXPECT_EQ(common_code_from_string(common_code_buffer, &common_code), true);
EXPECT_EQ(common_code, TEST_COMMON_CODE_OK);
EXPECT_STREQ(common_code_buffer, TEST_COMMON_CODE_STR_OK);
common_code_to_string_unsafe(TEST_COMMON_CODE_OK, common_code_buffer);
EXPECT_STREQ(common_code_buffer, TEST_COMMON_CODE_STR_OK);
EXPECT_EQ(common_code_to_string_shorten(TEST_COMMON_CODE_OK, common_code_buffer), true);
EXPECT_EQ(common_code_from_string(common_code_buffer, &common_code), true);
EXPECT_STREQ(common_code_buffer, TEST_COMMON_CODE_STR_SHR_OK);
common_code_to_string_shorten_unsafe(TEST_COMMON_CODE_OK, common_code_buffer);
EXPECT_STREQ(common_code_buffer, TEST_COMMON_CODE_STR_SHR_OK);
EXPECT_EQ(common_code_from_string(TEST_COMMON_CODE_STR_OK, &common_code), true);
EXPECT_EQ(common_code, TEST_COMMON_CODE_OK);
EXPECT_EQ(common_code_from_string(TEST_COMMON_CODE_STR_SHR_OK, &common_code), true);
EXPECT_EQ(common_code, TEST_COMMON_CODE_OK);
}

Loading…
Cancel
Save