From 230b24944c12b0af39b431ea23e334d6bb7531df Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Wed, 17 Jul 2019 21:07:05 +0200 Subject: [PATCH] Fix sign conversion warnings --- include/fmt/core.h | 20 ++++++++--------- include/fmt/format.h | 51 ++++++++++++++++++++++---------------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index de398d29..0ee92fcf 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -447,13 +447,13 @@ class basic_parse_context : private ErrorHandler { } // Returns the next argument index. - FMT_CONSTEXPR unsigned next_arg_id() { - if (next_arg_id_ >= 0) return internal::to_unsigned(next_arg_id_++); + FMT_CONSTEXPR int next_arg_id() { + if (next_arg_id_ >= 0) return next_arg_id_++; on_error("cannot switch from manual to automatic argument indexing"); return 0; } - FMT_CONSTEXPR bool check_arg_id(unsigned) { + FMT_CONSTEXPR bool check_arg_id(int) { if (next_arg_id_ > 0) { on_error("cannot switch from automatic to manual argument indexing"); return false; @@ -1051,7 +1051,7 @@ template class basic_format_context { internal::locale_ref loc = internal::locale_ref()) : out_(out), args_(ctx_args), loc_(loc) {} - format_arg arg(unsigned id) const { return args_.get(id); } + format_arg arg(int id) const { return args_.get(id); } // Checks if manual indexing is used and returns the argument with the // specified name. @@ -1123,7 +1123,7 @@ inline format_arg_store make_format_args( /** Formatting arguments. */ template class basic_format_args { public: - using size_type = unsigned; + using size_type = int; using format_arg = basic_format_arg; private: @@ -1142,8 +1142,8 @@ template class basic_format_args { bool is_packed() const { return (types_ & internal::is_unpacked_bit) == 0; } - internal::type type(unsigned index) const { - unsigned shift = index * 4; + internal::type type(int index) const { + int shift = index * 4; return static_cast((types_ & (0xfull << shift)) >> shift); } @@ -1152,7 +1152,7 @@ template class basic_format_args { void set_data(const internal::value* values) { values_ = values; } void set_data(const format_arg* args) { args_ = args; } - format_arg do_get(size_type index) const { + format_arg do_get(int index) const { format_arg arg; if (!is_packed()) { auto num_args = max_size(); @@ -1192,14 +1192,14 @@ template class basic_format_args { } /** Returns the argument at specified index. */ - format_arg get(size_type index) const { + format_arg get(int index) const { format_arg arg = do_get(index); if (arg.type_ == internal::named_arg_type) arg = arg.value_.named_arg->template deserialize(); return arg; } - size_type max_size() const { + int max_size() const { unsigned long long max_packed = internal::max_packed_args; return static_cast( is_packed() ? max_packed : types_ & ~internal::is_unpacked_bit); diff --git a/include/fmt/format.h b/include/fmt/format.h index 3879c649..464883c1 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1254,13 +1254,13 @@ void arg_map::init(const basic_format_args& args) { if (map_) return; map_ = new entry[args.max_size()]; if (args.is_packed()) { - for (unsigned i = 0;; ++i) { + for (int i = 0;; ++i) { internal::type arg_type = args.type(i); if (arg_type == internal::none_type) return; if (arg_type == internal::named_arg_type) push_back(args.values_[i]); } } - for (unsigned i = 0;; ++i) { + for (int i = 0;; ++i) { auto type = args.args_[i].type_; if (type == internal::none_type) return; if (type == internal::named_arg_type) push_back(args.args_[i].value_); @@ -1312,9 +1312,10 @@ template class basic_writer { char_type fill = specs.fill[0]; std::size_t padding = 0; if (specs.align == align::numeric) { - if (internal::to_unsigned(specs.width) > size) { - padding = specs.width - size; - size = specs.width; + auto unsiged_width = internal::to_unsigned(specs.width); + if (unsiged_width > size) { + padding = unsiged_width - size; + size = unsiged_width; } } else if (specs.precision > num_digits) { size = prefix.size() + internal::to_unsigned(specs.precision); @@ -1563,7 +1564,7 @@ template class basic_writer { UIntPtr value; int num_digits; - size_t size() const { return num_digits + 2; } + size_t size() const { return to_unsigned(num_digits) + 2; } size_t width() const { return size(); } template void operator()(It&& it) const { @@ -1585,7 +1586,8 @@ template class basic_writer { // // where is written by f(it). template void write_padded(const format_specs& specs, F&& f) { - unsigned width = specs.width; // User-perceived width (in code points). + // User-perceived width (in code points). + unsigned width = to_unsigned(specs.width); size_t size = f.size(); // The number of code units. size_t num_code_points = width != 0 ? f.width() : size; if (width <= num_code_points) return f(reserve(size)); @@ -1686,7 +1688,7 @@ template class basic_writer { void write_pointer(UIntPtr value, const format_specs* specs) { int num_digits = internal::count_digits<4>(value); auto pw = pointer_writer{value, num_digits}; - if (!specs) return pw(reserve(num_digits + 2)); + if (!specs) return pw(reserve(to_unsigned(num_digits) + 2)); format_specs specs_copy = *specs; if (specs_copy.align == align::none) specs_copy.align = align::right; write_padded(specs_copy, pw); @@ -1840,9 +1842,8 @@ template FMT_CONSTEXPR bool is_name_start(Char c) { // Parses the range [begin, end) as an unsigned integer. This function assumes // that the range is non-empty and the first character is a digit. template -FMT_CONSTEXPR unsigned parse_nonnegative_int(const Char*& begin, - const Char* end, - ErrorHandler&& eh) { +FMT_CONSTEXPR int parse_nonnegative_int(const Char*& begin, const Char* end, + ErrorHandler&& eh) { assert(begin != end && '0' <= *begin && *begin <= '9'); if (*begin == '0') { ++begin; @@ -1862,7 +1863,7 @@ FMT_CONSTEXPR unsigned parse_nonnegative_int(const Char*& begin, ++begin; } while (begin != end && '0' <= *begin && *begin <= '9'); if (value > max_int) eh.on_error("number is too big"); - return value; + return static_cast(value); } template class custom_formatter { @@ -1952,9 +1953,9 @@ template class specs_setter { specs_.fill[0] = Char('0'); } - FMT_CONSTEXPR void on_width(unsigned width) { specs_.width = width; } - FMT_CONSTEXPR void on_precision(unsigned precision) { - specs_.precision = static_cast(precision); + FMT_CONSTEXPR void on_width(int width) { specs_.width = width; } + FMT_CONSTEXPR void on_precision(int precision) { + specs_.precision = precision; } FMT_CONSTEXPR void end_precision() {} @@ -2110,7 +2111,7 @@ struct string_view_metadata { template FMT_CONSTEXPR string_view_metadata(basic_string_view primary_string, basic_string_view view) - : offset_(view.data() - primary_string.data()), size_(view.size()) {} + : offset_(to_unsigned(view.data() - primary_string.data())), size_(view.size()) {} FMT_CONSTEXPR string_view_metadata(std::size_t offset, std::size_t size) : offset_(offset), size_(size) {} template @@ -2127,12 +2128,12 @@ enum class arg_id_kind { none, index, name }; // An argument reference. template struct arg_ref { FMT_CONSTEXPR arg_ref() : kind(arg_id_kind::none), val() {} - FMT_CONSTEXPR explicit arg_ref(unsigned index) + FMT_CONSTEXPR explicit arg_ref(int index) : kind(arg_id_kind::index), val(index) {} FMT_CONSTEXPR explicit arg_ref(string_view_metadata name) : kind(arg_id_kind::name), val(name) {} - FMT_CONSTEXPR arg_ref& operator=(unsigned idx) { + FMT_CONSTEXPR arg_ref& operator=(int idx) { kind = arg_id_kind::index; val.index = idx; return *this; @@ -2141,10 +2142,10 @@ template struct arg_ref { arg_id_kind kind; union value { FMT_CONSTEXPR value() : index(0u) {} - FMT_CONSTEXPR value(unsigned id) : index(id) {} + FMT_CONSTEXPR value(int id) : index(id) {} FMT_CONSTEXPR value(string_view_metadata n) : name(n) {} - unsigned index; + int index; string_view_metadata name; } val; }; @@ -2190,7 +2191,7 @@ class dynamic_specs_handler private: using arg_ref_type = arg_ref; - FMT_CONSTEXPR arg_ref_type make_arg_ref(unsigned arg_id) { + FMT_CONSTEXPR arg_ref_type make_arg_ref(int arg_id) { context_.check_arg_id(arg_id); return arg_ref_type(arg_id); } @@ -2202,7 +2203,7 @@ class dynamic_specs_handler FMT_CONSTEXPR arg_ref_type make_arg_ref(basic_string_view arg_id) { context_.check_arg_id(arg_id); basic_string_view format_str(context_.begin(), - context_.end() - context_.begin()); + to_unsigned(context_.end() - context_.begin())); const auto id_metadata = string_view_metadata(format_str, arg_id); return arg_ref_type(id_metadata); } @@ -2218,7 +2219,7 @@ FMT_CONSTEXPR const Char* parse_arg_id(const Char* begin, const Char* end, Char c = *begin; if (c == '}' || c == ':') return handler(), begin; if (c >= '0' && c <= '9') { - unsigned index = parse_nonnegative_int(begin, end, handler); + int index = parse_nonnegative_int(begin, end, handler); if (begin == end || (*begin != '}' && *begin != ':')) return handler.on_error("invalid format string"), begin; handler(index); @@ -2239,7 +2240,7 @@ template struct width_adapter { explicit FMT_CONSTEXPR width_adapter(SpecHandler& h) : handler(h) {} FMT_CONSTEXPR void operator()() { handler.on_dynamic_width(auto_id()); } - FMT_CONSTEXPR void operator()(unsigned id) { handler.on_dynamic_width(id); } + FMT_CONSTEXPR void operator()(int id) { handler.on_dynamic_width(id); } FMT_CONSTEXPR void operator()(basic_string_view id) { handler.on_dynamic_width(id); } @@ -2256,7 +2257,7 @@ template struct precision_adapter { explicit FMT_CONSTEXPR precision_adapter(SpecHandler& h) : handler(h) {} FMT_CONSTEXPR void operator()() { handler.on_dynamic_precision(auto_id()); } - FMT_CONSTEXPR void operator()(unsigned id) { + FMT_CONSTEXPR void operator()(int id) { handler.on_dynamic_precision(id); } FMT_CONSTEXPR void operator()(basic_string_view id) {