From 89321b86bf1fdf8d2facfdc31b1b3e3a5efb48de Mon Sep 17 00:00:00 2001 From: Florin Iucha Date: Sun, 1 Dec 2019 13:01:19 -0500 Subject: [PATCH] fixup! Clean-up sign-conversion warnings in public headers --- include/fmt/chrono.h | 9 +++++++++ include/fmt/compile.h | 2 +- include/fmt/format.h | 12 +++++++----- include/fmt/printf.h | 28 +++++++++++++++++----------- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index ee4801b5..45b7b8a9 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -795,6 +795,15 @@ struct chrono_formatter { std::chrono::duration d) : context(ctx), out(o), val(static_cast(std::abs(d.count()))), negative(d.count() < 0) { + if (d.count() < 0) { + val = static_cast(- d.count()); + negative = true; + } + else { + val = static_cast(d.count()); + negative = false; + } + // this may overflow and/or the result may not fit in the // target type. #if FMT_SAFE_DURATION_CAST diff --git a/include/fmt/compile.h b/include/fmt/compile.h index e8f5aa72..5829f623 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -141,7 +141,7 @@ class format_string_compiler : public error_handler { auto it = parse_format_specs(begin, end, handler); if (*it != '}') on_error("missing '}' in format string"); repl.arg_id = part_.part_kind == part::kind::arg_index - ? arg_ref(static_cast(part_.val.arg_index)) + ? arg_ref(part_.val.arg_index) : arg_ref(part_.val.str); auto part = part::make_replacement(repl); part.arg_id_end = begin; diff --git a/include/fmt/format.h b/include/fmt/format.h index 65da1355..7edeb105 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1244,7 +1244,7 @@ int snprintf_float(T value, int precision, float_specs specs, buffer& buf); template T promote_float(T value) { return value; } -inline double promote_float(float value) { return static_cast(value); } +inline double promote_float(float value) { return value; } template FMT_CONSTEXPR void handle_int_type_spec(char spec, Handler&& handler) { @@ -2620,18 +2620,18 @@ class format_string_checker { public: explicit FMT_CONSTEXPR format_string_checker( basic_string_view format_str, ErrorHandler eh) - : arg_id_(max_value()), + : arg_id_(ARG_ID_SENTINEL), context_(format_str, eh), parse_funcs_{&parse_format_specs...} {} FMT_CONSTEXPR void on_text(const Char*, const Char*) {} FMT_CONSTEXPR void on_arg_id() { - arg_id_ = static_cast(context_.next_arg_id()); + arg_id_ = context_.next_arg_id(); check_arg_id(); } FMT_CONSTEXPR void on_arg_id(int id) { - arg_id_ = static_cast(id); + arg_id_ = id; context_.check_arg_id(id); check_arg_id(); } @@ -2661,7 +2661,9 @@ class format_string_checker { // Format specifier parsing function. using parse_func = const Char* (*)(parse_context_type&); - unsigned arg_id_; + enum { ARG_ID_SENTINEL = -1 }; + + int arg_id_; parse_context_type context_; parse_func parse_funcs_[num_args > 0 ? num_args : 1]; }; diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 2eb177c7..92876ccf 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -183,6 +183,12 @@ internal::truncating_iterator printf( basic_format_args args) { return Context(it, format, args).format(); } + +enum +{ + ARG_INDEX_SENTINEL = -1 +}; + } // namespace internal using internal::printf; // For printing into memory_buffer. @@ -335,10 +341,10 @@ template class basic_printf_context { // Returns the argument with specified index or, if arg_index is equal // to the maximum unsigned value, the next argument. - format_arg get_arg(unsigned arg_index = internal::max_value()); + format_arg get_arg(int arg_index = internal::ARG_INDEX_SENTINEL); // Parses argument index, flags and width and returns the argument index. - unsigned parse_header(const Char*& it, const Char* end, format_specs& specs); + int parse_header(const Char*& it, const Char* end, format_specs& specs); public: /** @@ -397,18 +403,18 @@ void basic_printf_context::parse_flags(format_specs& specs, template typename basic_printf_context::format_arg -basic_printf_context::get_arg(unsigned arg_index) { - if (arg_index == internal::max_value()) - arg_index = static_cast(parse_ctx_.next_arg_id()); +basic_printf_context::get_arg(int arg_index) { + if (arg_index == internal::ARG_INDEX_SENTINEL) + arg_index = parse_ctx_.next_arg_id(); else - parse_ctx_.check_arg_id(static_cast(--arg_index)); - return internal::get_arg(*this, static_cast(arg_index)); + parse_ctx_.check_arg_id(--arg_index); + return internal::get_arg(*this, arg_index); } template -unsigned basic_printf_context::parse_header( +int basic_printf_context::parse_header( const Char*& it, const Char* end, format_specs& specs) { - unsigned arg_index = internal::max_value(); + int arg_index = internal::ARG_INDEX_SENTINEL; char_type c = *it; if (c >= '0' && c <= '9') { // Parse an argument index (if followed by '$') or a width possibly @@ -417,7 +423,7 @@ unsigned basic_printf_context::parse_header( int value = parse_nonnegative_int(it, end, eh); if (it != end && *it == '$') { // value is an argument index ++it; - arg_index = static_cast(value); + arg_index = value; } else { if (c == '0') specs.fill[0] = '0'; if (value != 0) { @@ -464,7 +470,7 @@ OutputIt basic_printf_context::format() { specs.align = align::right; // Parse argument index, flags and width. - unsigned arg_index = parse_header(it, end, specs); + int arg_index = parse_header(it, end, specs); if (arg_index == 0) on_error("argument index out of range"); // Parse precision.