Browse Source

update: using Exp instead of Exception

master
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) {
if (!CommonCode::check(common_code)) { // check input common code
throw klotski::CommonCodeException("common code invalid");
throw klotski::CommonCodeExp("common code invalid");
}
code_ = common_code;
}

8
src/klotski_core/common_code/common_code.h

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

8
src/klotski_core/common_code/serialize.cc

@ -1,7 +1,7 @@
#include "common_code.h"
using klotski::CommonCode;
using klotski::CommonCodeException;
using klotski::CommonCodeExp;
/// -------------------------- 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
/// 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
@ -74,14 +74,14 @@ uint64_t CommonCode::string_decode(const std::string &common_code) { // convert
} else if (bit >= 'a' && bit <= 'f') { // a ~ f
result |= (bit - 87);
} 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
/// check whether common code is valid
if (!CommonCode::check(result)) { // check converted common code
throw CommonCodeException("common code invalid");
throw CommonCodeExp("common code invalid");
}
return result;
}

2
src/klotski_core/raw_code/convert.cc

@ -3,7 +3,7 @@
using klotski::RawCode;
using klotski::CommonCode;
using klotski::RawCodeException;
using klotski::RawCodeExp;
/// -------------------------- 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) {
if (!RawCode::check(raw_code)) { // check input raw code
throw klotski::RawCodeException("raw code invalid");
throw klotski::RawCodeExp("raw code invalid");
}
code_ = raw_code;
}

8
src/klotski_core/raw_code/raw_code.h

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

10
src/klotski_core/short_code/serialize.cc

@ -2,7 +2,7 @@
#include "serialize_chars.h"
using klotski::ShortCode;
using klotski::ShortCodeException;
using klotski::ShortCodeExp;
/// --------------------------- 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
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;
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
}
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
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
throw ShortCodeException("short code invalid");
throw ShortCodeExp("short code invalid");
}
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) {
if (!ShortCode::check(short_code)) { // check input short code
throw klotski::ShortCodeException("short code invalid");
throw klotski::ShortCodeExp("short code invalid");
}
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;
class ShortCodeException : public std::runtime_error {
class ShortCodeExp : public std::runtime_error {
public:
ShortCodeException() : std::runtime_error("invalid short code") {}
explicit ShortCodeException(const std::string &msg) : std::runtime_error(msg) {}
~ShortCodeException() noexcept override = default;
ShortCodeExp() : std::runtime_error("invalid short code") {}
explicit ShortCodeExp(const std::string &msg) : std::runtime_error(msg) {}
~ShortCodeExp() noexcept override = default;
};
/// 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;
try {
func();
} catch (klotski::CommonCodeException&) {
} catch (klotski::CommonCodeExp &) {
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;
try {
func();
} catch (klotski::ShortCodeException&) {
} catch (klotski::ShortCodeExp &) {
panic_flag = true;
}
EXPECT_EQ(panic_flag, true);

Loading…
Cancel
Save