diff --git a/include/ozo/detail/base36.h b/include/ozo/detail/base36.h index a6a1c20db..76bb898f7 100644 --- a/include/ozo/detail/base36.h +++ b/include/ozo/detail/base36.h @@ -1,13 +1,37 @@ #pragma once -#include -#include +#include +#include #include +#include namespace ozo::detail { -inline long b36tol(std::string_view in) { - return strtol(in.data(), nullptr, 36); +template +constexpr auto ultob36() { + constexpr int base = 36; + constexpr int d = u % base; + constexpr char sym = boost::hana::if_(d < 10, '0' + d, 'A' + d - 10); + constexpr auto str = boost::hana::string_c; + + if constexpr (u / base > 0) { + return ultob36() + str; + } else { + return str; + } +} + +template +constexpr auto ltob36() { + return ultob36(i)>(); +} + +inline void reverse(std::string& str) { + const std::size_t size = str.size(); + const std::size_t times = std::ceil(size / 2); + for (std::size_t count = 0; count < times; count++) { + std::swap(str[count], str[size - count - 1]); + } } inline std::string ltob36(long i) { @@ -20,7 +44,8 @@ inline std::string ltob36(long i) { u /= base; } while (u > 0); - boost::range::reverse(out); + reverse(out); + return out; } diff --git a/include/ozo/error.h b/include/ozo/error.h index 64e1900ea..bf953c574 100644 --- a/include/ozo/error.h +++ b/include/ozo/error.h @@ -524,7 +524,9 @@ class category final : public error_category { const char* name() const noexcept override final { return "ozo::sqlstate::category"; } std::string message(int value) const override final { - #define OZO_SQLSTATE_NAME(value) case value: return std::string(#value) + "(" + detail::ltob36(value) + ")"; + #define OZO_SQLSTATE_NAME(value) case value: return boost::hana::to(\ + BOOST_HANA_STRING(#value) + BOOST_HANA_STRING("(") +\ + detail::ltob36() + BOOST_HANA_STRING(")")); switch (value) { OZO_SQLSTATE_NAME(successful_completion) OZO_SQLSTATE_NAME(warning) diff --git a/tests/binary_serialization.cpp b/tests/binary_serialization.cpp index 3b9cc767a..48b33663f 100644 --- a/tests/binary_serialization.cpp +++ b/tests/binary_serialization.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include diff --git a/tests/detail/base36.cpp b/tests/detail/base36.cpp index eb2fd8deb..5f83d52fa 100644 --- a/tests/detail/base36.cpp +++ b/tests/detail/base36.cpp @@ -1,19 +1,18 @@ #include +#include #include namespace { -TEST(b36tol, should_with_HV001_return_29999809) { - EXPECT_EQ(ozo::detail::b36tol("HV001"), 29999809); +TEST(ltob36, should_verify_runtime_version_of_ltob36) { + EXPECT_EQ(ozo::detail::ltob36(29999809), "HV001"); + EXPECT_EQ(ozo::detail::ltob36(833328), "HV00"); + EXPECT_EQ(ozo::detail::ltob36(0), "0"); } -TEST(b36tol, should_with_hv001_return_29999809) { - EXPECT_EQ(ozo::detail::b36tol("hv001"), 29999809); -} - -TEST(ltob36, should_with_29999809_return_HV001) { - EXPECT_EQ("HV001", ozo::detail::ltob36(29999809)); -} +static_assert(ozo::detail::ltob36<29999809>() == BOOST_HANA_STRING("HV001")); +static_assert(ozo::detail::ltob36<833328>() == BOOST_HANA_STRING("HV00")); +static_assert(ozo::detail::ltob36<0>() == BOOST_HANA_STRING("0")); } // namespace