Parameterize formatting argument on char type.

This commit is contained in:
Victor Zverovich
2016-12-11 13:22:45 -08:00
parent 9cf6c8fdc6
commit 0854f8c3bf
6 changed files with 515 additions and 495 deletions

View File

@ -443,8 +443,7 @@ template <typename Char>
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
format_args args); format_args args);
FMT_FUNC int vfprintf(std::FILE *f, CStringRef format, FMT_FUNC int vfprintf(std::FILE *f, CStringRef format, printf_args args) {
basic_format_args<printf_context<char>> args) {
MemoryWriter w; MemoryWriter w;
printf(w, format, args); printf(w, format, args);
std::size_t size = w.size(); std::size_t size = w.size();
@ -475,7 +474,7 @@ template int internal::CharTraits<char>::format_float(
template void internal::FixedBuffer<wchar_t>::grow(std::size_t); template void internal::FixedBuffer<wchar_t>::grow(std::size_t);
template void internal::ArgMap<wchar_t>::init(const format_args &args); template void internal::ArgMap<wchar_t>::init(const wformat_args &args);
template void printf_context<wchar_t>::format(WWriter &writer); template void printf_context<wchar_t>::format(WWriter &writer);

View File

@ -414,6 +414,8 @@ class BasicStringRef {
std::size_t size_; std::size_t size_;
public: public:
BasicStringRef() : data_(0), size_(0) {}
/** Constructs a string reference object from a C string and a size. */ /** Constructs a string reference object from a C string and a size. */
BasicStringRef(const Char *s, std::size_t size) : data_(s), size_(size) {} BasicStringRef(const Char *s, std::size_t size) : data_(s), size_(size) {}
@ -1027,16 +1029,102 @@ struct Value {
CSTRING, STRING, WSTRING, POINTER, CUSTOM CSTRING, STRING, WSTRING, POINTER, CUSTOM
}; };
}; };
template <typename Char>
class ArgMap;
} // namespace internal } // namespace internal
template <typename Context, typename Char>
class basic_format_args;
// A formatting argument. It is a trivially copyable/constructible type to // A formatting argument. It is a trivially copyable/constructible type to
// allow storage in internal::MemoryBuffer. // allow storage in internal::MemoryBuffer.
struct format_arg : internal::Value { template <typename Char>
Type type; class basic_format_arg : public internal::Value {
protected:
Type type_;
explicit operator bool() const noexcept { return type != NONE; } template <typename Visitor, typename CharType>
friend typename std::result_of<Visitor(int)>::type
visit(Visitor &&vis, basic_format_arg<CharType> arg);
template <typename Context, typename CharType>
friend class basic_format_args;
template <typename CharType>
friend class internal::ArgMap;
void check_type() const {
FMT_ASSERT(type_ > NAMED_ARG, "invalid argument type");
}
public:
explicit operator bool() const noexcept { return type_ != NONE; }
bool is_integral() const {
check_type();
return type_ <= LAST_INTEGER_TYPE;
}
bool is_numeric() const {
check_type();
return type_ <= LAST_NUMERIC_TYPE;
}
bool is_pointer() const {
check_type();
return type_ == POINTER;
}
}; };
typedef basic_format_arg<char> format_arg;
typedef basic_format_arg<wchar_t> wformat_arg;
/**
\rst
Visits an argument dispatching to the appropriate visit method based on
the argument type. For example, if the argument type is ``double`` then
``vis(value)`` will be called with the value of type ``double``.
\endrst
*/
template <typename Visitor, typename Char>
typename std::result_of<Visitor(int)>::type
visit(Visitor &&vis, basic_format_arg<Char> arg) {
switch (arg.type_) {
case format_arg::NONE:
case format_arg::NAMED_ARG:
FMT_ASSERT(false, "invalid argument type");
break;
case format_arg::INT:
return vis(arg.int_value);
case format_arg::UINT:
return vis(arg.uint_value);
case format_arg::LONG_LONG:
return vis(arg.long_long_value);
case format_arg::ULONG_LONG:
return vis(arg.ulong_long_value);
case format_arg::BOOL:
return vis(arg.int_value != 0);
case format_arg::CHAR:
return vis(static_cast<Char>(arg.int_value));
case format_arg::DOUBLE:
return vis(arg.double_value);
case format_arg::LONG_DOUBLE:
return vis(arg.long_double_value);
case format_arg::CSTRING:
return vis(arg.string.value);
case format_arg::STRING:
return vis(arg.string);
case format_arg::WSTRING:
return vis(arg.wstring);
case format_arg::POINTER:
return vis(arg.pointer);
case format_arg::CUSTOM:
return vis(arg.custom);
}
return typename std::result_of<Visitor(int)>::type();
}
namespace internal { namespace internal {
template <typename Char> template <typename Char>
@ -1251,7 +1339,7 @@ constexpr Type type() { return gettype<typename std::decay<T>::type>(); }
// Makes a format_arg object from any type. // Makes a format_arg object from any type.
template <typename Context> template <typename Context>
class MakeValue : public format_arg { class MakeValue : public basic_format_arg<typename Context::char_type> {
public: public:
typedef typename Context::char_type Char; typedef typename Context::char_type Char;
@ -1279,13 +1367,13 @@ class MakeValue : public format_arg {
MakeValue(typename WCharHelper<WStringRef, Char>::Unsupported); MakeValue(typename WCharHelper<WStringRef, Char>::Unsupported);
void set_string(StringRef str) { void set_string(StringRef str) {
string.value = str.data(); this->string.value = str.data();
string.size = str.size(); this->string.size = str.size();
} }
void set_string(WStringRef str) { void set_string(WStringRef str) {
wstring.value = str.data(); this->wstring.value = str.data();
wstring.size = str.size(); this->wstring.size = str.size();
} }
// Formats an argument of a custom type, such as a user-defined class. // Formats an argument of a custom type, such as a user-defined class.
@ -1302,8 +1390,8 @@ class MakeValue : public format_arg {
#define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \ #define FMT_MAKE_VALUE_(Type, field, TYPE, rhs) \
MakeValue(Type value) { \ MakeValue(Type value) { \
static_assert(internal::type<Type>() == TYPE, "invalid type"); \ static_assert(internal::type<Type>() == MakeValue::TYPE, "invalid type"); \
field = rhs; \ this->field = rhs; \
} }
#define FMT_MAKE_VALUE(Type, field, TYPE) \ #define FMT_MAKE_VALUE(Type, field, TYPE) \
@ -1319,16 +1407,16 @@ class MakeValue : public format_arg {
// To minimize the number of types we need to deal with, long is // To minimize the number of types we need to deal with, long is
// translated either to int or to long long depending on its size. // translated either to int or to long long depending on its size.
if (const_check(sizeof(long) == sizeof(int))) if (const_check(sizeof(long) == sizeof(int)))
int_value = static_cast<int>(value); this->int_value = static_cast<int>(value);
else else
long_long_value = value; this->long_long_value = value;
} }
MakeValue(unsigned long value) { MakeValue(unsigned long value) {
if (const_check(sizeof(unsigned long) == sizeof(unsigned))) if (const_check(sizeof(unsigned long) == sizeof(unsigned)))
uint_value = static_cast<unsigned>(value); this->uint_value = static_cast<unsigned>(value);
else else
ulong_long_value = value; this->ulong_long_value = value;
} }
FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG) FMT_MAKE_VALUE(LongLong, long_long_value, LONG_LONG)
@ -1343,14 +1431,14 @@ class MakeValue : public format_arg {
#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
typedef typename WCharHelper<wchar_t, Char>::Supported WChar; typedef typename WCharHelper<wchar_t, Char>::Supported WChar;
MakeValue(WChar value) { MakeValue(WChar value) {
static_assert(internal::type<WChar>() == CHAR, "invalid type"); static_assert(internal::type<WChar>() == MakeValue::CHAR, "invalid type");
int_value = value; this->int_value = value;
} }
#endif #endif
#define FMT_MAKE_STR_VALUE(Type, TYPE) \ #define FMT_MAKE_STR_VALUE(Type, TYPE) \
MakeValue(Type value) { \ MakeValue(Type value) { \
static_assert(internal::type<Type>() == TYPE, "invalid type"); \ static_assert(internal::type<Type>() == MakeValue::TYPE, "invalid type"); \
set_string(value); \ set_string(value); \
} }
@ -1366,7 +1454,7 @@ class MakeValue : public format_arg {
#define FMT_MAKE_WSTR_VALUE(Type, TYPE) \ #define FMT_MAKE_WSTR_VALUE(Type, TYPE) \
MakeValue(typename WCharHelper<Type, Char>::Supported value) { \ MakeValue(typename WCharHelper<Type, Char>::Supported value) { \
static_assert(internal::type<Type>() == TYPE, "invalid type"); \ static_assert(internal::type<Type>() == MakeValue::TYPE, "invalid type"); \
set_string(value); \ set_string(value); \
} }
@ -1382,49 +1470,51 @@ class MakeValue : public format_arg {
MakeValue(const T &value, MakeValue(const T &value,
typename EnableIf<Not< typename EnableIf<Not<
ConvertToInt<T>::value>::value, int>::type = 0) { ConvertToInt<T>::value>::value, int>::type = 0) {
static_assert(internal::type<T>() == CUSTOM, "invalid type"); static_assert(internal::type<T>() == MakeValue::CUSTOM, "invalid type");
custom.value = &value; this->custom.value = &value;
custom.format = &format_custom_arg<T>; this->custom.format = &format_custom_arg<T>;
} }
template <typename T> template <typename T>
MakeValue(const T &value, MakeValue(const T &value,
typename EnableIf<ConvertToInt<T>::value, int>::type = 0) { typename EnableIf<ConvertToInt<T>::value, int>::type = 0) {
static_assert(internal::type<T>() == INT, "invalid type"); static_assert(internal::type<T>() == MakeValue::INT, "invalid type");
int_value = value; this->int_value = value;
} }
// Additional template param `Char_` is needed here because make_type always // Additional template param `Char_` is needed here because make_type always
// uses char. // uses char.
template <typename Char_> template <typename Char_>
MakeValue(const NamedArg<Char_> &value) { MakeValue(const NamedArg<Char_> &value) {
static_assert(internal::type<const NamedArg<Char_> &>() == NAMED_ARG, static_assert(
"invalid type"); internal::type<const NamedArg<Char_> &>() == MakeValue::NAMED_ARG,
pointer = &value; "invalid type");
this->pointer = &value;
} }
}; };
template <typename Context> template <typename Context>
class MakeArg : public format_arg { class MakeArg : public basic_format_arg<typename Context::char_type> {
public: public:
MakeArg() { MakeArg() {
type = format_arg::NONE; this->type_ = format_arg::NONE;
} }
template <typename T> template <typename T>
MakeArg(const T &value) MakeArg(const T &value)
: format_arg(MakeValue<Context>(value)) { : basic_format_arg<typename Context::char_type>(MakeValue<Context>(value)) {
type = internal::type<T>(); this->type_ = internal::type<T>();
} }
}; };
template <typename Char> template <typename Char>
struct NamedArg : format_arg { struct NamedArg : basic_format_arg<Char> {
BasicStringRef<Char> name; BasicStringRef<Char> name;
template <typename T> template <typename T>
NamedArg(BasicStringRef<Char> argname, const T &value) NamedArg(BasicStringRef<Char> argname, const T &value)
: format_arg(MakeArg< basic_format_context<Char> >(value)), name(argname) {} : basic_format_arg<Char>(MakeArg< basic_format_context<Char> >(value)),
name(argname) {}
}; };
class RuntimeError : public std::runtime_error { class RuntimeError : public std::runtime_error {
@ -1433,9 +1523,6 @@ class RuntimeError : public std::runtime_error {
~RuntimeError() throw(); ~RuntimeError() throw();
}; };
template <typename Char>
class ArgMap;
template <typename Arg, typename... Args> template <typename Arg, typename... Args>
constexpr uint64_t make_type() { constexpr uint64_t make_type() {
return type<Arg>() | (make_type<Args...>() << 4); return type<Arg>() | (make_type<Args...>() << 4);
@ -1482,8 +1569,12 @@ inline format_arg_store<format_context, Args...>
} }
/** Formatting arguments. */ /** Formatting arguments. */
template <typename Context> template <typename Context, typename Char>
class basic_format_args { class basic_format_args {
public:
typedef unsigned size_type;
typedef basic_format_arg<Char> format_arg;
private: private:
// To reduce compiled code size per formatting function call, types of first // To reduce compiled code size per formatting function call, types of first
// MAX_PACKED_ARGS arguments are passed in the types_ field. // MAX_PACKED_ARGS arguments are passed in the types_ field.
@ -1498,21 +1589,43 @@ class basic_format_args {
const format_arg *args_; const format_arg *args_;
}; };
format_arg::Type type(unsigned index) const { typename format_arg::Type type(unsigned index) const {
unsigned shift = index * 4; unsigned shift = index * 4;
uint64_t mask = 0xf; uint64_t mask = 0xf;
return static_cast<format_arg::Type>((types_ & (mask << shift)) >> shift); return static_cast<typename format_arg::Type>(
(types_ & (mask << shift)) >> shift);
} }
template <typename Char> friend class internal::ArgMap<Char>;
friend class internal::ArgMap;
void set_data(const internal::Value *values) { values_ = values; } void set_data(const internal::Value *values) { values_ = values; }
void set_data(const format_arg *args) { args_ = args; } void set_data(const format_arg *args) { args_ = args; }
public: format_arg get(size_type index) const {
typedef unsigned size_type; format_arg arg;
bool use_values = type(internal::MAX_PACKED_ARGS - 1) == format_arg::NONE;
if (index < internal::MAX_PACKED_ARGS) {
typename format_arg::Type arg_type = type(index);
internal::Value &val = arg;
if (arg_type != format_arg::NONE)
val = use_values ? values_[index] : args_[index];
arg.type_ = arg_type;
return arg;
}
if (use_values) {
// The index is greater than the number of arguments that can be stored
// in values, so return a "none" argument.
arg.type_ = format_arg::NONE;
return arg;
}
for (unsigned i = internal::MAX_PACKED_ARGS; i <= index; ++i) {
if (args_[i].type_ == format_arg::NONE)
return args_[i];
}
return args_[index];
}
public:
basic_format_args() : types_(0) {} basic_format_args() : types_(0) {}
template <typename... Args> template <typename... Args>
@ -1523,77 +1636,14 @@ class basic_format_args {
/** Returns the argument at specified index. */ /** Returns the argument at specified index. */
format_arg operator[](size_type index) const { format_arg operator[](size_type index) const {
format_arg arg; format_arg arg = get(index);
bool use_values = type(internal::MAX_PACKED_ARGS - 1) == format_arg::NONE; return arg.type_ == format_arg::NAMED_ARG ?
if (index < internal::MAX_PACKED_ARGS) { *static_cast<const format_arg*>(arg.pointer) : arg;
format_arg::Type arg_type = type(index);
internal::Value &val = arg;
if (arg_type != format_arg::NONE)
val = use_values ? values_[index] : args_[index];
arg.type = arg_type;
return arg;
}
if (use_values) {
// The index is greater than the number of arguments that can be stored
// in values, so return a "none" argument.
arg.type = format_arg::NONE;
return arg;
}
for (unsigned i = internal::MAX_PACKED_ARGS; i <= index; ++i) {
if (args_[i].type == format_arg::NONE)
return args_[i];
}
return args_[index];
} }
}; };
typedef basic_format_args<basic_format_context<char>> format_args; typedef basic_format_args<basic_format_context<char>, char> format_args;
typedef basic_format_args<basic_format_context<wchar_t>> wformat_args; typedef basic_format_args<basic_format_context<wchar_t>, wchar_t> wformat_args;
/**
\rst
Visits an argument dispatching to the appropriate visit method based on
the argument type. For example, if the argument type is ``double`` then
``vis(value)`` will be called with the value of type ``double``.
\endrst
*/
template <typename Visitor>
typename std::result_of<Visitor(int)>::type visit(Visitor &&vis,
format_arg arg) {
switch (arg.type) {
case format_arg::NONE:
case format_arg::NAMED_ARG:
FMT_ASSERT(false, "invalid argument type");
break;
case format_arg::INT:
return vis(arg.int_value);
case format_arg::UINT:
return vis(arg.uint_value);
case format_arg::LONG_LONG:
return vis(arg.long_long_value);
case format_arg::ULONG_LONG:
return vis(arg.ulong_long_value);
case format_arg::BOOL:
return vis(arg.int_value != 0);
case format_arg::CHAR:
return vis(static_cast<wchar_t>(arg.int_value));
case format_arg::DOUBLE:
return vis(arg.double_value);
case format_arg::LONG_DOUBLE:
return vis(arg.long_double_value);
case format_arg::CSTRING:
return vis(arg.string.value);
case format_arg::STRING:
return vis(arg.string);
case format_arg::WSTRING:
return vis(arg.wstring);
case format_arg::POINTER:
return vis(arg.pointer);
case format_arg::CUSTOM:
return vis(arg.custom);
}
return typename std::result_of<Visitor(int)>::type();
}
enum Alignment { enum Alignment {
ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER, ALIGN_NUMERIC
@ -1822,16 +1872,17 @@ template <typename Char>
class ArgMap { class ArgMap {
private: private:
typedef std::vector< typedef std::vector<
std::pair<fmt::BasicStringRef<Char>, format_arg> > MapType; std::pair<fmt::BasicStringRef<Char>, basic_format_arg<Char> > > MapType;
typedef typename MapType::value_type Pair; typedef typename MapType::value_type Pair;
MapType map_; MapType map_;
public: public:
template <typename Formatter> template <typename Context>
void init(const basic_format_args<Formatter> &args); void init(const basic_format_args<Context, Char> &args);
const format_arg *find(const fmt::BasicStringRef<Char> &name) const { const basic_format_arg<Char>
*find(const fmt::BasicStringRef<Char> &name) const {
// The list is unsorted, so just return the first matching name. // The list is unsorted, so just return the first matching name.
for (typename MapType::const_iterator it = map_.begin(), end = map_.end(); for (typename MapType::const_iterator it = map_.begin(), end = map_.end();
it != end; ++it) { it != end; ++it) {
@ -1843,8 +1894,8 @@ class ArgMap {
}; };
template <typename Char> template <typename Char>
template <typename Formatter> template <typename Context>
void ArgMap<Char>::init(const basic_format_args<Formatter> &args) { void ArgMap<Char>::init(const basic_format_args<Context, Char> &args) {
if (!map_.empty()) if (!map_.empty())
return; return;
typedef internal::NamedArg<Char> NamedArg; typedef internal::NamedArg<Char> NamedArg;
@ -1874,8 +1925,8 @@ void ArgMap<Char>::init(const basic_format_args<Formatter> &args) {
map_.push_back(Pair(named_arg->name, *named_arg)); map_.push_back(Pair(named_arg->name, *named_arg));
} }
} }
for (unsigned i = MAX_PACKED_ARGS;/*nothing*/; ++i) { for (unsigned i = MAX_PACKED_ARGS; ; ++i) {
switch (args.args_[i].type) { switch (args.args_[i].type_) {
case format_arg::NONE: case format_arg::NONE:
return; return;
case format_arg::NAMED_ARG: case format_arg::NAMED_ARG:
@ -1955,7 +2006,7 @@ class ArgFormatterBase {
write(value); write(value);
} }
void operator()(wchar_t value) { void operator()(Char value) {
if (spec_.type_ && spec_.type_ != 'c') { if (spec_.type_ && spec_.type_ != 'c') {
spec_.flags_ |= CHAR_FLAG; spec_.flags_ |= CHAR_FLAG;
writer_.write_int(value, spec_); writer_.write_int(value, spec_);
@ -2016,29 +2067,24 @@ template <typename Char, typename Context>
class format_context_base { class format_context_base {
private: private:
const Char *ptr_; const Char *ptr_;
basic_format_args<Context> args_; basic_format_args<Context, Char> args_;
int next_arg_index_; int next_arg_index_;
protected: protected:
format_context_base(const Char *format_str, basic_format_args<Context> args) typedef basic_format_arg<Char> format_arg;
format_context_base(const Char *format_str,
basic_format_args<Context, Char> args)
: ptr_(format_str), args_(args), next_arg_index_(0) {} : ptr_(format_str), args_(args), next_arg_index_(0) {}
~format_context_base() {} ~format_context_base() {}
basic_format_args<Context> args() const { return args_; } basic_format_args<Context, Char> args() const { return args_; }
// Returns the argument with specified index. // Returns the argument with specified index.
format_arg do_get_arg(unsigned arg_index, const char *&error) { format_arg do_get_arg(unsigned arg_index, const char *&error) {
format_arg arg = args_[arg_index]; format_arg arg = args_[arg_index];
switch (arg.type) { if (!arg)
case format_arg::NONE: error = "argument index out of range";
error = "argument index out of range";
break;
case format_arg::NAMED_ARG:
arg = *static_cast<const format_arg*>(arg.pointer);
break;
default:
/*nothing*/;
}
return arg; return arg;
} }
@ -2107,13 +2153,14 @@ class basic_format_context :
FMT_DISALLOW_COPY_AND_ASSIGN(basic_format_context); FMT_DISALLOW_COPY_AND_ASSIGN(basic_format_context);
typedef internal::format_context_base<Char, basic_format_context> Base; typedef internal::format_context_base<Char, basic_format_context<Char>> Base;
using typename Base::format_arg;
using Base::get_arg; using Base::get_arg;
// Checks if manual indexing is used and returns the argument with // Checks if manual indexing is used and returns the argument with
// specified name. // specified name.
format_arg get_arg(BasicStringRef<Char> name, const char *&error); basic_format_arg<Char> get_arg(BasicStringRef<Char> name, const char *&error);
public: public:
/** The character type for the output. */ /** The character type for the output. */
@ -2126,11 +2173,11 @@ class basic_format_context :
\endrst \endrst
*/ */
basic_format_context(const Char *format_str, basic_format_context(const Char *format_str,
basic_format_args<basic_format_context> args) basic_format_args<basic_format_context, Char> args)
: Base(format_str, args) {} : Base(format_str, args) {}
// Parses argument id and returns corresponding argument. // Parses argument id and returns corresponding argument.
format_arg parse_arg_id(); basic_format_arg<Char> parse_arg_id();
using Base::ptr; using Base::ptr;
}; };
@ -2364,7 +2411,7 @@ class BasicWriter {
} }
void vwrite(BasicCStringRef<Char> format, void vwrite(BasicCStringRef<Char> format,
basic_format_args<basic_format_context<Char>> args); basic_format_args<basic_format_context<Char>, Char> args);
/** /**
\rst \rst
Writes formatted data. Writes formatted data.
@ -3281,45 +3328,121 @@ unsigned parse_nonnegative_int(const Char *&s) {
return value; return value;
} }
inline void require_numeric_argument(const format_arg &arg, char spec) { template <typename Char>
if (arg.type > format_arg::LAST_NUMERIC_TYPE) { inline void require_numeric_argument(
std::string message = const basic_format_arg<Char> &arg, char spec) {
fmt::format("format specifier '{}' requires numeric argument", spec); if (!arg.is_numeric()) {
FMT_THROW(fmt::format_error(message)); FMT_THROW(fmt::format_error(
fmt::format("format specifier '{}' requires numeric argument", spec)));
} }
} }
struct IsUnsigned {
template <typename T>
typename std::enable_if<std::is_unsigned<T>::value, bool>::type
operator()(T value) {
return true;
}
template <typename T>
typename std::enable_if<!std::is_unsigned<T>::value, bool>::type
operator()(T value) {
return false;
}
};
template <typename Char> template <typename Char>
void check_sign(const Char *&s, const format_arg &arg) { void check_sign(const Char *&s, const basic_format_arg<Char> &arg) {
char sign = static_cast<char>(*s); char sign = static_cast<char>(*s);
require_numeric_argument(arg, sign); require_numeric_argument(arg, sign);
if (arg.type == format_arg::UINT || arg.type == format_arg::ULONG_LONG) { if (visit(IsUnsigned(), arg)) {
FMT_THROW(format_error(fmt::format( FMT_THROW(format_error(fmt::format(
"format specifier '{}' requires signed argument", sign))); "format specifier '{}' requires signed argument", sign)));
} }
++s; ++s;
} }
template <typename Char, typename Context>
class CustomFormatter {
private:
BasicWriter<Char> &writer_;
Context &ctx_;
public:
CustomFormatter(BasicWriter<Char> &writer, Context &ctx)
: writer_(writer), ctx_(ctx) {}
bool operator()(format_arg::CustomValue custom) {
custom.format(&writer_, custom.value, &ctx_);
return true;
}
template <typename T>
bool operator()(T) { return false; }
};
template <typename T>
struct IsInteger {
enum {
value = std::is_integral<T>::value && !std::is_same<T, bool>::value &&
!std::is_same<T, char>::value && !std::is_same<T, wchar_t>::value
};
};
struct WidthHandler {
template <typename T>
typename std::enable_if<IsInteger<T>::value, ULongLong>::type
operator()(T value) {
if (is_negative(value))
FMT_THROW(format_error("negative width"));
return value;
}
template <typename T>
typename std::enable_if<!IsInteger<T>::value, ULongLong>::type
operator()(T value) {
FMT_THROW(format_error("width is not integer"));
return 0;
}
};
struct PrecisionHandler {
template <typename T>
typename std::enable_if<IsInteger<T>::value, ULongLong>::type
operator()(T value) {
if (is_negative(value))
FMT_THROW(format_error("negative precision"));
return value;
}
template <typename T>
typename std::enable_if<!IsInteger<T>::value, ULongLong>::type
operator()(T value) {
FMT_THROW(format_error("precision is not integer"));
return 0;
}
};
} // namespace internal } // namespace internal
template <typename Char> template <typename Char>
inline format_arg basic_format_context<Char>::get_arg( inline basic_format_arg<Char> basic_format_context<Char>::get_arg(
BasicStringRef<Char> name, const char *&error) { BasicStringRef<Char> name, const char *&error) {
if (this->check_no_auto_index(error)) { if (this->check_no_auto_index(error)) {
map_.init(this->args()); map_.init(this->args());
const format_arg *arg = map_.find(name); const basic_format_arg<Char> *arg = map_.find(name);
if (arg) if (arg)
return *arg; return *arg;
error = "argument not found"; error = "argument not found";
} }
return format_arg(); return basic_format_arg<Char>();
} }
template <typename Char> template <typename Char>
inline format_arg basic_format_context<Char>::parse_arg_id() { inline basic_format_arg<Char> basic_format_context<Char>::parse_arg_id() {
const Char *&s = this->ptr(); const Char *&s = this->ptr();
if (!internal::is_name_start(*s)) { if (!internal::is_name_start(*s)) {
const char *error = 0; const char *error = 0;
format_arg arg = *s < '0' || *s > '9' ? basic_format_arg<Char> arg = *s < '0' || *s > '9' ?
this->next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error); this->next_arg(error) : get_arg(internal::parse_nonnegative_int(s), error);
if (error) { if (error) {
FMT_THROW(format_error( FMT_THROW(format_error(
@ -3333,7 +3456,8 @@ inline format_arg basic_format_context<Char>::parse_arg_id() {
c = *++s; c = *++s;
} while (internal::is_name_start(c) || ('0' <= c && c <= '9')); } while (internal::is_name_start(c) || ('0' <= c && c <= '9'));
const char *error = 0; const char *error = 0;
format_arg arg = get_arg(BasicStringRef<Char>(start, s - start), error); basic_format_arg<Char> arg =
get_arg(BasicStringRef<Char>(start, s - start), error);
if (error) if (error)
FMT_THROW(format_error(error)); FMT_THROW(format_error(error));
return arg; return arg;
@ -3341,15 +3465,13 @@ inline format_arg basic_format_context<Char>::parse_arg_id() {
// Formats a single argument. // Formats a single argument.
template <typename ArgFormatter, typename Char, typename Context> template <typename ArgFormatter, typename Char, typename Context>
void do_format_arg(BasicWriter<Char> &writer, const format_arg &arg, void do_format_arg(BasicWriter<Char> &writer, const basic_format_arg<Char> &arg,
Context &ctx) { Context &ctx) {
const Char *&s = ctx.ptr(); const Char *&s = ctx.ptr();
FormatSpec spec; FormatSpec spec;
if (*s == ':') { if (*s == ':') {
if (arg.type == format_arg::CUSTOM) { if (visit(internal::CustomFormatter<Char, Context>(writer, ctx), arg))
arg.custom.format(&writer, arg.custom.value, &ctx);
return; return;
}
++s; ++s;
// Parse fill and alignment. // Parse fill and alignment.
if (Char c = *s) { if (Char c = *s) {
@ -3420,33 +3542,13 @@ void do_format_arg(BasicWriter<Char> &writer, const format_arg &arg,
spec.width_ = internal::parse_nonnegative_int(s); spec.width_ = internal::parse_nonnegative_int(s);
} else if (*s == '{') { } else if (*s == '{') {
++s; ++s;
format_arg width_arg = ctx.parse_arg_id(); auto width_arg = ctx.parse_arg_id();
if (*s++ != '}') if (*s++ != '}')
FMT_THROW(format_error("invalid format string")); FMT_THROW(format_error("invalid format string"));
ULongLong value = 0; ULongLong width = visit(internal::WidthHandler(), width_arg);
switch (width_arg.type) { if (width > (std::numeric_limits<int>::max)())
case format_arg::INT:
if (width_arg.int_value < 0)
FMT_THROW(format_error("negative width"));
value = width_arg.int_value;
break;
case format_arg::UINT:
value = width_arg.uint_value;
break;
case format_arg::LONG_LONG:
if (width_arg.long_long_value < 0)
FMT_THROW(format_error("negative width"));
value = width_arg.long_long_value;
break;
case format_arg::ULONG_LONG:
value = width_arg.ulong_long_value;
break;
default:
FMT_THROW(format_error("width is not integer"));
}
if (value > (std::numeric_limits<int>::max)())
FMT_THROW(format_error("number is too big")); FMT_THROW(format_error("number is too big"));
spec.width_ = static_cast<int>(value); spec.width_ = static_cast<int>(width);
} }
// Parse precision. // Parse precision.
@ -3457,41 +3559,21 @@ void do_format_arg(BasicWriter<Char> &writer, const format_arg &arg,
spec.precision_ = internal::parse_nonnegative_int(s); spec.precision_ = internal::parse_nonnegative_int(s);
} else if (*s == '{') { } else if (*s == '{') {
++s; ++s;
format_arg precision_arg = ctx.parse_arg_id(); auto precision_arg = ctx.parse_arg_id();
if (*s++ != '}') if (*s++ != '}')
FMT_THROW(format_error("invalid format string")); FMT_THROW(format_error("invalid format string"));
ULongLong value = 0; ULongLong precision =
switch (precision_arg.type) { visit(internal::PrecisionHandler(), precision_arg);
case format_arg::INT: if (precision > (std::numeric_limits<int>::max)())
if (precision_arg.int_value < 0)
FMT_THROW(format_error("negative precision"));
value = precision_arg.int_value;
break;
case format_arg::UINT:
value = precision_arg.uint_value;
break;
case format_arg::LONG_LONG:
if (precision_arg.long_long_value < 0)
FMT_THROW(format_error("negative precision"));
value = precision_arg.long_long_value;
break;
case format_arg::ULONG_LONG:
value = precision_arg.ulong_long_value;
break;
default:
FMT_THROW(format_error("precision is not integer"));
}
if (value > (std::numeric_limits<int>::max)())
FMT_THROW(format_error("number is too big")); FMT_THROW(format_error("number is too big"));
spec.precision_ = static_cast<int>(value); spec.precision_ = static_cast<int>(precision);
} else { } else {
FMT_THROW(format_error("missing precision specifier")); FMT_THROW(format_error("missing precision specifier"));
} }
if (arg.type <= format_arg::LAST_INTEGER_TYPE || if (arg.is_integral() || arg.is_pointer()) {
arg.type == format_arg::POINTER) {
FMT_THROW(format_error( FMT_THROW(format_error(
fmt::format("precision not allowed in {} format specifier", fmt::format("precision not allowed in {} format specifier",
arg.type == format_arg::POINTER ? "pointer" : "integer"))); arg.is_pointer() ? "pointer" : "integer")));
} }
} }
@ -3510,7 +3592,7 @@ void do_format_arg(BasicWriter<Char> &writer, const format_arg &arg,
/** Formats arguments and writes the output to the writer. */ /** Formats arguments and writes the output to the writer. */
template <typename ArgFormatter, typename Char, typename Context> template <typename ArgFormatter, typename Char, typename Context>
void vformat(BasicWriter<Char> &writer, BasicCStringRef<Char> format_str, void vformat(BasicWriter<Char> &writer, BasicCStringRef<Char> format_str,
basic_format_args<Context> args) { basic_format_args<Context, Char> args) {
basic_format_context<Char> ctx(format_str.c_str(), args); basic_format_context<Char> ctx(format_str.c_str(), args);
const Char *&s = ctx.ptr(); const Char *&s = ctx.ptr();
const Char *start = s; const Char *start = s;
@ -3536,7 +3618,7 @@ void vformat(BasicWriter<Char> &writer, BasicCStringRef<Char> format_str,
template <typename Char> template <typename Char>
inline void BasicWriter<Char>::vwrite( inline void BasicWriter<Char>::vwrite(
BasicCStringRef<Char> format, BasicCStringRef<Char> format,
basic_format_args<basic_format_context<Char>> args) { basic_format_args<basic_format_context<Char>, Char> args) {
vformat<ArgFormatter<Char>>(*this, format, args); vformat<ArgFormatter<Char>>(*this, format, args);
} }
} // namespace fmt } // namespace fmt

View File

@ -40,7 +40,7 @@ struct IntChecker<true> {
static bool fits_in_int(int) { return true; } static bool fits_in_int(int) { return true; }
}; };
class PrecisionHandler { class PrintfPrecisionHandler {
public: public:
template <typename T> template <typename T>
typename std::enable_if<std::is_integral<T>::value, int>::type typename std::enable_if<std::is_integral<T>::value, int>::type
@ -80,14 +80,14 @@ struct is_same<T, T> {
enum { value = 1 }; enum { value = 1 };
}; };
template <typename T> template <typename T, typename Char>
class ArgConverter { class ArgConverter {
private: private:
format_arg &arg_; basic_format_arg<Char> &arg_;
wchar_t type_; Char type_;
public: public:
ArgConverter(format_arg &arg, wchar_t type) ArgConverter(basic_format_arg<Char> &arg, Char type)
: arg_(arg), type_(type) {} : arg_(arg), type_(type) {}
void operator()(bool value) { void operator()(bool value) {
@ -101,27 +101,27 @@ class ArgConverter {
bool is_signed = type_ == 'd' || type_ == 'i'; bool is_signed = type_ == 'd' || type_ == 'i';
typedef typename internal::Conditional< typedef typename internal::Conditional<
is_same<T, void>::value, U, T>::type TargetType; is_same<T, void>::value, U, T>::type TargetType;
typedef basic_format_context<Char> format_context;
if (sizeof(TargetType) <= sizeof(int)) { if (sizeof(TargetType) <= sizeof(int)) {
// Extra casts are used to silence warnings. // Extra casts are used to silence warnings.
if (is_signed) { if (is_signed) {
arg_.type = format_arg::INT; arg_ = internal::MakeArg<format_context>(
arg_.int_value = static_cast<int>(static_cast<TargetType>(value)); static_cast<int>(static_cast<TargetType>(value)));
} else { } else {
arg_.type = format_arg::UINT;
typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned; typedef typename internal::MakeUnsigned<TargetType>::Type Unsigned;
arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value)); arg_ = internal::MakeArg<format_context>(
static_cast<unsigned>(static_cast<Unsigned>(value)));
} }
} else { } else {
if (is_signed) { if (is_signed) {
arg_.type = format_arg::LONG_LONG;
// glibc's printf doesn't sign extend arguments of smaller types: // glibc's printf doesn't sign extend arguments of smaller types:
// std::printf("%lld", -42); // prints "4294967254" // std::printf("%lld", -42); // prints "4294967254"
// but we don't have to do the same because it's a UB. // but we don't have to do the same because it's a UB.
arg_.long_long_value = static_cast<LongLong>(value); arg_ = internal::MakeArg<format_context>(
static_cast<LongLong>(value));
} else { } else {
arg_.type = format_arg::ULONG_LONG; arg_ = internal::MakeArg<format_context>(
arg_.ulong_long_value = static_cast<typename internal::MakeUnsigned<U>::Type>(value));
static_cast<typename internal::MakeUnsigned<U>::Type>(value);
} }
} }
} }
@ -137,26 +137,27 @@ class ArgConverter {
// If T is void, the argument is converted to corresponding signed or unsigned // If T is void, the argument is converted to corresponding signed or unsigned
// type depending on the type specifier: 'd' and 'i' - signed, other - // type depending on the type specifier: 'd' and 'i' - signed, other -
// unsigned). // unsigned).
template <typename T> template <typename T, typename Char>
void convert_arg(format_arg &arg, wchar_t type) { void convert_arg(basic_format_arg<Char> &arg, Char type) {
visit(ArgConverter<T>(arg, type), arg); visit(ArgConverter<T, Char>(arg, type), arg);
} }
// Converts an integer argument to char for printf. // Converts an integer argument to char for printf.
template <typename Char>
class CharConverter { class CharConverter {
private: private:
format_arg &arg_; basic_format_arg<Char> &arg_;
FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter); FMT_DISALLOW_COPY_AND_ASSIGN(CharConverter);
public: public:
explicit CharConverter(format_arg &arg) : arg_(arg) {} explicit CharConverter(basic_format_arg<Char> &arg) : arg_(arg) {}
template <typename T> template <typename T>
typename std::enable_if<std::is_integral<T>::value>::type typename std::enable_if<std::is_integral<T>::value>::type
operator()(T value) { operator()(T value) {
arg_.type = format_arg::CHAR; arg_ =
arg_.int_value = static_cast<char>(value); internal::MakeArg<basic_format_context<Char>>(static_cast<char>(value));
} }
template <typename T> template <typename T>
@ -168,14 +169,14 @@ class CharConverter {
// 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.
class WidthHandler { class PrintfWidthHandler {
private: private:
FormatSpec &spec_; FormatSpec &spec_;
FMT_DISALLOW_COPY_AND_ASSIGN(WidthHandler); FMT_DISALLOW_COPY_AND_ASSIGN(PrintfWidthHandler);
public: public:
explicit WidthHandler(FormatSpec &spec) : spec_(spec) {} explicit PrintfWidthHandler(FormatSpec &spec) : spec_(spec) {}
template <typename T> template <typename T>
typename std::enable_if<std::is_integral<T>::value, unsigned>::type typename std::enable_if<std::is_integral<T>::value, unsigned>::type
@ -239,7 +240,7 @@ class PrintfArgFormatter : public internal::ArgFormatterBase<Char> {
} }
/** Formats a character. */ /** Formats a character. */
void operator()(wchar_t value) { void operator()(Char value) {
const FormatSpec &fmt_spec = this->spec(); const FormatSpec &fmt_spec = this->spec();
BasicWriter<Char> &w = this->writer(); BasicWriter<Char> &w = this->writer();
if (fmt_spec.type_ && fmt_spec.type_ != 'c') if (fmt_spec.type_ && fmt_spec.type_ != 'c')
@ -282,7 +283,7 @@ class PrintfArgFormatter : public internal::ArgFormatterBase<Char> {
/** Formats an argument of a custom (user-defined) type. */ /** Formats an argument of a custom (user-defined) type. */
void operator()(format_arg::CustomValue c) { void operator()(format_arg::CustomValue c) {
const Char format_str[] = {'}', '\0'}; const Char format_str[] = {'}', '\0'};
auto args = basic_format_args<basic_format_context<Char>>(); auto args = basic_format_args<basic_format_context<Char>, Char>();
basic_format_context<Char> ctx(format_str, args); basic_format_context<Char> ctx(format_str, args);
c.format(&this->writer(), c.value, &ctx); c.format(&this->writer(), c.value, &ctx);
} }
@ -305,7 +306,7 @@ class printf_context :
// Returns the argument with specified index or, if arg_index is equal // Returns the argument with specified index or, if arg_index is equal
// to the maximum unsigned value, the next argument. // to the maximum unsigned value, the next argument.
format_arg get_arg( basic_format_arg<Char> get_arg(
const Char *s, const Char *s,
unsigned arg_index = (std::numeric_limits<unsigned>::max)()); unsigned arg_index = (std::numeric_limits<unsigned>::max)());
@ -321,7 +322,7 @@ class printf_context :
\endrst \endrst
*/ */
explicit printf_context(BasicCStringRef<Char> format_str, explicit printf_context(BasicCStringRef<Char> format_str,
basic_format_args<printf_context> args) basic_format_args<printf_context, Char> args)
: Base(format_str.c_str(), args) {} : Base(format_str.c_str(), args) {}
/** Formats stored arguments and writes the output to the writer. */ /** Formats stored arguments and writes the output to the writer. */
@ -355,11 +356,12 @@ void printf_context<Char, AF>::parse_flags(FormatSpec &spec, const Char *&s) {
} }
template <typename Char, typename AF> template <typename Char, typename AF>
format_arg printf_context<Char, AF>::get_arg(const Char *s, basic_format_arg<Char> printf_context<Char, AF>::get_arg(
unsigned arg_index) { const Char *s, unsigned arg_index) {
(void)s; (void)s;
const char *error = 0; const char *error = 0;
format_arg arg = arg_index == std::numeric_limits<unsigned>::max() ? basic_format_arg<Char> arg =
arg_index == std::numeric_limits<unsigned>::max() ?
this->next_arg(error) : Base::get_arg(arg_index - 1, error); this->next_arg(error) : Base::get_arg(arg_index - 1, error);
if (error) if (error)
FMT_THROW(format_error(!*s ? "invalid format string" : error)); FMT_THROW(format_error(!*s ? "invalid format string" : error));
@ -395,7 +397,7 @@ unsigned printf_context<Char, AF>::parse_header(
spec.width_ = internal::parse_nonnegative_int(s); spec.width_ = internal::parse_nonnegative_int(s);
} else if (*s == '*') { } else if (*s == '*') {
++s; ++s;
spec.width_ = visit(internal::WidthHandler(spec), get_arg(s)); spec.width_ = visit(internal::PrintfWidthHandler(spec), get_arg(s));
} }
return arg_index; return arg_index;
} }
@ -427,15 +429,15 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s)); spec.precision_ = static_cast<int>(internal::parse_nonnegative_int(s));
} else if (*s == '*') { } else if (*s == '*') {
++s; ++s;
spec.precision_ = visit(internal::PrecisionHandler(), get_arg(s)); spec.precision_ = visit(internal::PrintfPrecisionHandler(), get_arg(s));
} }
} }
format_arg arg = get_arg(s, arg_index); basic_format_arg<Char> arg = get_arg(s, arg_index);
if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg)) if (spec.flag(HASH_FLAG) && visit(internal::IsZeroInt(), arg))
spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG); spec.flags_ &= ~internal::to_unsigned<int>(HASH_FLAG);
if (spec.fill_ == '0') { if (spec.fill_ == '0') {
if (arg.type <= format_arg::LAST_NUMERIC_TYPE) if (arg.is_numeric())
spec.align_ = ALIGN_NUMERIC; spec.align_ = ALIGN_NUMERIC;
else else
spec.fill_ = ' '; // Ignore '0' flag for non-numeric types. spec.fill_ = ' '; // Ignore '0' flag for non-numeric types.
@ -478,7 +480,7 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
if (!*s) if (!*s)
FMT_THROW(format_error("invalid format string")); FMT_THROW(format_error("invalid format string"));
spec.type_ = static_cast<char>(*s++); spec.type_ = static_cast<char>(*s++);
if (arg.type <= format_arg::LAST_INTEGER_TYPE) { if (arg.is_integral()) {
// Normalize type. // Normalize type.
switch (spec.type_) { switch (spec.type_) {
case 'i': case 'u': case 'i': case 'u':
@ -486,7 +488,7 @@ void printf_context<Char, AF>::format(BasicWriter<Char> &writer) {
break; break;
case 'c': case 'c':
// TODO: handle wchar_t // TODO: handle wchar_t
visit(internal::CharConverter(arg), arg); visit(internal::CharConverter<Char>(arg), arg);
break; break;
} }
} }
@ -509,12 +511,13 @@ void format_value(BasicWriter<Char> &w, const T &value,
template <typename Char> template <typename Char>
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format,
basic_format_args<printf_context<Char>> args) { basic_format_args<printf_context<Char>, Char> args) {
printf_context<Char>(format, args).format(w); printf_context<Char>(format, args).format(w);
} }
inline std::string vsprintf(CStringRef format, typedef basic_format_args<printf_context<char>, char> printf_args;
basic_format_args<printf_context<char>> args) {
inline std::string vsprintf(CStringRef format, printf_args args) {
MemoryWriter w; MemoryWriter w;
printf(w, format, args); printf(w, format, args);
return w.str(); return w.str();
@ -534,8 +537,9 @@ inline std::string sprintf(CStringRef format_str, const Args & ... args) {
return vsprintf(format_str, make_xformat_args<printf_context<char>>(args...)); return vsprintf(format_str, make_xformat_args<printf_context<char>>(args...));
} }
inline std::wstring vsprintf(WCStringRef format, inline std::wstring vsprintf(
basic_format_args<printf_context<wchar_t>> args) { WCStringRef format,
basic_format_args<printf_context<wchar_t>, wchar_t> args) {
WMemoryWriter w; WMemoryWriter w;
printf(w, format, args); printf(w, format, args);
return w.str(); return w.str();
@ -547,8 +551,7 @@ inline std::wstring sprintf(WCStringRef format_str, const Args & ... args) {
return vsprintf(format_str, vargs); return vsprintf(format_str, vargs);
} }
FMT_API int vfprintf(std::FILE *f, CStringRef format, FMT_API int vfprintf(std::FILE *f, CStringRef format, printf_args args);
basic_format_args<printf_context<char>> args);
/** /**
\rst \rst
@ -565,8 +568,7 @@ inline int fprintf(std::FILE *f, CStringRef format_str, const Args & ... args) {
return vfprintf(f, format_str, vargs); return vfprintf(f, format_str, vargs);
} }
inline int vprintf(CStringRef format, inline int vprintf(CStringRef format, printf_args args) {
basic_format_args<printf_context<char>> args) {
return vfprintf(stdout, format, args); return vfprintf(stdout, format, args);
} }
@ -584,8 +586,7 @@ inline int printf(CStringRef format_str, const Args & ... args) {
return vprintf(format_str, make_xformat_args<printf_context<char>>(args...)); return vprintf(format_str, make_xformat_args<printf_context<char>>(args...));
} }
inline int vfprintf(std::ostream &os, CStringRef format_str, inline int vfprintf(std::ostream &os, CStringRef format_str, printf_args args) {
basic_format_args<printf_context<char>> args) {
MemoryWriter w; MemoryWriter w;
printf(w, format_str, args); printf(w, format_str, args);
internal::write(os, w); internal::write(os, w);

View File

@ -63,7 +63,7 @@ typedef fmt::printf_context<char, CustomPrintfArgFormatter>
std::string custom_vsprintf( std::string custom_vsprintf(
const char* format_str, const char* format_str,
fmt::basic_format_args<CustomPrintfFormatter> args) { fmt::basic_format_args<CustomPrintfFormatter, char> args) {
fmt::MemoryWriter writer; fmt::MemoryWriter writer;
CustomPrintfFormatter formatter(format_str, args); CustomPrintfFormatter formatter(format_str, args);
formatter.format(writer); formatter.format(writer);

View File

@ -41,13 +41,25 @@
#undef min #undef min
#undef max #undef max
template <typename T>
struct ValueExtractor {
T operator()(T value) {
return value;
}
template <typename U>
T operator()(U) {
throw std::runtime_error(fmt::format("invalid type {}", typeid(U).name()));
return T();
}
};
TEST(FormatTest, ArgConverter) { TEST(FormatTest, ArgConverter) {
using fmt::format_arg; using fmt::format_arg;
format_arg arg = format_arg(); fmt::LongLong value = std::numeric_limits<fmt::LongLong>::max();
arg.type = format_arg::LONG_LONG; format_arg arg = fmt::internal::MakeArg<fmt::format_context>(value);
arg.long_long_value = std::numeric_limits<fmt::LongLong>::max(); visit(fmt::internal::ArgConverter<fmt::LongLong, char>(arg, 'd'), arg);
visit(fmt::internal::ArgConverter<fmt::LongLong>(arg, 'd'), arg); EXPECT_EQ(value, visit(ValueExtractor<fmt::LongLong>(), arg));
EXPECT_EQ(format_arg::LONG_LONG, arg.type);
} }
TEST(FormatTest, FormatNegativeNaN) { TEST(FormatTest, FormatNegativeNaN) {

View File

@ -49,13 +49,17 @@
#include "fmt/format.h" #include "fmt/format.h"
#undef min
#undef max #undef max
using fmt::basic_format_arg;
using fmt::format_arg; using fmt::format_arg;
using fmt::Buffer; using fmt::Buffer;
using fmt::StringRef; using fmt::StringRef;
using fmt::internal::MemoryBuffer; using fmt::internal::MemoryBuffer;
using fmt::internal::Value;
using testing::_;
using testing::Return; using testing::Return;
using testing::StrictMock; using testing::StrictMock;
@ -70,11 +74,9 @@ void format_value(fmt::BasicWriter<Char> &w, Test,
} }
template <typename Char, typename T> template <typename Char, typename T>
format_arg make_arg(const T &value) { basic_format_arg<Char> make_arg(const T &value) {
typedef fmt::internal::MakeValue< fmt::basic_format_context<Char> > MakeValue; typedef fmt::internal::MakeArg< fmt::basic_format_context<Char> > MakeArg;
format_arg arg = MakeValue(value); return MakeArg(value);
arg.type = fmt::internal::type<T>();
return arg;
} }
} // namespace } // namespace
@ -406,175 +408,9 @@ TEST(UtilTest, Increment) {
EXPECT_STREQ("200", s); EXPECT_STREQ("200", s);
} }
template <format_arg::Type>
struct ArgInfo;
#define ARG_INFO(type_code, Type, field) \
template <> \
struct ArgInfo<format_arg::type_code> { \
static Type get(const format_arg &arg) { return arg.field; } \
}
ARG_INFO(INT, int, int_value);
ARG_INFO(UINT, unsigned, uint_value);
ARG_INFO(LONG_LONG, fmt::LongLong, long_long_value);
ARG_INFO(ULONG_LONG, fmt::ULongLong, ulong_long_value);
ARG_INFO(BOOL, int, int_value);
ARG_INFO(CHAR, int, int_value);
ARG_INFO(DOUBLE, double, double_value);
ARG_INFO(LONG_DOUBLE, long double, long_double_value);
ARG_INFO(CSTRING, const char *, string.value);
ARG_INFO(STRING, const char *, string.value);
ARG_INFO(WSTRING, const wchar_t *, wstring.value);
ARG_INFO(POINTER, const void *, pointer);
ARG_INFO(CUSTOM, format_arg::CustomValue, custom);
#define CHECK_ARG_INFO(Type, field, value) { \
format_arg arg = format_arg(); \
arg.field = value; \
EXPECT_EQ(value, ArgInfo<format_arg::Type>::get(arg)); \
}
TEST(ArgTest, ArgInfo) {
CHECK_ARG_INFO(INT, int_value, 42);
CHECK_ARG_INFO(UINT, uint_value, 42u);
CHECK_ARG_INFO(LONG_LONG, long_long_value, 42);
CHECK_ARG_INFO(ULONG_LONG, ulong_long_value, 42u);
CHECK_ARG_INFO(DOUBLE, double_value, 4.2);
CHECK_ARG_INFO(LONG_DOUBLE, long_double_value, 4.2);
CHECK_ARG_INFO(CHAR, int_value, 'x');
const char STR[] = "abc";
CHECK_ARG_INFO(CSTRING, string.value, STR);
const wchar_t WSTR[] = L"abc";
CHECK_ARG_INFO(WSTRING, wstring.value, WSTR);
int p = 0;
CHECK_ARG_INFO(POINTER, pointer, &p);
format_arg arg = format_arg();
arg.custom.value = &p;
EXPECT_EQ(&p, ArgInfo<format_arg::CUSTOM>::get(arg).value);
}
#define EXPECT_ARG_(Char, type_code, MakeArgType, ExpectedType, value) { \
MakeArgType input = static_cast<MakeArgType>(value); \
format_arg arg = make_arg<Char>(input); \
EXPECT_EQ(format_arg::type_code, arg.type); \
ExpectedType expected_value = static_cast<ExpectedType>(value); \
EXPECT_EQ(expected_value, ArgInfo<format_arg::type_code>::get(arg)); \
}
#define EXPECT_ARG(type_code, Type, value) \
EXPECT_ARG_(char, type_code, Type, Type, value)
#define EXPECT_ARGW(type_code, Type, value) \
EXPECT_ARG_(wchar_t, type_code, Type, Type, value)
TEST(ArgTest, MakeArg) {
// Test bool.
EXPECT_ARG_(char, BOOL, bool, int, true);
EXPECT_ARG_(wchar_t, BOOL, bool, int, true);
// Test char.
EXPECT_ARG(CHAR, char, 'a');
EXPECT_ARG(CHAR, char, CHAR_MIN);
EXPECT_ARG(CHAR, char, CHAR_MAX);
// Test wchar_t.
EXPECT_ARGW(CHAR, wchar_t, L'a');
EXPECT_ARGW(CHAR, wchar_t, WCHAR_MIN);
EXPECT_ARGW(CHAR, wchar_t, WCHAR_MAX);
// Test signed/unsigned char.
EXPECT_ARG(INT, signed char, 42);
EXPECT_ARG(INT, signed char, SCHAR_MIN);
EXPECT_ARG(INT, signed char, SCHAR_MAX);
EXPECT_ARG(UINT, unsigned char, 42);
EXPECT_ARG(UINT, unsigned char, UCHAR_MAX );
// Test short.
EXPECT_ARG(INT, short, 42);
EXPECT_ARG(INT, short, SHRT_MIN);
EXPECT_ARG(INT, short, SHRT_MAX);
EXPECT_ARG(UINT, unsigned short, 42);
EXPECT_ARG(UINT, unsigned short, USHRT_MAX);
// Test int.
EXPECT_ARG(INT, int, 42);
EXPECT_ARG(INT, int, INT_MIN);
EXPECT_ARG(INT, int, INT_MAX);
EXPECT_ARG(UINT, unsigned, 42);
EXPECT_ARG(UINT, unsigned, UINT_MAX);
// Test long.
#if LONG_MAX == INT_MAX
# define LONG INT
# define ULONG UINT
# define long_value int_value
# define ulong_value uint_value
#else
# define LONG LONG_LONG
# define ULONG ULONG_LONG
# define long_value long_long_value
# define ulong_value ulong_long_value
#endif
EXPECT_ARG(LONG, long, 42);
EXPECT_ARG(LONG, long, LONG_MIN);
EXPECT_ARG(LONG, long, LONG_MAX);
EXPECT_ARG(ULONG, unsigned long, 42);
EXPECT_ARG(ULONG, unsigned long, ULONG_MAX);
// Test long long.
EXPECT_ARG(LONG_LONG, fmt::LongLong, 42);
EXPECT_ARG(LONG_LONG, fmt::LongLong, LLONG_MIN);
EXPECT_ARG(LONG_LONG, fmt::LongLong, LLONG_MAX);
EXPECT_ARG(ULONG_LONG, fmt::ULongLong, 42);
EXPECT_ARG(ULONG_LONG, fmt::ULongLong, ULLONG_MAX);
// Test float.
EXPECT_ARG(DOUBLE, float, 4.2);
EXPECT_ARG(DOUBLE, float, FLT_MIN);
EXPECT_ARG(DOUBLE, float, FLT_MAX);
// Test double.
EXPECT_ARG(DOUBLE, double, 4.2);
EXPECT_ARG(DOUBLE, double, DBL_MIN);
EXPECT_ARG(DOUBLE, double, DBL_MAX);
// Test long double.
EXPECT_ARG(LONG_DOUBLE, long double, 4.2);
EXPECT_ARG(LONG_DOUBLE, long double, LDBL_MIN);
EXPECT_ARG(LONG_DOUBLE, long double, LDBL_MAX);
// Test string.
char STR[] = "test";
EXPECT_ARG(CSTRING, char*, STR);
EXPECT_ARG(CSTRING, const char*, STR);
EXPECT_ARG(STRING, std::string, STR);
EXPECT_ARG(STRING, fmt::StringRef, STR);
// Test wide string.
wchar_t WSTR[] = L"test";
EXPECT_ARGW(WSTRING, wchar_t*, WSTR);
EXPECT_ARGW(WSTRING, const wchar_t*, WSTR);
EXPECT_ARGW(WSTRING, std::wstring, WSTR);
EXPECT_ARGW(WSTRING, fmt::WStringRef, WSTR);
int n = 42;
EXPECT_ARG(POINTER, void*, &n);
EXPECT_ARG(POINTER, const void*, &n);
::Test t;
format_arg arg = make_arg<char>(t);
EXPECT_EQ(format_arg::CUSTOM, arg.type);
EXPECT_EQ(&t, arg.custom.value);
fmt::MemoryWriter w;
fmt::format_context ctx("}", fmt::format_args());
arg.custom.format(&w, &t, &ctx);
EXPECT_EQ("test", w.str());
}
TEST(UtilTest, FormatArgs) { TEST(UtilTest, FormatArgs) {
fmt::format_args args; fmt::format_args args;
EXPECT_EQ(format_arg::NONE, args[1].type); EXPECT_FALSE(args[1]);
} }
struct CustomFormatter { struct CustomFormatter {
@ -595,73 +431,163 @@ TEST(UtilTest, MakeValueWithCustomFormatter) {
EXPECT_TRUE(ctx.called); EXPECT_TRUE(ctx.called);
} }
struct Result { namespace fmt {
format_arg arg; namespace internal {
Result() : arg(make_arg<char>(0xdeadbeef)) {} bool operator==(Value::CustomValue lhs, Value::CustomValue rhs) {
return lhs.value == rhs.value;
template <typename T>
Result(const T& value) : arg(make_arg<char>(value)) {}
Result(const wchar_t *s) : arg(make_arg<wchar_t>(s)) {}
};
struct TestVisitor {
Result operator()(int value) { return value; }
Result operator()(unsigned value) { return value; }
Result operator()(fmt::LongLong value) { return value; }
Result operator()(fmt::ULongLong value) { return value; }
Result operator()(double value) { return value; }
Result operator()(long double value) { return value; }
Result operator()(wchar_t value) { return static_cast<char>(value); }
Result operator()(const char *s) { return s; }
Result operator()(fmt::format_arg::StringValue<char> s) {
return s.value;
}
Result operator()(fmt::format_arg::StringValue<wchar_t> s) {
return s.value;
}
Result operator()(const void *p) { return p; }
Result operator()(fmt::format_arg::CustomValue c) {
return *static_cast<const ::Test*>(c.value);
}
};
#define EXPECT_RESULT_(Char, type_code, value) { \
format_arg arg = make_arg<Char>(value); \
Result result = fmt::visit(TestVisitor(), arg); \
EXPECT_EQ(format_arg::type_code, result.arg.type); \
EXPECT_EQ(value, ArgInfo<format_arg::type_code>::get(result.arg)); \
} }
#define EXPECT_RESULT(type_code, value) \ template <typename T>
EXPECT_RESULT_(char, type_code, value) bool operator==(Value::StringValue<T> lhs, Value::StringValue<T> rhs) {
#define EXPECT_RESULTW(type_code, value) \ return std::basic_string<T>(lhs.value, lhs.size) ==
EXPECT_RESULT_(wchar_t, type_code, value) std::basic_string<T>(rhs.value, rhs.size);
}
}
}
TEST(ArgVisitorTest, VisitAll) { template <typename T>
EXPECT_RESULT(INT, 42); struct MockVisitor {
EXPECT_RESULT(UINT, 42u); // Use a unique result type to make sure that there are no undesirable
EXPECT_RESULT(LONG_LONG, 42ll); // conversions.
EXPECT_RESULT(ULONG_LONG, 42ull); struct Result {};
EXPECT_RESULT(DOUBLE, 4.2);
EXPECT_RESULT(LONG_DOUBLE, 4.2l); MockVisitor() {
EXPECT_RESULT(CHAR, 'x'); ON_CALL(*this, visit(_)).WillByDefault(Return(Result()));
const char STR[] = "abc"; }
EXPECT_RESULT(CSTRING, STR);
const wchar_t WSTR[] = L"abc"; MOCK_METHOD1_T(visit, Result (T value));
EXPECT_RESULTW(WSTRING, WSTR); MOCK_METHOD0_T(unexpected, void ());
const void *p = STR;
EXPECT_RESULT(POINTER, p); Result operator()(T value) { return visit(value); }
::Test t;
Result result = visit(TestVisitor(), make_arg<char>(t)); template <typename U>
EXPECT_EQ(format_arg::CUSTOM, result.arg.type); Result operator()(U value) {
EXPECT_EQ(&t, result.arg.custom.value); unexpected();
return Result();
}
};
template <typename T>
struct VisitType { typedef T Type; };
#define VISIT_TYPE(Type_, VisitType_) \
template <> \
struct VisitType<Type_> { typedef VisitType_ Type; }
VISIT_TYPE(signed char, int);
VISIT_TYPE(unsigned char, unsigned);
VISIT_TYPE(short, int);
VISIT_TYPE(unsigned short, unsigned);
#if LONG_MAX == INT_MAX
VISIT_TYPE(long, int);
VISIT_TYPE(unsigned long, unsigned);
#else
VISIT_TYPE(long, fmt::LongLong);
VISIT_TYPE(unsigned long, fmt::ULongLong);
#endif
VISIT_TYPE(float, double);
#define CHECK_ARG_(Char, expected, value) { \
testing::StrictMock<MockVisitor<decltype(expected)>> visitor; \
EXPECT_CALL(visitor, visit(expected)); \
fmt::visit(visitor, make_arg<Char>(value)); \
}
#define CHECK_ARG(value) { \
typename VisitType<decltype(value)>::Type expected = value; \
CHECK_ARG_(char, expected, value) \
CHECK_ARG_(wchar_t, expected, value) \
}
template <typename T>
class NumericArgTest : public testing::Test {};
typedef ::testing::Types<
bool, signed char, unsigned char, signed, unsigned short,
int, unsigned, long, unsigned long, fmt::LongLong, fmt::ULongLong,
float, double, long double> Types;
TYPED_TEST_CASE(NumericArgTest, Types);
template <typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type test_value() {
return static_cast<T>(42);
}
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type
test_value() {
return static_cast<T>(4.2);
}
TYPED_TEST(NumericArgTest, MakeAndVisit) {
CHECK_ARG(test_value<TypeParam>());
CHECK_ARG(std::numeric_limits<TypeParam>::min());
CHECK_ARG(std::numeric_limits<TypeParam>::max());
}
TEST(UtilTest, CharArg) {
CHECK_ARG_(char, 'a', 'a');
CHECK_ARG_(wchar_t, L'a', 'a');
CHECK_ARG_(wchar_t, L'a', L'a');
}
TEST(UtilTest, StringArg) {
char str_data[] = "test";
char *str = str_data;
const char *cstr = str;
CHECK_ARG_(char, cstr, str);
CHECK_ARG_(wchar_t, cstr, str);
CHECK_ARG(cstr);
Value::StringValue<char> strval = {str, 4};
CHECK_ARG_(char, strval, std::string(str));
CHECK_ARG_(wchar_t, strval, std::string(str));
CHECK_ARG_(char, strval, fmt::StringRef(str));
CHECK_ARG_(wchar_t, strval, fmt::StringRef(str));
}
TEST(UtilTest, WStringArg) {
wchar_t str_data[] = L"test";
wchar_t *str = str_data;
const wchar_t *cstr = str;
Value::StringValue<wchar_t> strval = {str, 4};
CHECK_ARG_(wchar_t, strval, str);
CHECK_ARG_(wchar_t, strval, cstr);
CHECK_ARG_(wchar_t, strval, std::wstring(str));
CHECK_ARG_(wchar_t, strval, fmt::WStringRef(str));
}
TEST(UtilTest, PointerArg) {
void *p = 0;
const void *cp = 0;
CHECK_ARG_(char, cp, p);
CHECK_ARG_(wchar_t, cp, p);
CHECK_ARG(cp);
}
TEST(UtilTest, CustomArg) {
::Test test;
typedef MockVisitor<Value::CustomValue> Visitor;
testing::StrictMock<Visitor> visitor;
EXPECT_CALL(visitor, visit(_)).WillOnce(
testing::Invoke([&](Value::CustomValue custom) {
EXPECT_EQ(&test, custom.value);
fmt::MemoryWriter w;
fmt::format_context ctx("}", fmt::format_args());
custom.format(&w, &test, &ctx);
EXPECT_EQ("test", w.str());
return Visitor::Result();
}));
fmt::visit(visitor, make_arg<char>(test));
} }
TEST(ArgVisitorTest, VisitInvalidArg) { TEST(ArgVisitorTest, VisitInvalidArg) {
format_arg arg = format_arg(); format_arg arg = format_arg();
arg.type = static_cast<format_arg::Type>(format_arg::NONE); EXPECT_ASSERT(visit(MockVisitor<int>(), arg), "invalid argument type");
EXPECT_ASSERT(visit(TestVisitor(), arg), "invalid argument type");
} }
// Tests fmt::internal::count_digits for integer type Int. // Tests fmt::internal::count_digits for integer type Int.