Refactored get_init_array() and get_zero_array() to get_init_array(char); Made all free functions inline

This commit is contained in:
Andreas Tsouchlos 2021-11-26 14:27:45 +01:00
parent c0d0bb5bad
commit a0230a7e27
4 changed files with 56 additions and 46 deletions

View File

@ -20,7 +20,7 @@ namespace detail {
template <ConstString s>
constexpr int get_output_len() {
constexpr inline int get_output_len() {
constexpr auto parse_result = parse_string<s>();
static_assert(parse_result.is_valid, "Syntax error in format string");
@ -28,7 +28,7 @@ constexpr int get_output_len() {
}
template <fmt_node_t fmt_node, typename T>
constexpr void check_fmt_params() {
constexpr inline void check_fmt_params() {
static_assert(fmt_node.length > fmt_node.precision + 1,
"Insufficient length for desired precision");
}
@ -40,19 +40,21 @@ constexpr void check_fmt_params() {
*
*/
// TODO: Error handling
template <std::integral arg_t>
constexpr void format_arg(char* dest, fmt_data_t fmt_data, arg_t arg) {
constexpr inline void format_arg(char* dest, fmt_data_t fmt_data, arg_t arg) {
// constexpr auto error_array = get_init_array<fmt_data.length>('f');
detail::format_integral(dest, arg, fmt_data);
};
// TODO: Error handling
template <std::floating_point arg_t>
constexpr void format_arg(char* dest, fmt_data_t fmt_data, arg_t) {
constexpr inline void format_arg(char* dest, fmt_data_t fmt_data, arg_t) {
*(dest) = 'f';
*(dest + fmt_data.length - fmt_data.precision - 1) = '.';
};
// TODO: Error handling
constexpr void format_arg(char* dest, fmt_data_t fmt_data, const char* arg) {
constexpr inline void format_arg(char* dest, fmt_data_t fmt_data, const char* arg) {
const std::size_t len = const_strlen(arg);
if (len > fmt_data.length) return;
@ -68,20 +70,21 @@ constexpr void format_arg(char* dest, fmt_data_t fmt_data, const char* arg) {
};
// End of recursion
template <auto ast>
constexpr void format_args(char*) {
constexpr inline void format_args(char*) {
}
template <auto fmt_data_array, typename first_arg_t, typename... args_t>
constexpr void format_args(char* dest, first_arg_t first_arg, args_t... args) {
constexpr inline void format_args(char* dest, first_arg_t first_arg, args_t... args) {
format_arg(dest + fmt_data_array[0].position, fmt_data_array[0], first_arg);
format_args<drop_first(fmt_data_array)>(dest, args...);
}
template <auto ast>
consteval std::array<char, get_ast_output_len<ast>()> get_preproc_string() {
auto result = get_zero_array<get_ast_output_len<ast>()>();
consteval inline std::array<char, get_ast_output_len<ast>()> get_preproc_string() {
auto result = get_init_array<get_ast_output_len<ast>()>('0');
int i = 0;
@ -107,7 +110,7 @@ consteval std::array<char, get_ast_output_len<ast>()> get_preproc_string() {
template <detail::ConstString s, typename... args_t>
constexpr auto format(args_t... args) {
constexpr inline auto format(args_t... args) {
constexpr auto ast = detail::parse_string<s>().value;
constexpr auto fmt_data = detail::get_fmt_data<ast>();

View File

@ -47,9 +47,6 @@ constexpr inline void copy2(char* dst, const char* src) {
template <typename UInt>
constexpr inline void format_decimal(char* out, UInt value, int size) {
// TODO: Error handling (Maybe calculate "ffff..." as a constant expression
// and then set the out pointer with a branchless statement)
out += size;
while (value >= 100) {
out -= 2;

View File

@ -48,7 +48,7 @@ namespace detail {
template <ConstString s>
constexpr unsigned count_braces() {
constexpr inline unsigned count_braces() {
unsigned result = 0;
for (unsigned i = 0; i < s.size(); ++i) {
@ -59,7 +59,7 @@ constexpr unsigned count_braces() {
}
template <ConstString s>
constexpr unsigned strlen_braces() {
constexpr inline unsigned strlen_braces() {
unsigned result = 0;
bool brace_open = false;
@ -81,7 +81,7 @@ constexpr unsigned strlen_braces() {
}
template <ConstString s>
constexpr int get_ast_len() {
constexpr inline int get_ast_len() {
return (s.size() - strlen_braces<s>() + count_braces<s>());
}
@ -94,13 +94,13 @@ constexpr int get_ast_len() {
template <ConstString s>
constexpr bool is_digit(unsigned i) {
constexpr inline bool is_digit(unsigned i) {
return (s[i] > 47) && (s[i] < 58);
}
template <ConstString s>
constexpr parse_result_t<unsigned> parse_number(unsigned i) {
constexpr inline parse_result_t<unsigned> parse_number(unsigned i) {
unsigned number = 0;
if (!is_digit<s>(i)) {
@ -117,7 +117,7 @@ constexpr parse_result_t<unsigned> parse_number(unsigned i) {
}
template <ConstString s>
constexpr parse_result_t<FormatType> parse_type(unsigned i) {
constexpr inline parse_result_t<FormatType> parse_type(unsigned i) {
switch (s[i]) {
case 's':
return {true, ++i, FormatType::s};
@ -159,7 +159,7 @@ constexpr parse_result_t<FormatType> parse_type(unsigned i) {
}
template <ConstString s>
constexpr parse_result_t<fmt_node_t> parse_fmt_string(unsigned i) {
constexpr inline parse_result_t<fmt_node_t> parse_fmt_string(unsigned i) {
fmt_node_t result;
if (s[i] == '0') {
@ -193,7 +193,7 @@ constexpr parse_result_t<fmt_node_t> parse_fmt_string(unsigned i) {
}
template <ConstString s>
constexpr parse_result_t<fmt_node_t> parse_braces(unsigned i) {
constexpr inline parse_result_t<fmt_node_t> parse_braces(unsigned i) {
if (s[i] == '}') {
++i;
return {true, i, {}};
@ -214,7 +214,7 @@ constexpr parse_result_t<fmt_node_t> parse_braces(unsigned i) {
}
template <ConstString s>
constexpr parse_result_t<string_result_t<get_ast_len<s>()>> parse_string() {
constexpr inline parse_result_t<string_result_t<get_ast_len<s>()>> parse_string() {
parse_result_t<string_result_t<get_ast_len<s>()>> result;
result.is_valid = true;

View File

@ -10,7 +10,7 @@
namespace detail {
constexpr std::size_t const_pow(std::size_t base, std::size_t pow) {
constexpr inline std::size_t const_pow(std::size_t base, std::size_t pow) {
if (pow == 0)
return 1;
else
@ -18,34 +18,44 @@ constexpr std::size_t const_pow(std::size_t base, std::size_t pow) {
}
//template <std::size_t t_n>
//constexpr std::array<char, t_n> get_zero_array() {
// std::array<char, t_n> result;
//
// for (auto& c : result)
// c = '0';
//
// return result;
//}
template <std::size_t t_n>
constexpr std::array<char, t_n> get_zero_array() {
constexpr inline std::array<char, t_n> get_init_array(char val) {
std::array<char, t_n> result;
for (auto& c : result)
c = '0';
c = val;
return result;
}
template <fmt_node_t fmt_node>
constexpr std::array<char, fmt_node.length> get_init_array() {
std::array<char, fmt_node.length> result;
if constexpr (fmt_node.has_zero_padding) {
for (auto& c : result)
c = '0';
} else {
for (auto& c : result)
c = ' ';
}
return result;
}
//template <fmt_node_t fmt_node>
//constexpr std::array<char, fmt_node.length> get_init_array() {
// std::array<char, fmt_node.length> result;
//
// if constexpr (fmt_node.has_zero_padding) {
// for (auto& c : result)
// c = '0';
// } else {
// for (auto& c : result)
// c = ' ';
// }
//
// return result;
//}
template <auto ast>
consteval std::size_t count_ast_format_nodes() {
consteval inline std::size_t count_ast_format_nodes() {
std::size_t result = 0;
for (const auto& node : ast)
@ -56,7 +66,7 @@ consteval std::size_t count_ast_format_nodes() {
template <auto ast>
consteval std::array<fmt_data_t, count_ast_format_nodes<ast>()> get_fmt_data() {
consteval inline std::array<fmt_data_t, count_ast_format_nodes<ast>()> get_fmt_data() {
std::array<fmt_data_t, count_ast_format_nodes<ast>()> result = {};
std::size_t position = 0;
@ -84,7 +94,7 @@ consteval std::array<fmt_data_t, count_ast_format_nodes<ast>()> get_fmt_data() {
template <typename elem_t, std::size_t t_n>
consteval std::array<elem_t, t_n - 1>
consteval inline std::array<elem_t, t_n - 1>
drop_first(std::array<elem_t, t_n> array) {
static_assert(t_n > 0,
"Can't drop first element of array with no elements");
@ -98,7 +108,7 @@ drop_first(std::array<elem_t, t_n> array) {
template <auto t_ast>
consteval int get_ast_output_len() {
consteval inline int get_ast_output_len() {
unsigned result = 0;
for (const auto& ast_node : t_ast) {
@ -111,7 +121,7 @@ consteval int get_ast_output_len() {
return result;
}
constexpr std::size_t const_strlen(const char* arg) {
constexpr inline std::size_t const_strlen(const char* arg) {
if (std::is_constant_evaluated()) {
return *arg ? 1 + const_strlen(arg + 1) : 0;
} else {