forked from fmtlib/fmt
IsNegative -> is_negative. Simplify WidthHandler and ArgFormatter.
This commit is contained in:
47
format.cc
47
format.cc
@ -171,27 +171,29 @@ const Char *find_closing_brace(const Char *s, int num_open_braces = 1) {
|
|||||||
|
|
||||||
// Checks if an argument is a valid printf width specifier and sets
|
// Checks if an argument is a valid printf width specifier and sets
|
||||||
// left alignment if it is negative.
|
// left alignment if it is negative.
|
||||||
struct WidthHandler : public fmt::internal::ArgVisitor<WidthHandler, ULongLong> {
|
struct WidthHandler : public fmt::internal::ArgVisitor<WidthHandler, unsigned> {
|
||||||
private:
|
private:
|
||||||
fmt::FormatSpec &spec_;
|
fmt::FormatSpec &spec_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit WidthHandler(fmt::FormatSpec &spec) : spec_(spec) {}
|
explicit WidthHandler(fmt::FormatSpec &spec) : spec_(spec) {}
|
||||||
|
|
||||||
ULongLong visit_unhandled_arg() {
|
unsigned visit_unhandled_arg() {
|
||||||
throw fmt::FormatError("width is not integer");
|
throw fmt::FormatError("width is not integer");
|
||||||
}
|
}
|
||||||
|
|
||||||
ULongLong visit_any_int(fmt::LongLong value) {
|
template <typename T>
|
||||||
ULongLong width = value;
|
unsigned visit_any_int(T value) {
|
||||||
if (value < 0) {
|
typedef typename fmt::internal::IntTraits<T>::MainType UnsignedType;
|
||||||
|
UnsignedType width = value;
|
||||||
|
if (fmt::internal::is_negative(value)) {
|
||||||
spec_.align_ = fmt::ALIGN_LEFT;
|
spec_.align_ = fmt::ALIGN_LEFT;
|
||||||
width = 0 - width;
|
width = 0 - width;
|
||||||
}
|
}
|
||||||
return width;
|
if (width > INT_MAX)
|
||||||
|
throw fmt::FormatError("number is too big in format");
|
||||||
|
return static_cast<unsigned>(width);
|
||||||
}
|
}
|
||||||
|
|
||||||
ULongLong visit_any_uint(ULongLong value) { return value; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// This function template is used to prevent compile errors when handling
|
// This function template is used to prevent compile errors when handling
|
||||||
@ -421,24 +423,11 @@ class fmt::internal::ArgFormatter :
|
|||||||
ArgFormatter(fmt::BasicWriter<Char> &w, fmt::FormatSpec &s, const Char *fmt)
|
ArgFormatter(fmt::BasicWriter<Char> &w, fmt::FormatSpec &s, const Char *fmt)
|
||||||
: writer_(w), spec_(s), format_(fmt) {}
|
: writer_(w), spec_(s), format_(fmt) {}
|
||||||
|
|
||||||
void visit_int(int value) {
|
template <typename T>
|
||||||
writer_.FormatInt(value, spec_);
|
void visit_any_int(T value) { writer_.FormatInt(value, spec_); }
|
||||||
}
|
|
||||||
void visit_uint(unsigned value) {
|
template <typename T>
|
||||||
writer_.FormatInt(value, spec_);
|
void visit_any_double(T value) { writer_.FormatDouble(value, spec_); }
|
||||||
}
|
|
||||||
void visit_long_long(LongLong value) {
|
|
||||||
writer_.FormatInt(value, spec_);
|
|
||||||
}
|
|
||||||
void visit_ulong_long(ULongLong value) {
|
|
||||||
writer_.FormatInt(value, spec_);
|
|
||||||
}
|
|
||||||
void visit_double(double value) {
|
|
||||||
writer_.FormatDouble(value, spec_);
|
|
||||||
}
|
|
||||||
void visit_long_double(long double value) {
|
|
||||||
writer_.FormatDouble(value, spec_);
|
|
||||||
}
|
|
||||||
|
|
||||||
void visit_char(int value) {
|
void visit_char(int value) {
|
||||||
if (spec_.type_ && spec_.type_ != 'c')
|
if (spec_.type_ && spec_.type_ != 'c')
|
||||||
@ -773,11 +762,7 @@ unsigned fmt::internal::PrintfParser<Char>::ParseHeader(
|
|||||||
spec.width_ = ParseNonnegativeInt(s, error);
|
spec.width_ = ParseNonnegativeInt(s, error);
|
||||||
} else if (*s == '*') {
|
} else if (*s == '*') {
|
||||||
++s;
|
++s;
|
||||||
ULongLong width = WidthHandler(spec).visit(HandleArgIndex(UINT_MAX, error));
|
spec.width_ = WidthHandler(spec).visit(HandleArgIndex(UINT_MAX, error));
|
||||||
if (width <= INT_MAX)
|
|
||||||
spec.width_ = static_cast<unsigned>(width);
|
|
||||||
else if (!error)
|
|
||||||
error = "number is too big in format";
|
|
||||||
}
|
}
|
||||||
return arg_index;
|
return arg_index;
|
||||||
}
|
}
|
||||||
|
50
format.h
50
format.h
@ -393,20 +393,20 @@ struct TypeSelector<false> { typedef uint64_t Type; };
|
|||||||
template <bool IsSigned>
|
template <bool IsSigned>
|
||||||
struct SignChecker {
|
struct SignChecker {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static bool IsNegative(T) { return false; }
|
static bool is_negative(T) { return false; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct SignChecker<true> {
|
struct SignChecker<true> {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
static bool IsNegative(T value) { return value < 0; }
|
static bool is_negative(T value) { return value < 0; }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Returns true if value is negative, false otherwise.
|
// Returns true if value is negative, false otherwise.
|
||||||
// Same as (value < 0) but doesn't produce warnings if T is an unsigned type.
|
// Same as (value < 0) but doesn't produce warnings if T is an unsigned type.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline bool IsNegative(T value) {
|
inline bool is_negative(T value) {
|
||||||
return SignChecker<std::numeric_limits<T>::is_signed>::IsNegative(value);
|
return SignChecker<std::numeric_limits<T>::is_signed>::is_negative(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SignBitNoInline(double value);
|
int SignBitNoInline(double value);
|
||||||
@ -734,31 +734,47 @@ class ArgVisitor {
|
|||||||
Result visit_unhandled_arg() { return Result(); }
|
Result visit_unhandled_arg() { return Result(); }
|
||||||
|
|
||||||
Result visit_int(int value) {
|
Result visit_int(int value) {
|
||||||
return FMT_DISPATCH(visit_any_int(value));
|
return FMT_DISPATCH(visit_any_signed(value));
|
||||||
}
|
}
|
||||||
Result visit_long_long(LongLong value) {
|
Result visit_long_long(LongLong value) {
|
||||||
return FMT_DISPATCH(visit_any_int(value));
|
return FMT_DISPATCH(visit_any_signed(value));
|
||||||
}
|
}
|
||||||
Result visit_any_int(LongLong) {
|
|
||||||
return FMT_DISPATCH(visit_unhandled_arg());
|
// Visit any signed integer.
|
||||||
|
template <typename T>
|
||||||
|
Result visit_any_signed(T value) {
|
||||||
|
return FMT_DISPATCH(visit_any_int(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
Result visit_uint(unsigned value) {
|
Result visit_uint(unsigned value) {
|
||||||
return FMT_DISPATCH(visit_any_uint(value));
|
return FMT_DISPATCH(visit_any_unsigned(value));
|
||||||
}
|
}
|
||||||
Result visit_ulong_long(ULongLong value) {
|
Result visit_ulong_long(ULongLong value) {
|
||||||
return FMT_DISPATCH(visit_any_uint(value));
|
return FMT_DISPATCH(visit_any_unsigned(value));
|
||||||
}
|
}
|
||||||
Result visit_any_uint(ULongLong) {
|
|
||||||
|
// Visit any unsigned integer.
|
||||||
|
template <typename T>
|
||||||
|
Result visit_any_unsigned(T value) {
|
||||||
|
return FMT_DISPATCH(visit_any_int(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Result visit_any_int(T) {
|
||||||
return FMT_DISPATCH(visit_unhandled_arg());
|
return FMT_DISPATCH(visit_unhandled_arg());
|
||||||
}
|
}
|
||||||
|
|
||||||
Result visit_double(double) {
|
Result visit_double(double value) {
|
||||||
return FMT_DISPATCH(visit_unhandled_arg());
|
return FMT_DISPATCH(visit_any_double(value));
|
||||||
}
|
}
|
||||||
Result visit_long_double(long double) {
|
Result visit_long_double(long double value) {
|
||||||
|
return FMT_DISPATCH(visit_any_double(value));
|
||||||
|
}
|
||||||
|
template <typename T>
|
||||||
|
Result visit_any_double(T) {
|
||||||
return FMT_DISPATCH(visit_unhandled_arg());
|
return FMT_DISPATCH(visit_unhandled_arg());
|
||||||
}
|
}
|
||||||
|
|
||||||
Result visit_char(int) {
|
Result visit_char(int) {
|
||||||
return FMT_DISPATCH(visit_unhandled_arg());
|
return FMT_DISPATCH(visit_unhandled_arg());
|
||||||
}
|
}
|
||||||
@ -1603,7 +1619,7 @@ void BasicWriter<Char>::FormatInt(T value, const Spec &spec) {
|
|||||||
typedef typename internal::IntTraits<T>::MainType UnsignedType;
|
typedef typename internal::IntTraits<T>::MainType UnsignedType;
|
||||||
UnsignedType abs_value = value;
|
UnsignedType abs_value = value;
|
||||||
char prefix[4] = "";
|
char prefix[4] = "";
|
||||||
if (internal::IsNegative(value)) {
|
if (internal::is_negative(value)) {
|
||||||
prefix[0] = '-';
|
prefix[0] = '-';
|
||||||
++prefix_size;
|
++prefix_size;
|
||||||
abs_value = 0 - abs_value;
|
abs_value = 0 - abs_value;
|
||||||
@ -1892,7 +1908,7 @@ class FormatInt {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
inline void FormatDec(char *&buffer, T value) {
|
inline void FormatDec(char *&buffer, T value) {
|
||||||
typename internal::IntTraits<T>::MainType abs_value = value;
|
typename internal::IntTraits<T>::MainType abs_value = value;
|
||||||
if (internal::IsNegative(value)) {
|
if (internal::is_negative(value)) {
|
||||||
*buffer++ = '-';
|
*buffer++ = '-';
|
||||||
abs_value = 0 - abs_value;
|
abs_value = 0 - abs_value;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user