|
|
|
@ -52,17 +52,6 @@
|
|
|
|
|
|
|
|
|
|
using fmt::internal::Arg;
|
|
|
|
|
|
|
|
|
|
// Check if exceptions are disabled.
|
|
|
|
|
#if defined(__GNUC__) && !defined(__EXCEPTIONS)
|
|
|
|
|
# define FMT_EXCEPTIONS 0
|
|
|
|
|
#endif
|
|
|
|
|
#if defined(_MSC_VER) && !_HAS_EXCEPTIONS
|
|
|
|
|
# define FMT_EXCEPTIONS 0
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef FMT_EXCEPTIONS
|
|
|
|
|
# define FMT_EXCEPTIONS 1
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if FMT_EXCEPTIONS
|
|
|
|
|
# define FMT_TRY try
|
|
|
|
|
# define FMT_CATCH(x) catch (x)
|
|
|
|
@ -71,14 +60,6 @@ using fmt::internal::Arg;
|
|
|
|
|
# define FMT_CATCH(x) if (false)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef FMT_THROW
|
|
|
|
|
# if FMT_EXCEPTIONS
|
|
|
|
|
# define FMT_THROW(x) throw x
|
|
|
|
|
# else
|
|
|
|
|
# define FMT_THROW(x) assert(false)
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef FMT_HEADER_ONLY
|
|
|
|
|
# define FMT_FUNC inline
|
|
|
|
|
#else
|
|
|
|
@ -226,10 +207,15 @@ void format_error_code(fmt::Writer &out, int error_code,
|
|
|
|
|
out.clear();
|
|
|
|
|
static const char SEP[] = ": ";
|
|
|
|
|
static const char ERROR_STR[] = "error ";
|
|
|
|
|
fmt::internal::IntTraits<int>::MainType ec_value = error_code;
|
|
|
|
|
// Subtract 2 to account for terminating null characters in SEP and ERROR_STR.
|
|
|
|
|
std::size_t error_code_size = sizeof(SEP) + sizeof(ERROR_STR) - 2;
|
|
|
|
|
error_code_size += fmt::internal::count_digits(ec_value);
|
|
|
|
|
typedef fmt::internal::IntTraits<int>::MainType MainType;
|
|
|
|
|
MainType abs_value = static_cast<MainType>(error_code);
|
|
|
|
|
if (internal::is_negative(error_code)) {
|
|
|
|
|
abs_value = 0 - abs_value;
|
|
|
|
|
++error_code_size;
|
|
|
|
|
}
|
|
|
|
|
error_code_size += fmt::internal::count_digits(abs_value);
|
|
|
|
|
if (message.size() <= fmt::internal::INLINE_BUFFER_SIZE - error_code_size)
|
|
|
|
|
out << message << SEP;
|
|
|
|
|
out << ERROR_STR << error_code;
|
|
|
|
@ -253,50 +239,6 @@ class IsZeroInt : public fmt::internal::ArgVisitor<IsZeroInt, bool> {
|
|
|
|
|
bool visit_any_int(T value) { return value == 0; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Parses an unsigned integer advancing s to the end of the parsed input.
|
|
|
|
|
// This function assumes that the first character of s is a digit.
|
|
|
|
|
template <typename Char>
|
|
|
|
|
int parse_nonnegative_int(const Char *&s) {
|
|
|
|
|
assert('0' <= *s && *s <= '9');
|
|
|
|
|
unsigned value = 0;
|
|
|
|
|
do {
|
|
|
|
|
unsigned new_value = value * 10 + (*s++ - '0');
|
|
|
|
|
// Check if value wrapped around.
|
|
|
|
|
if (new_value < value) {
|
|
|
|
|
value = UINT_MAX;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
value = new_value;
|
|
|
|
|
} while ('0' <= *s && *s <= '9');
|
|
|
|
|
if (value > INT_MAX)
|
|
|
|
|
FMT_THROW(fmt::FormatError("number is too big"));
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
inline bool is_name_start(Char c) {
|
|
|
|
|
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void require_numeric_argument(const Arg &arg, char spec) {
|
|
|
|
|
if (arg.type > Arg::LAST_NUMERIC_TYPE) {
|
|
|
|
|
std::string message =
|
|
|
|
|
fmt::format("format specifier '{}' requires numeric argument", spec);
|
|
|
|
|
FMT_THROW(fmt::FormatError(message));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
void check_sign(const Char *&s, const Arg &arg) {
|
|
|
|
|
char sign = static_cast<char>(*s);
|
|
|
|
|
require_numeric_argument(arg, sign);
|
|
|
|
|
if (arg.type == Arg::UINT || arg.type == Arg::ULONG_LONG) {
|
|
|
|
|
FMT_THROW(fmt::FormatError(fmt::format(
|
|
|
|
|
"format specifier '{}' requires signed argument", sign)));
|
|
|
|
|
}
|
|
|
|
|
++s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checks if an argument is a valid printf width specifier and sets
|
|
|
|
|
// left alignment if it is negative.
|
|
|
|
|
class WidthHandler : public fmt::internal::ArgVisitor<WidthHandler, unsigned> {
|
|
|
|
@ -315,7 +257,7 @@ class WidthHandler : public fmt::internal::ArgVisitor<WidthHandler, unsigned> {
|
|
|
|
|
template <typename T>
|
|
|
|
|
unsigned visit_any_int(T value) {
|
|
|
|
|
typedef typename fmt::internal::IntTraits<T>::MainType UnsignedType;
|
|
|
|
|
UnsignedType width = value;
|
|
|
|
|
UnsignedType width = static_cast<UnsignedType>(value);
|
|
|
|
|
if (fmt::internal::is_negative(value)) {
|
|
|
|
|
spec_.align_ = fmt::ALIGN_LEFT;
|
|
|
|
|
width = 0 - width;
|
|
|
|
@ -341,8 +283,21 @@ class PrecisionHandler :
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Converts an integer argument to an integral type T for printf.
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
|
struct is_same {
|
|
|
|
|
enum { value = 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
struct is_same<T, T> {
|
|
|
|
|
enum { value = 1 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// An argument visitor that converts an integer argument to T for printf,
|
|
|
|
|
// if T is an integral type. If T is void, the argument is converted to
|
|
|
|
|
// corresponding signed or unsigned type depending on the type specifier:
|
|
|
|
|
// 'd' and 'i' - signed, other - unsigned)
|
|
|
|
|
template <typename T = void>
|
|
|
|
|
class ArgConverter : public fmt::internal::ArgVisitor<ArgConverter<T>, void> {
|
|
|
|
|
private:
|
|
|
|
|
fmt::internal::Arg &arg_;
|
|
|
|
@ -363,21 +318,25 @@ class ArgConverter : public fmt::internal::ArgVisitor<ArgConverter<T>, void> {
|
|
|
|
|
void visit_any_int(U value) {
|
|
|
|
|
bool is_signed = type_ == 'd' || type_ == 'i';
|
|
|
|
|
using fmt::internal::Arg;
|
|
|
|
|
if (sizeof(T) <= sizeof(int)) {
|
|
|
|
|
typedef typename fmt::internal::Conditional<
|
|
|
|
|
is_same<T, void>::value, U, T>::type TargetType;
|
|
|
|
|
if (sizeof(TargetType) <= sizeof(int)) {
|
|
|
|
|
// Extra casts are used to silence warnings.
|
|
|
|
|
if (is_signed) {
|
|
|
|
|
arg_.type = Arg::INT;
|
|
|
|
|
arg_.int_value = static_cast<int>(static_cast<T>(value));
|
|
|
|
|
arg_.int_value = static_cast<int>(static_cast<TargetType>(value));
|
|
|
|
|
} else {
|
|
|
|
|
arg_.type = Arg::UINT;
|
|
|
|
|
arg_.uint_value = static_cast<unsigned>(
|
|
|
|
|
static_cast<typename fmt::internal::MakeUnsigned<T>::Type>(value));
|
|
|
|
|
typedef typename fmt::internal::MakeUnsigned<TargetType>::Type Unsigned;
|
|
|
|
|
arg_.uint_value = static_cast<unsigned>(static_cast<Unsigned>(value));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (is_signed) {
|
|
|
|
|
arg_.type = Arg::LONG_LONG;
|
|
|
|
|
arg_.long_long_value =
|
|
|
|
|
static_cast<typename fmt::internal::MakeUnsigned<U>::Type>(value);
|
|
|
|
|
// glibc's printf doesn't sign extend arguments of smaller types:
|
|
|
|
|
// std::printf("%lld", -42); // prints "4294967254"
|
|
|
|
|
// but we don't have to do the same because it's a UB.
|
|
|
|
|
arg_.long_long_value = static_cast<fmt::LongLong>(value);
|
|
|
|
|
} else {
|
|
|
|
|
arg_.type = Arg::ULONG_LONG;
|
|
|
|
|
arg_.ulong_long_value =
|
|
|
|
@ -403,138 +362,46 @@ class CharConverter : public fmt::internal::ArgVisitor<CharConverter, void> {
|
|
|
|
|
arg_.int_value = static_cast<char>(value);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
template <typename Impl, typename Char>
|
|
|
|
|
class BasicArgFormatter : public ArgVisitor<Impl, void> {
|
|
|
|
|
private:
|
|
|
|
|
BasicWriter<Char> &writer_;
|
|
|
|
|
FormatSpec &spec_;
|
|
|
|
|
|
|
|
|
|
FMT_DISALLOW_COPY_AND_ASSIGN(BasicArgFormatter);
|
|
|
|
|
|
|
|
|
|
void write_pointer(const void *p) {
|
|
|
|
|
spec_.flags_ = HASH_FLAG;
|
|
|
|
|
spec_.type_ = 'x';
|
|
|
|
|
writer_.write_int(reinterpret_cast<uintptr_t>(p), spec_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
BasicWriter<Char> &writer() { return writer_; }
|
|
|
|
|
FormatSpec &spec() { return spec_; }
|
|
|
|
|
|
|
|
|
|
void write_bool(bool value) {
|
|
|
|
|
const char *str_value = value ? "true" : "false";
|
|
|
|
|
Arg::StringValue<char> str = { str_value, strlen(str_value) };
|
|
|
|
|
writer_.write_str(str, spec_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
BasicArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
|
|
|
|
: writer_(w), spec_(s) {}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
void visit_any_int(T value) { writer_.write_int(value, spec_); }
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
void visit_any_double(T value) { writer_.write_double(value, spec_); }
|
|
|
|
|
|
|
|
|
|
void visit_bool(bool value) {
|
|
|
|
|
if (spec_.type_)
|
|
|
|
|
return visit_any_int(value);
|
|
|
|
|
write_bool(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit_char(int value) {
|
|
|
|
|
if (spec_.type_ && spec_.type_ != 'c') {
|
|
|
|
|
spec_.flags_ |= CHAR_FLAG;
|
|
|
|
|
writer_.write_int(value, spec_);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (spec_.align_ == ALIGN_NUMERIC || spec_.flags_ != 0)
|
|
|
|
|
FMT_THROW(FormatError("invalid format specifier for char"));
|
|
|
|
|
typedef typename BasicWriter<Char>::CharPtr CharPtr;
|
|
|
|
|
Char fill = internal::CharTraits<Char>::cast(spec_.fill());
|
|
|
|
|
CharPtr out = CharPtr();
|
|
|
|
|
const unsigned CHAR_WIDTH = 1;
|
|
|
|
|
if (spec_.width_ > CHAR_WIDTH) {
|
|
|
|
|
out = writer_.grow_buffer(spec_.width_);
|
|
|
|
|
if (spec_.align_ == ALIGN_RIGHT) {
|
|
|
|
|
std::fill_n(out, spec_.width_ - CHAR_WIDTH, fill);
|
|
|
|
|
out += spec_.width_ - CHAR_WIDTH;
|
|
|
|
|
} else if (spec_.align_ == ALIGN_CENTER) {
|
|
|
|
|
out = writer_.fill_padding(out, spec_.width_,
|
|
|
|
|
internal::check(CHAR_WIDTH), fill);
|
|
|
|
|
} else {
|
|
|
|
|
std::fill_n(out + CHAR_WIDTH, spec_.width_ - CHAR_WIDTH, fill);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
out = writer_.grow_buffer(CHAR_WIDTH);
|
|
|
|
|
}
|
|
|
|
|
*out = internal::CharTraits<Char>::cast(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit_cstring(const char *value) {
|
|
|
|
|
if (spec_.type_ == 'p')
|
|
|
|
|
return write_pointer(value);
|
|
|
|
|
Arg::StringValue<char> str = {value, 0};
|
|
|
|
|
writer_.write_str(str, spec_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit_string(Arg::StringValue<char> value) {
|
|
|
|
|
writer_.write_str(value, spec_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using ArgVisitor<Impl, void>::visit_wstring;
|
|
|
|
|
|
|
|
|
|
void visit_wstring(Arg::StringValue<Char> value) {
|
|
|
|
|
writer_.write_str(value, spec_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit_pointer(const void *value) {
|
|
|
|
|
if (spec_.type_ && spec_.type_ != 'p')
|
|
|
|
|
report_unknown_type(spec_.type_, "pointer");
|
|
|
|
|
write_pointer(value);
|
|
|
|
|
// Write the content of w to os.
|
|
|
|
|
void write(std::ostream &os, fmt::Writer &w) {
|
|
|
|
|
const char *data = w.data();
|
|
|
|
|
typedef internal::MakeUnsigned<std::streamsize>::Type UnsignedStreamSize;
|
|
|
|
|
UnsignedStreamSize size = w.size();
|
|
|
|
|
UnsignedStreamSize max_size =
|
|
|
|
|
internal::to_unsigned((std::numeric_limits<std::streamsize>::max)());
|
|
|
|
|
do {
|
|
|
|
|
UnsignedStreamSize n = size <= max_size ? size : max_size;
|
|
|
|
|
os.write(data, static_cast<std::streamsize>(n));
|
|
|
|
|
data += n;
|
|
|
|
|
size -= n;
|
|
|
|
|
} while (size != 0);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// An argument formatter.
|
|
|
|
|
template <typename Char>
|
|
|
|
|
class ArgFormatter : public BasicArgFormatter<ArgFormatter<Char>, Char> {
|
|
|
|
|
private:
|
|
|
|
|
BasicFormatter<Char> &formatter_;
|
|
|
|
|
const Char *format_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ArgFormatter(BasicFormatter<Char> &f, FormatSpec &s, const Char *fmt)
|
|
|
|
|
: BasicArgFormatter<ArgFormatter<Char>, Char>(f.writer(), s),
|
|
|
|
|
formatter_(f), format_(fmt) {}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void visit_custom(Arg::CustomValue c) {
|
|
|
|
|
c.format(&formatter_, c.value, &format_);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
class PrintfArgFormatter :
|
|
|
|
|
public BasicArgFormatter<PrintfArgFormatter<Char>, Char> {
|
|
|
|
|
public ArgFormatterBase<PrintfArgFormatter<Char>, Char> {
|
|
|
|
|
|
|
|
|
|
void write_null_pointer() {
|
|
|
|
|
this->writer() << "(nil)";
|
|
|
|
|
this->spec().type_ = 0;
|
|
|
|
|
this->write("(nil)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef ArgFormatterBase<PrintfArgFormatter<Char>, Char> Base;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
|
|
|
|
: BasicArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
|
|
|
|
|
: ArgFormatterBase<PrintfArgFormatter<Char>, Char>(w, s) {}
|
|
|
|
|
|
|
|
|
|
void visit_bool(bool value) {
|
|
|
|
|
FormatSpec &fmt_spec = this->spec();
|
|
|
|
|
if (fmt_spec.type_ != 's')
|
|
|
|
|
return this->visit_any_int(value);
|
|
|
|
|
fmt_spec.type_ = 0;
|
|
|
|
|
this->write_bool(value);
|
|
|
|
|
this->write(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit_char(int value) {
|
|
|
|
@ -561,17 +428,17 @@ class PrintfArgFormatter :
|
|
|
|
|
|
|
|
|
|
void visit_cstring(const char *value) {
|
|
|
|
|
if (value)
|
|
|
|
|
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_cstring(value);
|
|
|
|
|
Base::visit_cstring(value);
|
|
|
|
|
else if (this->spec().type_ == 'p')
|
|
|
|
|
write_null_pointer();
|
|
|
|
|
else
|
|
|
|
|
this->writer() << "(null)";
|
|
|
|
|
this->write("(null)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit_pointer(const void *value) {
|
|
|
|
|
if (value)
|
|
|
|
|
BasicArgFormatter<PrintfArgFormatter<Char>, Char>::visit_pointer(value);
|
|
|
|
|
else
|
|
|
|
|
return Base::visit_pointer(value);
|
|
|
|
|
this->spec().type_ = 0;
|
|
|
|
|
write_null_pointer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -721,30 +588,28 @@ FMT_FUNC void fmt::WindowsError::init(
|
|
|
|
|
FMT_FUNC void fmt::internal::format_windows_error(
|
|
|
|
|
fmt::Writer &out, int error_code,
|
|
|
|
|
fmt::StringRef message) FMT_NOEXCEPT {
|
|
|
|
|
class String {
|
|
|
|
|
private:
|
|
|
|
|
LPWSTR str_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
String() : str_() {}
|
|
|
|
|
~String() { LocalFree(str_); }
|
|
|
|
|
LPWSTR *ptr() { return &str_; }
|
|
|
|
|
LPCWSTR c_str() const { return str_; }
|
|
|
|
|
};
|
|
|
|
|
FMT_TRY {
|
|
|
|
|
String system_message;
|
|
|
|
|
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
|
|
|
|
error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
|
reinterpret_cast<LPWSTR>(system_message.ptr()), 0, 0)) {
|
|
|
|
|
MemoryBuffer<wchar_t, INLINE_BUFFER_SIZE> buffer;
|
|
|
|
|
buffer.resize(INLINE_BUFFER_SIZE);
|
|
|
|
|
for (;;) {
|
|
|
|
|
wchar_t *system_message = &buffer[0];
|
|
|
|
|
int result = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
|
0, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
|
system_message, static_cast<uint32_t>(buffer.size()), 0);
|
|
|
|
|
if (result != 0) {
|
|
|
|
|
UTF16ToUTF8 utf8_message;
|
|
|
|
|
if (utf8_message.convert(system_message.c_str()) == ERROR_SUCCESS) {
|
|
|
|
|
if (utf8_message.convert(system_message) == ERROR_SUCCESS) {
|
|
|
|
|
out << message << ": " << utf8_message;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
|
|
|
|
break; // Can't get error message, report error code instead.
|
|
|
|
|
buffer.resize(buffer.size() * 2);
|
|
|
|
|
}
|
|
|
|
|
} FMT_CATCH(...) {}
|
|
|
|
|
format_error_code(out, error_code, message);
|
|
|
|
|
fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // FMT_USE_WINDOWS_H
|
|
|
|
@ -767,7 +632,7 @@ FMT_FUNC void fmt::internal::format_system_error(
|
|
|
|
|
buffer.resize(buffer.size() * 2);
|
|
|
|
|
}
|
|
|
|
|
} FMT_CATCH(...) {}
|
|
|
|
|
format_error_code(out, error_code, message);
|
|
|
|
|
fmt::format_error_code(out, error_code, message); // 'fmt::' is for bcc32.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
@ -786,7 +651,7 @@ void fmt::internal::ArgMap<Char>::init(const ArgList &args) {
|
|
|
|
|
return;
|
|
|
|
|
case internal::Arg::NAMED_ARG:
|
|
|
|
|
named_arg = static_cast<const NamedArg*>(args.values_[i].pointer);
|
|
|
|
|
map_.insert(Pair(named_arg->name, *named_arg));
|
|
|
|
|
map_.push_back(Pair(named_arg->name, *named_arg));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
/*nothing*/;
|
|
|
|
@ -798,7 +663,7 @@ void fmt::internal::ArgMap<Char>::init(const ArgList &args) {
|
|
|
|
|
internal::Arg::Type arg_type = args.type(i);
|
|
|
|
|
if (arg_type == internal::Arg::NAMED_ARG) {
|
|
|
|
|
named_arg = static_cast<const NamedArg*>(args.args_[i].pointer);
|
|
|
|
|
map_.insert(Pair(named_arg->name, *named_arg));
|
|
|
|
|
map_.push_back(Pair(named_arg->name, *named_arg));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for (unsigned i = ArgList::MAX_PACKED_ARGS;/*nothing*/; ++i) {
|
|
|
|
@ -807,7 +672,7 @@ void fmt::internal::ArgMap<Char>::init(const ArgList &args) {
|
|
|
|
|
return;
|
|
|
|
|
case internal::Arg::NAMED_ARG:
|
|
|
|
|
named_arg = static_cast<const NamedArg*>(args.args_[i].pointer);
|
|
|
|
|
map_.insert(Pair(named_arg->name, *named_arg));
|
|
|
|
|
map_.push_back(Pair(named_arg->name, *named_arg));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
/*nothing*/;
|
|
|
|
@ -820,70 +685,6 @@ void fmt::internal::FixedBuffer<Char>::grow(std::size_t) {
|
|
|
|
|
FMT_THROW(std::runtime_error("buffer overflow"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
template <typename StrChar>
|
|
|
|
|
void fmt::BasicWriter<Char>::write_str(
|
|
|
|
|
const Arg::StringValue<StrChar> &s, const FormatSpec &spec) {
|
|
|
|
|
// Check if StrChar is convertible to Char.
|
|
|
|
|
internal::CharTraits<Char>::convert(StrChar());
|
|
|
|
|
if (spec.type_ && spec.type_ != 's')
|
|
|
|
|
internal::report_unknown_type(spec.type_, "string");
|
|
|
|
|
const StrChar *str_value = s.value;
|
|
|
|
|
std::size_t str_size = s.size;
|
|
|
|
|
if (str_size == 0) {
|
|
|
|
|
if (!str_value) {
|
|
|
|
|
FMT_THROW(FormatError("string pointer is null"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (*str_value)
|
|
|
|
|
str_size = std::char_traits<StrChar>::length(str_value);
|
|
|
|
|
}
|
|
|
|
|
std::size_t precision = spec.precision_;
|
|
|
|
|
if (spec.precision_ >= 0 && precision < str_size)
|
|
|
|
|
str_size = spec.precision_;
|
|
|
|
|
write_str(str_value, str_size, spec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
inline Arg fmt::BasicFormatter<Char>::get_arg(
|
|
|
|
|
BasicStringRef<Char> arg_name, const char *&error) {
|
|
|
|
|
if (check_no_auto_index(error)) {
|
|
|
|
|
map_.init(args());
|
|
|
|
|
const Arg *arg = map_.find(arg_name);
|
|
|
|
|
if (arg)
|
|
|
|
|
return *arg;
|
|
|
|
|
error = "argument not found";
|
|
|
|
|
}
|
|
|
|
|
return Arg();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
inline Arg fmt::BasicFormatter<Char>::parse_arg_index(const Char *&s) {
|
|
|
|
|
const char *error = 0;
|
|
|
|
|
Arg arg = *s < '0' || *s > '9' ?
|
|
|
|
|
next_arg(error) : get_arg(parse_nonnegative_int(s), error);
|
|
|
|
|
if (error) {
|
|
|
|
|
FMT_THROW(FormatError(
|
|
|
|
|
*s != '}' && *s != ':' ? "invalid format string" : error));
|
|
|
|
|
}
|
|
|
|
|
return arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
inline Arg fmt::BasicFormatter<Char>::parse_arg_name(const Char *&s) {
|
|
|
|
|
assert(is_name_start(*s));
|
|
|
|
|
const Char *start = s;
|
|
|
|
|
Char c;
|
|
|
|
|
do {
|
|
|
|
|
c = *++s;
|
|
|
|
|
} while (is_name_start(c) || ('0' <= c && c <= '9'));
|
|
|
|
|
const char *error = 0;
|
|
|
|
|
Arg arg = get_arg(fmt::BasicStringRef<Char>(start, s - start), error);
|
|
|
|
|
if (error)
|
|
|
|
|
FMT_THROW(fmt::FormatError(error));
|
|
|
|
|
return arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC Arg fmt::internal::FormatterBase::do_get_arg(
|
|
|
|
|
unsigned arg_index, const char *&error) {
|
|
|
|
|
Arg arg = args_[arg_index];
|
|
|
|
@ -893,34 +694,13 @@ FMT_FUNC Arg fmt::internal::FormatterBase::do_get_arg(
|
|
|
|
|
break;
|
|
|
|
|
case Arg::NAMED_ARG:
|
|
|
|
|
arg = *static_cast<const internal::Arg*>(arg.pointer);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
/*nothing*/;
|
|
|
|
|
}
|
|
|
|
|
return arg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Arg fmt::internal::FormatterBase::next_arg(const char *&error) {
|
|
|
|
|
if (next_arg_index_ >= 0)
|
|
|
|
|
return do_get_arg(next_arg_index_++, error);
|
|
|
|
|
error = "cannot switch from manual to automatic argument indexing";
|
|
|
|
|
return Arg();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool fmt::internal::FormatterBase::check_no_auto_index(
|
|
|
|
|
const char *&error) {
|
|
|
|
|
if (next_arg_index_ > 0) {
|
|
|
|
|
error = "cannot switch from automatic to manual argument indexing";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
next_arg_index_ = -1;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Arg fmt::internal::FormatterBase::get_arg(
|
|
|
|
|
unsigned arg_index, const char *&error) {
|
|
|
|
|
return check_no_auto_index(error) ? do_get_arg(arg_index, error) : Arg();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
void fmt::internal::PrintfFormatter<Char>::parse_flags(
|
|
|
|
|
FormatSpec &spec, const Char *&s) {
|
|
|
|
@ -1019,7 +799,7 @@ void fmt::internal::PrintfFormatter<Char>::format(
|
|
|
|
|
if (*s == '.') {
|
|
|
|
|
++s;
|
|
|
|
|
if ('0' <= *s && *s <= '9') {
|
|
|
|
|
spec.precision_ = parse_nonnegative_int(s);
|
|
|
|
|
spec.precision_ = static_cast<int>(parse_nonnegative_int(s));
|
|
|
|
|
} else if (*s == '*') {
|
|
|
|
|
++s;
|
|
|
|
|
spec.precision_ = PrecisionHandler().visit(get_arg(s));
|
|
|
|
@ -1028,7 +808,7 @@ void fmt::internal::PrintfFormatter<Char>::format(
|
|
|
|
|
|
|
|
|
|
Arg arg = get_arg(s, arg_index);
|
|
|
|
|
if (spec.flag(HASH_FLAG) && IsZeroInt().visit(arg))
|
|
|
|
|
spec.flags_ &= ~HASH_FLAG;
|
|
|
|
|
spec.flags_ &= ~to_unsigned<int>(HASH_FLAG);
|
|
|
|
|
if (spec.fill_ == '0') {
|
|
|
|
|
if (arg.type <= Arg::LAST_NUMERIC_TYPE)
|
|
|
|
|
spec.align_ = ALIGN_NUMERIC;
|
|
|
|
@ -1065,7 +845,7 @@ void fmt::internal::PrintfFormatter<Char>::format(
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
--s;
|
|
|
|
|
ArgConverter<int>(arg, *s).visit(arg);
|
|
|
|
|
ArgConverter<void>(arg, *s).visit(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse type.
|
|
|
|
@ -1093,205 +873,17 @@ void fmt::internal::PrintfFormatter<Char>::format(
|
|
|
|
|
write(writer, start, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
const Char *fmt::BasicFormatter<Char>::format(
|
|
|
|
|
const Char *&format_str, const Arg &arg) {
|
|
|
|
|
const Char *s = format_str;
|
|
|
|
|
FormatSpec spec;
|
|
|
|
|
if (*s == ':') {
|
|
|
|
|
if (arg.type == Arg::CUSTOM) {
|
|
|
|
|
arg.custom.format(this, arg.custom.value, &s);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
++s;
|
|
|
|
|
// Parse fill and alignment.
|
|
|
|
|
if (Char c = *s) {
|
|
|
|
|
const Char *p = s + 1;
|
|
|
|
|
spec.align_ = ALIGN_DEFAULT;
|
|
|
|
|
do {
|
|
|
|
|
switch (*p) {
|
|
|
|
|
case '<':
|
|
|
|
|
spec.align_ = ALIGN_LEFT;
|
|
|
|
|
break;
|
|
|
|
|
case '>':
|
|
|
|
|
spec.align_ = ALIGN_RIGHT;
|
|
|
|
|
break;
|
|
|
|
|
case '=':
|
|
|
|
|
spec.align_ = ALIGN_NUMERIC;
|
|
|
|
|
break;
|
|
|
|
|
case '^':
|
|
|
|
|
spec.align_ = ALIGN_CENTER;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (spec.align_ != ALIGN_DEFAULT) {
|
|
|
|
|
if (p != s) {
|
|
|
|
|
if (c == '}') break;
|
|
|
|
|
if (c == '{')
|
|
|
|
|
FMT_THROW(FormatError("invalid fill character '{'"));
|
|
|
|
|
s += 2;
|
|
|
|
|
spec.fill_ = c;
|
|
|
|
|
} else ++s;
|
|
|
|
|
if (spec.align_ == ALIGN_NUMERIC)
|
|
|
|
|
require_numeric_argument(arg, '=');
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} while (--p >= s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse sign.
|
|
|
|
|
switch (*s) {
|
|
|
|
|
case '+':
|
|
|
|
|
check_sign(s, arg);
|
|
|
|
|
spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
|
|
|
|
|
break;
|
|
|
|
|
case '-':
|
|
|
|
|
check_sign(s, arg);
|
|
|
|
|
spec.flags_ |= MINUS_FLAG;
|
|
|
|
|
break;
|
|
|
|
|
case ' ':
|
|
|
|
|
check_sign(s, arg);
|
|
|
|
|
spec.flags_ |= SIGN_FLAG;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*s == '#') {
|
|
|
|
|
require_numeric_argument(arg, '#');
|
|
|
|
|
spec.flags_ |= HASH_FLAG;
|
|
|
|
|
++s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse zero flag.
|
|
|
|
|
if (*s == '0') {
|
|
|
|
|
require_numeric_argument(arg, '0');
|
|
|
|
|
spec.align_ = ALIGN_NUMERIC;
|
|
|
|
|
spec.fill_ = '0';
|
|
|
|
|
++s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse width.
|
|
|
|
|
if ('0' <= *s && *s <= '9') {
|
|
|
|
|
spec.width_ = parse_nonnegative_int(s);
|
|
|
|
|
} else if (*s == '{') {
|
|
|
|
|
++s;
|
|
|
|
|
Arg width_arg = is_name_start(*s) ?
|
|
|
|
|
parse_arg_name(s) : parse_arg_index(s);
|
|
|
|
|
if (*s++ != '}')
|
|
|
|
|
FMT_THROW(FormatError("invalid format string"));
|
|
|
|
|
ULongLong value = 0;
|
|
|
|
|
switch (width_arg.type) {
|
|
|
|
|
case Arg::INT:
|
|
|
|
|
if (width_arg.int_value < 0)
|
|
|
|
|
FMT_THROW(FormatError("negative width"));
|
|
|
|
|
value = width_arg.int_value;
|
|
|
|
|
break;
|
|
|
|
|
case Arg::UINT:
|
|
|
|
|
value = width_arg.uint_value;
|
|
|
|
|
break;
|
|
|
|
|
case Arg::LONG_LONG:
|
|
|
|
|
if (width_arg.long_long_value < 0)
|
|
|
|
|
FMT_THROW(FormatError("negative width"));
|
|
|
|
|
value = width_arg.long_long_value;
|
|
|
|
|
break;
|
|
|
|
|
case Arg::ULONG_LONG:
|
|
|
|
|
value = width_arg.ulong_long_value;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
FMT_THROW(FormatError("width is not integer"));
|
|
|
|
|
}
|
|
|
|
|
if (value > INT_MAX)
|
|
|
|
|
FMT_THROW(FormatError("number is too big"));
|
|
|
|
|
spec.width_ = static_cast<int>(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse precision.
|
|
|
|
|
if (*s == '.') {
|
|
|
|
|
++s;
|
|
|
|
|
spec.precision_ = 0;
|
|
|
|
|
if ('0' <= *s && *s <= '9') {
|
|
|
|
|
spec.precision_ = parse_nonnegative_int(s);
|
|
|
|
|
} else if (*s == '{') {
|
|
|
|
|
++s;
|
|
|
|
|
Arg precision_arg =
|
|
|
|
|
is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
|
|
|
|
|
if (*s++ != '}')
|
|
|
|
|
FMT_THROW(FormatError("invalid format string"));
|
|
|
|
|
ULongLong value = 0;
|
|
|
|
|
switch (precision_arg.type) {
|
|
|
|
|
case Arg::INT:
|
|
|
|
|
if (precision_arg.int_value < 0)
|
|
|
|
|
FMT_THROW(FormatError("negative precision"));
|
|
|
|
|
value = precision_arg.int_value;
|
|
|
|
|
break;
|
|
|
|
|
case Arg::UINT:
|
|
|
|
|
value = precision_arg.uint_value;
|
|
|
|
|
break;
|
|
|
|
|
case Arg::LONG_LONG:
|
|
|
|
|
if (precision_arg.long_long_value < 0)
|
|
|
|
|
FMT_THROW(FormatError("negative precision"));
|
|
|
|
|
value = precision_arg.long_long_value;
|
|
|
|
|
break;
|
|
|
|
|
case Arg::ULONG_LONG:
|
|
|
|
|
value = precision_arg.ulong_long_value;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
FMT_THROW(FormatError("precision is not integer"));
|
|
|
|
|
}
|
|
|
|
|
if (value > INT_MAX)
|
|
|
|
|
FMT_THROW(FormatError("number is too big"));
|
|
|
|
|
spec.precision_ = static_cast<int>(value);
|
|
|
|
|
} else {
|
|
|
|
|
FMT_THROW(FormatError("missing precision specifier"));
|
|
|
|
|
}
|
|
|
|
|
if (arg.type <= Arg::LAST_INTEGER_TYPE || arg.type == Arg::POINTER) {
|
|
|
|
|
FMT_THROW(FormatError(
|
|
|
|
|
fmt::format("precision not allowed in {} format specifier",
|
|
|
|
|
arg.type == Arg::POINTER ? "pointer" : "integer")));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse type.
|
|
|
|
|
if (*s != '}' && *s)
|
|
|
|
|
spec.type_ = static_cast<char>(*s++);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (*s++ != '}')
|
|
|
|
|
FMT_THROW(FormatError("missing '}' in format string"));
|
|
|
|
|
|
|
|
|
|
// Format argument.
|
|
|
|
|
internal::ArgFormatter<Char>(*this, spec, s - 1).visit(arg);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename Char>
|
|
|
|
|
void fmt::BasicFormatter<Char>::format(BasicCStringRef<Char> format_str) {
|
|
|
|
|
const Char *s = format_str.c_str();
|
|
|
|
|
const Char *start = s;
|
|
|
|
|
while (*s) {
|
|
|
|
|
Char c = *s++;
|
|
|
|
|
if (c != '{' && c != '}') continue;
|
|
|
|
|
if (*s == c) {
|
|
|
|
|
write(writer_, start, s);
|
|
|
|
|
start = ++s;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (c == '}')
|
|
|
|
|
FMT_THROW(FormatError("unmatched '}' in format string"));
|
|
|
|
|
write(writer_, start, s - 1);
|
|
|
|
|
Arg arg = is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
|
|
|
|
|
start = s = format(s, arg);
|
|
|
|
|
}
|
|
|
|
|
write(writer_, start, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC void fmt::report_system_error(
|
|
|
|
|
int error_code, fmt::StringRef message) FMT_NOEXCEPT {
|
|
|
|
|
report_error(internal::format_system_error, error_code, message);
|
|
|
|
|
// 'fmt::' is for bcc32.
|
|
|
|
|
fmt::report_error(internal::format_system_error, error_code, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if FMT_USE_WINDOWS_H
|
|
|
|
|
FMT_FUNC void fmt::report_windows_error(
|
|
|
|
|
int error_code, fmt::StringRef message) FMT_NOEXCEPT {
|
|
|
|
|
report_error(internal::format_windows_error, error_code, message);
|
|
|
|
|
// 'fmt::' is for bcc32.
|
|
|
|
|
fmt::report_error(internal::format_windows_error, error_code, message);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
@ -1305,10 +897,11 @@ FMT_FUNC void fmt::print(CStringRef format_str, ArgList args) {
|
|
|
|
|
print(stdout, format_str, args);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC void fmt::print(std::ostream &os, CStringRef format_str, ArgList args) {
|
|
|
|
|
FMT_FUNC void fmt::print(std::ostream &os, CStringRef format_str,
|
|
|
|
|
ArgList args) {
|
|
|
|
|
MemoryWriter w;
|
|
|
|
|
w.write(format_str, args);
|
|
|
|
|
os.write(w.data(), w.size());
|
|
|
|
|
write(os, w);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC void fmt::print_colored(Color c, CStringRef format, ArgList args) {
|
|
|
|
@ -1326,6 +919,13 @@ FMT_FUNC int fmt::fprintf(std::FILE *f, CStringRef format, ArgList args) {
|
|
|
|
|
return std::fwrite(w.data(), 1, size, f) < size ? -1 : static_cast<int>(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMT_FUNC int fmt::fprintf(std::ostream &os, CStringRef format, ArgList args) {
|
|
|
|
|
MemoryWriter w;
|
|
|
|
|
printf(w, format, args);
|
|
|
|
|
write(os, w);
|
|
|
|
|
return static_cast<int>(w.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef FMT_HEADER_ONLY
|
|
|
|
|
|
|
|
|
|
template struct fmt::internal::BasicData<void>;
|
|
|
|
@ -1334,10 +934,7 @@ template struct fmt::internal::BasicData<void>;
|
|
|
|
|
|
|
|
|
|
template void fmt::internal::FixedBuffer<char>::grow(std::size_t);
|
|
|
|
|
|
|
|
|
|
template const char *fmt::BasicFormatter<char>::format(
|
|
|
|
|
const char *&format_str, const fmt::internal::Arg &arg);
|
|
|
|
|
|
|
|
|
|
template void fmt::BasicFormatter<char>::format(CStringRef format);
|
|
|
|
|
template void fmt::internal::ArgMap<char>::init(const fmt::ArgList &args);
|
|
|
|
|
|
|
|
|
|
template void fmt::internal::PrintfFormatter<char>::format(
|
|
|
|
|
BasicWriter<char> &writer, CStringRef format);
|
|
|
|
@ -1354,11 +951,7 @@ template int fmt::internal::CharTraits<char>::format_float(
|
|
|
|
|
|
|
|
|
|
template void fmt::internal::FixedBuffer<wchar_t>::grow(std::size_t);
|
|
|
|
|
|
|
|
|
|
template const wchar_t *fmt::BasicFormatter<wchar_t>::format(
|
|
|
|
|
const wchar_t *&format_str, const fmt::internal::Arg &arg);
|
|
|
|
|
|
|
|
|
|
template void fmt::BasicFormatter<wchar_t>::format(
|
|
|
|
|
BasicCStringRef<wchar_t> format);
|
|
|
|
|
template void fmt::internal::ArgMap<wchar_t>::init(const fmt::ArgList &args);
|
|
|
|
|
|
|
|
|
|
template void fmt::internal::PrintfFormatter<wchar_t>::format(
|
|
|
|
|
BasicWriter<wchar_t> &writer, WCStringRef format);
|
|
|
|
|