diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 9abe7c4f..ca4ed30a 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,7 +793,10 @@ struct chrono_formatter { explicit chrono_formatter(FormatContext& ctx, OutputIt o, std::chrono::duration d) - : context(ctx), out(o), val(d.count()), negative(false) { + : context(ctx), + out(o), + val(static_cast(d.count())), + negative(false) { if (d.count() < 0) { val = 0 - val; negative = true; @@ -1023,8 +1026,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..5829f623 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); } @@ -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 46ada8d6..01f41f5c 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -2567,7 +2567,7 @@ class format_string_checker { public: explicit FMT_CONSTEXPR format_string_checker( basic_string_view format_str, ErrorHandler eh) - : arg_id_(max_value()), + : arg_id_(-1), context_(format_str, eh), parse_funcs_{&parse_format_specs...} {} @@ -2608,7 +2608,7 @@ class format_string_checker { // Format specifier parsing function. using parse_func = const Char* (*)(parse_context_type&); - unsigned arg_id_; + 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 cdbb65e6..a2fa945d 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -333,12 +333,12 @@ template class basic_printf_context { static void parse_flags(format_specs& specs, const Char*& it, const Char* end); - // 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()); + // Returns the argument with specified index or, if arg_index is -1, the next + // argument. + format_arg get_arg(int arg_index = -1); // 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: /** @@ -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_; } @@ -397,8 +397,8 @@ 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()) +basic_printf_context::get_arg(int arg_index) { + if (arg_index < 0) arg_index = parse_ctx_.next_arg_id(); else parse_ctx_.check_arg_id(--arg_index); @@ -406,15 +406,15 @@ basic_printf_context::get_arg(unsigned 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 = -1; char_type c = *it; if (c >= '0' && c <= '9') { // 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; @@ -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; @@ -464,7 +464,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. @@ -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; }