From 14954a3816b39cd6de4647207caf0fc6f54a7db1 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Sun, 29 Jul 2018 11:09:51 +0100 Subject: [PATCH] Add helpers to ReversedSizedArray. --- .../implem/reversed_sized_array_bt.hpp | 23 ++++++ include/duckhandy/int_conv.hpp | 11 +++ test/unit/int_conv_test.cpp | 71 ++++++++++--------- 3 files changed, 70 insertions(+), 35 deletions(-) diff --git a/include/duckhandy/implem/reversed_sized_array_bt.hpp b/include/duckhandy/implem/reversed_sized_array_bt.hpp index b59dad7..7bdbbf6 100644 --- a/include/duckhandy/implem/reversed_sized_array_bt.hpp +++ b/include/duckhandy/implem/reversed_sized_array_bt.hpp @@ -21,6 +21,11 @@ #include #include #include +#if !defined(INT_CONV_WITHOUT_HELPERS) +# include +# include +# include +#endif namespace dhandy { template @@ -41,10 +46,28 @@ namespace dhandy { constexpr iterator begin() { return m_data.begin() + m_curr + 1; } constexpr iterator end() { return m_data.end(); } +#if !defined(INT_CONV_WITHOUT_HELPERS) + constexpr std::string_view to_string_view() const { return std::string_view(data(), size() - 1); } + bool operator== (const std::string_view& other) const { return to_string_view() == other; } + bool operator!= (const std::string_view& other) const { return not operator==(other); } + bool operator== (const std::string& other) const { return to_string_view() == other; } + bool operator!= (const std::string& other) const { return not operator==(other); } + bool operator== (const char* other) const { return to_string_view() == std::string_view(other); } + bool operator!= (const char* other) const { return not operator==(other); } +#endif + private: std::array m_data {}; std::size_t m_curr {S - 1}; }; + +#if !defined(INT_CONV_WITHOUT_HELPERS) + template + std::basic_ostream& operator<< (std::basic_ostream& stream, const ReversedSizedArray& arr) { + stream << arr.to_string_view(); + return stream; + } +#endif } //namespace dhandy #endif diff --git a/include/duckhandy/int_conv.hpp b/include/duckhandy/int_conv.hpp index 84aefef..1067022 100644 --- a/include/duckhandy/int_conv.hpp +++ b/include/duckhandy/int_conv.hpp @@ -29,6 +29,10 @@ #if defined(__SSE4_1__) # include #endif +#if !defined(INT_CONV_WITHOUT_HELPERS) +# include +# include +#endif namespace dhandy { namespace implem { @@ -286,6 +290,13 @@ namespace dhandy { inline R ary_to_int (C* beg, C* end) { return implem::AryConversion::from_ary(beg, end); } + +#if !defined(INT_CONV_WITHOUT_HELPERS) + template + std::string to_string (I num) { + return std::string(int_to_ary(num).to_string_view()); + } +#endif } //namespace dhandy #endif diff --git a/test/unit/int_conv_test.cpp b/test/unit/int_conv_test.cpp index 9f2f1f6..c911f40 100644 --- a/test/unit/int_conv_test.cpp +++ b/test/unit/int_conv_test.cpp @@ -19,6 +19,7 @@ #include "duckhandy/int_conv.hpp" #include "duckhandy/string_bt.hpp" #include "sprout/cstring/strlen.hpp" +#include "sprout/preprocessor/comma.hpp" #include template using int_info_10 = dhandy::implem::int_info; @@ -67,46 +68,46 @@ TEST_CASE ("Check int to char array conversions", "[s2i][int_conv]") { static_assert(int_info_10::is_signed == true, "Wrong sign detection"); static_assert(string(0xAB12).data()) + 1>(int_to_ary(0xAB12).data()) == make_string("-21742"), "Algorithm error"); - //CHECK(to_string_view(int_to_ary(0x123456789A)) == "78187493530"); - //CHECK(to_string_view(int_to_ary(-1)) == "-1"); - //CHECK(to_string_view(int_to_ary(0x1000000000000000)) == "1152921504606846976"); - //CHECK(to_string_view(int_to_ary(0xF000000000000000)) == "-1152921504606846976"); + CHECK(int_to_ary(0x123456789A) == "78187493530"); + CHECK(int_to_ary(-1) == "-1"); + CHECK(int_to_ary(0x1000000000000000) == "1152921504606846976"); + CHECK(int_to_ary(0xF000000000000000) == "-1152921504606846976"); - //CHECK(to_string_view(int_to_ary(0xFFFF)) == "ffff"); - //CHECK(to_string_view(int_to_ary(0xCACA)) == "caca"); - //CHECK(to_string_view(int_to_ary(0x10)) == "10"); - //CHECK(to_string_view(int_to_ary(0x10)) == "10"); - //CHECK(to_string_view(int_to_ary(0xF000)) == "f000"); - //CHECK(to_string_view(int_to_ary(0xFEFE)) == "fefe"); - //CHECK(to_string_view(int_to_ary(0xFEFE)) == "fefe"); - //CHECK(to_string_view(int_to_ary(0423)) == "423"); - //CHECK(to_string_view(int_to_ary(0777)) == "777"); - //CHECK(to_string_view(int_to_ary(0)) == "0"); - //CHECK(to_string_view(int_to_ary(0)) == "0"); - //CHECK(to_string_view(int_to_ary(0)) == "0"); - //CHECK(to_string_view(int_to_ary(0)) == "0"); - //CHECK(to_string_view(int_to_ary(0)) == "0"); - //CHECK(to_string_view(int_to_ary(0)) == "0"); - //CHECK(to_string_view(int_to_ary(0)) == "0"); - //CHECK(to_string_view(int_to_ary(0)) == "0"); - //CHECK(to_string_view(int_to_ary(false)) == "0"); - //CHECK(to_string_view(int_to_ary(true)) == "1"); - //CHECK(to_string_view(int_to_ary(false)) == "0"); - //CHECK(to_string_view(int_to_ary(true)) == "1"); - //CHECK(to_string_view(int_to_ary(0b10101010)) == "10101010"); - //CHECK(to_string_view(int_to_ary(0b10101010)) == "10101010"); - //CHECK(to_string_view(int_to_ary(0b11111111)) == "11111111"); - //CHECK(to_string_view(int_to_ary(0b11111111)) == "11111111"); - //CHECK(to_string_view(int_to_ary(0b111100001111)) == "111100001111"); - //CHECK(to_string_view(int_to_ary(0b111100001111)) == "111100001111"); - //CHECK(to_string_view(int_to_ary(9223372036854775807)) == "1y2p0ij32e8e7"); - //CHECK(to_string_view(int_to_ary(0x8000000000000001)) == "1y2p0ij32e8e9"); + CHECK(int_to_ary(0xFFFF) == "ffff"); + CHECK(int_to_ary(0xCACA) == "caca"); + CHECK(int_to_ary(0x10) == "10"); + CHECK(int_to_ary(0x10) == "10"); + CHECK(int_to_ary(0xF000) == "f000"); + CHECK(int_to_ary(0xFEFE) == "fefe"); + CHECK(int_to_ary(0xFEFE) == "fefe"); + CHECK(int_to_ary(0423) == "423"); + CHECK(int_to_ary(0777) == "777"); + CHECK(int_to_ary(0) == "0"); + CHECK(int_to_ary(0) == "0"); + CHECK(int_to_ary(0) == "0"); + CHECK(int_to_ary(0) == "0"); + CHECK(int_to_ary(0) == "0"); + CHECK(int_to_ary(0) == "0"); + CHECK(int_to_ary(0) == "0"); + CHECK(int_to_ary(0) == "0"); + CHECK(int_to_ary(false) == "0"); + CHECK(int_to_ary(true) == "1"); + CHECK(int_to_ary(false) == "0"); + CHECK(int_to_ary(true) == "1"); + CHECK(int_to_ary(0b10101010) == "10101010"); + CHECK(int_to_ary(0b10101010) == "10101010"); + CHECK(int_to_ary(0b11111111) == "11111111"); + CHECK(int_to_ary(0b11111111) == "11111111"); + CHECK(int_to_ary(0b111100001111) == "111100001111"); + CHECK(int_to_ary(0b111100001111) == "111100001111"); + CHECK(int_to_ary(9223372036854775807) == "1y2p0ij32e8e7"); + CHECK(int_to_ary(0x8000000000000001) == "1y2p0ij32e8e9"); #if defined(__GNUC__) static_assert(std::is_integral<__int128_t>::value, "Warning, int128 won't pickup the optimized base 10 conversion path"); __int128_t num = 10000000000000000000U; - //CHECK(to_string_view(int_to_ary<__int128_t, 10>(num * 100)) == "1000000000000000000000"); + CHECK(int_to_ary<__int128_t SPROUT_PP_COMMA() 10>(num * 100) == "1000000000000000000000"); num = 0xFFFFFFFFFFFFFFFF; - //CHECK(to_string_view(int_to_ary<__int128_t, 16>(num * 0x10000 + 0xffff)) == "ffffffffffffffffffff"); + CHECK(int_to_ary<__int128_t SPROUT_PP_COMMA() 16>(num * 0x10000 + 0xffff) == "ffffffffffffffffffff"); #endif }