mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 10:47:35 +02:00
More fixes for https://github.com/cppformat/cppformat/issues/50.
This commit is contained in:
60
format.cc
60
format.cc
@ -443,17 +443,17 @@ class fmt::internal::ArgFormatter :
|
|||||||
CharPtr out = CharPtr();
|
CharPtr out = CharPtr();
|
||||||
if (spec_.width_ > 1) {
|
if (spec_.width_ > 1) {
|
||||||
Char fill = static_cast<Char>(spec_.fill());
|
Char fill = static_cast<Char>(spec_.fill());
|
||||||
out = writer_.GrowBuffer(spec_.width_);
|
out = writer_.grow_buffer(spec_.width_);
|
||||||
if (spec_.align_ == fmt::ALIGN_RIGHT) {
|
if (spec_.align_ == fmt::ALIGN_RIGHT) {
|
||||||
std::fill_n(out, spec_.width_ - 1, fill);
|
std::fill_n(out, spec_.width_ - 1, fill);
|
||||||
out += spec_.width_ - 1;
|
out += spec_.width_ - 1;
|
||||||
} else if (spec_.align_ == fmt::ALIGN_CENTER) {
|
} else if (spec_.align_ == fmt::ALIGN_CENTER) {
|
||||||
out = writer_.FillPadding(out, spec_.width_, 1, fill);
|
out = writer_.fill_padding(out, spec_.width_, 1, fill);
|
||||||
} else {
|
} else {
|
||||||
std::fill_n(out + 1, spec_.width_ - 1, fill);
|
std::fill_n(out + 1, spec_.width_ - 1, fill);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
out = writer_.GrowBuffer(1);
|
out = writer_.grow_buffer(1);
|
||||||
}
|
}
|
||||||
*out = static_cast<Char>(value);
|
*out = static_cast<Char>(value);
|
||||||
}
|
}
|
||||||
@ -489,7 +489,7 @@ void fmt::internal::FormatErrorReporter<Char>::operator()(
|
|||||||
// content area.
|
// content area.
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
typename fmt::BasicWriter<Char>::CharPtr
|
typename fmt::BasicWriter<Char>::CharPtr
|
||||||
fmt::BasicWriter<Char>::FillPadding(CharPtr buffer,
|
fmt::BasicWriter<Char>::fill_padding(CharPtr buffer,
|
||||||
unsigned total_size, std::size_t content_size, wchar_t fill) {
|
unsigned total_size, std::size_t content_size, wchar_t fill) {
|
||||||
std::size_t padding = total_size - content_size;
|
std::size_t padding = total_size - content_size;
|
||||||
std::size_t left_padding = padding / 2;
|
std::size_t left_padding = padding / 2;
|
||||||
@ -631,9 +631,9 @@ void fmt::BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
|
|||||||
if (spec.align() == ALIGN_CENTER &&
|
if (spec.align() == ALIGN_CENTER &&
|
||||||
spec.width() > static_cast<unsigned>(n)) {
|
spec.width() > static_cast<unsigned>(n)) {
|
||||||
unsigned width = spec.width();
|
unsigned width = spec.width();
|
||||||
CharPtr p = GrowBuffer(width);
|
CharPtr p = grow_buffer(width);
|
||||||
std::copy(p, p + n, p + (width - n) / 2);
|
std::copy(p, p + n, p + (width - n) / 2);
|
||||||
FillPadding(p, spec.width(), n, fill);
|
fill_padding(p, spec.width(), n, fill);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (spec.fill() != ' ' || sign) {
|
if (spec.fill() != ' ' || sign) {
|
||||||
@ -642,7 +642,7 @@ void fmt::BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
|
|||||||
if (sign)
|
if (sign)
|
||||||
*(start - 1) = sign;
|
*(start - 1) = sign;
|
||||||
}
|
}
|
||||||
GrowBuffer(n);
|
grow_buffer(n);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// If n is negative we ask to increase the capacity by at least 1,
|
// If n is negative we ask to increase the capacity by at least 1,
|
||||||
@ -672,7 +672,7 @@ void fmt::BasicWriter<Char>::write_str(
|
|||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
inline const Arg
|
inline const Arg
|
||||||
&fmt::BasicFormatter<Char>::ParseArgIndex(const Char *&s) {
|
&fmt::BasicFormatter<Char>::parse_arg_index(const Char *&s) {
|
||||||
unsigned arg_index = 0;
|
unsigned arg_index = 0;
|
||||||
if (*s < '0' || *s > '9') {
|
if (*s < '0' || *s > '9') {
|
||||||
if (*s != '}' && *s != ':')
|
if (*s != '}' && *s != ':')
|
||||||
@ -696,7 +696,7 @@ inline const Arg
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void fmt::BasicFormatter<Char>::CheckSign(
|
void fmt::BasicFormatter<Char>::check_sign(
|
||||||
const Char *&s, const Arg &arg) {
|
const Char *&s, const Arg &arg) {
|
||||||
char sign = static_cast<char>(*s);
|
char sign = static_cast<char>(*s);
|
||||||
if (arg.type > Arg::LAST_NUMERIC_TYPE) {
|
if (arg.type > Arg::LAST_NUMERIC_TYPE) {
|
||||||
@ -742,7 +742,7 @@ const Arg &fmt::internal::FormatterBase::handle_arg_index(unsigned arg_index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void fmt::internal::PrintfFormatter<Char>::ParseFlags(
|
void fmt::internal::PrintfFormatter<Char>::parse_flags(
|
||||||
FormatSpec &spec, const Char *&s) {
|
FormatSpec &spec, const Char *&s) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
switch (*s++) {
|
switch (*s++) {
|
||||||
@ -769,7 +769,7 @@ void fmt::internal::PrintfFormatter<Char>::ParseFlags(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
unsigned fmt::internal::PrintfFormatter<Char>::ParseHeader(
|
unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
|
||||||
const Char *&s, FormatSpec &spec) {
|
const Char *&s, FormatSpec &spec) {
|
||||||
unsigned arg_index = UINT_MAX;
|
unsigned arg_index = UINT_MAX;
|
||||||
Char c = *s;
|
Char c = *s;
|
||||||
@ -791,7 +791,7 @@ unsigned fmt::internal::PrintfFormatter<Char>::ParseHeader(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ParseFlags(spec, s);
|
parse_flags(spec, s);
|
||||||
// Parse width.
|
// Parse width.
|
||||||
if (*s >= '0' && *s <= '9') {
|
if (*s >= '0' && *s <= '9') {
|
||||||
spec.width_ = ParseNonnegativeInt(s, error_);
|
spec.width_ = ParseNonnegativeInt(s, error_);
|
||||||
@ -803,7 +803,7 @@ unsigned fmt::internal::PrintfFormatter<Char>::ParseHeader(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void fmt::internal::PrintfFormatter<Char>::Format(
|
void fmt::internal::PrintfFormatter<Char>::format(
|
||||||
BasicWriter<Char> &writer, BasicStringRef<Char> format,
|
BasicWriter<Char> &writer, BasicStringRef<Char> format,
|
||||||
const ArgList &args) {
|
const ArgList &args) {
|
||||||
const Char *start = format.c_str();
|
const Char *start = format.c_str();
|
||||||
@ -835,7 +835,7 @@ void fmt::internal::PrintfFormatter<Char>::Format(
|
|||||||
// is OK for both cases.
|
// is OK for both cases.
|
||||||
|
|
||||||
// Parse argument index, flags and width.
|
// Parse argument index, flags and width.
|
||||||
unsigned arg_index = ParseHeader(s, spec);
|
unsigned arg_index = parse_header(s, spec);
|
||||||
|
|
||||||
// Parse precision.
|
// Parse precision.
|
||||||
if (*s == '.') {
|
if (*s == '.') {
|
||||||
@ -906,7 +906,7 @@ void fmt::internal::PrintfFormatter<Char>::Format(
|
|||||||
CharPtr out = CharPtr();
|
CharPtr out = CharPtr();
|
||||||
if (spec.width_ > 1) {
|
if (spec.width_ > 1) {
|
||||||
Char fill = ' ';
|
Char fill = ' ';
|
||||||
out = writer.GrowBuffer(spec.width_);
|
out = writer.grow_buffer(spec.width_);
|
||||||
if (spec.align_ != ALIGN_LEFT) {
|
if (spec.align_ != ALIGN_LEFT) {
|
||||||
std::fill_n(out, spec.width_ - 1, fill);
|
std::fill_n(out, spec.width_ - 1, fill);
|
||||||
out += spec.width_ - 1;
|
out += spec.width_ - 1;
|
||||||
@ -914,7 +914,7 @@ void fmt::internal::PrintfFormatter<Char>::Format(
|
|||||||
std::fill_n(out + 1, spec.width_ - 1, fill);
|
std::fill_n(out + 1, spec.width_ - 1, fill);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
out = writer.GrowBuffer(1);
|
out = writer.grow_buffer(1);
|
||||||
}
|
}
|
||||||
*out = static_cast<Char>(arg.int_value);
|
*out = static_cast<Char>(arg.int_value);
|
||||||
break;
|
break;
|
||||||
@ -1000,15 +1000,15 @@ const Char *fmt::BasicFormatter<Char>::format(
|
|||||||
// Parse sign.
|
// Parse sign.
|
||||||
switch (*s) {
|
switch (*s) {
|
||||||
case '+':
|
case '+':
|
||||||
CheckSign(s, arg);
|
check_sign(s, arg);
|
||||||
spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
|
spec.flags_ |= SIGN_FLAG | PLUS_FLAG;
|
||||||
break;
|
break;
|
||||||
case '-':
|
case '-':
|
||||||
CheckSign(s, arg);
|
check_sign(s, arg);
|
||||||
spec.flags_ |= MINUS_FLAG;
|
spec.flags_ |= MINUS_FLAG;
|
||||||
break;
|
break;
|
||||||
case ' ':
|
case ' ':
|
||||||
CheckSign(s, arg);
|
check_sign(s, arg);
|
||||||
spec.flags_ |= SIGN_FLAG;
|
spec.flags_ |= SIGN_FLAG;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -1046,7 +1046,7 @@ const Char *fmt::BasicFormatter<Char>::format(
|
|||||||
} else if (*s == '{') {
|
} else if (*s == '{') {
|
||||||
++s;
|
++s;
|
||||||
++report_error_.num_open_braces;
|
++report_error_.num_open_braces;
|
||||||
const Arg &precision_arg = ParseArgIndex(s);
|
const Arg &precision_arg = parse_arg_index(s);
|
||||||
ULongLong value = 0;
|
ULongLong value = 0;
|
||||||
switch (precision_arg.type) {
|
switch (precision_arg.type) {
|
||||||
case Arg::INT:
|
case Arg::INT:
|
||||||
@ -1098,7 +1098,7 @@ const Char *fmt::BasicFormatter<Char>::format(
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
void fmt::BasicFormatter<Char>::Format(
|
void fmt::BasicFormatter<Char>::format(
|
||||||
BasicStringRef<Char> format_str, const ArgList &args) {
|
BasicStringRef<Char> format_str, const ArgList &args) {
|
||||||
const Char *s = start_ = format_str.c_str();
|
const Char *s = start_ = format_str.c_str();
|
||||||
args_ = args;
|
args_ = args;
|
||||||
@ -1115,20 +1115,20 @@ void fmt::BasicFormatter<Char>::Format(
|
|||||||
throw FormatError("unmatched '}' in format");
|
throw FormatError("unmatched '}' in format");
|
||||||
report_error_.num_open_braces = 1;
|
report_error_.num_open_braces = 1;
|
||||||
write(writer_, start_, s - 1);
|
write(writer_, start_, s - 1);
|
||||||
Arg arg = ParseArgIndex(s);
|
Arg arg = parse_arg_index(s);
|
||||||
s = format(s, arg);
|
s = format(s, arg);
|
||||||
}
|
}
|
||||||
write(writer_, start_, s);
|
write(writer_, start_, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void fmt::ReportSystemError(
|
void fmt::report_system_error(
|
||||||
int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) {
|
int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) {
|
||||||
// FIXME: format_system_error may throw
|
// FIXME: format_system_error may throw
|
||||||
ReportError(internal::format_system_error, error_code, message);
|
ReportError(internal::format_system_error, error_code, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
void fmt::ReportWinError(
|
void fmt::report_windows_error(
|
||||||
int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) {
|
int error_code, fmt::StringRef message) FMT_NOEXCEPT(true) {
|
||||||
// FIXME: format_windows_error may throw
|
// FIXME: format_windows_error may throw
|
||||||
ReportError(internal::format_windows_error, error_code, message);
|
ReportError(internal::format_windows_error, error_code, message);
|
||||||
@ -1170,25 +1170,25 @@ void fmt::printf(StringRef format, const ArgList &args) {
|
|||||||
// Explicit instantiations for char.
|
// Explicit instantiations for char.
|
||||||
|
|
||||||
template fmt::BasicWriter<char>::CharPtr
|
template fmt::BasicWriter<char>::CharPtr
|
||||||
fmt::BasicWriter<char>::FillPadding(CharPtr buffer,
|
fmt::BasicWriter<char>::fill_padding(CharPtr buffer,
|
||||||
unsigned total_size, std::size_t content_size, wchar_t fill);
|
unsigned total_size, std::size_t content_size, wchar_t fill);
|
||||||
|
|
||||||
template void fmt::BasicFormatter<char>::Format(
|
template void fmt::BasicFormatter<char>::format(
|
||||||
BasicStringRef<char> format, const ArgList &args);
|
BasicStringRef<char> format, const ArgList &args);
|
||||||
|
|
||||||
template void fmt::internal::PrintfFormatter<char>::Format(
|
template void fmt::internal::PrintfFormatter<char>::format(
|
||||||
BasicWriter<char> &writer, BasicStringRef<char> format, const ArgList &args);
|
BasicWriter<char> &writer, BasicStringRef<char> format, const ArgList &args);
|
||||||
|
|
||||||
// Explicit instantiations for wchar_t.
|
// Explicit instantiations for wchar_t.
|
||||||
|
|
||||||
template fmt::BasicWriter<wchar_t>::CharPtr
|
template fmt::BasicWriter<wchar_t>::CharPtr
|
||||||
fmt::BasicWriter<wchar_t>::FillPadding(CharPtr buffer,
|
fmt::BasicWriter<wchar_t>::fill_padding(CharPtr buffer,
|
||||||
unsigned total_size, std::size_t content_size, wchar_t fill);
|
unsigned total_size, std::size_t content_size, wchar_t fill);
|
||||||
|
|
||||||
template void fmt::BasicFormatter<wchar_t>::Format(
|
template void fmt::BasicFormatter<wchar_t>::format(
|
||||||
BasicStringRef<wchar_t> format, const ArgList &args);
|
BasicStringRef<wchar_t> format, const ArgList &args);
|
||||||
|
|
||||||
template void fmt::internal::PrintfFormatter<wchar_t>::Format(
|
template void fmt::internal::PrintfFormatter<wchar_t>::format(
|
||||||
BasicWriter<wchar_t> &writer, BasicStringRef<wchar_t> format,
|
BasicWriter<wchar_t> &writer, BasicStringRef<wchar_t> format,
|
||||||
const ArgList &args);
|
const ArgList &args);
|
||||||
|
|
||||||
|
69
format.h
69
format.h
@ -867,14 +867,14 @@ protected:
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
class PrintfFormatter : private FormatterBase {
|
class PrintfFormatter : private FormatterBase {
|
||||||
private:
|
private:
|
||||||
void ParseFlags(FormatSpec &spec, const Char *&s);
|
void parse_flags(FormatSpec &spec, const Char *&s);
|
||||||
|
|
||||||
// Parses argument index, flags and width and returns the parsed
|
// Parses argument index, flags and width and returns the parsed
|
||||||
// argument index.
|
// argument index.
|
||||||
unsigned ParseHeader(const Char *&s, FormatSpec &spec);
|
unsigned parse_header(const Char *&s, FormatSpec &spec);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void Format(BasicWriter<Char> &writer,
|
void format(BasicWriter<Char> &writer,
|
||||||
BasicStringRef<Char> format, const ArgList &args);
|
BasicStringRef<Char> format, const ArgList &args);
|
||||||
};
|
};
|
||||||
} // namespace internal
|
} // namespace internal
|
||||||
@ -888,16 +888,16 @@ private:
|
|||||||
internal::FormatErrorReporter<Char> report_error_;
|
internal::FormatErrorReporter<Char> report_error_;
|
||||||
|
|
||||||
// Parses argument index and returns an argument with this index.
|
// Parses argument index and returns an argument with this index.
|
||||||
const internal::Arg &ParseArgIndex(const Char *&s);
|
const internal::Arg &parse_arg_index(const Char *&s);
|
||||||
|
|
||||||
void CheckSign(const Char *&s, const internal::Arg &arg);
|
void check_sign(const Char *&s, const internal::Arg &arg);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit BasicFormatter(BasicWriter<Char> &w) : writer_(w) {}
|
explicit BasicFormatter(BasicWriter<Char> &w) : writer_(w) {}
|
||||||
|
|
||||||
BasicWriter<Char> &writer() { return writer_; }
|
BasicWriter<Char> &writer() { return writer_; }
|
||||||
|
|
||||||
void Format(BasicStringRef<Char> format_str, const ArgList &args);
|
void format(BasicStringRef<Char> format_str, const ArgList &args);
|
||||||
|
|
||||||
const Char *format(const Char *format_str, const internal::Arg &arg);
|
const Char *format(const Char *format_str, const internal::Arg &arg);
|
||||||
};
|
};
|
||||||
@ -1299,33 +1299,34 @@ class BasicWriter {
|
|||||||
typedef typename internal::CharTraits<Char>::CharPtr CharPtr;
|
typedef typename internal::CharTraits<Char>::CharPtr CharPtr;
|
||||||
|
|
||||||
#if _SECURE_SCL
|
#if _SECURE_SCL
|
||||||
static Char *GetBase(CharPtr p) { return p.base(); }
|
// Returns pointer value.
|
||||||
|
static Char *get(CharPtr p) { return p.base(); }
|
||||||
#else
|
#else
|
||||||
static Char *GetBase(Char *p) { return p; }
|
static Char *get(Char *p) { return p; }
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static CharPtr FillPadding(CharPtr buffer,
|
static CharPtr fill_padding(CharPtr buffer,
|
||||||
unsigned total_size, std::size_t content_size, wchar_t fill);
|
unsigned total_size, std::size_t content_size, wchar_t fill);
|
||||||
|
|
||||||
// Grows the buffer by n characters and returns a pointer to the newly
|
// Grows the buffer by n characters and returns a pointer to the newly
|
||||||
// allocated area.
|
// allocated area.
|
||||||
CharPtr GrowBuffer(std::size_t n) {
|
CharPtr grow_buffer(std::size_t n) {
|
||||||
std::size_t size = buffer_.size();
|
std::size_t size = buffer_.size();
|
||||||
buffer_.resize(size + n);
|
buffer_.resize(size + n);
|
||||||
return internal::make_ptr(&buffer_[size], n);
|
return internal::make_ptr(&buffer_[size], n);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare a buffer for integer formatting.
|
// Prepare a buffer for integer formatting.
|
||||||
CharPtr PrepareBufferForInt(unsigned num_digits,
|
CharPtr prepare_int_buffer(unsigned num_digits,
|
||||||
const EmptySpec &, const char *prefix, unsigned prefix_size) {
|
const EmptySpec &, const char *prefix, unsigned prefix_size) {
|
||||||
unsigned size = prefix_size + num_digits;
|
unsigned size = prefix_size + num_digits;
|
||||||
CharPtr p = GrowBuffer(size);
|
CharPtr p = grow_buffer(size);
|
||||||
std::copy(prefix, prefix + prefix_size, p);
|
std::copy(prefix, prefix + prefix_size, p);
|
||||||
return p + size - 1;
|
return p + size - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Spec>
|
template <typename Spec>
|
||||||
CharPtr PrepareBufferForInt(unsigned num_digits,
|
CharPtr prepare_int_buffer(unsigned num_digits,
|
||||||
const Spec &spec, const char *prefix, unsigned prefix_size);
|
const Spec &spec, const char *prefix, unsigned prefix_size);
|
||||||
|
|
||||||
// Formats an integer.
|
// Formats an integer.
|
||||||
@ -1432,7 +1433,7 @@ class BasicWriter {
|
|||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
void write(BasicStringRef<Char> format, const ArgList &args) {
|
void write(BasicStringRef<Char> format, const ArgList &args) {
|
||||||
BasicFormatter<Char>(*this).Format(format, args);
|
BasicFormatter<Char>(*this).format(format, args);
|
||||||
}
|
}
|
||||||
FMT_VARIADIC_VOID(write, fmt::BasicStringRef<Char>)
|
FMT_VARIADIC_VOID(write, fmt::BasicStringRef<Char>)
|
||||||
|
|
||||||
@ -1519,18 +1520,18 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
|
|||||||
const StrChar *s, std::size_t size, const AlignSpec &spec) {
|
const StrChar *s, std::size_t size, const AlignSpec &spec) {
|
||||||
CharPtr out = CharPtr();
|
CharPtr out = CharPtr();
|
||||||
if (spec.width() > size) {
|
if (spec.width() > size) {
|
||||||
out = GrowBuffer(spec.width());
|
out = grow_buffer(spec.width());
|
||||||
Char fill = static_cast<Char>(spec.fill());
|
Char fill = static_cast<Char>(spec.fill());
|
||||||
if (spec.align() == ALIGN_RIGHT) {
|
if (spec.align() == ALIGN_RIGHT) {
|
||||||
std::fill_n(out, spec.width() - size, fill);
|
std::fill_n(out, spec.width() - size, fill);
|
||||||
out += spec.width() - size;
|
out += spec.width() - size;
|
||||||
} else if (spec.align() == ALIGN_CENTER) {
|
} else if (spec.align() == ALIGN_CENTER) {
|
||||||
out = FillPadding(out, spec.width(), size, fill);
|
out = fill_padding(out, spec.width(), size, fill);
|
||||||
} else {
|
} else {
|
||||||
std::fill_n(out + size, spec.width() - size, fill);
|
std::fill_n(out + size, spec.width() - size, fill);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
out = GrowBuffer(size);
|
out = grow_buffer(size);
|
||||||
}
|
}
|
||||||
std::copy(s, s + size, out);
|
std::copy(s, s + size, out);
|
||||||
return out;
|
return out;
|
||||||
@ -1539,7 +1540,7 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
template <typename Spec>
|
template <typename Spec>
|
||||||
typename fmt::BasicWriter<Char>::CharPtr
|
typename fmt::BasicWriter<Char>::CharPtr
|
||||||
fmt::BasicWriter<Char>::PrepareBufferForInt(
|
fmt::BasicWriter<Char>::prepare_int_buffer(
|
||||||
unsigned num_digits, const Spec &spec,
|
unsigned num_digits, const Spec &spec,
|
||||||
const char *prefix, unsigned prefix_size) {
|
const char *prefix, unsigned prefix_size) {
|
||||||
unsigned width = spec.width();
|
unsigned width = spec.width();
|
||||||
@ -1553,35 +1554,35 @@ typename fmt::BasicWriter<Char>::CharPtr
|
|||||||
unsigned number_size = prefix_size + spec.precision();
|
unsigned number_size = prefix_size + spec.precision();
|
||||||
AlignSpec subspec(number_size, '0', ALIGN_NUMERIC);
|
AlignSpec subspec(number_size, '0', ALIGN_NUMERIC);
|
||||||
if (number_size >= width)
|
if (number_size >= width)
|
||||||
return PrepareBufferForInt(num_digits, subspec, prefix, prefix_size);
|
return prepare_int_buffer(num_digits, subspec, prefix, prefix_size);
|
||||||
buffer_.reserve(width);
|
buffer_.reserve(width);
|
||||||
unsigned fill_size = width - number_size;
|
unsigned fill_size = width - number_size;
|
||||||
if (align != ALIGN_LEFT) {
|
if (align != ALIGN_LEFT) {
|
||||||
CharPtr p = GrowBuffer(fill_size);
|
CharPtr p = grow_buffer(fill_size);
|
||||||
std::fill(p, p + fill_size, fill);
|
std::fill(p, p + fill_size, fill);
|
||||||
}
|
}
|
||||||
CharPtr result = PrepareBufferForInt(
|
CharPtr result = prepare_int_buffer(
|
||||||
num_digits, subspec, prefix, prefix_size);
|
num_digits, subspec, prefix, prefix_size);
|
||||||
if (align == ALIGN_LEFT) {
|
if (align == ALIGN_LEFT) {
|
||||||
CharPtr p = GrowBuffer(fill_size);
|
CharPtr p = grow_buffer(fill_size);
|
||||||
std::fill(p, p + fill_size, fill);
|
std::fill(p, p + fill_size, fill);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
unsigned size = prefix_size + num_digits;
|
unsigned size = prefix_size + num_digits;
|
||||||
if (width <= size) {
|
if (width <= size) {
|
||||||
CharPtr p = GrowBuffer(size);
|
CharPtr p = grow_buffer(size);
|
||||||
std::copy(prefix, prefix + prefix_size, p);
|
std::copy(prefix, prefix + prefix_size, p);
|
||||||
return p + size - 1;
|
return p + size - 1;
|
||||||
}
|
}
|
||||||
CharPtr p = GrowBuffer(width);
|
CharPtr p = grow_buffer(width);
|
||||||
CharPtr end = p + width;
|
CharPtr end = p + width;
|
||||||
if (align == ALIGN_LEFT) {
|
if (align == ALIGN_LEFT) {
|
||||||
std::copy(prefix, prefix + prefix_size, p);
|
std::copy(prefix, prefix + prefix_size, p);
|
||||||
p += size;
|
p += size;
|
||||||
std::fill(p, end, fill);
|
std::fill(p, end, fill);
|
||||||
} else if (align == ALIGN_CENTER) {
|
} else if (align == ALIGN_CENTER) {
|
||||||
p = FillPadding(p, width, size, fill);
|
p = fill_padding(p, width, size, fill);
|
||||||
std::copy(prefix, prefix + prefix_size, p);
|
std::copy(prefix, prefix + prefix_size, p);
|
||||||
p += size;
|
p += size;
|
||||||
} else {
|
} else {
|
||||||
@ -1617,9 +1618,9 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
|
|||||||
switch (spec.type()) {
|
switch (spec.type()) {
|
||||||
case 0: case 'd': {
|
case 0: case 'd': {
|
||||||
unsigned num_digits = internal::count_digits(abs_value);
|
unsigned num_digits = internal::count_digits(abs_value);
|
||||||
CharPtr p = PrepareBufferForInt(
|
CharPtr p = prepare_int_buffer(
|
||||||
num_digits, spec, prefix, prefix_size) + 1 - num_digits;
|
num_digits, spec, prefix, prefix_size) + 1 - num_digits;
|
||||||
internal::format_decimal(GetBase(p), abs_value, num_digits);
|
internal::format_decimal(get(p), abs_value, num_digits);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'x': case 'X': {
|
case 'x': case 'X': {
|
||||||
@ -1632,7 +1633,7 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
|
|||||||
do {
|
do {
|
||||||
++num_digits;
|
++num_digits;
|
||||||
} while ((n >>= 4) != 0);
|
} while ((n >>= 4) != 0);
|
||||||
Char *p = GetBase(PrepareBufferForInt(
|
Char *p = get(prepare_int_buffer(
|
||||||
num_digits, spec, prefix, prefix_size));
|
num_digits, spec, prefix, prefix_size));
|
||||||
n = abs_value;
|
n = abs_value;
|
||||||
const char *digits = spec.type() == 'x' ?
|
const char *digits = spec.type() == 'x' ?
|
||||||
@ -1652,8 +1653,7 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
|
|||||||
do {
|
do {
|
||||||
++num_digits;
|
++num_digits;
|
||||||
} while ((n >>= 1) != 0);
|
} while ((n >>= 1) != 0);
|
||||||
Char *p = GetBase(PrepareBufferForInt(
|
Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
|
||||||
num_digits, spec, prefix, prefix_size));
|
|
||||||
n = abs_value;
|
n = abs_value;
|
||||||
do {
|
do {
|
||||||
*p-- = '0' + (n & 1);
|
*p-- = '0' + (n & 1);
|
||||||
@ -1668,8 +1668,7 @@ void BasicWriter<Char>::write_int(T value, const Spec &spec) {
|
|||||||
do {
|
do {
|
||||||
++num_digits;
|
++num_digits;
|
||||||
} while ((n >>= 3) != 0);
|
} while ((n >>= 3) != 0);
|
||||||
Char *p = GetBase(PrepareBufferForInt(
|
Char *p = get(prepare_int_buffer(num_digits, spec, prefix, prefix_size));
|
||||||
num_digits, spec, prefix, prefix_size));
|
|
||||||
n = abs_value;
|
n = abs_value;
|
||||||
do {
|
do {
|
||||||
*p-- = '0' + (n & 7);
|
*p-- = '0' + (n & 7);
|
||||||
@ -1693,7 +1692,7 @@ void format(BasicFormatter<Char> &f, const Char *format_str, const T &value) {
|
|||||||
|
|
||||||
// Reports a system error without throwing an exception.
|
// Reports a system error without throwing an exception.
|
||||||
// Can be used to report errors from destructors.
|
// Can be used to report errors from destructors.
|
||||||
void ReportSystemError(int error_code, StringRef message) FMT_NOEXCEPT(true);
|
void report_system_error(int error_code, StringRef message) FMT_NOEXCEPT(true);
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
@ -1722,7 +1721,7 @@ class WindowsError : public SystemError {
|
|||||||
|
|
||||||
// Reports a Windows error without throwing an exception.
|
// Reports a Windows error without throwing an exception.
|
||||||
// Can be used to report errors from destructors.
|
// Can be used to report errors from destructors.
|
||||||
void ReportWinError(int error_code, StringRef message) FMT_NOEXCEPT(true);
|
void report_windows_error(int error_code, StringRef message) FMT_NOEXCEPT(true);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -1803,7 +1802,7 @@ void print(std::ostream &os, StringRef format, const ArgList &args);
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
void printf(BasicWriter<Char> &w,
|
void printf(BasicWriter<Char> &w,
|
||||||
BasicStringRef<Char> format, const ArgList &args) {
|
BasicStringRef<Char> format, const ArgList &args) {
|
||||||
internal::PrintfFormatter<Char>().Format(w, format, args);
|
internal::PrintfFormatter<Char>().format(w, format, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline std::string sprintf(StringRef format, const ArgList &args) {
|
inline std::string sprintf(StringRef format, const ArgList &args) {
|
||||||
|
4
posix.cc
4
posix.cc
@ -67,7 +67,7 @@ inline std::size_t convert_rwcount(std::size_t count) { return count; }
|
|||||||
|
|
||||||
fmt::BufferedFile::~BufferedFile() FMT_NOEXCEPT(true) {
|
fmt::BufferedFile::~BufferedFile() FMT_NOEXCEPT(true) {
|
||||||
if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
|
if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
|
||||||
fmt::ReportSystemError(errno, "cannot close file");
|
fmt::report_system_error(errno, "cannot close file");
|
||||||
}
|
}
|
||||||
|
|
||||||
void fmt::BufferedFile::close() {
|
void fmt::BufferedFile::close() {
|
||||||
@ -102,7 +102,7 @@ fmt::File::~File() FMT_NOEXCEPT(true) {
|
|||||||
// Don't retry close in case of EINTR!
|
// Don't retry close in case of EINTR!
|
||||||
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
|
// See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
|
||||||
if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0)
|
if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0)
|
||||||
fmt::ReportSystemError(errno, "cannot close file");
|
fmt::report_system_error(errno, "cannot close file");
|
||||||
}
|
}
|
||||||
|
|
||||||
void fmt::File::close() {
|
void fmt::File::close() {
|
||||||
|
@ -448,12 +448,6 @@ TEST(UtilTest, StrError) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(UtilTest, SystemError) {
|
|
||||||
fmt::SystemError e(EDOM, "test");
|
|
||||||
EXPECT_EQ(fmt::format("test: {}", GetSystemErrorMessage(EDOM)), e.what());
|
|
||||||
EXPECT_EQ(EDOM, e.error_code());
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef void (*FormatErrorMessage)(
|
typedef void (*FormatErrorMessage)(
|
||||||
fmt::Writer &out, int error_code, StringRef message);
|
fmt::Writer &out, int error_code, StringRef message);
|
||||||
|
|
||||||
@ -478,7 +472,10 @@ TEST(UtilTest, FormatSystemError) {
|
|||||||
GetSystemErrorMessage(EDOM)), message.str());
|
GetSystemErrorMessage(EDOM)), message.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(UtilTest, ThrowSystemError) {
|
TEST(UtilTest, SystemError) {
|
||||||
|
fmt::SystemError e(EDOM, "test");
|
||||||
|
EXPECT_EQ(fmt::format("test: {}", GetSystemErrorMessage(EDOM)), e.what());
|
||||||
|
EXPECT_EQ(EDOM, e.error_code());
|
||||||
CheckThrowError<fmt::SystemError>(EDOM, fmt::internal::format_system_error);
|
CheckThrowError<fmt::SystemError>(EDOM, fmt::internal::format_system_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -486,12 +483,12 @@ TEST(UtilTest, ReportSystemError) {
|
|||||||
fmt::Writer out;
|
fmt::Writer out;
|
||||||
fmt::internal::format_system_error(out, EDOM, "test error");
|
fmt::internal::format_system_error(out, EDOM, "test error");
|
||||||
out << '\n';
|
out << '\n';
|
||||||
EXPECT_WRITE(stderr, fmt::ReportSystemError(EDOM, "test error"), out.str());
|
EXPECT_WRITE(stderr, fmt::report_system_error(EDOM, "test error"), out.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
TEST(UtilTest, FormatWinErrorMessage) {
|
TEST(UtilTest, FormatWindowsError) {
|
||||||
LPWSTR message = 0;
|
LPWSTR message = 0;
|
||||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, 0,
|
||||||
@ -506,17 +503,17 @@ TEST(UtilTest, FormatWinErrorMessage) {
|
|||||||
actual_message.str());
|
actual_message.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(UtilTest, ThrowWinError) {
|
TEST(UtilTest, WindowsError) {
|
||||||
CheckThrowError<fmt::WindowsError>(
|
CheckThrowError<fmt::WindowsError>(
|
||||||
ERROR_FILE_EXISTS, fmt::internal::format_windows_error);
|
ERROR_FILE_EXISTS, fmt::internal::format_windows_error);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(UtilTest, ReportWinError) {
|
TEST(UtilTest, ReportWindowsError) {
|
||||||
fmt::Writer out;
|
fmt::Writer out;
|
||||||
fmt::internal::format_windows_error(out, ERROR_FILE_EXISTS, "test error");
|
fmt::internal::format_windows_error(out, ERROR_FILE_EXISTS, "test error");
|
||||||
out << '\n';
|
out << '\n';
|
||||||
EXPECT_WRITE(stderr,
|
EXPECT_WRITE(stderr,
|
||||||
fmt::ReportWinError(ERROR_FILE_EXISTS, "test error"), out.str());
|
fmt::report_windows_error(ERROR_FILE_EXISTS, "test error"), out.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // _WIN32
|
#endif // _WIN32
|
||||||
|
Reference in New Issue
Block a user