Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constexpr version of ozo::detail::ltob36 #260

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions include/ozo/detail/base36.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
#pragma once

#include <boost/range/algorithm/reverse.hpp>
#include <string_view>
#include <boost/hana/string.hpp>
#include <boost/hana/if.hpp>
#include <string>
#include <cmath>

namespace ozo::detail {

inline long b36tol(std::string_view in) {
return strtol(in.data(), nullptr, 36);
template<unsigned long u>
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<sym>;

if constexpr (u / base > 0) {
return ultob36<u / base>() + str;
} else {
return str;
}
}

template<long i>
constexpr auto ltob36() {
return ultob36<static_cast<unsigned long>(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) {
Expand All @@ -20,7 +44,8 @@ inline std::string ltob36(long i) {
u /= base;
} while (u > 0);

boost::range::reverse(out);
reverse(out);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's wrong with boost::range::reverse?


return out;
}

Expand Down
4 changes: 3 additions & 1 deletion include/ozo/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<char const*>(\
BOOST_HANA_STRING(#value) + BOOST_HANA_STRING("(") +\
detail::ltob36<value>() + BOOST_HANA_STRING(")"));
switch (value) {
OZO_SQLSTATE_NAME(successful_completion)
OZO_SQLSTATE_NAME(warning)
Expand Down
1 change: 1 addition & 0 deletions tests/binary_serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <ozo/io/array.h>
#include <ozo/ext/std.h>
#include <ozo/pg/types.h>
#include <boost/range/iterator_range.hpp>

#include <gtest/gtest.h>
#include <gmock/gmock.h>
Expand Down
17 changes: 8 additions & 9 deletions tests/detail/base36.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#include <ozo/detail/base36.h>
#include <boost/hana/comparing.hpp>

#include <gtest/gtest.h>

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