From 1fab9fc128d5cb36b2d8f3a462495074186b60be Mon Sep 17 00:00:00 2001 From: Florin Iucha Date: Fri, 29 Nov 2019 20:57:14 -0500 Subject: [PATCH] Clean-up sign-conversion warnings in public headers --- include/fmt/chrono.h | 12 ++++-------- include/fmt/compile.h | 14 ++++++-------- include/fmt/format.h | 6 +++--- include/fmt/printf.h | 20 ++++++++++---------- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 9abe7c4f..ee4801b5 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -696,7 +696,7 @@ inline int to_nonnegative_int(T value, int upper) { template ::value)> inline T mod(T x, int y) { - return x % y; + return x % static_cast(y); } template ::value)> inline T mod(T x, int y) { @@ -793,11 +793,7 @@ struct chrono_formatter { explicit chrono_formatter(FormatContext& ctx, OutputIt o, std::chrono::duration d) - : context(ctx), out(o), val(d.count()), negative(false) { - if (d.count() < 0) { - val = 0 - val; - negative = true; - } + : context(ctx), out(o), val(static_cast(std::abs(d.count()))), negative(d.count() < 0) { // this may overflow and/or the result may not fit in the // target type. @@ -1023,8 +1019,8 @@ struct formatter, Char> { void on_error(const char* msg) { FMT_THROW(format_error(msg)); } void on_fill(Char fill) { f.specs.fill[0] = fill; } void on_align(align_t align) { f.specs.align = align; } - void on_width(unsigned width) { f.specs.width = width; } - void on_precision(unsigned _precision) { f.precision = _precision; } + void on_width(int width) { f.specs.width = width; } + void on_precision(int _precision) { f.precision = _precision; } void end_precision() {} template void on_dynamic_width(Id arg_id) { diff --git a/include/fmt/compile.h b/include/fmt/compile.h index f65f5a74..e8f5aa72 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -26,11 +26,11 @@ template struct format_part { kind part_kind; union value { - unsigned arg_index; + int arg_index; basic_string_view str; replacement repl; - FMT_CONSTEXPR value(unsigned index = 0) : arg_index(index) {} + FMT_CONSTEXPR value(int index = 0) : arg_index(index) {} FMT_CONSTEXPR value(basic_string_view s) : str(s) {} FMT_CONSTEXPR value(replacement r) : repl(r) {} } val; @@ -40,7 +40,7 @@ template struct format_part { FMT_CONSTEXPR format_part(kind k = kind::arg_index, value v = {}) : part_kind(k), val(v) {} - static FMT_CONSTEXPR format_part make_arg_index(unsigned index) { + static FMT_CONSTEXPR format_part make_arg_index(int index) { return format_part(kind::arg_index, index); } static FMT_CONSTEXPR format_part make_arg_name(basic_string_view name) { @@ -62,7 +62,7 @@ template struct part_counter { } FMT_CONSTEXPR void on_arg_id() { ++num_parts; } - FMT_CONSTEXPR void on_arg_id(unsigned) { ++num_parts; } + FMT_CONSTEXPR void on_arg_id(int) { ++num_parts; } FMT_CONSTEXPR void on_arg_id(basic_string_view) { ++num_parts; } FMT_CONSTEXPR void on_replacement_field(const Char*) {} @@ -119,7 +119,7 @@ class format_string_compiler : public error_handler { part_ = part::make_arg_index(parse_context_.next_arg_id()); } - FMT_CONSTEXPR void on_arg_id(unsigned id) { + FMT_CONSTEXPR void on_arg_id(int id) { parse_context_.check_arg_id(id); part_ = part::make_arg_index(id); } @@ -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(part_.val.arg_index) + ? arg_ref(static_cast(part_.val.arg_index)) : arg_ref(part_.val.str); auto part = part::make_replacement(repl); part.arg_id_end = begin; @@ -512,8 +512,6 @@ template ::value)> std::basic_string format(const CompiledFormat& cf, const Args&... args) { basic_memory_buffer buffer; - using range = buffer_range; - using context = buffer_context; cf.format(std::back_inserter(buffer), args...); return to_string(buffer); } diff --git a/include/fmt/format.h b/include/fmt/format.h index 9344ac0d..65da1355 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 value; } +inline double promote_float(float value) { return static_cast(value); } template FMT_CONSTEXPR void handle_int_type_spec(char spec, Handler&& handler) { @@ -2627,11 +2627,11 @@ class format_string_checker { FMT_CONSTEXPR void on_text(const Char*, const Char*) {} FMT_CONSTEXPR void on_arg_id() { - arg_id_ = context_.next_arg_id(); + arg_id_ = static_cast(context_.next_arg_id()); check_arg_id(); } FMT_CONSTEXPR void on_arg_id(int id) { - arg_id_ = id; + arg_id_ = static_cast(id); context_.check_arg_id(id); check_arg_id(); } diff --git a/include/fmt/printf.h b/include/fmt/printf.h index cdbb65e6..2eb177c7 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -355,7 +355,7 @@ template class basic_printf_context { OutputIt out() { return out_; } void advance_to(OutputIt it) { out_ = it; } - format_arg arg(unsigned id) const { return args_.get(id); } + format_arg arg(int id) const { return args_.get(id); } basic_format_parse_context& parse_context() { return parse_ctx_; } @@ -399,10 +399,10 @@ template typename basic_printf_context::format_arg basic_printf_context::get_arg(unsigned arg_index) { if (arg_index == internal::max_value()) - arg_index = parse_ctx_.next_arg_id(); + arg_index = static_cast(parse_ctx_.next_arg_id()); else - parse_ctx_.check_arg_id(--arg_index); - return internal::get_arg(*this, arg_index); + parse_ctx_.check_arg_id(static_cast(--arg_index)); + return internal::get_arg(*this, static_cast(arg_index)); } template @@ -414,10 +414,10 @@ unsigned basic_printf_context::parse_header( // Parse an argument index (if followed by '$') or a width possibly // preceded with '0' flag(s). internal::error_handler eh; - unsigned value = parse_nonnegative_int(it, end, eh); + int value = parse_nonnegative_int(it, end, eh); if (it != end && *it == '$') { // value is an argument index ++it; - arg_index = value; + arg_index = static_cast(value); } else { if (c == '0') specs.fill[0] = '0'; if (value != 0) { @@ -436,8 +436,8 @@ unsigned basic_printf_context::parse_header( specs.width = parse_nonnegative_int(it, end, eh); } else if (*it == '*') { ++it; - specs.width = visit_format_arg( - internal::printf_width_handler(specs), get_arg()); + specs.width = static_cast(visit_format_arg( + internal::printf_width_handler(specs), get_arg())); } } return arg_index; @@ -473,11 +473,11 @@ OutputIt basic_printf_context::format() { c = it != end ? *it : 0; if ('0' <= c && c <= '9') { internal::error_handler eh; - specs.precision = static_cast(parse_nonnegative_int(it, end, eh)); + specs.precision = parse_nonnegative_int(it, end, eh); } else if (c == '*') { ++it; specs.precision = - visit_format_arg(internal::printf_precision_handler(), get_arg()); + static_cast(visit_format_arg(internal::printf_precision_handler(), get_arg())); } else { specs.precision = 0; }