From f3252455bbb7cea64129a3796b84280eb3d9ddbc Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 9 Jan 2023 21:01:32 +0800 Subject: [PATCH] feat: CommonCode unsafe create --- src/common_code/common_code.cc | 14 ++++++++++++-- src/common_code/common_code.h | 3 +++ src/main.cc | 6 ++++++ src/short_code/convert.cc | 4 ++-- src/short_code/short_code.cc | 4 ++-- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/common_code/common_code.cc b/src/common_code/common_code.cc index e8b4be9..2772180 100644 --- a/src/common_code/common_code.cc +++ b/src/common_code/common_code.cc @@ -8,14 +8,20 @@ inline uint8_t last_zero_num(uint32_t bin) { // get last zero number return __builtin_popcount(bin >> 1); } -uint64_t CommonCode::unwrap() const { - return code; // get raw uint64_t code +uint64_t CommonCode::unwrap() const { // get raw uint64_t code + return code; } ShortCode CommonCode::to_short_code() const { // convert to short code return ShortCode(*this); } +CommonCode CommonCode::unsafe_create(uint64_t code) { // create CommonCode without check + auto common_code = CommonCode(); // init directly + common_code.code = code; + return common_code; +} + CommonCode::CommonCode(uint64_t common_code) { if (!CommonCode::check(common_code)) { // check input common code throw std::invalid_argument("invalid common code"); @@ -23,6 +29,10 @@ CommonCode::CommonCode(uint64_t common_code) { code = common_code; } +CommonCode::CommonCode(const ShortCode &short_code) { + code = short_code.to_common_code().unwrap(); // init from short code +} + CommonCode::CommonCode(const std::string &common_code_str) { if (common_code_str.length() > 9 || common_code_str.length() == 0) { // check string length throw std::invalid_argument("common code format error"); diff --git a/src/common_code/common_code.h b/src/common_code/common_code.h index c8529d3..79d66d1 100644 --- a/src/common_code/common_code.h +++ b/src/common_code/common_code.h @@ -11,11 +11,14 @@ public: uint64_t unwrap() const; ShortCode to_short_code() const; static bool check(uint64_t common_code); + static CommonCode unsafe_create(uint64_t code); std::string to_string(bool shorten = false) const; explicit CommonCode(uint64_t common_code); + explicit CommonCode(const ShortCode &short_code); explicit CommonCode(const std::string &common_code_str); private: uint64_t code; + CommonCode() = default; }; diff --git a/src/main.cc b/src/main.cc index ffd86b9..a91bd59 100644 --- a/src/main.cc +++ b/src/main.cc @@ -117,6 +117,12 @@ int main() { // std::cout << ShortCode(CommonCode(0x6EC0F8800), ShortCode::NORMAL).to_string() << std::endl; // std::cout << ShortCode(CommonCode(0x6EC0F8800), ShortCode::FAST).to_string() << std::endl; +// std::cout << CommonCode(0x6EC0F8800).to_short_code().to_string() << std::endl; + + std::cout << CommonCode(ShortCode(14323231)).to_string() << std::endl; + std::cout << ShortCode(14323231).to_common_code().to_string() << std::endl; + + std::cout << ShortCode(CommonCode(0x6EC0F8800)).to_string() << std::endl; std::cout << CommonCode(0x6EC0F8800).to_short_code().to_string() << std::endl; return 0; diff --git a/src/short_code/convert.cc b/src/short_code/convert.cc index 9f1cdfb..8a3420b 100644 --- a/src/short_code/convert.cc +++ b/src/short_code/convert.cc @@ -5,9 +5,9 @@ CommonCode ShortCode::to_common_code() const { // convert to common code if (ShortCode::check_mode() == ShortCode::NORMAL) { - return CommonCode(tiny_decode(code)); // using normal mode + return CommonCode::unsafe_create(tiny_decode(code)); // using normal mode } - return CommonCode(all_cases_list[code]); // using fast mode + return CommonCode::unsafe_create(all_cases_list[code]); // using fast mode } ShortCode::ShortCode(const CommonCode &common_code) { // convert from common code diff --git a/src/short_code/short_code.cc b/src/short_code/short_code.cc index 9443665..43aad3e 100644 --- a/src/short_code/short_code.cc +++ b/src/short_code/short_code.cc @@ -2,8 +2,8 @@ #include "short_code.h" #include "short_code_chars.h" -uint32_t ShortCode::unwrap() const { - return code; // get raw uint32_t code +uint32_t ShortCode::unwrap() const { // get raw uint32_t code + return code; } bool ShortCode::check(uint32_t short_code) {