Add helpers to ReversedSizedArray.
This commit is contained in:
parent
ab74f8ab3a
commit
14954a3816
3 changed files with 70 additions and 35 deletions
|
@ -21,6 +21,11 @@
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#if !defined(INT_CONV_WITHOUT_HELPERS)
|
||||||
|
# include <string_view>
|
||||||
|
# include <string>
|
||||||
|
# include <sstream>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace dhandy {
|
namespace dhandy {
|
||||||
template <typename T, std::size_t S>
|
template <typename T, std::size_t S>
|
||||||
|
@ -41,10 +46,28 @@ namespace dhandy {
|
||||||
constexpr iterator begin() { return m_data.begin() + m_curr + 1; }
|
constexpr iterator begin() { return m_data.begin() + m_curr + 1; }
|
||||||
constexpr iterator end() { return m_data.end(); }
|
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:
|
private:
|
||||||
std::array<T, S> m_data {};
|
std::array<T, S> m_data {};
|
||||||
std::size_t m_curr {S - 1};
|
std::size_t m_curr {S - 1};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if !defined(INT_CONV_WITHOUT_HELPERS)
|
||||||
|
template <typename T, std::size_t S>
|
||||||
|
std::basic_ostream<T>& operator<< (std::basic_ostream<T>& stream, const ReversedSizedArray<T, S>& arr) {
|
||||||
|
stream << arr.to_string_view();
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
} //namespace dhandy
|
} //namespace dhandy
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -29,6 +29,10 @@
|
||||||
#if defined(__SSE4_1__)
|
#if defined(__SSE4_1__)
|
||||||
# include <smmintrin.h>
|
# include <smmintrin.h>
|
||||||
#endif
|
#endif
|
||||||
|
#if !defined(INT_CONV_WITHOUT_HELPERS)
|
||||||
|
# include <string_view>
|
||||||
|
# include <string>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace dhandy {
|
namespace dhandy {
|
||||||
namespace implem {
|
namespace implem {
|
||||||
|
@ -286,6 +290,13 @@ namespace dhandy {
|
||||||
inline R ary_to_int (C* beg, C* end) {
|
inline R ary_to_int (C* beg, C* end) {
|
||||||
return implem::AryConversion<R, Base, Tr>::from_ary(beg, end);
|
return implem::AryConversion<R, Base, Tr>::from_ary(beg, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(INT_CONV_WITHOUT_HELPERS)
|
||||||
|
template <typename I>
|
||||||
|
std::string to_string (I num) {
|
||||||
|
return std::string(int_to_ary(num).to_string_view());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
} //namespace dhandy
|
} //namespace dhandy
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
#include "duckhandy/int_conv.hpp"
|
#include "duckhandy/int_conv.hpp"
|
||||||
#include "duckhandy/string_bt.hpp"
|
#include "duckhandy/string_bt.hpp"
|
||||||
#include "sprout/cstring/strlen.hpp"
|
#include "sprout/cstring/strlen.hpp"
|
||||||
|
#include "sprout/preprocessor/comma.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
template <typename T> using int_info_10 = dhandy::implem::int_info<T, 10>;
|
template <typename T> using int_info_10 = dhandy::implem::int_info<T, 10>;
|
||||||
|
@ -67,46 +68,46 @@ TEST_CASE ("Check int to char array conversions", "[s2i][int_conv]") {
|
||||||
static_assert(int_info_10<int16_t>::is_signed == true, "Wrong sign detection");
|
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");
|
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(int_to_ary<int64_t>(0x123456789A) == "78187493530");
|
||||||
//CHECK(to_string_view(int_to_ary<int64_t>(-1)) == "-1");
|
CHECK(int_to_ary<int64_t>(-1) == "-1");
|
||||||
//CHECK(to_string_view(int_to_ary<int64_t>(0x1000000000000000)) == "1152921504606846976");
|
CHECK(int_to_ary<int64_t>(0x1000000000000000) == "1152921504606846976");
|
||||||
//CHECK(to_string_view(int_to_ary<int64_t>(0xF000000000000000)) == "-1152921504606846976");
|
CHECK(int_to_ary<int64_t>(0xF000000000000000) == "-1152921504606846976");
|
||||||
|
|
||||||
//CHECK(to_string_view(int_to_ary<uint16_t, 16>(0xFFFF)) == "ffff");
|
CHECK(int_to_ary<uint16_t SPROUT_PP_COMMA() 16>(0xFFFF) == "ffff");
|
||||||
//CHECK(to_string_view(int_to_ary<uint16_t, 16>(0xCACA)) == "caca");
|
CHECK(int_to_ary<uint16_t SPROUT_PP_COMMA() 16>(0xCACA) == "caca");
|
||||||
//CHECK(to_string_view(int_to_ary<uint16_t, 16>(0x10)) == "10");
|
CHECK(int_to_ary<uint16_t SPROUT_PP_COMMA() 16>(0x10) == "10");
|
||||||
//CHECK(to_string_view(int_to_ary<int16_t, 16>(0x10)) == "10");
|
CHECK(int_to_ary<int16_t SPROUT_PP_COMMA() 16>(0x10) == "10");
|
||||||
//CHECK(to_string_view(int_to_ary<int16_t, 16>(0xF000)) == "f000");
|
CHECK(int_to_ary<int16_t SPROUT_PP_COMMA() 16>(0xF000) == "f000");
|
||||||
//CHECK(to_string_view(int_to_ary<uint16_t, 16>(0xFEFE)) == "fefe");
|
CHECK(int_to_ary<uint16_t SPROUT_PP_COMMA() 16>(0xFEFE) == "fefe");
|
||||||
//CHECK(to_string_view(int_to_ary<int16_t, 16>(0xFEFE)) == "fefe");
|
CHECK(int_to_ary<int16_t SPROUT_PP_COMMA() 16>(0xFEFE) == "fefe");
|
||||||
//CHECK(to_string_view(int_to_ary<uint32_t, 8>(0423)) == "423");
|
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 8>(0423) == "423");
|
||||||
//CHECK(to_string_view(int_to_ary<uint32_t, 8>(0777)) == "777");
|
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 8>(0777) == "777");
|
||||||
//CHECK(to_string_view(int_to_ary<int32_t, 8>(0)) == "0");
|
CHECK(int_to_ary<int32_t SPROUT_PP_COMMA() 8>(0) == "0");
|
||||||
//CHECK(to_string_view(int_to_ary<uint32_t, 8>(0)) == "0");
|
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 8>(0) == "0");
|
||||||
//CHECK(to_string_view(int_to_ary<int32_t, 2>(0)) == "0");
|
CHECK(int_to_ary<int32_t SPROUT_PP_COMMA() 2>(0) == "0");
|
||||||
//CHECK(to_string_view(int_to_ary<uint32_t, 2>(0)) == "0");
|
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 2>(0) == "0");
|
||||||
//CHECK(to_string_view(int_to_ary<int32_t, 16>(0)) == "0");
|
CHECK(int_to_ary<int32_t SPROUT_PP_COMMA() 16>(0) == "0");
|
||||||
//CHECK(to_string_view(int_to_ary<uint32_t, 16>(0)) == "0");
|
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 16>(0) == "0");
|
||||||
//CHECK(to_string_view(int_to_ary<int32_t, 10>(0)) == "0");
|
CHECK(int_to_ary<int32_t SPROUT_PP_COMMA() 10>(0) == "0");
|
||||||
//CHECK(to_string_view(int_to_ary<uint32_t, 10>(0)) == "0");
|
CHECK(int_to_ary<uint32_t SPROUT_PP_COMMA() 10>(0) == "0");
|
||||||
//CHECK(to_string_view(int_to_ary<bool, 8>(false)) == "0");
|
CHECK(int_to_ary<bool SPROUT_PP_COMMA() 8>(false) == "0");
|
||||||
//CHECK(to_string_view(int_to_ary<bool, 8>(true)) == "1");
|
CHECK(int_to_ary<bool SPROUT_PP_COMMA() 8>(true) == "1");
|
||||||
//CHECK(to_string_view(int_to_ary<bool, 10>(false)) == "0");
|
CHECK(int_to_ary<bool SPROUT_PP_COMMA() 10>(false) == "0");
|
||||||
//CHECK(to_string_view(int_to_ary<bool, 10>(true)) == "1");
|
CHECK(int_to_ary<bool SPROUT_PP_COMMA() 10>(true) == "1");
|
||||||
//CHECK(to_string_view(int_to_ary<uint8_t, 2>(0b10101010)) == "10101010");
|
CHECK(int_to_ary<uint8_t SPROUT_PP_COMMA() 2>(0b10101010) == "10101010");
|
||||||
//CHECK(to_string_view(int_to_ary<int8_t, 2>(0b10101010)) == "10101010");
|
CHECK(int_to_ary<int8_t SPROUT_PP_COMMA() 2>(0b10101010) == "10101010");
|
||||||
//CHECK(to_string_view(int_to_ary<uint8_t, 2>(0b11111111)) == "11111111");
|
CHECK(int_to_ary<uint8_t SPROUT_PP_COMMA() 2>(0b11111111) == "11111111");
|
||||||
//CHECK(to_string_view(int_to_ary<int8_t, 2>(0b11111111)) == "11111111");
|
CHECK(int_to_ary<int8_t SPROUT_PP_COMMA() 2>(0b11111111) == "11111111");
|
||||||
//CHECK(to_string_view(int_to_ary<uint16_t, 2>(0b111100001111)) == "111100001111");
|
CHECK(int_to_ary<uint16_t SPROUT_PP_COMMA() 2>(0b111100001111) == "111100001111");
|
||||||
//CHECK(to_string_view(int_to_ary<int16_t, 2>(0b111100001111)) == "111100001111");
|
CHECK(int_to_ary<int16_t SPROUT_PP_COMMA() 2>(0b111100001111) == "111100001111");
|
||||||
//CHECK(to_string_view(int_to_ary<int64_t, 36>(9223372036854775807)) == "1y2p0ij32e8e7");
|
CHECK(int_to_ary<int64_t SPROUT_PP_COMMA() 36>(9223372036854775807) == "1y2p0ij32e8e7");
|
||||||
//CHECK(to_string_view(int_to_ary<int64_t, 36>(0x8000000000000001)) == "1y2p0ij32e8e9");
|
CHECK(int_to_ary<int64_t SPROUT_PP_COMMA() 36>(0x8000000000000001) == "1y2p0ij32e8e9");
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
static_assert(std::is_integral<__int128_t>::value, "Warning, int128 won't pickup the optimized base 10 conversion path");
|
static_assert(std::is_integral<__int128_t>::value, "Warning, int128 won't pickup the optimized base 10 conversion path");
|
||||||
__int128_t num = 10000000000000000000U;
|
__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;
|
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
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue