From 77acf29c4db13a1dd1ae0f3a668bf8496e9ba97a Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 7 Aug 2015 14:06:22 +0300 Subject: [PATCH] Updated to latest cppformat lib --- include/spdlog/details/format.cc | 58 +++++++++++------------ include/spdlog/details/format.h | 79 +++++++++++++++++++------------- 2 files changed, 74 insertions(+), 63 deletions(-) diff --git a/include/spdlog/details/format.cc b/include/spdlog/details/format.cc index fb0ef9ea..828f0ee8 100644 --- a/include/spdlog/details/format.cc +++ b/include/spdlog/details/format.cc @@ -207,8 +207,8 @@ int safe_strerror( } public: - StrError(int error_code, char *&buffer, std::size_t buffer_size) - : error_code_(error_code), buffer_(buffer), buffer_size_(buffer_size) {} + StrError(int err_code, char *&buf, std::size_t buf_size) + : error_code_(err_code), buffer_(buf), buffer_size_(buf_size) {} int run() { strerror_r(0, 0, ""); // Suppress a warning about unused strerror_r. @@ -521,25 +521,25 @@ public: : BasicArgFormatter, Char>(w, s) {} void visit_char(int value) { - const FormatSpec &spec = this->spec(); - BasicWriter &writer = this->writer(); - if (spec.type_ && spec.type_ != 'c') - writer.write_int(value, spec); + const FormatSpec &fmt_spec = this->spec(); + BasicWriter &w = this->writer(); + if (fmt_spec.type_ && fmt_spec.type_ != 'c') + w.write_int(value, fmt_spec); typedef typename BasicWriter::CharPtr CharPtr; CharPtr out = CharPtr(); - if (spec.width_ > 1) { + if (fmt_spec.width_ > 1) { Char fill = ' '; - out = writer.grow_buffer(spec.width_); - if (spec.align_ != ALIGN_LEFT) { - std::fill_n(out, spec.width_ - 1, fill); - out += spec.width_ - 1; + out = w.grow_buffer(fmt_spec.width_); + if (fmt_spec.align_ != ALIGN_LEFT) { + std::fill_n(out, fmt_spec.width_ - 1, fill); + out += fmt_spec.width_ - 1; } else { - std::fill_n(out + 1, spec.width_ - 1, fill); + std::fill_n(out + 1, fmt_spec.width_ - 1, fill); } } else { - out = writer.grow_buffer(1); + out = w.grow_buffer(1); } *out = static_cast(value); } @@ -959,10 +959,8 @@ unsigned fmt::internal::PrintfFormatter::parse_header( template void fmt::internal::PrintfFormatter::format( - BasicWriter &writer, BasicCStringRef format_str, - const ArgList &args) { + BasicWriter &writer, BasicCStringRef format_str) { const Char *start = format_str.c_str(); - set_args(args); const Char *s = start; while (*s) { Char c = *s++; @@ -1227,7 +1225,6 @@ const Char *fmt::BasicFormatter::format( if (*s++ != '}') FMT_THROW(FormatError("missing '}' in format string")); - start_ = s; // Format argument. internal::ArgFormatter(*this, spec, s - 1).visit(arg); @@ -1235,25 +1232,24 @@ const Char *fmt::BasicFormatter::format( } template -void fmt::BasicFormatter::format( - BasicCStringRef format_str, const ArgList &args) { - const Char *s = start_ = format_str.c_str(); - set_args(args); +void fmt::BasicFormatter::format(BasicCStringRef 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; + write(writer_, start, s); + start = ++s; continue; } if (c == '}') 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); - s = format(s, arg); + start = s = format(s, arg); } - write(writer_, start_, s); + write(writer_, start, s); } FMT_FUNC void fmt::report_system_error( @@ -1310,11 +1306,10 @@ template void fmt::internal::FixedBuffer::grow(std::size_t); template const char *fmt::BasicFormatter::format( const char *&format_str, const fmt::internal::Arg &arg); -template void fmt::BasicFormatter::format( - CStringRef format, const ArgList &args); +template void fmt::BasicFormatter::format(CStringRef format); template void fmt::internal::PrintfFormatter::format( - BasicWriter &writer, CStringRef format, const ArgList &args); + BasicWriter &writer, CStringRef format); template int fmt::internal::CharTraits::format_float( char *buffer, std::size_t size, const char *format, @@ -1332,11 +1327,10 @@ template const wchar_t *fmt::BasicFormatter::format( const wchar_t *&format_str, const fmt::internal::Arg &arg); template void fmt::BasicFormatter::format( - BasicCStringRef format, const ArgList &args); + BasicCStringRef format); template void fmt::internal::PrintfFormatter::format( - BasicWriter &writer, WCStringRef format, - const ArgList &args); + BasicWriter &writer, WCStringRef format); template int fmt::internal::CharTraits::format_float( wchar_t *buffer, std::size_t size, const wchar_t *format, diff --git a/include/spdlog/details/format.h b/include/spdlog/details/format.h index aaeb011d..bf02fc72 100644 --- a/include/spdlog/details/format.h +++ b/include/spdlog/details/format.h @@ -27,7 +27,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef FMT_FORMAT_H_ #define FMT_FORMAT_H_ - #define FMT_HEADER_ONLY #include @@ -763,24 +762,23 @@ inline unsigned count_digits(uint32_t n) { // Formats a decimal unsigned integer value writing into buffer. template inline void format_decimal(Char *buffer, UInt value, unsigned num_digits) { - --num_digits; + buffer += num_digits; while (value >= 100) { // 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 // "Three Optimization Tips for C++". See speed-test for a comparison. unsigned index = (value % 100) * 2; value /= 100; - buffer[num_digits] = Data::DIGITS[index + 1]; - buffer[num_digits - 1] = Data::DIGITS[index]; - num_digits -= 2; + *--buffer = Data::DIGITS[index + 1]; + *--buffer = Data::DIGITS[index]; } if (value < 10) { - *buffer = static_cast('0' + value); + *--buffer = static_cast('0' + value); return; } unsigned index = static_cast(value * 2); - buffer[1] = Data::DIGITS[index + 1]; - buffer[0] = Data::DIGITS[index]; + *--buffer = Data::DIGITS[index + 1]; + *--buffer = Data::DIGITS[index]; } #ifndef _WIN32 @@ -924,11 +922,11 @@ private: static const T &get(); - static yes &check(fmt::ULongLong); - static no &check(...); + static yes &convert(fmt::ULongLong); + static no &convert(...); public: - enum { value = (sizeof(check(get())) == sizeof(yes)) }; + enum { value = (sizeof(convert(get())) == sizeof(yes)) }; }; #define FMT_CONVERTIBLE_TO_INT(Type) \ @@ -1127,8 +1125,8 @@ struct NamedArg : Arg { BasicStringRef name; template - NamedArg(BasicStringRef name, const T &value) - : name(name), Arg(MakeValue(value)) { + NamedArg(BasicStringRef argname, const T &value) + : name(argname), Arg(MakeValue(value)) { type = static_cast(MakeValue::type(value)); } }; @@ -1363,7 +1361,7 @@ protected: return args_; } - void set_args(const ArgList &args) { + explicit FormatterBase(const ArgList &args) { args_ = args; next_arg_index_ = 0; } @@ -1399,8 +1397,8 @@ private: unsigned parse_header(const Char *&s, FormatSpec &spec); public: - void format(BasicWriter &writer, - BasicCStringRef format_str, const ArgList &args); + explicit PrintfFormatter(const ArgList &args) : FormatterBase(args) {} + void format(BasicWriter &writer, BasicCStringRef format_str); }; } // namespace internal @@ -1409,7 +1407,6 @@ template class BasicFormatter : private internal::FormatterBase { private: BasicWriter &writer_; - const Char *start_; internal::ArgMap map_; FMT_DISALLOW_COPY_AND_ASSIGN(BasicFormatter); @@ -1427,13 +1424,14 @@ private: internal::Arg parse_arg_name(const Char *&s); public: - explicit BasicFormatter(BasicWriter &w) : writer_(w) {} + BasicFormatter(const ArgList &args, BasicWriter &w) + : FormatterBase(args), writer_(w) {} BasicWriter &writer() { return writer_; } - void format(BasicCStringRef format_str, const ArgList &args); + void format(BasicCStringRef format_str); const Char *format(const Char *&format_str, const internal::Arg &arg); }; @@ -1996,6 +1994,28 @@ private: return internal::make_ptr(&buffer_[size], n); } + // Writes an unsigned decimal integer. + template + 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 + void write_decimal(Int value) { + typename internal::IntTraits::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. CharPtr prepare_int_buffer(unsigned num_digits, const EmptySpec &, const char *prefix, unsigned prefix_size) { @@ -2123,24 +2143,27 @@ public: \endrst */ void write(BasicCStringRef format, ArgList args) { - BasicFormatter(*this).format(format, args); + BasicFormatter(args, *this).format(format); } FMT_VARIADIC_VOID(write, BasicCStringRef) BasicWriter &operator<<(int value) { - return *this << IntFormatSpec(value); + write_decimal(value); + return *this; } BasicWriter &operator<<(unsigned value) { return *this << IntFormatSpec(value); } BasicWriter &operator<<(long value) { - return *this << IntFormatSpec(value); + write_decimal(value); + return *this; } BasicWriter &operator<<(unsigned long value) { return *this << IntFormatSpec(value); } BasicWriter &operator<<(LongLong value) { - return *this << IntFormatSpec(value); + write_decimal(value); + return *this; } /** @@ -2638,12 +2661,6 @@ public: typedef BasicMemoryWriter MemoryWriter; typedef BasicMemoryWriter WMemoryWriter; -#if defined(WIN32) && defined(SPDLOG_USE_WCHAR) -#define TMemoryWriter WMemoryWriter -#else -#define TMemoryWriter MemoryWriter -#endif - /** \rst 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 void printf(BasicWriter &w, BasicCStringRef format, ArgList args) { - internal::PrintfFormatter().format(w, format, args); + internal::PrintfFormatter(args).format(w, format); } /** @@ -3158,4 +3175,4 @@ FMT_VARIADIC(int, fprintf, std::FILE *, CStringRef) # include "format.cc" #endif -#endif // FMT_FORMAT_H_ +#endif // FMT_FORMAT_H_ \ No newline at end of file