Skip to content

Commit

Permalink
Clean-up sign-conversion warnings in public headers
Browse files Browse the repository at this point in the history
  • Loading branch information
0x8000-0000 committed Dec 5, 2019
1 parent 1219b65 commit 7c24cc1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
19 changes: 11 additions & 8 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ inline int to_nonnegative_int(T value, int upper) {

template <typename T, FMT_ENABLE_IF(std::is_integral<T>::value)>
inline T mod(T x, int y) {
return x % y;
return x % static_cast<T>(y);
}
template <typename T, FMT_ENABLE_IF(std::is_floating_point<T>::value)>
inline T mod(T x, int y) {
Expand Down Expand Up @@ -793,11 +793,14 @@ struct chrono_formatter {

explicit chrono_formatter(FormatContext& ctx, OutputIt o,
std::chrono::duration<Rep, Period> d)
: context(ctx), out(o), val(d.count()), negative(false) {
if (d.count() < 0) {
val = 0 - val;
negative = true;
}
: context(ctx), out(o), negative(d.count() < 0) {

if (negative) {
val = static_cast<rep>(- d.count());
}
else {
val = static_cast<rep>(d.count());
}

// this may overflow and/or the result may not fit in the
// target type.
Expand Down Expand Up @@ -1023,8 +1026,8 @@ struct formatter<std::chrono::duration<Rep, Period>, 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 <typename Id> void on_dynamic_width(Id arg_id) {
Expand Down
12 changes: 5 additions & 7 deletions include/fmt/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ template <typename Char> struct format_part {

kind part_kind;
union value {
unsigned arg_index;
int arg_index;
basic_string_view<Char> 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<Char> s) : str(s) {}
FMT_CONSTEXPR value(replacement r) : repl(r) {}
} val;
Expand All @@ -40,7 +40,7 @@ template <typename Char> 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<Char> name) {
Expand All @@ -62,7 +62,7 @@ template <typename Char> 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<Char>) { ++num_parts; }

FMT_CONSTEXPR void on_replacement_field(const Char*) {}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -512,8 +512,6 @@ template <typename CompiledFormat, typename... Args,
CompiledFormat>::value)>
std::basic_string<Char> format(const CompiledFormat& cf, const Args&... args) {
basic_memory_buffer<Char> buffer;
using range = buffer_range<Char>;
using context = buffer_context<Char>;
cf.format(std::back_inserter(buffer), args...);
return to_string(buffer);
}
Expand Down
4 changes: 2 additions & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2621,7 +2621,7 @@ class format_string_checker {
public:
explicit FMT_CONSTEXPR format_string_checker(
basic_string_view<Char> format_str, ErrorHandler eh)
: arg_id_(max_value<unsigned>()),
: arg_id_(-1),
context_(format_str, eh),
parse_funcs_{&parse_format_specs<Args, parse_context_type>...} {}

Expand Down Expand Up @@ -2662,7 +2662,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];
};
Expand Down
32 changes: 19 additions & 13 deletions include/fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ internal::truncating_iterator<OutputIt> printf(
basic_format_args<Context> args) {
return Context(it, format, args).format();
}

enum
{
ARG_INDEX_SENTINEL = -1
};

} // namespace internal

using internal::printf; // For printing into memory_buffer.
Expand Down Expand Up @@ -335,10 +341,10 @@ template <typename OutputIt, typename Char> 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<unsigned>());
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:
/**
Expand All @@ -355,7 +361,7 @@ template <typename OutputIt, typename Char> 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<Char>& parse_context() { return parse_ctx_; }

Expand Down Expand Up @@ -397,24 +403,24 @@ void basic_printf_context<OutputIt, Char>::parse_flags(format_specs& specs,

template <typename OutputIt, typename Char>
typename basic_printf_context<OutputIt, Char>::format_arg
basic_printf_context<OutputIt, Char>::get_arg(unsigned arg_index) {
if (arg_index == internal::max_value<unsigned>())
basic_printf_context<OutputIt, Char>::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(--arg_index);
return internal::get_arg(*this, arg_index);
}

template <typename OutputIt, typename Char>
unsigned basic_printf_context<OutputIt, Char>::parse_header(
int basic_printf_context<OutputIt, Char>::parse_header(
const Char*& it, const Char* end, format_specs& specs) {
unsigned arg_index = internal::max_value<unsigned>();
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
// 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;
Expand All @@ -436,8 +442,8 @@ unsigned basic_printf_context<OutputIt, Char>::parse_header(
specs.width = parse_nonnegative_int(it, end, eh);
} else if (*it == '*') {
++it;
specs.width = visit_format_arg(
internal::printf_width_handler<char_type>(specs), get_arg());
specs.width = static_cast<int>(visit_format_arg(
internal::printf_width_handler<char_type>(specs), get_arg()));
}
}
return arg_index;
Expand All @@ -464,7 +470,7 @@ OutputIt basic_printf_context<OutputIt, Char>::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.
Expand All @@ -473,11 +479,11 @@ OutputIt basic_printf_context<OutputIt, Char>::format() {
c = it != end ? *it : 0;
if ('0' <= c && c <= '9') {
internal::error_handler eh;
specs.precision = static_cast<int>(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<int>(visit_format_arg(internal::printf_precision_handler(), get_arg()));
} else {
specs.precision = 0;
}
Expand Down

0 comments on commit 7c24cc1

Please sign in to comment.