Implement constexpr int_to_ary.
This commit is contained in:
parent
c64baaf917
commit
e06b87e8f8
5 changed files with 258 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
tags
|
tags
|
||||||
|
compile_commands.json
|
||||||
|
|
50
include/duckhandy/implem/reversed_sized_array_bt.hpp
Normal file
50
include/duckhandy/implem/reversed_sized_array_bt.hpp
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
/* Copyright 2016, Michele Santullo
|
||||||
|
* This file is part of "duckhandy".
|
||||||
|
*
|
||||||
|
* "duckhandy" is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* "duckhandy" is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef idFC25566D624140559C54B39FFFE52F04
|
||||||
|
#define idFC25566D624140559C54B39FFFE52F04
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace dhandy {
|
||||||
|
template <typename T, std::size_t S>
|
||||||
|
class ReversedSizedArray {
|
||||||
|
static_assert(S > 0, "This container requires size to be at least 1");
|
||||||
|
static_assert(std::is_trivial<T>::value, "Only use this container with trivial types");
|
||||||
|
public:
|
||||||
|
using iterator = typename std::array<T, S>::iterator;
|
||||||
|
constexpr ReversedSizedArray() = default;
|
||||||
|
~ReversedSizedArray() = default;
|
||||||
|
|
||||||
|
constexpr std::size_t size() const { return S - (m_curr + 1); }
|
||||||
|
constexpr bool empty() const { return m_curr + 1 == S; }
|
||||||
|
constexpr const T operator[] (std::size_t idx) const { if (idx >= size()) throw std::out_of_range("Out of bound array access"); return m_data[idx + m_curr + 1]; }
|
||||||
|
constexpr T& operator[] (std::size_t idx) { if (idx >= size()) throw std::out_of_range("Out of bound array access"); return m_data[idx + m_curr + 1]; }
|
||||||
|
constexpr void push_front (const T& itm) { if (size() == S) throw std::length_error("ReversedSizedArray is full"); m_data[m_curr--] = itm; }
|
||||||
|
constexpr const T* data() const { return m_data.data() + m_curr + 1; }
|
||||||
|
constexpr iterator begin() { return m_data.begin() + m_curr + 1; }
|
||||||
|
constexpr iterator end() { return m_data.end(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::array<T, S> m_data {};
|
||||||
|
std::size_t m_curr {S - 1};
|
||||||
|
};
|
||||||
|
} //namespace dhandy
|
||||||
|
|
||||||
|
#endif
|
113
include/duckhandy/int_conv.hpp
Normal file
113
include/duckhandy/int_conv.hpp
Normal file
|
@ -0,0 +1,113 @@
|
||||||
|
/* Copyright 2016, 2017 Michele Santullo
|
||||||
|
* This file is part of "duckhandy".
|
||||||
|
*
|
||||||
|
* "duckhandy" is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* "duckhandy" is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef id4754A95F12BE4ADEA65642A056A51907
|
||||||
|
#define id4754A95F12BE4ADEA65642A056A51907
|
||||||
|
|
||||||
|
#include "implem/reversed_sized_array_bt.hpp"
|
||||||
|
#include "sprout/math/log10.hpp"
|
||||||
|
#include "sprout/math/abs.hpp"
|
||||||
|
#include "sprout/math/ceil.hpp"
|
||||||
|
#include <cstddef>
|
||||||
|
#include <array>
|
||||||
|
#include <limits>
|
||||||
|
#if !defined(INT_CONV_WITHOUT_HELPERS)
|
||||||
|
# include <string_view>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace dhandy {
|
||||||
|
namespace implem {
|
||||||
|
template <typename I, std::size_t Base>
|
||||||
|
constexpr std::size_t max_digit_count = static_cast<std::size_t>(
|
||||||
|
sprout::ceil(
|
||||||
|
sprout::log10(sprout::abs(static_cast<long double>(std::numeric_limits<I>::max()))) /
|
||||||
|
sprout::log10(static_cast<long double>(Base))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
template <typename I, unsigned int Base>
|
||||||
|
struct int_info {
|
||||||
|
static_assert(Base > 1, "Invalid base");
|
||||||
|
static const constexpr bool always_unsigned = (Base == 16 or Base == 2);
|
||||||
|
static const constexpr bool is_signed = std::numeric_limits<I>::is_signed and not always_unsigned;
|
||||||
|
static const constexpr std::size_t max_len = max_digit_count<typename std::conditional<always_unsigned, std::make_unsigned_t<I>, I>::type, Base> + is_signed;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename I, unsigned int Base, bool Signed=int_info<I, Base>::is_signed>
|
||||||
|
struct IsNegative { static constexpr bool check (I) { return false; } };
|
||||||
|
template <typename I, unsigned int Base>
|
||||||
|
struct IsNegative<I, Base, true> { static constexpr bool check (I in) { return in < I(0); } };
|
||||||
|
|
||||||
|
template <typename I, unsigned int Base>
|
||||||
|
constexpr bool is_negative (I in) {
|
||||||
|
return IsNegative<I, Base>::check(in);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename I,
|
||||||
|
std::size_t Base,
|
||||||
|
bool IsSigned=std::numeric_limits<I>::is_signed,
|
||||||
|
bool ForceUnsigned=int_info<I, Base>::always_unsigned
|
||||||
|
> struct NumberAdaptation {
|
||||||
|
static const constexpr bool BecomesUnsigned = IsSigned and ForceUnsigned;
|
||||||
|
using UnsignedType = typename std::make_unsigned<I>::type;
|
||||||
|
using CastedType = typename std::conditional<BecomesUnsigned, UnsignedType, I>::type;
|
||||||
|
|
||||||
|
template <typename L>
|
||||||
|
static constexpr L abs(L in) { return (not BecomesUnsigned and std::numeric_limits<L>::is_signed and in < L(0) ? -in : in); }
|
||||||
|
static constexpr CastedType cast (I in) { return static_cast<CastedType>(in); }
|
||||||
|
};
|
||||||
|
} //namespace implem
|
||||||
|
|
||||||
|
template <typename C, C FirstLetter='a'>
|
||||||
|
struct DefaultTranslator {
|
||||||
|
static constexpr C to_digit (unsigned int num) {
|
||||||
|
return (num <= 9 ?
|
||||||
|
static_cast<C>(num + '0') :
|
||||||
|
static_cast<C>(num + FirstLetter - 10)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
static constexpr C minus() { return '-'; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename I, unsigned int Base=10, typename Tr=DefaultTranslator<char>>
|
||||||
|
constexpr inline ReversedSizedArray<std::decay_t<decltype(std::declval<Tr>().to_digit(1))>, implem::int_info<I, Base>::max_len + 1> int_to_ary (I in) {
|
||||||
|
using RetType = ReversedSizedArray<std::decay_t<decltype(std::declval<Tr>().to_digit(1))>, implem::int_info<I, Base>::max_len + 1>;
|
||||||
|
using Num = implem::NumberAdaptation<std::decay_t<I>, Base>;
|
||||||
|
|
||||||
|
const bool was_negative = implem::is_negative<I, Base>(in);
|
||||||
|
|
||||||
|
RetType arr;
|
||||||
|
arr.push_front('\0');
|
||||||
|
do {
|
||||||
|
arr.push_front(Tr::to_digit(static_cast<int>(Num::abs(Num::cast(in)) % Base)));
|
||||||
|
in = static_cast<I>(Num::cast(in) / static_cast<I>(Base));
|
||||||
|
} while (in);
|
||||||
|
if (was_negative)
|
||||||
|
arr.push_front(Tr::minus());
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if !defined(INT_CONV_WITHOUT_HELPERS)
|
||||||
|
template <typename T, std::size_t S>
|
||||||
|
std::basic_string_view<T> to_string_view (const ReversedSizedArray<T, S>& ary) {
|
||||||
|
return std::basic_string_view<T>(ary.data(), ary.size() - 1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
} //namespace dhandy
|
||||||
|
|
||||||
|
#endif
|
|
@ -4,8 +4,9 @@ add_executable(${PROJECT_NAME}
|
||||||
main.cpp
|
main.cpp
|
||||||
lexical_cast_test.cpp
|
lexical_cast_test.cpp
|
||||||
endianness_test.cpp
|
endianness_test.cpp
|
||||||
|
int_conv_test.cpp
|
||||||
)
|
)
|
||||||
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
|
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17)
|
||||||
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME}
|
target_include_directories(${PROJECT_NAME}
|
||||||
|
|
92
test/unit/int_conv_test.cpp
Normal file
92
test/unit/int_conv_test.cpp
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
/* Copyright 2017, Michele Santullo
|
||||||
|
* This file is part of "duckhandy".
|
||||||
|
*
|
||||||
|
* "duckhandy" is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* "duckhandy" is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "catch.hpp"
|
||||||
|
#include "duckhandy/int_conv.hpp"
|
||||||
|
#include "duckhandy/string_bt.hpp"
|
||||||
|
#include "sprout/cstring/strlen.hpp"
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
template <typename T> using int_info_10 = dhandy::implem::int_info<T, 10>;
|
||||||
|
template <typename T> using int_info_16 = dhandy::implem::int_info<T, 16>;
|
||||||
|
template <typename T> using int_info_2 = dhandy::implem::int_info<T, 2>;
|
||||||
|
|
||||||
|
TEST_CASE ("Check int to char array conversions", "[s2i][int_conv]") {
|
||||||
|
using dhandy::int_to_ary;
|
||||||
|
using dhandy::bt::string;
|
||||||
|
using dhandy::bt::make_string;
|
||||||
|
using sprout::strlen;
|
||||||
|
|
||||||
|
CHECK(int_info_10<uint8_t>::max_len == 3);
|
||||||
|
CHECK(int_info_10<uint16_t>::max_len == 5);
|
||||||
|
CHECK(int_info_10<uint32_t>::max_len == 10);
|
||||||
|
CHECK(int_info_10<int8_t>::max_len == 4);
|
||||||
|
CHECK(int_info_10<int16_t>::max_len == 6);
|
||||||
|
CHECK(int_info_10<int32_t>::max_len == 11);
|
||||||
|
|
||||||
|
CHECK(int_info_16<uint8_t>::max_len == 2);
|
||||||
|
CHECK(int_info_16<uint16_t>::max_len == 4);
|
||||||
|
CHECK(int_info_16<uint32_t>::max_len == 8);
|
||||||
|
CHECK(int_info_16<int8_t>::max_len == 2);
|
||||||
|
CHECK(int_info_16<int16_t>::max_len == 4);
|
||||||
|
CHECK(int_info_16<int32_t>::max_len == 8);
|
||||||
|
|
||||||
|
CHECK(int_info_2<uint8_t>::max_len == 8);
|
||||||
|
CHECK(int_info_2<uint16_t>::max_len == 16);
|
||||||
|
CHECK(int_info_2<uint32_t>::max_len == 32);
|
||||||
|
CHECK(int_info_2<int8_t>::max_len == 8);
|
||||||
|
CHECK(int_info_2<int16_t>::max_len == 16);
|
||||||
|
CHECK(int_info_2<int32_t>::max_len == 32);
|
||||||
|
|
||||||
|
static_assert(int_to_ary(5)[0] == '5', "Algorithm error");
|
||||||
|
static_assert(string<strlen(int_to_ary(10).data()) + 1>(int_to_ary(10).data()) == make_string("10"), "Algorithm error");
|
||||||
|
static_assert(string<strlen(int_to_ary(101).data()) + 1>(int_to_ary(101).data()) == make_string("101"), "Algorithm error");
|
||||||
|
static_assert(string<strlen(int_to_ary<uint16_t>(0xAB12).data()) + 1>(int_to_ary<uint16_t>(0xAB12).data()) == make_string("43794"), "Algorithm error");
|
||||||
|
static_assert(int_info_10<int16_t>::is_signed == true, "Wrong sign detection");
|
||||||
|
static_assert(string<strlen(int_to_ary<int16_t>(0xAB12).data()) + 1>(int_to_ary<int16_t>(0xAB12).data()) == make_string("-21742"), "Algorithm error");
|
||||||
|
|
||||||
|
CHECK(to_string_view(int_to_ary<int64_t>(0x123456789A)) == "78187493530");
|
||||||
|
CHECK(to_string_view(int_to_ary<int64_t>(-1)) == "-1");
|
||||||
|
CHECK(to_string_view(int_to_ary<int64_t>(0x1000000000000000)) == "1152921504606846976");
|
||||||
|
CHECK(to_string_view(int_to_ary<int64_t>(0xF000000000000000)) == "-1152921504606846976");
|
||||||
|
|
||||||
|
CHECK(to_string_view(int_to_ary<uint16_t, 16>(0xFFFF)) == "ffff");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint16_t, 16>(0xCACA)) == "caca");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint16_t, 16>(0x10)) == "10");
|
||||||
|
CHECK(to_string_view(int_to_ary<int16_t, 16>(0x10)) == "10");
|
||||||
|
CHECK(to_string_view(int_to_ary<int16_t, 16>(0xF000)) == "f000");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint16_t, 16>(0xFEFE)) == "fefe");
|
||||||
|
CHECK(to_string_view(int_to_ary<int16_t, 16>(0xFEFE)) == "fefe");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint32_t, 8>(0423)) == "423");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint32_t, 8>(0777)) == "777");
|
||||||
|
CHECK(to_string_view(int_to_ary<int32_t, 8>(0)) == "0");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint32_t, 8>(0)) == "0");
|
||||||
|
CHECK(to_string_view(int_to_ary<int32_t, 2>(0)) == "0");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint32_t, 2>(0)) == "0");
|
||||||
|
CHECK(to_string_view(int_to_ary<int32_t, 16>(0)) == "0");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint32_t, 16>(0)) == "0");
|
||||||
|
CHECK(to_string_view(int_to_ary<int32_t, 10>(0)) == "0");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint32_t, 10>(0)) == "0");
|
||||||
|
//CHECK(to_string_view(int_to_ary<bool, 8>(false)) == "0");
|
||||||
|
//CHECK(to_string_view(int_to_ary<bool, 8>(true)) == "0");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint8_t, 2>(0b10101010)) == "10101010");
|
||||||
|
CHECK(to_string_view(int_to_ary<int8_t, 2>(0b10101010)) == "10101010");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint8_t, 2>(0b11111111)) == "11111111");
|
||||||
|
CHECK(to_string_view(int_to_ary<int8_t, 2>(0b11111111)) == "11111111");
|
||||||
|
CHECK(to_string_view(int_to_ary<uint16_t, 2>(0b111100001111)) == "111100001111");
|
||||||
|
CHECK(to_string_view(int_to_ary<int16_t, 2>(0b111100001111)) == "111100001111");
|
||||||
|
}
|
Loading…
Reference in a new issue