diff --git a/include/basen/hex.hpp b/include/basen/hex.hpp index de4a6d2..3dc2412 100644 --- a/include/basen/hex.hpp +++ b/include/basen/hex.hpp @@ -14,24 +14,24 @@ namespace hex bool isValid(std::string_view str) noexcept; /** - * @throw std::overflow_error if if there is an overflow + * @throw basen::Exception(OVERFLOW) if if there is an overflow */ size_t sizeEncoded(std::span data); size_t sizeDecoded(std::string_view str) noexcept; /** - * @throw std::length_error if not enough allocated length + * @throw basen::Exception(LENGTH) if not enough allocated length * @warning contain leading zeros, returns count of them */ void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size); - std::string encode(std::span data) noexcept; + std::string encode(std::span data); /** - * @throw std::length_error if not enough allocated length - * @throw std::logic_error if out of digits map - * @throw std::logic_error if str_size %2 != 0 (isn't hex) + * @throw basen::Exception(LENGTH) if not enough allocated length + * @throw basen::Exception(OUT_OF_ALPH) if out of digits map + * @throw basen::Exception(PADDING) if str_size %2 != 0 (isn't hex) * @warning contain leading zeros, returns count of them */ void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size); - std::vector decode(std::string_view str) noexcept; + std::vector decode(std::string_view str); } \ No newline at end of file diff --git a/src/hex.cpp b/src/hex.cpp index eaaf018..62b5c43 100644 --- a/src/hex.cpp +++ b/src/hex.cpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace hex @@ -38,7 +39,7 @@ namespace hex { if (data.size() > std::numeric_limits::max() / 2) { - throw std::overflow_error("hex::sizeEncoded: overflow"); + throw basen::Exception(basen::Exception::Code::OVERFLOW); } return data.size() * 2; } @@ -50,7 +51,7 @@ namespace hex { if (str_size < hex::sizeEncoded(std::span(data, data_size))) { - throw std::length_error("hex::encode: not enough allocated length"); + throw basen::Exception(basen::Exception::Code::LENGTH); } for (size_t i = 0; i < data_size; i++) { @@ -58,7 +59,7 @@ namespace hex str[i * 2 + 1] = digits[data[i] & 0x0F]; } } - std::string encode(std::span data) noexcept + std::string encode(std::span data) { std::string str(hex::sizeEncoded(data), ' '); hex::encode(data.data(), data.size(), str.data(), str.size()); @@ -68,22 +69,22 @@ namespace hex { if (str_size % 2 != 0) { - throw std::logic_error("hex::decode: isn't hex"); + throw basen::Exception(basen::Exception::Code::PADDING); } if (data_size < hex::sizeDecoded(std::string_view(str, str_size))) { - throw std::length_error("hex::decode: not enough allocated length"); + throw basen::Exception(basen::Exception::Code::LENGTH); } if (!hex::isValid(str, str_size)) { - throw std::logic_error("hex::decode: out of digits map"); + throw basen::Exception(basen::Exception::Code::OUT_OF_ALPH); } for (size_t i = 0; i * 2 < str_size; i++) { data[i] = map[(uint8_t)str[i * 2]] << 4 | map[(uint8_t)str[i * 2 + 1]]; } } - std::vector decode(std::string_view str) noexcept + std::vector decode(std::string_view str) { std::vector data(hex::sizeDecoded(str)); hex::decode(str.data(), str.size(), data.data(), data.size()); diff --git a/test/test-hex.cpp b/test/test-hex.cpp index 30a739c..c277d65 100644 --- a/test/test-hex.cpp +++ b/test/test-hex.cpp @@ -1,5 +1,6 @@ #include +#include #include #include @@ -20,8 +21,8 @@ TEST(hex, encode) EXPECT_EQ("74657374", encode(data)); std::string str = ""; - EXPECT_THROW(encode(data.data(), std::numeric_limits::max(), str.data(), str.size()), std::overflow_error); - EXPECT_THROW(encode(data.data(), data.size(), str.data(), str.size()), std::length_error); + EXPECT_THROW(encode(data.data(), std::numeric_limits::max(), str.data(), str.size()), basen::Exception); + EXPECT_THROW(encode(data.data(), data.size(), str.data(), str.size()), basen::Exception); EXPECT_NO_THROW(encode(data.data(), 0, str.data(), str.size())); } TEST(hex, encode_1e7) @@ -34,9 +35,9 @@ TEST(hex, decode) std::vector data = {0x61, 0x6e, 0x6f}; EXPECT_EQ(decode("616e6f"), data); - EXPECT_THROW(decode("FFF", 3, data.data(), data.size()), std::logic_error); - EXPECT_THROW(decode("!@#!", 4, data.data(), data.size()), std::logic_error); - EXPECT_THROW(decode("FF", 2, data.data(), 0), std::length_error); + EXPECT_THROW(decode("FFF", 3, data.data(), data.size()), basen::Exception); + EXPECT_THROW(decode("!@#!", 4, data.data(), data.size()), basen::Exception); + EXPECT_THROW(decode("FF", 2, data.data(), 0), basen::Exception); EXPECT_NO_THROW(decode("", 0, data.data(), 0)); } TEST(hex, decode_1e7)