From 742f1e0421f06aae4ee38a3ef211a9ae68d1f4b7 Mon Sep 17 00:00:00 2001 From: massaraksh111 Date: Mon, 15 Jun 2020 13:49:04 +0300 Subject: [PATCH] Add constexpr version of ozo::detail::ltob36 --- include/ozo/detail/base36.h | 19 +++++++++++++++++++ include/ozo/error.h | 4 +++- tests/detail/base36.cpp | 3 +++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/include/ozo/detail/base36.h b/include/ozo/detail/base36.h index a6a1c20db..75384015e 100644 --- a/include/ozo/detail/base36.h +++ b/include/ozo/detail/base36.h @@ -1,6 +1,8 @@ #pragma once #include +#include +#include #include #include @@ -10,6 +12,23 @@ inline long b36tol(std::string_view in) { return strtol(in.data(), nullptr, 36); } +template +constexpr auto ultob36() { + constexpr int base = 36; + constexpr int d = i % base; + if constexpr (i > 0) { + const char sym = boost::hana::if_(d < 10, '0' + d, 'A' + d - 10); + return ultob36() + boost::hana::string_c; + } else { + return BOOST_HANA_STRING(""); + } +} + +template +constexpr auto ltob36() { + return ultob36(i)>(); +} + inline std::string ltob36(long i) { auto u = static_cast(i); std::string 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/detail/base36.cpp b/tests/detail/base36.cpp index eb2fd8deb..7812227e1 100644 --- a/tests/detail/base36.cpp +++ b/tests/detail/base36.cpp @@ -1,4 +1,5 @@ #include +#include #include @@ -16,4 +17,6 @@ TEST(ltob36, should_with_29999809_return_HV001) { EXPECT_EQ("HV001", ozo::detail::ltob36(29999809)); } +static_assert(BOOST_HANA_STRING("HV001") == ozo::detail::ltob36<29999809>()); + } // namespace