Updated to latest cppformat lib

This commit is contained in:
gabime 2015-08-07 14:06:22 +03:00
parent cadd181d8d
commit 77acf29c4d
2 changed files with 74 additions and 63 deletions

View File

@ -207,8 +207,8 @@ int safe_strerror(
} }
public: public:
StrError(int error_code, char *&buffer, std::size_t buffer_size) StrError(int err_code, char *&buf, std::size_t buf_size)
: error_code_(error_code), buffer_(buffer), buffer_size_(buffer_size) {} : error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {}
int run() { int run() {
strerror_r(0, 0, ""); // Suppress a warning about unused strerror_r. strerror_r(0, 0, ""); // Suppress a warning about unused strerror_r.
@ -521,25 +521,25 @@ public:
: BasicArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {} : BasicArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
void visit_char(int value) { void visit_char(int value) {
const FormatSpec &spec = this->spec(); const FormatSpec &fmt_spec = this->spec();
BasicWriter<Char> &writer = this->writer(); BasicWriter<Char> &w = this->writer();
if (spec.type_ && spec.type_ != 'c') if (fmt_spec.type_ && fmt_spec.type_ != 'c')
writer.write_int(value, spec); w.write_int(value, fmt_spec);
typedef typename BasicWriter<Char>::CharPtr CharPtr; typedef typename BasicWriter<Char>::CharPtr CharPtr;
CharPtr out = CharPtr(); CharPtr out = CharPtr();
if (spec.width_ > 1) { if (fmt_spec.width_ > 1) {
Char fill = ' '; Char fill = ' ';
out = writer.grow_buffer(spec.width_); out = w.grow_buffer(fmt_spec.width_);
if (spec.align_ != ALIGN_LEFT) { if (fmt_spec.align_ != ALIGN_LEFT) {
std::fill_n(out, spec.width_ - 1, fill); std::fill_n(out, fmt_spec.width_ - 1, fill);
out += spec.width_ - 1; out += fmt_spec.width_ - 1;
} }
else { else {
std::fill_n(out + 1, spec.width_ - 1, fill); std::fill_n(out + 1, fmt_spec.width_ - 1, fill);
} }
} }
else { else {
out = writer.grow_buffer(1); out = w.grow_buffer(1);
} }
*out = static_cast<Char>(value); *out = static_cast<Char>(value);
} }
@ -959,10 +959,8 @@ unsigned fmt::internal::PrintfFormatter<Char>::parse_header(
template <typename Char> template <typename Char>
void fmt::internal::PrintfFormatter<Char>::format( void fmt::internal::PrintfFormatter<Char>::format(
BasicWriter<Char> &writer, BasicCStringRef<Char> format_str, BasicWriter<Char> &writer, BasicCStringRef<Char> format_str) {
const ArgList &args) {
const Char *start = format_str.c_str(); const Char *start = format_str.c_str();
set_args(args);
const Char *s = start; const Char *s = start;
while (*s) { while (*s) {
Char c = *s++; Char c = *s++;
@ -1227,7 +1225,6 @@ const Char *fmt::BasicFormatter<Char>::format(
if (*s++ != '}') if (*s++ != '}')
FMT_THROW(FormatError("missing '}' in format string")); FMT_THROW(FormatError("missing '}' in format string"));
start_ = s;
// Format argument. // Format argument.
internal::ArgFormatter<Char>(*this, spec, s - 1).visit(arg); internal::ArgFormatter<Char>(*this, spec, s - 1).visit(arg);
@ -1235,25 +1232,24 @@ const Char *fmt::BasicFormatter<Char>::format(
} }
template <typename Char> template <typename Char>
void fmt::BasicFormatter<Char>::format( void fmt::BasicFormatter<Char>::format(BasicCStringRef<Char> format_str) {
BasicCStringRef<Char> format_str, const ArgList &args) { const Char *s = format_str.c_str();
const Char *s = start_ = format_str.c_str(); const Char *start = s;
set_args(args);
while (*s) { while (*s) {
Char c = *s++; Char c = *s++;
if (c != '{' && c != '}') continue; if (c != '{' && c != '}') continue;
if (*s == c) { if (*s == c) {
write(writer_, start_, s); write(writer_, start, s);
start_ = ++s; start = ++s;
continue; continue;
} }
if (c == '}') if (c == '}')
FMT_THROW(FormatError("unmatched '}' in format string")); FMT_THROW(FormatError("unmatched '}' in format string"));
write(writer_, start_, s - 1); write(writer_, start, s - 1);
Arg arg = is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s); Arg arg = is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
s = format(s, arg); start = s = format(s, arg);
} }
write(writer_, start_, s); write(writer_, start, s);
} }
FMT_FUNC void fmt::report_system_error( FMT_FUNC void fmt::report_system_error(
@ -1310,11 +1306,10 @@ template void fmt::internal::FixedBuffer<char>::grow(std::size_t);
template const char *fmt::BasicFormatter<char>::format( template const char *fmt::BasicFormatter<char>::format(
const char *&format_str, const fmt::internal::Arg &arg); const char *&format_str, const fmt::internal::Arg &arg);
template void fmt::BasicFormatter<char>::format( template void fmt::BasicFormatter<char>::format(CStringRef format);
CStringRef format, const ArgList &args);
template void fmt::internal::PrintfFormatter<char>::format( template void fmt::internal::PrintfFormatter<char>::format(
BasicWriter<char> &writer, CStringRef format, const ArgList &args); BasicWriter<char> &writer, CStringRef format);
template int fmt::internal::CharTraits<char>::format_float( template int fmt::internal::CharTraits<char>::format_float(
char *buffer, std::size_t size, const char *format, char *buffer, std::size_t size, const char *format,
@ -1332,11 +1327,10 @@ template const wchar_t *fmt::BasicFormatter<wchar_t>::format(
const wchar_t *&format_str, const fmt::internal::Arg &arg); const wchar_t *&format_str, const fmt::internal::Arg &arg);
template void fmt::BasicFormatter<wchar_t>::format( template void fmt::BasicFormatter<wchar_t>::format(
BasicCStringRef<wchar_t> format, const ArgList &args); BasicCStringRef<wchar_t> format);
template void fmt::internal::PrintfFormatter<wchar_t>::format( template void fmt::internal::PrintfFormatter<wchar_t>::format(
BasicWriter<wchar_t> &writer, WCStringRef format, BasicWriter<wchar_t> &writer, WCStringRef format);
const ArgList &args);
template int fmt::internal::CharTraits<wchar_t>::format_float( template int fmt::internal::CharTraits<wchar_t>::format_float(
wchar_t *buffer, std::size_t size, const wchar_t *format, wchar_t *buffer, std::size_t size, const wchar_t *format,

View File

@ -27,7 +27,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef FMT_FORMAT_H_ #ifndef FMT_FORMAT_H_
#define FMT_FORMAT_H_ #define FMT_FORMAT_H_
#define FMT_HEADER_ONLY #define FMT_HEADER_ONLY
#include <stdint.h> #include <stdint.h>
@ -763,24 +762,23 @@ inline unsigned count_digits(uint32_t n) {
// Formats a decimal unsigned integer value writing into buffer. // Formats a decimal unsigned integer value writing into buffer.
template <typename UInt, typename Char> template <typename UInt, typename Char>
inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) { inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) {
--num_digits; buffer += num_digits;
while (value >= 100) { while (value >= 100) {
// Integer division is slow so do it for a group of two digits instead // Integer division is slow so do it for a group of two digits instead
// of for every digit. The idea comes from the talk by Alexandrescu // of for every digit. The idea comes from the talk by Alexandrescu
// "Three Optimization Tips for C++". See speed-test for a comparison. // "Three Optimization Tips for C++". See speed-test for a comparison.
unsigned index = (value % 100) * 2; unsigned index = (value % 100) * 2;
value /= 100; value /= 100;
buffer[num_digits] = Data::DIGITS[index + 1]; *--buffer = Data::DIGITS[index + 1];
buffer[num_digits - 1] = Data::DIGITS[index]; *--buffer = Data::DIGITS[index];
num_digits -= 2;
} }
if (value < 10) { if (value < 10) {
*buffer = static_cast<char>('0' + value); *--buffer = static_cast<char>('0' + value);
return; return;
} }
unsigned index = static_cast<unsigned>(value * 2); unsigned index = static_cast<unsigned>(value * 2);
buffer[1] = Data::DIGITS[index + 1]; *--buffer = Data::DIGITS[index + 1];
buffer[0] = Data::DIGITS[index]; *--buffer = Data::DIGITS[index];
} }
#ifndef _WIN32 #ifndef _WIN32
@ -924,11 +922,11 @@ private:
static const T &get(); static const T &get();
static yes &check(fmt::ULongLong); static yes &convert(fmt::ULongLong);
static no &check(...); static no &convert(...);
public: public:
enum { value = (sizeof(check(get())) == sizeof(yes)) }; enum { value = (sizeof(convert(get())) == sizeof(yes)) };
}; };
#define FMT_CONVERTIBLE_TO_INT(Type) \ #define FMT_CONVERTIBLE_TO_INT(Type) \
@ -1127,8 +1125,8 @@ struct NamedArg : Arg {
BasicStringRef<Char> name; BasicStringRef<Char> name;
template <typename T> template <typename T>
NamedArg(BasicStringRef<Char> name, const T &value) NamedArg(BasicStringRef<Char> argname, const T &value)
: name(name), Arg(MakeValue<Char>(value)) { : name(argname), Arg(MakeValue<Char>(value)) {
type = static_cast<internal::Arg::Type>(MakeValue<Char>::type(value)); type = static_cast<internal::Arg::Type>(MakeValue<Char>::type(value));
} }
}; };
@ -1363,7 +1361,7 @@ protected:
return args_; return args_;
} }
void set_args(const ArgList &args) { explicit FormatterBase(const ArgList &args) {
args_ = args; args_ = args;
next_arg_index_ = 0; next_arg_index_ = 0;
} }
@ -1399,8 +1397,8 @@ private:
unsigned parse_header(const Char *&s, FormatSpec &spec); unsigned parse_header(const Char *&s, FormatSpec &spec);
public: public:
void format(BasicWriter<Char> &writer, explicit PrintfFormatter(const ArgList &args) : FormatterBase(args) {}
BasicCStringRef<Char> format_str, const ArgList &args); void format(BasicWriter<Char> &writer, BasicCStringRef<Char> format_str);
}; };
} // namespace internal } // namespace internal
@ -1409,7 +1407,6 @@ template <typename Char>
class BasicFormatter : private internal::FormatterBase { class BasicFormatter : private internal::FormatterBase {
private: private:
BasicWriter<Char> &writer_; BasicWriter<Char> &writer_;
const Char *start_;
internal::ArgMap<Char> map_; internal::ArgMap<Char> map_;
FMT_DISALLOW_COPY_AND_ASSIGN(BasicFormatter); FMT_DISALLOW_COPY_AND_ASSIGN(BasicFormatter);
@ -1427,13 +1424,14 @@ private:
internal::Arg parse_arg_name(const Char *&s); internal::Arg parse_arg_name(const Char *&s);
public: public:
explicit BasicFormatter(BasicWriter<Char> &w) : writer_(w) {} BasicFormatter(const ArgList &args, BasicWriter<Char> &w)
: FormatterBase(args), writer_(w) {}
BasicWriter<Char> &writer() { BasicWriter<Char> &writer() {
return writer_; return writer_;
} }
void format(BasicCStringRef<Char> format_str, const ArgList &args); void format(BasicCStringRef<Char> format_str);
const Char *format(const Char *&format_str, const internal::Arg &arg); const Char *format(const Char *&format_str, const internal::Arg &arg);
}; };
@ -1996,6 +1994,28 @@ private:
return internal::make_ptr(&buffer_[size], n); return internal::make_ptr(&buffer_[size], n);
} }
// Writes an unsigned decimal integer.
template <typename UInt>
Char *write_unsigned_decimal(UInt value, unsigned prefix_size = 0) {
unsigned num_digits = internal::count_digits(value);
Char *ptr = get(grow_buffer(prefix_size + num_digits));
internal::format_decimal(ptr + prefix_size, value, num_digits);
return ptr;
}
// Writes a decimal integer.
template <typename Int>
void write_decimal(Int value) {
typename internal::IntTraits<Int>::MainType abs_value = value;
if (internal::is_negative(value)) {
abs_value = 0 - abs_value;
*write_unsigned_decimal(abs_value, 1) = '-';
}
else {
write_unsigned_decimal(abs_value, 0);
}
}
// Prepare a buffer for integer formatting. // Prepare a buffer for integer formatting.
CharPtr prepare_int_buffer(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) {
@ -2123,24 +2143,27 @@ public:
\endrst \endrst
*/ */
void write(BasicCStringRef<Char> format, ArgList args) { void write(BasicCStringRef<Char> format, ArgList args) {
BasicFormatter<Char>(*this).format(format, args); BasicFormatter<Char>(args, *this).format(format);
} }
FMT_VARIADIC_VOID(write, BasicCStringRef<Char>) FMT_VARIADIC_VOID(write, BasicCStringRef<Char>)
BasicWriter &operator<<(int value) { BasicWriter &operator<<(int value) {
return *this << IntFormatSpec<int>(value); write_decimal(value);
return *this;
} }
BasicWriter &operator<<(unsigned value) { BasicWriter &operator<<(unsigned value) {
return *this << IntFormatSpec<unsigned>(value); return *this << IntFormatSpec<unsigned>(value);
} }
BasicWriter &operator<<(long value) { BasicWriter &operator<<(long value) {
return *this << IntFormatSpec<long>(value); write_decimal(value);
return *this;
} }
BasicWriter &operator<<(unsigned long value) { BasicWriter &operator<<(unsigned long value) {
return *this << IntFormatSpec<unsigned long>(value); return *this << IntFormatSpec<unsigned long>(value);
} }
BasicWriter &operator<<(LongLong value) { BasicWriter &operator<<(LongLong value) {
return *this << IntFormatSpec<LongLong>(value); write_decimal(value);
return *this;
} }
/** /**
@ -2638,12 +2661,6 @@ public:
typedef BasicMemoryWriter<char> MemoryWriter; typedef BasicMemoryWriter<char> MemoryWriter;
typedef BasicMemoryWriter<wchar_t> WMemoryWriter; typedef BasicMemoryWriter<wchar_t> WMemoryWriter;
#if defined(WIN32) && defined(SPDLOG_USE_WCHAR)
#define TMemoryWriter WMemoryWriter
#else
#define TMemoryWriter MemoryWriter
#endif
/** /**
\rst \rst
This class template provides operations for formatting and writing data This class template provides operations for formatting and writing data
@ -2823,7 +2840,7 @@ void print(std::ostream &os, CStringRef format_str, ArgList args);
template <typename Char> template <typename Char>
void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args) { void printf(BasicWriter<Char> &w, BasicCStringRef<Char> format, ArgList args) {
internal::PrintfFormatter<Char>().format(w, format, args); internal::PrintfFormatter<Char>(args).format(w, format);
} }
/** /**
@ -3158,4 +3175,4 @@ FMT_VARIADIC(int, fprintf, std::FILE *, CStringRef)
# include "format.cc" # include "format.cc"
#endif #endif
#endif // FMT_FORMAT_H_ #endif // FMT_FORMAT_H_