Cleaning up and simplifying code a bit

This commit is contained in:
King_DuckZ 2021-05-28 02:03:52 +02:00
parent 7f8f092372
commit e62da38cf9

View file

@ -70,6 +70,9 @@ namespace dhandy {
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; 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, typename Tr>
using RevArray = ReversedSizedArray<typename Tr::char_type, int_info<I, Base>::max_len + 1>;
template <typename I, unsigned int Base, bool Signed=int_info<I, 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; } }; struct IsNegative { static constexpr bool check (I) { return false; } };
template <typename I, unsigned int Base> template <typename I, unsigned int Base>
@ -99,14 +102,13 @@ namespace dhandy {
template <typename I, unsigned int Base, typename Tr, typename=void> template <typename I, unsigned int Base, typename Tr, typename=void>
struct IntConversion { struct IntConversion {
static constexpr ReversedSizedArray<std::decay_t<decltype(std::declval<Tr>().to_digit(1))>, implem::int_info<I, Base>::max_len + 1> to_ary (I in) { static constexpr RevArray<I, Base, Tr> 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<I, Base>; using Num = implem::NumberAdaptation<I, Base>;
const bool was_negative = implem::is_negative<I, Base>(in); const bool was_negative = implem::is_negative<I, Base>(in);
RetType arr; RevArray<I, Base, Tr> arr;
arr.push_front('\0'); arr.push_front(Tr::NullChar);
do { do {
arr.push_front(Tr::to_digit(static_cast<int>(Num::abs(Num::cast(in)) % Base))); 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)); in = static_cast<I>(Num::cast(in) / static_cast<I>(Base));
@ -119,11 +121,9 @@ namespace dhandy {
template <unsigned int Base, typename Tr> template <unsigned int Base, typename Tr>
struct IntConversion<bool, Base, Tr, void> { struct IntConversion<bool, Base, Tr, void> {
static constexpr ReversedSizedArray<std::decay_t<decltype(std::declval<Tr>().to_digit(1))>, 2> to_ary (bool in) { static constexpr ReversedSizedArray<typename Tr::char_type, 2> to_ary (bool in) {
using RetType = ReversedSizedArray<std::decay_t<decltype(std::declval<Tr>().to_digit(1))>, 2>; ReversedSizedArray<typename Tr::char_type, 2> arr;
arr.push_front(Tr::NullChar);
RetType arr;
arr.push_front('\0');
arr.push_front(in ? Tr::to_digit(1) : Tr::to_digit(0)); arr.push_front(in ? Tr::to_digit(1) : Tr::to_digit(0));
return arr; return arr;
} }
@ -131,8 +131,8 @@ namespace dhandy {
template <typename I, typename Tr> template <typename I, typename Tr>
struct IntConversion<I, 10, Tr, typename std::enable_if<std::is_integral<I>::value and not std::is_same<I, bool>::value>::type> { struct IntConversion<I, 10, Tr, typename std::enable_if<std::is_integral<I>::value and not std::is_same<I, bool>::value>::type> {
static constexpr ReversedSizedArray<std::decay_t<decltype(std::declval<Tr>().to_digit(1))>, implem::int_info<I, 10>::max_len + 1> to_ary (I in) { static constexpr RevArray<I, 10, Tr> to_ary (I in) {
using RetType = ReversedSizedArray<std::decay_t<decltype(std::declval<Tr>().to_digit(1))>, implem::int_info<I, 10>::max_len + 1>; using RetType = RevArray<I, 10, Tr>;
using Num = implem::NumberAdaptation<I, 10>; using Num = implem::NumberAdaptation<I, 10>;
const constexpr char lookup[200] = { const constexpr char lookup[200] = {
@ -147,7 +147,7 @@ namespace dhandy {
const bool was_negative = implem::is_negative<I, 10>(in); const bool was_negative = implem::is_negative<I, 10>(in);
RetType arr; RetType arr;
arr.push_front('\0'); arr.push_front(Tr::NullChar);
while (Num::abs(in) >= static_cast<I>(100)) { while (Num::abs(in) >= static_cast<I>(100)) {
const auto index = (Num::abs(in) % 100) * 2; const auto index = (Num::abs(in) % 100) * 2;
arr.push_front(Tr::to_digit(static_cast<int>(lookup[index + 1]))); arr.push_front(Tr::to_digit(static_cast<int>(lookup[index + 1])));
@ -266,13 +266,15 @@ namespace dhandy {
} }
} //namespace implem } //namespace implem
template <typename C, C FDigit='0', C FLetter='a', C CPlus='+', C CMinus='-'> template <typename C, C FDigit='0', C FLetter='a', C CPlus='+', C CMinus='-', C CNull='\0'>
struct ASCIITranslator { struct ASCIITranslator {
typedef C char_type;
static const constexpr bool BehavesLikeASCII = true; static const constexpr bool BehavesLikeASCII = true;
static const constexpr C FirstDigit = FDigit; static const constexpr C FirstDigit = FDigit;
static const constexpr C FirstLetter = FLetter; static const constexpr C FirstLetter = FLetter;
static const constexpr C Plus = CPlus; static const constexpr C Plus = CPlus;
static const constexpr C Minus = CMinus; static const constexpr C Minus = CMinus;
static const constexpr C NullChar = CNull;
static constexpr C to_digit (unsigned int num) { static constexpr C to_digit (unsigned int num) {
return (num <= 9 ? return (num <= 9 ?
@ -293,7 +295,7 @@ namespace dhandy {
template <typename I, unsigned int Base=10, typename Tr=ASCIITranslator<char>> template <typename I, unsigned int Base=10, typename Tr=ASCIITranslator<char>>
constexpr inline auto int_to_ary (I in) { constexpr inline auto int_to_ary (I in) {
return implem::IntConversion<std::decay_t<I>, Base, Tr>::to_ary(in); return implem::IntConversion<std::remove_cvref_t<I>, Base, Tr>::to_ary(in);
} }
template <typename R, typename C, unsigned int Base=10, typename Tr=ASCIITranslator<C>> template <typename R, typename C, unsigned int Base=10, typename Tr=ASCIITranslator<C>>