14 Commits

Author SHA1 Message Date
1c46307798 Formatting & commented out do_count_digits_decimal() 2022-03-18 13:34:25 +01:00
b7159c3606 Merge pull request 'fix/zero_length_array' (#10) from fix/zero_length_array into master
Reviewed-on: http://git.mercurial-manifold.eu/an.tsouchlos/const_fmt/pulls/10
2022-03-18 00:24:51 +00:00
2be63fda5a Changed the way recursion ends in const_fmt::format_args<>() 2022-03-18 01:23:56 +01:00
036b17fca9 Formatting 2022-03-18 01:15:32 +01:00
045363c733 Merge pull request 'feature/no_std_lib' (#9) from feature/no_std_lib into master
Reviewed-on: http://git.mercurial-manifold.eu/an.tsouchlos/const_fmt/pulls/9
2022-03-17 22:45:07 +00:00
4d05a9a8fa Added const versions of std::array::begin() and std::array::end() 2022-03-17 22:44:17 +01:00
ee1153c393 Changed copy constructor signature of std::array implementation 2022-03-17 22:11:30 +01:00
b29695e148 Replaced memcpy with manual while loop 2022-03-17 18:04:06 +01:00
86930fd8b0 Added conepts; Fixed bug in const_fmt::std::true_type 2022-02-24 16:14:38 +01:00
7796be7cd4 Reimplemented is_integral and is_floating_point with different approach; Made functions constexpr inline 2022-02-23 23:12:34 +01:00
73a50cae0f Added is_integral and is_floating_point 2022-02-23 20:42:56 +01:00
91398ef23d Added remove_cv, remove_const and remove_volatile 2022-02-23 19:51:55 +01:00
b5ecd73b18 Formatting 2022-02-23 19:47:06 +01:00
826f3a54b3 Changed comment 2022-02-23 19:46:33 +01:00
3 changed files with 148 additions and 54 deletions

View File

@@ -66,15 +66,12 @@ constexpr inline void format_arg(char* dest, const char* arg) {
}; };
// End of recursion
template <auto ast>
constexpr inline void format_args(char*) {
}
template <auto fmt_data_array, typename first_arg_t, typename... args_t> template <auto fmt_data_array, typename first_arg_t, typename... args_t>
constexpr inline void format_args(char* dest, first_arg_t first_arg, constexpr inline void format_args(char* dest, first_arg_t first_arg,
args_t... args) { args_t... args) {
format_arg<fmt_data_array[0]>(dest + fmt_data_array[0].position, first_arg); format_arg<fmt_data_array[0]>(dest + fmt_data_array[0].position, first_arg);
if constexpr(fmt_data_array.size() > 1)
format_args<drop_first(fmt_data_array)>(dest, args...); format_args<drop_first(fmt_data_array)>(dest, args...);
} }

View File

@@ -51,7 +51,9 @@ inline int do_count_digits_decimal(uint64_t n) {
6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10,
10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15, 10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20}; 15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20};
auto t = bsr2log10[__builtin_clzll(n | 1) ^ 63]; auto t = bsr2log10[__builtin_clzll(n | 1) ^ 63];
static constexpr const uint64_t zero_or_powers_of_10[] = { static constexpr const uint64_t zero_or_powers_of_10[] = {
0, 0, FMT_POWERS_OF_10(1U), FMT_POWERS_OF_10(1000000000ULL), 0, 0, FMT_POWERS_OF_10(1U), FMT_POWERS_OF_10(1000000000ULL),
10000000000000000000ULL}; 10000000000000000000ULL};
@@ -80,9 +82,9 @@ constexpr inline auto count_digits_base(uint64_t n) -> int {
return (result + count_digits_base<FormatType::b>(n)); return (result + count_digits_base<FormatType::b>(n));
} else { } else {
if (!std::is_constant_evaluated()) { // if (!std::is_constant_evaluated()) {
return do_count_digits_decimal(n); // return do_count_digits_decimal(n);
} // }
return count_digits_decimal_fallback(n); return count_digits_decimal_fallback(n);
} }
} }
@@ -161,7 +163,7 @@ constexpr inline void format_base(char* out, uint_t value, int n_digits,
} }
if (value < divisor) { if (value < divisor) {
*--out = digits2_base<t_format_type>(value*divisor)[0]; *--out = digits2_base<t_format_type>(value * divisor)[0];
return; return;
} }

View File

@@ -4,9 +4,9 @@
/* /*
* *
* Disclaimer: Probably very bad implementation of some features of the C++ * Disclaimer: Very bad (partially at least) implementation of some features of
* Standard Library. Not meant as a full-on stdlib implementation, only for * the C++ Standard Library. Not meant as a full-on stdlib implementation, only
* usage in this project (Underlined by the fact, that in this case the * for usage in this project (Underlined by the fact, that in this case the
* namespace std is actually const_fmt::std) * namespace std is actually const_fmt::std)
* *
*/ */
@@ -36,31 +36,105 @@ using size_t = uint16_t;
*/ */
// clang-format off // various
template<typename T>
struct remove_reference {
using type = T;
};
template<typename T>
struct remove_reference<T &> {
using type = T;
};
template<typename T>
struct remove_reference<T &&> {
using type = T;
};
template<typename T>
using remove_reference_t = typename std::remove_reference<T>::type;
// clang-format on
constexpr inline bool is_constant_evaluated() noexcept { constexpr inline bool is_constant_evaluated() noexcept {
return __builtin_is_constant_evaluated(); return __builtin_is_constant_evaluated();
} }
struct true_type {
constexpr static bool value = true;
};
struct false_type {
constexpr static bool value = false;
};
// is_same
template <typename fist_t, typename second_t>
struct is_same : public false_type {};
template <typename type_t>
struct is_same<type_t, type_t> : public true_type {};
// is_one_of
template <typename...>
struct is_one_of;
template <typename type_t>
struct is_one_of<type_t> {
constexpr static bool value = false;
};
template <typename type_t, typename first_t, typename... rest_t>
struct is_one_of<type_t, first_t, rest_t...> {
constexpr static bool value = std::is_same<type_t, first_t>::value ||
is_one_of<type_t, rest_t...>::value;
};
// remove_x
// clang-format off
template <typename type_t> struct remove_cv { typedef type_t type; };
template <typename type_t> struct remove_cv<const type_t> { typedef type_t type; };
template <typename type_t> struct remove_cv<volatile type_t> { typedef type_t type; };
template <typename type_t> struct remove_cv<const volatile type_t> { typedef type_t type; };
template <typename type_t> struct remove_const { typedef type_t type; };
template <typename type_t> struct remove_const<const type_t> { typedef type_t type; };
template <typename type_t> struct remove_volatile { typedef type_t type; };
template <typename type_t> struct remove_volatile<volatile type_t> { typedef type_t type; };
template <typename type_t> struct remove_reference { using type = type_t; };
template <typename type_t> struct remove_reference<type_t &> { using type = type_t; };
template <typename type_t> struct remove_reference<type_t &&> { using type = type_t; };
template <typename type_t> using remove_reference_t = typename std::remove_reference<type_t>::type;
template <typename type_t> using remove_cv_t = typename std::remove_cv<type_t>::type;
template <typename type_t> using remove_const_t = typename std::remove_const<type_t>::type;
template <typename type_t> using remove_volatile_t = typename std::remove_volatile<type_t>::type;
// clang-format on
// is_integral
template <typename type_t>
using is_integral =
is_one_of<remove_cv_t<type_t>, bool, char, signed char, unsigned char,
wchar_t, char16_t, char32_t, short, unsigned short, int,
unsigned int, long, unsigned long, long long, unsigned long long>;
// is_signed
template <typename _Tp>
using is_signed_integer = is_one_of<remove_cv_t<_Tp>, signed char, signed short,
signed int, signed long, signed long long>;
// is_floating_point
template <typename type_t>
using is_floating_point =
is_one_of<remove_cv_t<type_t>, float, double, long double>;
// make_unsigned
// clang-format off // clang-format off
@@ -75,6 +149,16 @@ template <> struct make_unsigned<signed long long> { using type = unsigned long
// clang-format on // clang-format on
// value definitions
template <typename type_t>
inline constexpr bool is_integral_v = is_integral<type_t>::value;
template <typename type_t>
inline constexpr bool is_signed_integer_v = is_signed_integer<type_t>::value;
template <typename type_t>
inline constexpr bool is_floating_point_v = is_floating_point<type_t>::value;
/* /*
* *
* concepts * concepts
@@ -82,17 +166,17 @@ template <> struct make_unsigned<signed long long> { using type = unsigned long
*/ */
//template <typename _Tp> template <typename type_t>
//concept integral = is_integral_v<_Tp>; concept integral = is_integral_v<type_t>;
//
//template <typename _Tp> template <typename type_t>
//concept signed_integral = integral<_Tp> && is_signed_v<_Tp>; concept signed_integral = is_signed_integer_v<type_t>;
//
//template <typename _Tp> template <typename type_t>
//concept unsigned_integral = integral<_Tp> && !signed_integral<_Tp>; concept unsigned_integral = integral<type_t> && !signed_integral<type_t>;
//
//template <typename _Tp> template <typename type_t>
//concept floating_point = is_floating_point_v<_Tp>; concept floating_point = is_floating_point_v<type_t>;
/* /*
@@ -103,8 +187,11 @@ template <> struct make_unsigned<signed long long> { using type = unsigned long
template <typename input_t, typename output_t> template <typename input_t, typename output_t>
void copy(input_t* start, input_t* end, output_t* dest_start) { constexpr inline void copy(const input_t* start, const input_t* end,
memcpy(start, dest_start, end - start); output_t* dest_start) {
auto temp = start;
while (temp != end)
*(dest_start++) = *(temp++);
} }
@@ -121,7 +208,7 @@ std::remove_reference_t<T>&& move(T&& arg) noexcept {
} }
template <typename T> template <typename T>
void swap(T& t1, T& t2) { constexpr inline void swap(T& t1, T& t2) {
T temp = std::move(t1); T temp = std::move(t1);
t1 = std::move(t2); t1 = std::move(t2);
t2 = std::move(temp); t2 = std::move(temp);
@@ -151,13 +238,10 @@ public:
} }
constexpr array() noexcept = default; constexpr array() noexcept = default;
constexpr array(const array&) = default;
constexpr array(array&) = default;
constexpr array(array&&) = default; constexpr array(array&&) = default;
constexpr array& operator=(array& other) = default; constexpr array& operator=(const array& other) = default;
constexpr array& operator=(array&& other) = default; constexpr array& operator=(array&& other) = default;
constexpr void swap(array<data_t, t_size>& other) noexcept { constexpr void swap(array<data_t, t_size>& other) noexcept {
@@ -167,6 +251,9 @@ public:
} }
} }
constexpr std::size_t size() const noexcept {
return t_size;
}
constexpr data_t& operator[](std::size_t index) noexcept { constexpr data_t& operator[](std::size_t index) noexcept {
return m_data[index]; return m_data[index];
@@ -188,6 +275,14 @@ public:
return (&(m_data[t_size - 1]) + 1); return (&(m_data[t_size - 1]) + 1);
} }
constexpr const_iterator begin() const noexcept {
return &(m_data[0]);
}
constexpr const_iterator end() const noexcept {
return (&(m_data[t_size - 1]) + 1);
}
constexpr const_iterator cbegin() const noexcept { constexpr const_iterator cbegin() const noexcept {
return &(m_data[0]); return &(m_data[0]);
} }