From 03dccc3c918c67cfbebadd3a774f59701fc1485a Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 2 Feb 2013 20:29:02 -0800 Subject: [PATCH] Rename BasicFormatter to BasicWriter. --- format.cc | 1 - format.h | 43 +++++++++++++++++---------------- format_test.cc | 65 +++++++++++++++++++++++++------------------------- 3 files changed, 55 insertions(+), 54 deletions(-) diff --git a/format.cc b/format.cc index a0ba02df..381a9adc 100644 --- a/format.cc +++ b/format.cc @@ -48,7 +48,6 @@ inline int SignBit(double value) { return signbit(value); } #include using std::size_t; -using fmt::BasicFormatter; using fmt::IntFormatter; using fmt::Formatter; using fmt::AlignSpec; diff --git a/format.h b/format.h index 54eaeb64..d78d8197 100644 --- a/format.h +++ b/format.h @@ -310,7 +310,7 @@ IntFormatter > hexu(int value); **Example**:: - std::string s = str(BasicFormatter() << pad(hex(0xcafe), 8, '0')); + std::string s = str(BasicWriter() << pad(hex(0xcafe), 8, '0')); // s == "0000cafe" \endrst @@ -352,7 +352,7 @@ DEFINE_INT_FORMATTERS(unsigned) DEFINE_INT_FORMATTERS(unsigned long) template -class BasicFormatter { +class BasicWriter { private: // Returns the number of decimal digits in n. Trailing zeros are not counted // except for n == 0 in which case CountDigits returns 1. @@ -438,26 +438,26 @@ class BasicFormatter { return std::basic_string(&buffer_[0], buffer_.size()); } - BasicFormatter &operator<<(int value) { + BasicWriter &operator<<(int value) { return *this << IntFormatter >(value, TypeSpec<0>()); } - BasicFormatter &operator<<(unsigned value) { + BasicWriter &operator<<(unsigned value) { return *this << IntFormatter >(value, TypeSpec<0>()); } - BasicFormatter &operator<<(Char value) { + BasicWriter &operator<<(Char value) { *GrowBuffer(1) = value; return *this; } - BasicFormatter &operator<<(const Char *value) { + BasicWriter &operator<<(const Char *value) { std::size_t size = std::strlen(value); std::strncpy(GrowBuffer(size), value, size); return *this; } template - BasicFormatter &operator<<(const IntFormatter &f); + BasicWriter &operator<<(const IntFormatter &f); void Write(const std::basic_string &s, const FormatSpec &spec) { FormatString(s.data(), s.size(), spec); @@ -471,7 +471,7 @@ class BasicFormatter { // Fills the padding around the content and returns the pointer to the // content area. template -Char *BasicFormatter::FillPadding(Char *buffer, +Char *BasicWriter::FillPadding(Char *buffer, unsigned total_size, std::size_t content_size, char fill) { std::size_t padding = total_size - content_size; std::size_t left_padding = padding / 2; @@ -483,7 +483,7 @@ Char *BasicFormatter::FillPadding(Char *buffer, } template -void BasicFormatter::FormatDecimal( +void BasicWriter::FormatDecimal( Char *buffer, uint64_t value, unsigned num_digits) { --num_digits; while (value >= 100) { @@ -506,7 +506,7 @@ void BasicFormatter::FormatDecimal( } template -Char *BasicFormatter::PrepareFilledBuffer( +Char *BasicWriter::PrepareFilledBuffer( unsigned size, const AlignSpec &spec, char sign) { unsigned width = spec.width(); if (width <= size) { @@ -542,7 +542,7 @@ Char *BasicFormatter::PrepareFilledBuffer( template template -void BasicFormatter::FormatDouble( +void BasicWriter::FormatDouble( T value, const FormatSpec &spec, int precision) { // Check type. char type = spec.type(); @@ -687,7 +687,7 @@ void BasicFormatter::FormatDouble( } template -char *BasicFormatter::FormatString( +char *BasicWriter::FormatString( const char *s, std::size_t size, const FormatSpec &spec) { char *out = 0; if (spec.width() > size) { @@ -709,7 +709,7 @@ char *BasicFormatter::FormatString( template template -BasicFormatter &BasicFormatter::operator<<( +BasicWriter &BasicWriter::operator<<( const IntFormatter &f) { T value = f.value(); unsigned size = 0; @@ -726,9 +726,9 @@ BasicFormatter &BasicFormatter::operator<<( } switch (f.type()) { case 0: case 'd': { - unsigned num_digits = BasicFormatter::CountDigits(abs_value); + unsigned num_digits = BasicWriter::CountDigits(abs_value); Char *p = PrepareFilledBuffer(size + num_digits, f, sign) - num_digits + 1; - BasicFormatter::FormatDecimal(p, abs_value, num_digits); + BasicWriter::FormatDecimal(p, abs_value, num_digits); break; } case 'x': case 'X': { @@ -774,9 +774,12 @@ BasicFormatter &BasicFormatter::operator<<( return *this; } +typedef BasicWriter Writer; +typedef BasicWriter WWriter; + // The default formatting function. template -void Format(BasicFormatter &f, const FormatSpec &spec, const T &value) { +void Format(BasicWriter &f, const FormatSpec &spec, const T &value) { std::basic_ostringstream os; os << value; f.Write(os.str(), spec); @@ -806,7 +809,7 @@ void Format(BasicFormatter &f, const FormatSpec &spec, const T &value) { The buffer can be accessed using :meth:`data` or :meth:`c_str`. \endrst */ -class Formatter : public BasicFormatter { +class Formatter : public BasicWriter { private: enum Type { // Numeric types should go first. @@ -927,7 +930,7 @@ class Formatter : public BasicFormatter { // Formats an argument of a custom type, such as a user-defined class. template void FormatCustomArg(const void *arg, const FormatSpec &spec) { - BasicFormatter &f = *this; + BasicWriter &f = *this; Format(f, spec, *static_cast(arg)); } @@ -960,12 +963,12 @@ class Formatter : public BasicFormatter { }; template -inline std::basic_string str(const BasicFormatter &f) { +inline std::basic_string str(const BasicWriter &f) { return f.str(); } template -inline const Char *c_str(const BasicFormatter &f) { return f.c_str(); } +inline const Char *c_str(const BasicWriter &f) { return f.c_str(); } std::string str(internal::FormatterProxy p); const char *c_str(internal::FormatterProxy p); diff --git a/format_test.cc b/format_test.cc index 6d0f55af..51f082aa 100644 --- a/format_test.cc +++ b/format_test.cc @@ -46,7 +46,7 @@ using std::size_t; using std::sprintf; using fmt::internal::Array; -using fmt::BasicFormatter; +using fmt::BasicWriter; using fmt::Formatter; using fmt::Format; using fmt::FormatError; @@ -863,8 +863,7 @@ class Date { } template - friend BasicFormatter &operator<<( - BasicFormatter &f, const Date &d) { + friend BasicWriter &operator<<(BasicWriter &f, const Date &d) { return f << d.year_ << '-' << d.month_ << '-' << d.day_; } }; @@ -880,7 +879,7 @@ TEST(FormatterTest, FormatUsingIOStreams) { class Answer {}; template -void Format(fmt::BasicFormatter &f, const fmt::FormatSpec &spec, Answer) { +void Format(BasicWriter &f, const fmt::FormatSpec &spec, Answer) { f.Write("42", spec); } @@ -921,7 +920,7 @@ TEST(FormatterTest, FormatterAppend) { TEST(FormatterTest, FormatterExamples) { using fmt::hex; - EXPECT_EQ("0000cafe", str(BasicFormatter() << pad(hex(0xcafe), 8, '0'))); + EXPECT_EQ("0000cafe", str(BasicWriter() << pad(hex(0xcafe), 8, '0'))); std::string message = str(Format("The answer is {}") << 42); EXPECT_EQ("The answer is 42", message); @@ -1071,11 +1070,11 @@ TEST(TempFormatterTest, Examples) { TEST(StrTest, oct) { using fmt::oct; - EXPECT_EQ("12", str(BasicFormatter() << oct(static_cast(012)))); - EXPECT_EQ("12", str(BasicFormatter() << oct(012))); - EXPECT_EQ("34", str(BasicFormatter() << oct(034u))); - EXPECT_EQ("56", str(BasicFormatter() << oct(056l))); - EXPECT_EQ("70", str(BasicFormatter() << oct(070ul))); + EXPECT_EQ("12", str(BasicWriter() << oct(static_cast(012)))); + EXPECT_EQ("12", str(BasicWriter() << oct(012))); + EXPECT_EQ("34", str(BasicWriter() << oct(034u))); + EXPECT_EQ("56", str(BasicWriter() << oct(056l))); + EXPECT_EQ("70", str(BasicWriter() << oct(070ul))); } TEST(StrTest, hex) { @@ -1085,17 +1084,17 @@ TEST(StrTest, hex) { // This shouldn't compile: //fmt::IntFormatter > (*phex2)(short value) = hex; - EXPECT_EQ("cafe", str(BasicFormatter() << hex(0xcafe))); - EXPECT_EQ("babe", str(BasicFormatter() << hex(0xbabeu))); - EXPECT_EQ("dead", str(BasicFormatter() << hex(0xdeadl))); - EXPECT_EQ("beef", str(BasicFormatter() << hex(0xbeeful))); + EXPECT_EQ("cafe", str(BasicWriter() << hex(0xcafe))); + EXPECT_EQ("babe", str(BasicWriter() << hex(0xbabeu))); + EXPECT_EQ("dead", str(BasicWriter() << hex(0xdeadl))); + EXPECT_EQ("beef", str(BasicWriter() << hex(0xbeeful))); } TEST(StrTest, hexu) { - EXPECT_EQ("CAFE", str(BasicFormatter() << hexu(0xcafe))); - EXPECT_EQ("BABE", str(BasicFormatter() << hexu(0xbabeu))); - EXPECT_EQ("DEAD", str(BasicFormatter() << hexu(0xdeadl))); - EXPECT_EQ("BEEF", str(BasicFormatter() << hexu(0xbeeful))); + EXPECT_EQ("CAFE", str(BasicWriter() << hexu(0xcafe))); + EXPECT_EQ("BABE", str(BasicWriter() << hexu(0xbabeu))); + EXPECT_EQ("DEAD", str(BasicWriter() << hexu(0xdeadl))); + EXPECT_EQ("BEEF", str(BasicWriter() << hexu(0xbeeful))); } class ISO8601DateFormatter { @@ -1105,8 +1104,8 @@ public: ISO8601DateFormatter(const Date &d) : date_(&d) {} template - friend BasicFormatter &operator<<( - BasicFormatter &f, const ISO8601DateFormatter &d) { + friend BasicWriter &operator<<( + BasicWriter &f, const ISO8601DateFormatter &d) { return f << pad(d.date_->year(), 4, '0') << '-' << pad(d.date_->month(), 2, '0') << '-' << pad(d.date_->day(), 2, '0'); } @@ -1116,17 +1115,17 @@ ISO8601DateFormatter iso8601(const Date &d) { return ISO8601DateFormatter(d); } TEST(StrTest, pad) { using fmt::hex; - EXPECT_EQ(" cafe", str(BasicFormatter() << pad(hex(0xcafe), 8))); - EXPECT_EQ(" babe", str(BasicFormatter() << pad(hex(0xbabeu), 8))); - EXPECT_EQ(" dead", str(BasicFormatter() << pad(hex(0xdeadl), 8))); - EXPECT_EQ(" beef", str(BasicFormatter() << pad(hex(0xbeeful), 8))); + EXPECT_EQ(" cafe", str(BasicWriter() << pad(hex(0xcafe), 8))); + EXPECT_EQ(" babe", str(BasicWriter() << pad(hex(0xbabeu), 8))); + EXPECT_EQ(" dead", str(BasicWriter() << pad(hex(0xdeadl), 8))); + EXPECT_EQ(" beef", str(BasicWriter() << pad(hex(0xbeeful), 8))); - EXPECT_EQ(" 11", str(BasicFormatter() << pad(11, 7))); - EXPECT_EQ(" 22", str(BasicFormatter() << pad(22u, 7))); - EXPECT_EQ(" 33", str(BasicFormatter() << pad(33l, 7))); - EXPECT_EQ(" 44", str(BasicFormatter() << pad(44lu, 7))); + EXPECT_EQ(" 11", str(BasicWriter() << pad(11, 7))); + EXPECT_EQ(" 22", str(BasicWriter() << pad(22u, 7))); + EXPECT_EQ(" 33", str(BasicWriter() << pad(33l, 7))); + EXPECT_EQ(" 44", str(BasicWriter() << pad(44lu, 7))); - BasicFormatter f; + BasicWriter f; f.Clear(); f << pad(42, 5, '0'); EXPECT_EQ("00042", f.str()); @@ -1141,12 +1140,12 @@ TEST(StrTest, pad) { TEST(StrTest, NoConflictWithIOManip) { using namespace std; using namespace fmt; - EXPECT_EQ("cafe", str(BasicFormatter() << hex(0xcafe))); - EXPECT_EQ("12", str(BasicFormatter() << oct(012))); + EXPECT_EQ("cafe", str(BasicWriter() << hex(0xcafe))); + EXPECT_EQ("12", str(BasicWriter() << oct(012))); } -TEST(StrTest, BasicFormatterWChar) { - EXPECT_EQ(L"cafe", str(BasicFormatter() << fmt::hex(0xcafe))); +TEST(StrTest, BasicWriterWChar) { + EXPECT_EQ(L"cafe", str(BasicWriter() << fmt::hex(0xcafe))); } template