recfactor: use FMT_THROW inside fmt.h to make code embedded-friendly

This commit is contained in:
Yves Delley
2022-12-13 17:45:00 +01:00
parent 02d6e104f0
commit 4603d24f2c
2 changed files with 18 additions and 17 deletions

View File

@@ -55,7 +55,7 @@ public:
constexpr fill_t& operator=(std::basic_string_view<Char> str) constexpr fill_t& operator=(std::basic_string_view<Char> str)
{ {
auto size = str.size(); auto size = str.size();
if (size > max_size) throw STD_FMT::format_error("invalid fill"); if (size > max_size) FMT_THROW(STD_FMT::format_error("invalid fill"));
for (size_t i = 0; i < size; ++i) data_[i] = str[i]; for (size_t i = 0; i < size; ++i) data_[i] = str[i];
size_ = static_cast<unsigned char>(size); size_ = static_cast<unsigned char>(size);
return *this; return *this;
@@ -98,11 +98,11 @@ struct width_checker {
{ {
if constexpr (is_integer<T>) { if constexpr (is_integer<T>) {
if constexpr (std::numeric_limits<T>::is_signed) { if constexpr (std::numeric_limits<T>::is_signed) {
if (value < 0) throw STD_FMT::format_error("negative width"); if (value < 0) FMT_THROW(STD_FMT::format_error("negative width"));
} }
return static_cast<unsigned long long>(value); return static_cast<unsigned long long>(value);
} else { } else {
throw STD_FMT::format_error("width is not integer"); FMT_THROW(STD_FMT::format_error("width is not integer"));
} }
} }
}; };
@@ -113,11 +113,11 @@ struct precision_checker {
{ {
if constexpr (is_integer<T>) { if constexpr (is_integer<T>) {
if constexpr (std::numeric_limits<T>::is_signed) { if constexpr (std::numeric_limits<T>::is_signed) {
if (value < 0) throw STD_FMT::format_error("negative precision"); if (value < 0) FMT_THROW(STD_FMT::format_error("negative precision"));
} }
return static_cast<unsigned long long>(value); return static_cast<unsigned long long>(value);
} else { } else {
throw STD_FMT::format_error("precision is not integer"); FMT_THROW(STD_FMT::format_error("precision is not integer"));
} }
} }
}; };
@@ -147,7 +147,7 @@ struct dynamic_format_specs : basic_format_specs<Char> {
[[nodiscard]] constexpr int verify_dynamic_arg_index_in_range(size_t idx) [[nodiscard]] constexpr int verify_dynamic_arg_index_in_range(size_t idx)
{ {
if (idx > static_cast<size_t>(std::numeric_limits<int>::max())) { if (idx > static_cast<size_t>(std::numeric_limits<int>::max())) {
throw STD_FMT::format_error("Dynamic width or precision index too large."); FMT_THROW(STD_FMT::format_error("Dynamic width or precision index too large."));
} }
return static_cast<int>(idx); return static_cast<int>(idx);
} }
@@ -171,7 +171,7 @@ template<class Handler, typename FormatContext>
const unsigned long long value = const unsigned long long value =
STD_FMT::visit_format_arg(Handler{}, ctx.arg(FMT_TO_ARG_ID(static_cast<size_t>(index)))); STD_FMT::visit_format_arg(Handler{}, ctx.arg(FMT_TO_ARG_ID(static_cast<size_t>(index))));
if (value > static_cast<unsigned long long>(std::numeric_limits<int>::max())) { if (value > static_cast<unsigned long long>(std::numeric_limits<int>::max())) {
throw STD_FMT::format_error("number is too big"); FMT_THROW(STD_FMT::format_error("number is too big"));
} }
return static_cast<int>(value); return static_cast<int>(value);
} }
@@ -195,7 +195,7 @@ template<std::input_iterator It, std::sentinel_for<It> S>
++begin; ++begin;
} while (begin != end && '0' <= *begin && *begin <= '9'); } while (begin != end && '0' <= *begin && *begin <= '9');
if (value > max_int) throw STD_FMT::format_error("Number is too big"); if (value > max_int) FMT_THROW(STD_FMT::format_error("Number is too big"));
return begin; return begin;
} }
@@ -222,12 +222,12 @@ template<std::input_iterator It, std::sentinel_for<It> S, typename IDHandler>
else else
++begin; ++begin;
if (begin == end || (*begin != '}' && *begin != ':')) if (begin == end || (*begin != '}' && *begin != ':'))
throw STD_FMT::format_error("invalid format string"); FMT_THROW(STD_FMT::format_error("invalid format string"));
else else
handler(index); handler(index);
return begin; return begin;
} }
throw STD_FMT::format_error("invalid format string"); FMT_THROW(STD_FMT::format_error("invalid format string"));
} }
template<std::input_iterator It, std::sentinel_for<It> S, typename IDHandler> template<std::input_iterator It, std::sentinel_for<It> S, typename IDHandler>
@@ -278,11 +278,11 @@ template<std::input_iterator It, std::sentinel_for<It> S, typename Handler>
if (width != -1) if (width != -1)
handler.on_width(width); handler.on_width(width);
else else
throw STD_FMT::format_error("number is too big"); FMT_THROW(STD_FMT::format_error("number is too big"));
} else if (*begin == '{') { } else if (*begin == '{') {
++begin; ++begin;
if (begin != end) begin = parse_arg_id(begin, end, width_adapter{handler}); if (begin != end) begin = parse_arg_id(begin, end, width_adapter{handler});
if (begin == end || *begin != '}') throw STD_FMT::format_error("invalid format string"); if (begin == end || *begin != '}') FMT_THROW(STD_FMT::format_error("invalid format string"));
++begin; ++begin;
} }
return begin; return begin;
@@ -305,13 +305,13 @@ template<std::input_iterator It, std::sentinel_for<It> S, typename Handler>
if (precision != -1) if (precision != -1)
handler.on_precision(precision); handler.on_precision(precision);
else else
throw STD_FMT::format_error("number is too big"); FMT_THROW(STD_FMT::format_error("number is too big"));
} else if (c == '{') { } else if (c == '{') {
++begin; ++begin;
if (begin != end) begin = parse_arg_id(begin, end, precision_adapter{handler}); if (begin != end) begin = parse_arg_id(begin, end, precision_adapter{handler});
if (begin == end || *begin++ != '}') throw STD_FMT::format_error("invalid format string"); if (begin == end || *begin++ != '}') FMT_THROW(STD_FMT::format_error("invalid format string"));
} else { } else {
throw STD_FMT::format_error("missing precision specifier"); FMT_THROW(STD_FMT::format_error("missing precision specifier"));
} }
return begin; return begin;
} }
@@ -355,7 +355,7 @@ template<std::input_iterator It, std::sentinel_for<It> S, typename Handler>
if (align != fmt_align::none) { if (align != fmt_align::none) {
if (p != begin) { if (p != begin) {
auto c = *begin; auto c = *begin;
if (c == '{') throw STD_FMT::format_error("invalid fill character '{'"); if (c == '{') FMT_THROW(STD_FMT::format_error("invalid fill character '{'"));
handler.on_fill(std::basic_string_view<std::iter_value_t<It>>(&*begin, static_cast<size_t>(p - begin))); handler.on_fill(std::basic_string_view<std::iter_value_t<It>>(&*begin, static_cast<size_t>(p - begin)));
begin = p + 1; begin = p + 1;
} else } else

View File

@@ -38,7 +38,7 @@
UNITS_DIAGNOSTIC_PUSH UNITS_DIAGNOSTIC_PUSH
UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE UNITS_DIAGNOSTIC_IGNORE_UNREACHABLE
UNITS_DIAGNOSTIC_IGNORE_SHADOW UNITS_DIAGNOSTIC_IGNORE_SHADOW
#include <fmt/format.h> #include <fmt/core.h>
UNITS_DIAGNOSTIC_POP UNITS_DIAGNOSTIC_POP
#define STD_FMT fmt #define STD_FMT fmt
@@ -58,5 +58,6 @@ UNITS_DIAGNOSTIC_POP
#define FMT_LOCALE(loc) loc #define FMT_LOCALE(loc) loc
#define FMT_TO_ARG_ID(arg) arg #define FMT_TO_ARG_ID(arg) arg
#define FMT_FROM_ARG_ID(arg) arg #define FMT_FROM_ARG_ID(arg) arg
#define FMT_THROW(arg) throw arg
#endif #endif