Skip to content

Commit 7935f05

Browse files
author
massaraksh111
committed
Add constexpr version of ozo::detail::ltob36
1 parent a7e1fa7 commit 7935f05

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

include/ozo/detail/base36.h

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
#pragma once
22

3-
#include <boost/range/algorithm/reverse.hpp>
4-
#include <string_view>
3+
#include <boost/hana/string.hpp>
4+
#include <boost/hana/if.hpp>
55
#include <string>
6+
#include <cmath>
67

78
namespace ozo::detail {
89

9-
inline long b36tol(std::string_view in) {
10-
return strtol(in.data(), nullptr, 36);
10+
template<unsigned long u>
11+
constexpr auto ultob36() {
12+
constexpr int base = 36;
13+
constexpr int d = u % base;
14+
constexpr char sym = boost::hana::if_(d < 10, '0' + d, 'A' + d - 10);
15+
constexpr auto str = boost::hana::string_c<sym>;
16+
17+
if constexpr (u / base > 0) {
18+
return ultob36<u / base>() + str;
19+
} else {
20+
return str;
21+
}
22+
}
23+
24+
template<long i>
25+
constexpr auto ltob36() {
26+
return ultob36<static_cast<unsigned long>(i)>();
27+
}
28+
29+
inline void reverse(std::string& str) {
30+
const std::size_t size = str.size();
31+
const std::size_t times = std::ceil(size / 2);
32+
for (std::size_t count = 0; count < times; count++) {
33+
std::swap(str[count], str[size - count - 1]);
34+
}
1135
}
1236

1337
inline std::string ltob36(long i) {
@@ -20,7 +44,8 @@ inline std::string ltob36(long i) {
2044
u /= base;
2145
} while (u > 0);
2246

23-
boost::range::reverse(out);
47+
reverse(out);
48+
2449
return out;
2550
}
2651

include/ozo/error.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,9 @@ class category final : public error_category {
524524
const char* name() const noexcept override final { return "ozo::sqlstate::category"; }
525525

526526
std::string message(int value) const override final {
527-
#define OZO_SQLSTATE_NAME(value) case value: return std::string(#value) + "(" + detail::ltob36(value) + ")";
527+
#define OZO_SQLSTATE_NAME(value) case value: return boost::hana::to<char const*>(\
528+
BOOST_HANA_STRING(#value) + BOOST_HANA_STRING("(") +\
529+
detail::ltob36<value>() + BOOST_HANA_STRING(")"));
528530
switch (value) {
529531
OZO_SQLSTATE_NAME(successful_completion)
530532
OZO_SQLSTATE_NAME(warning)

tests/binary_serialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <ozo/io/array.h>
33
#include <ozo/ext/std.h>
44
#include <ozo/pg/types.h>
5+
#include <boost/range/iterator_range.hpp>
56

67
#include <gtest/gtest.h>
78
#include <gmock/gmock.h>

tests/detail/base36.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
#include <ozo/detail/base36.h>
2+
#include <boost/hana/comparing.hpp>
23

34
#include <gtest/gtest.h>
45

56
namespace {
67

7-
TEST(b36tol, should_with_HV001_return_29999809) {
8-
EXPECT_EQ(ozo::detail::b36tol("HV001"), 29999809);
8+
TEST(ltob36, should_verify_runtime_version_of_ltob36) {
9+
EXPECT_EQ(ozo::detail::ltob36(29999809), "HV001");
10+
EXPECT_EQ(ozo::detail::ltob36(833328), "HV00");
11+
EXPECT_EQ(ozo::detail::ltob36(0), "0");
912
}
1013

11-
TEST(b36tol, should_with_hv001_return_29999809) {
12-
EXPECT_EQ(ozo::detail::b36tol("hv001"), 29999809);
13-
}
14-
15-
TEST(ltob36, should_with_29999809_return_HV001) {
16-
EXPECT_EQ("HV001", ozo::detail::ltob36(29999809));
17-
}
14+
static_assert(ozo::detail::ltob36<29999809>() == BOOST_HANA_STRING("HV001"));
15+
static_assert(ozo::detail::ltob36<833328>() == BOOST_HANA_STRING("HV00"));
16+
static_assert(ozo::detail::ltob36<0>() == BOOST_HANA_STRING("0"));
1817

1918
} // namespace

0 commit comments

Comments
 (0)