From 199db7640eb36ac347e8f3a25b89ab16333a3400 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sat, 29 May 2021 16:47:48 +0200 Subject: [PATCH] Add support for int_conv() --- .../implem/reversed_sized_array_bt.hpp | 16 ++++++++++++++++ include/duckhandy/int_conv.hpp | 18 ++++++++++++++++++ test/unit/int_conv_test.cpp | 17 +++++++++++++++++ 3 files changed, 51 insertions(+) diff --git a/include/duckhandy/implem/reversed_sized_array_bt.hpp b/include/duckhandy/implem/reversed_sized_array_bt.hpp index 30fcc80..225c817 100644 --- a/include/duckhandy/implem/reversed_sized_array_bt.hpp +++ b/include/duckhandy/implem/reversed_sized_array_bt.hpp @@ -26,6 +26,7 @@ # include # include #endif +#include namespace dhandy { template @@ -33,6 +34,8 @@ namespace dhandy { static_assert(S > 0, "This container requires size to be at least 1"); static_assert(std::is_trivial::value, "Only use this container with trivial types"); public: + constexpr static const std::size_t capacity = S; + using iterator = typename std::array::iterator; constexpr ReversedSizedArray() = default; ~ReversedSizedArray() = default; @@ -49,6 +52,7 @@ namespace dhandy { template constexpr V to() const { return V(data(), size() - (not empty() and not back() ? 1 : 0)); } + constexpr auto to_tuple() const; #if !defined(INT_CONV_WITHOUT_HELPERS) constexpr std::basic_string_view to_string_view() const { return to>(); } @@ -65,6 +69,13 @@ namespace dhandy { std::size_t m_curr {S - 1}; }; + namespace implem { + template + constexpr auto elements_or_empty_to_tuple (const std::array& elems, std::size_t from_idx, std::index_sequence) { + return std::make_tuple((from_idx + Indices < S ? elems[from_idx + Indices] : T{})...); + } + } //namespace implem + #if !defined(INT_CONV_WITHOUT_HELPERS) template inline @@ -73,6 +84,11 @@ namespace dhandy { return stream; } #endif + + template + constexpr auto ReversedSizedArray::to_tuple() const { + return implem::elements_or_empty_to_tuple(m_data, m_curr + 1, std::make_index_sequence{}); + } } //namespace dhandy #endif diff --git a/include/duckhandy/int_conv.hpp b/include/duckhandy/int_conv.hpp index b7edd46..1e31501 100644 --- a/include/duckhandy/int_conv.hpp +++ b/include/duckhandy/int_conv.hpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace dhandy { namespace implem { @@ -47,6 +48,23 @@ namespace dhandy { return dhandy::int_to_ary(in).to_string_view(); } }; + template + struct IntConv, F, Tr, true> { + private: + template + constexpr static std::array to_array (const std::tuple& vals, std::index_sequence) { + return std::array{std::get(vals)...}; + } + public: + constexpr static std::array conv (const F& in) { + return to_array( + dhandy::int_to_ary(in).to_tuple(), + std::make_index_sequence< + std::min(decltype(dhandy::int_to_ary(in))::capacity, N) + >{} + ); + } + }; template struct IntConv, Tr, false> { constexpr static T conv (const IntConvString& in) { diff --git a/test/unit/int_conv_test.cpp b/test/unit/int_conv_test.cpp index d187ae7..1bd46ce 100644 --- a/test/unit/int_conv_test.cpp +++ b/test/unit/int_conv_test.cpp @@ -224,6 +224,23 @@ TEST_CASE ("Check string_view conversions work as expected", "[i2s][int_conv]") CHECK("123" == str123); } +TEST_CASE ("Check std::array conversion works as expected", "[i2s][int_conv]") { + using std::array; + using dhandy::int_conv; + + auto arr748 = int_conv>(748); + CHECK(arr748[0] == '7'); + CHECK(arr748[1] == '4'); + CHECK(arr748[2] == '8'); + + auto arr19000 = int_conv>(19); + CHECK(arr19000[0] == '1'); + CHECK(arr19000[1] == '9'); + CHECK(arr19000[2] == '\0'); + CHECK(arr19000[3] == '\0'); + CHECK(arr19000[4] == '\0'); +} + TEST_CASE ("Check upcase/downcase int to array conversions", "[i2s][int_conv]") { using dhandy::int_conv; using std::string_view;