Browse Source

update: using Exp instead of Exception

legacy
Dnomd343 2 years ago
parent
commit
0cabf92b00
  1. 2
      src/klotski_core/common_code/common_code.cc
  2. 8
      src/klotski_core/common_code/common_code.h
  3. 8
      src/klotski_core/common_code/serialize.cc
  4. 2
      src/klotski_core/raw_code/convert.cc
  5. 2
      src/klotski_core/raw_code/raw_code.cc
  6. 8
      src/klotski_core/raw_code/raw_code.h
  7. 10
      src/klotski_core/short_code/serialize.cc
  8. 2
      src/klotski_core/short_code/short_code.cc
  9. 8
      src/klotski_core/short_code/short_code.h
  10. 2
      test/codec/common_code.cc
  11. 2
      test/codec/short_code.cc

2
src/klotski_core/common_code/common_code.cc

@ -21,7 +21,7 @@ CommonCode CommonCode::unsafe_create(uint64_t common_code) noexcept { // create
CommonCode::CommonCode(uint64_t common_code) { CommonCode::CommonCode(uint64_t common_code) {
if (!CommonCode::check(common_code)) { // check input common code if (!CommonCode::check(common_code)) { // check input common code
throw klotski::CommonCodeException("common code invalid"); throw klotski::CommonCodeExp("common code invalid");
} }
code_ = common_code; code_ = common_code;
} }

8
src/klotski_core/common_code/common_code.h

@ -63,11 +63,11 @@ namespace klotski {
class RawCode; class RawCode;
class ShortCode; class ShortCode;
class CommonCodeException : public std::runtime_error { class CommonCodeExp : public std::runtime_error {
public: public:
CommonCodeException() : std::runtime_error("invalid common code") {} CommonCodeExp() : std::runtime_error("invalid common code") {}
explicit CommonCodeException(const std::string &msg) : std::runtime_error(msg) {} explicit CommonCodeExp(const std::string &msg) : std::runtime_error(msg) {}
~CommonCodeException() noexcept override = default; ~CommonCodeExp() noexcept override = default;
}; };
class CommonCode { class CommonCode {

8
src/klotski_core/common_code/serialize.cc

@ -1,7 +1,7 @@
#include "common_code.h" #include "common_code.h"
using klotski::CommonCode; using klotski::CommonCode;
using klotski::CommonCodeException; using klotski::CommonCodeExp;
/// -------------------------- CommonCode to String --------------------------- /// -------------------------- CommonCode to String ---------------------------
@ -60,7 +60,7 @@ std::string CommonCode::string_encode(uint64_t common_code, bool shorten) noexce
uint64_t CommonCode::string_decode(const std::string &common_code) { // convert from (1 ~ 9)-bit string uint64_t CommonCode::string_decode(const std::string &common_code) { // convert from (1 ~ 9)-bit string
/// check string length /// check string length
if (common_code.length() > 9 || common_code.empty()) { // check string length if (common_code.length() > 9 || common_code.empty()) { // check string length
throw CommonCodeException("common code should length 1 ~ 9"); throw CommonCodeExp("common code should length 1 ~ 9");
} }
/// check every characters /// check every characters
@ -74,14 +74,14 @@ uint64_t CommonCode::string_decode(const std::string &common_code) { // convert
} else if (bit >= 'a' && bit <= 'f') { // a ~ f } else if (bit >= 'a' && bit <= 'f') { // a ~ f
result |= (bit - 87); result |= (bit - 87);
} else { } else {
throw CommonCodeException("common code with invalid character"); // unknown character throw CommonCodeExp("common code with invalid character"); // unknown character
} }
} }
result <<= (9 - common_code.length()) * 4; // low-bits fill with zero result <<= (9 - common_code.length()) * 4; // low-bits fill with zero
/// check whether common code is valid /// check whether common code is valid
if (!CommonCode::check(result)) { // check converted common code if (!CommonCode::check(result)) { // check converted common code
throw CommonCodeException("common code invalid"); throw CommonCodeExp("common code invalid");
} }
return result; return result;
} }

2
src/klotski_core/raw_code/convert.cc

@ -3,7 +3,7 @@
using klotski::RawCode; using klotski::RawCode;
using klotski::CommonCode; using klotski::CommonCode;
using klotski::RawCodeException; using klotski::RawCodeExp;
/// -------------------------- RawCode to CommonCode -------------------------- /// -------------------------- RawCode to CommonCode --------------------------

2
src/klotski_core/raw_code/raw_code.cc

@ -19,7 +19,7 @@ RawCode RawCode::unsafe_create(uint64_t raw_code) noexcept { // create without c
RawCode::RawCode(uint64_t raw_code) { RawCode::RawCode(uint64_t raw_code) {
if (!RawCode::check(raw_code)) { // check input raw code if (!RawCode::check(raw_code)) { // check input raw code
throw klotski::RawCodeException("raw code invalid"); throw klotski::RawCodeExp("raw code invalid");
} }
code_ = raw_code; code_ = raw_code;
} }

8
src/klotski_core/raw_code/raw_code.h

@ -47,11 +47,11 @@ namespace klotski {
class CommonCode; class CommonCode;
class RawCodeException : public std::runtime_error { class RawCodeExp : public std::runtime_error {
public: public:
RawCodeException() : std::runtime_error("invalid raw code") {} RawCodeExp() : std::runtime_error("invalid raw code") {}
explicit RawCodeException(const std::string &msg) : std::runtime_error(msg) {} explicit RawCodeExp(const std::string &msg) : std::runtime_error(msg) {}
~RawCodeException() noexcept override = default; ~RawCodeExp() noexcept override = default;
}; };
class RawCode { class RawCode {

10
src/klotski_core/short_code/serialize.cc

@ -2,7 +2,7 @@
#include "serialize_chars.h" #include "serialize_chars.h"
using klotski::ShortCode; using klotski::ShortCode;
using klotski::ShortCodeException; using klotski::ShortCodeExp;
/// --------------------------- ShortCode to String --------------------------- /// --------------------------- ShortCode to String ---------------------------
@ -42,7 +42,7 @@ std::string ShortCode::string_encode(uint32_t short_code) noexcept { // encode a
uint32_t ShortCode::string_decode(const std::string &short_code) { // 5-bits string decode uint32_t ShortCode::string_decode(const std::string &short_code) { // 5-bits string decode
if (short_code.length() != 5) { // check string length if (short_code.length() != 5) { // check string length
throw ShortCodeException("short code should length 5"); throw ShortCodeExp("short code should length 5");
} }
uint64_t result = 0; uint64_t result = 0;
for (auto bit : short_code) { for (auto bit : short_code) {
@ -51,15 +51,15 @@ uint32_t ShortCode::string_decode(const std::string &short_code) { // 5-bits str
bit -= 32; // convert to uppercase bit -= 32; // convert to uppercase
} }
if (bit < '1' || bit > 'Z') { // invalid characters if (bit < '1' || bit > 'Z') { // invalid characters
throw ShortCodeException("short code with invalid character"); throw ShortCodeExp("short code with invalid character");
} }
result += (bit = SHORT_CODE_TABLE_REV[bit - 49]); // table convert result += (bit = SHORT_CODE_TABLE_REV[bit - 49]); // table convert
if (bit == -1) { // invalid character if (bit == -1) { // invalid character
throw ShortCodeException("short code with invalid character"); throw ShortCodeExp("short code with invalid character");
} }
} }
if (!ShortCode::check(result)) { // check converted short code if (!ShortCode::check(result)) { // check converted short code
throw ShortCodeException("short code invalid"); throw ShortCodeExp("short code invalid");
} }
return result; // apply convert result return result; // apply convert result
} }

2
src/klotski_core/short_code/short_code.cc

@ -22,7 +22,7 @@ ShortCode ShortCode::unsafe_create(uint32_t short_code) noexcept { // create wit
ShortCode::ShortCode(uint32_t short_code) { ShortCode::ShortCode(uint32_t short_code) {
if (!ShortCode::check(short_code)) { // check input short code if (!ShortCode::check(short_code)) { // check input short code
throw klotski::ShortCodeException("short code invalid"); throw klotski::ShortCodeExp("short code invalid");
} }
code_ = short_code; code_ = short_code;
} }

8
src/klotski_core/short_code/short_code.h

@ -62,11 +62,11 @@ class CommonCode;
const uint32_t SHORT_CODE_LIMIT = 29334498; const uint32_t SHORT_CODE_LIMIT = 29334498;
class ShortCodeException : public std::runtime_error { class ShortCodeExp : public std::runtime_error {
public: public:
ShortCodeException() : std::runtime_error("invalid short code") {} ShortCodeExp() : std::runtime_error("invalid short code") {}
explicit ShortCodeException(const std::string &msg) : std::runtime_error(msg) {} explicit ShortCodeExp(const std::string &msg) : std::runtime_error(msg) {}
~ShortCodeException() noexcept override = default; ~ShortCodeExp() noexcept override = default;
}; };
/// For ShortCode, you must choose at least one mode (NORMAL or FAST) to convert, and /// For ShortCode, you must choose at least one mode (NORMAL or FAST) to convert, and

2
test/codec/common_code.cc

@ -17,7 +17,7 @@ static inline void SHOULD_PANIC(const std::function<void()> &func) {
bool panic_flag = false; bool panic_flag = false;
try { try {
func(); func();
} catch (klotski::CommonCodeException&) { } catch (klotski::CommonCodeExp &) {
panic_flag = true; panic_flag = true;
} }
EXPECT_EQ(panic_flag, true); EXPECT_EQ(panic_flag, true);

2
test/codec/short_code.cc

@ -20,7 +20,7 @@ static inline void SHOULD_PANIC(const std::function<void()> &func) {
bool panic_flag = false; bool panic_flag = false;
try { try {
func(); func();
} catch (klotski::ShortCodeException&) { } catch (klotski::ShortCodeExp &) {
panic_flag = true; panic_flag = true;
} }
EXPECT_EQ(panic_flag, true); EXPECT_EQ(panic_flag, true);

Loading…
Cancel
Save