From c1db293518a17add7a1dd3dbc126ae0d0b93fd12 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 24 Jul 2014 08:53:27 -0700 Subject: [PATCH] Don't use Writer's buffer directly in formatters. Unfriend BasicFormatter. --- format.cc | 38 +++++++++++++++++++------------------- format.h | 28 ++++++++++++++++------------ 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/format.cc b/format.cc index 31a74689..f57c0dbd 100644 --- a/format.cc +++ b/format.cc @@ -425,17 +425,17 @@ class fmt::internal::ArgFormatter : : formatter_(f), writer_(f.writer()), spec_(s), format_(fmt) {} template - void visit_any_int(T value) { writer_.FormatInt(value, spec_); } + void visit_any_int(T value) { writer_.write_int(value, spec_); } template - void visit_any_double(T value) { writer_.FormatDouble(value, spec_); } + void visit_any_double(T value) { writer_.write_double(value, spec_); } void visit_char(int value) { if (spec_.type_ && spec_.type_ != 'c') { switch (spec_.type_) { // TODO: don't duplicate integer format specifiers here case 'd': case 'x': case 'X': case 'b': case 'B': case 'o': - writer_.FormatInt(value, spec_); + writer_.write_int(value, spec_); break; default: internal::ReportUnknownType(spec_.type_, "char"); @@ -474,7 +474,7 @@ class fmt::internal::ArgFormatter : fmt::internal::ReportUnknownType(spec_.type_, "pointer"); spec_.flags_ = fmt::HASH_FLAG; spec_.type_ = 'x'; - writer_.FormatInt(reinterpret_cast(value), spec_); + writer_.write_int(reinterpret_cast(value), spec_); } void visit_custom(Arg::CustomValue c) { @@ -507,7 +507,7 @@ typename fmt::BasicWriter::CharPtr template template -void fmt::BasicWriter::FormatDouble(T value, const FormatSpec &spec) { +void fmt::BasicWriter::write_double(T value, const FormatSpec &spec) { // Check type. char type = spec.type(); bool upper = false; @@ -818,11 +818,11 @@ void fmt::internal::PrintfFormatter::Format( Char c = *s++; if (c != '%') continue; if (*s == c) { - writer.buffer_.append(start, s); + write(writer, start, s); start = ++s; continue; } - writer.buffer_.append(start, s - 1); + write(writer, start, s - 1); FormatSpec spec; spec.align_ = ALIGN_RIGHT; @@ -892,20 +892,20 @@ void fmt::internal::PrintfFormatter::Format( // Format argument. switch (arg.type) { case Arg::INT: - writer.FormatInt(arg.int_value, spec); + writer.write_int(arg.int_value, spec); break; case Arg::UINT: - writer.FormatInt(arg.uint_value, spec); + writer.write_int(arg.uint_value, spec); break; case Arg::LONG_LONG: - writer.FormatInt(arg.long_long_value, spec); + writer.write_int(arg.long_long_value, spec); break; case Arg::ULONG_LONG: - writer.FormatInt(arg.ulong_long_value, spec); + writer.write_int(arg.ulong_long_value, spec); break; case Arg::CHAR: { if (spec.type_ && spec.type_ != 'c') - writer.FormatInt(arg.int_value, spec); + writer.write_int(arg.int_value, spec); typedef typename BasicWriter::CharPtr CharPtr; CharPtr out = CharPtr(); if (spec.width_ > 1) { @@ -924,10 +924,10 @@ void fmt::internal::PrintfFormatter::Format( break; } case Arg::DOUBLE: - writer.FormatDouble(arg.double_value, spec); + writer.write_double(arg.double_value, spec); break; case Arg::LONG_DOUBLE: - writer.FormatDouble(arg.long_double_value, spec); + writer.write_double(arg.long_double_value, spec); break; case Arg::STRING: writer.write_str(arg.string, spec); @@ -940,7 +940,7 @@ void fmt::internal::PrintfFormatter::Format( internal::ReportUnknownType(spec.type_, "pointer"); spec.flags_= HASH_FLAG; spec.type_ = 'x'; - writer.FormatInt(reinterpret_cast(arg.pointer_value), spec); + writer.write_int(reinterpret_cast(arg.pointer_value), spec); break; case Arg::CUSTOM: if (spec.type_) @@ -952,7 +952,7 @@ void fmt::internal::PrintfFormatter::Format( break; } } - writer.buffer_.append(start, s); + write(writer, start, s); } template @@ -1111,18 +1111,18 @@ void fmt::BasicFormatter::Format( Char c = *s++; if (c != '{' && c != '}') continue; if (*s == c) { - writer_.buffer_.append(start_, s); + write(writer_, start_, s); start_ = ++s; continue; } if (c == '}') throw FormatError("unmatched '}' in format"); report_error_.num_open_braces = 1; - writer_.buffer_.append(start_, s - 1); + write(writer_, start_, s - 1); Arg arg = ParseArgIndex(s); s = format(s, arg); } - writer_.buffer_.append(start_, s); + write(writer_, start_, s); } void fmt::ReportSystemError( diff --git a/format.h b/format.h index 9c55d780..defeecad 100644 --- a/format.h +++ b/format.h @@ -853,6 +853,12 @@ protected: const Arg &handle_arg_index(unsigned arg_index); + template + void write(BasicWriter &w, const Char *start, const Char *end) { + if (start != end) + w << BasicStringRef(start, end - start); + } + // TODO }; @@ -1331,11 +1337,11 @@ class BasicWriter { // Formats an integer. template - void FormatInt(T value, const Spec &spec); + void write_int(T value, const Spec &spec); // Formats a floating-point number (double or long double). template - void FormatDouble(T value, const FormatSpec &spec); + void write_double(T value, const FormatSpec &spec); // Writes a formatted string. template @@ -1353,7 +1359,6 @@ class BasicWriter { void operator<<(typename internal::CharTraits::UnsupportedStrType); friend class internal::ArgFormatter; - friend class BasicFormatter; friend class internal::PrintfFormatter; public: @@ -1462,7 +1467,7 @@ class BasicWriter { } BasicWriter &operator<<(double value) { - FormatDouble(value, FormatSpec()); + write_double(value, FormatSpec()); return *this; } @@ -1471,7 +1476,7 @@ class BasicWriter { (``'g'``) and writes it to the stream. */ BasicWriter &operator<<(long double value) { - FormatDouble(value, FormatSpec()); + write_double(value, FormatSpec()); return *this; } @@ -1479,29 +1484,28 @@ class BasicWriter { Writes a character to the stream. */ BasicWriter &operator<<(char value) { - *GrowBuffer(1) = value; + buffer_.push_back(value); return *this; } BasicWriter &operator<<(wchar_t value) { - *GrowBuffer(1) = internal::CharTraits::convert(value); + buffer_.push_back(internal::CharTraits::convert(value)); return *this; } /** Writes *value* to the stream. */ - BasicWriter &operator<<(const fmt::BasicStringRef value) { + BasicWriter &operator<<(fmt::BasicStringRef value) { const Char *str = value.c_str(); - std::size_t size = value.size(); - std::copy(str, str + size, GrowBuffer(size)); + buffer_.append(str, str + value.size()); return *this; } template BasicWriter &operator<<(const IntFormatSpec &spec) { internal::CharTraits::convert(FillChar()); - FormatInt(spec.value(), spec); + write_int(spec.value(), spec); return *this; } @@ -1603,7 +1607,7 @@ typename fmt::BasicWriter::CharPtr template template -void BasicWriter::FormatInt(T value, const Spec &spec) { +void BasicWriter::write_int(T value, const Spec &spec) { unsigned prefix_size = 0; typedef typename internal::IntTraits::MainType UnsignedType; UnsignedType abs_value = value;