Rename BasicFormatter to BasicWriter.

This commit is contained in:
Victor Zverovich 2013-02-02 20:29:02 -08:00
parent 687301c516
commit 03dccc3c91
3 changed files with 55 additions and 54 deletions

View File

@ -48,7 +48,6 @@ inline int SignBit(double value) { return signbit(value); }
#include <algorithm>
using std::size_t;
using fmt::BasicFormatter;
using fmt::IntFormatter;
using fmt::Formatter;
using fmt::AlignSpec;

View File

@ -310,7 +310,7 @@ IntFormatter<int, TypeSpec<'X'> > 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 <typename Char>
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<Char>(&buffer_[0], buffer_.size());
}
BasicFormatter &operator<<(int value) {
BasicWriter &operator<<(int value) {
return *this << IntFormatter<int, TypeSpec<0> >(value, TypeSpec<0>());
}
BasicFormatter &operator<<(unsigned value) {
BasicWriter &operator<<(unsigned value) {
return *this << IntFormatter<unsigned, TypeSpec<0> >(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 <typename T, typename Spec>
BasicFormatter &operator<<(const IntFormatter<T, Spec> &f);
BasicWriter &operator<<(const IntFormatter<T, Spec> &f);
void Write(const std::basic_string<char> &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 <typename Char>
Char *BasicFormatter<Char>::FillPadding(Char *buffer,
Char *BasicWriter<Char>::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<Char>::FillPadding(Char *buffer,
}
template <typename Char>
void BasicFormatter<Char>::FormatDecimal(
void BasicWriter<Char>::FormatDecimal(
Char *buffer, uint64_t value, unsigned num_digits) {
--num_digits;
while (value >= 100) {
@ -506,7 +506,7 @@ void BasicFormatter<Char>::FormatDecimal(
}
template <typename Char>
Char *BasicFormatter<Char>::PrepareFilledBuffer(
Char *BasicWriter<Char>::PrepareFilledBuffer(
unsigned size, const AlignSpec &spec, char sign) {
unsigned width = spec.width();
if (width <= size) {
@ -542,7 +542,7 @@ Char *BasicFormatter<Char>::PrepareFilledBuffer(
template <typename Char>
template <typename T>
void BasicFormatter<Char>::FormatDouble(
void BasicWriter<Char>::FormatDouble(
T value, const FormatSpec &spec, int precision) {
// Check type.
char type = spec.type();
@ -687,7 +687,7 @@ void BasicFormatter<Char>::FormatDouble(
}
template <typename Char>
char *BasicFormatter<Char>::FormatString(
char *BasicWriter<Char>::FormatString(
const char *s, std::size_t size, const FormatSpec &spec) {
char *out = 0;
if (spec.width() > size) {
@ -709,7 +709,7 @@ char *BasicFormatter<Char>::FormatString(
template <typename Char>
template <typename T, typename Spec>
BasicFormatter<Char> &BasicFormatter<Char>::operator<<(
BasicWriter<Char> &BasicWriter<Char>::operator<<(
const IntFormatter<T, Spec> &f) {
T value = f.value();
unsigned size = 0;
@ -726,9 +726,9 @@ BasicFormatter<Char> &BasicFormatter<Char>::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<Char> &BasicFormatter<Char>::operator<<(
return *this;
}
typedef BasicWriter<char> Writer;
typedef BasicWriter<wchar_t> WWriter;
// The default formatting function.
template <typename Char, typename T>
void Format(BasicFormatter<Char> &f, const FormatSpec &spec, const T &value) {
void Format(BasicWriter<Char> &f, const FormatSpec &spec, const T &value) {
std::basic_ostringstream<Char> os;
os << value;
f.Write(os.str(), spec);
@ -806,7 +809,7 @@ void Format(BasicFormatter<Char> &f, const FormatSpec &spec, const T &value) {
The buffer can be accessed using :meth:`data` or :meth:`c_str`.
\endrst
*/
class Formatter : public BasicFormatter<char> {
class Formatter : public BasicWriter<char> {
private:
enum Type {
// Numeric types should go first.
@ -927,7 +930,7 @@ class Formatter : public BasicFormatter<char> {
// Formats an argument of a custom type, such as a user-defined class.
template <typename T>
void FormatCustomArg(const void *arg, const FormatSpec &spec) {
BasicFormatter &f = *this;
BasicWriter &f = *this;
Format(f, spec, *static_cast<const T*>(arg));
}
@ -960,12 +963,12 @@ class Formatter : public BasicFormatter<char> {
};
template <typename Char>
inline std::basic_string<Char> str(const BasicFormatter<Char> &f) {
inline std::basic_string<Char> str(const BasicWriter<Char> &f) {
return f.str();
}
template <typename Char>
inline const Char *c_str(const BasicFormatter<Char> &f) { return f.c_str(); }
inline const Char *c_str(const BasicWriter<Char> &f) { return f.c_str(); }
std::string str(internal::FormatterProxy p);
const char *c_str(internal::FormatterProxy p);

View File

@ -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 <typename Char>
friend BasicFormatter<Char> &operator<<(
BasicFormatter<Char> &f, const Date &d) {
friend BasicWriter<Char> &operator<<(BasicWriter<Char> &f, const Date &d) {
return f << d.year_ << '-' << d.month_ << '-' << d.day_;
}
};
@ -880,7 +879,7 @@ TEST(FormatterTest, FormatUsingIOStreams) {
class Answer {};
template <typename Char>
void Format(fmt::BasicFormatter<Char> &f, const fmt::FormatSpec &spec, Answer) {
void Format(BasicWriter<Char> &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<char>() << pad(hex(0xcafe), 8, '0')));
EXPECT_EQ("0000cafe", str(BasicWriter<char>() << 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<char>() << oct(static_cast<short>(012))));
EXPECT_EQ("12", str(BasicFormatter<char>() << oct(012)));
EXPECT_EQ("34", str(BasicFormatter<char>() << oct(034u)));
EXPECT_EQ("56", str(BasicFormatter<char>() << oct(056l)));
EXPECT_EQ("70", str(BasicFormatter<char>() << oct(070ul)));
EXPECT_EQ("12", str(BasicWriter<char>() << oct(static_cast<short>(012))));
EXPECT_EQ("12", str(BasicWriter<char>() << oct(012)));
EXPECT_EQ("34", str(BasicWriter<char>() << oct(034u)));
EXPECT_EQ("56", str(BasicWriter<char>() << oct(056l)));
EXPECT_EQ("70", str(BasicWriter<char>() << oct(070ul)));
}
TEST(StrTest, hex) {
@ -1085,17 +1084,17 @@ TEST(StrTest, hex) {
// This shouldn't compile:
//fmt::IntFormatter<short, fmt::TypeSpec<'x'> > (*phex2)(short value) = hex;
EXPECT_EQ("cafe", str(BasicFormatter<char>() << hex(0xcafe)));
EXPECT_EQ("babe", str(BasicFormatter<char>() << hex(0xbabeu)));
EXPECT_EQ("dead", str(BasicFormatter<char>() << hex(0xdeadl)));
EXPECT_EQ("beef", str(BasicFormatter<char>() << hex(0xbeeful)));
EXPECT_EQ("cafe", str(BasicWriter<char>() << hex(0xcafe)));
EXPECT_EQ("babe", str(BasicWriter<char>() << hex(0xbabeu)));
EXPECT_EQ("dead", str(BasicWriter<char>() << hex(0xdeadl)));
EXPECT_EQ("beef", str(BasicWriter<char>() << hex(0xbeeful)));
}
TEST(StrTest, hexu) {
EXPECT_EQ("CAFE", str(BasicFormatter<char>() << hexu(0xcafe)));
EXPECT_EQ("BABE", str(BasicFormatter<char>() << hexu(0xbabeu)));
EXPECT_EQ("DEAD", str(BasicFormatter<char>() << hexu(0xdeadl)));
EXPECT_EQ("BEEF", str(BasicFormatter<char>() << hexu(0xbeeful)));
EXPECT_EQ("CAFE", str(BasicWriter<char>() << hexu(0xcafe)));
EXPECT_EQ("BABE", str(BasicWriter<char>() << hexu(0xbabeu)));
EXPECT_EQ("DEAD", str(BasicWriter<char>() << hexu(0xdeadl)));
EXPECT_EQ("BEEF", str(BasicWriter<char>() << hexu(0xbeeful)));
}
class ISO8601DateFormatter {
@ -1105,8 +1104,8 @@ public:
ISO8601DateFormatter(const Date &d) : date_(&d) {}
template <typename Char>
friend BasicFormatter<Char> &operator<<(
BasicFormatter<Char> &f, const ISO8601DateFormatter &d) {
friend BasicWriter<Char> &operator<<(
BasicWriter<Char> &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<char>() << pad(hex(0xcafe), 8)));
EXPECT_EQ(" babe", str(BasicFormatter<char>() << pad(hex(0xbabeu), 8)));
EXPECT_EQ(" dead", str(BasicFormatter<char>() << pad(hex(0xdeadl), 8)));
EXPECT_EQ(" beef", str(BasicFormatter<char>() << pad(hex(0xbeeful), 8)));
EXPECT_EQ(" cafe", str(BasicWriter<char>() << pad(hex(0xcafe), 8)));
EXPECT_EQ(" babe", str(BasicWriter<char>() << pad(hex(0xbabeu), 8)));
EXPECT_EQ(" dead", str(BasicWriter<char>() << pad(hex(0xdeadl), 8)));
EXPECT_EQ(" beef", str(BasicWriter<char>() << pad(hex(0xbeeful), 8)));
EXPECT_EQ(" 11", str(BasicFormatter<char>() << pad(11, 7)));
EXPECT_EQ(" 22", str(BasicFormatter<char>() << pad(22u, 7)));
EXPECT_EQ(" 33", str(BasicFormatter<char>() << pad(33l, 7)));
EXPECT_EQ(" 44", str(BasicFormatter<char>() << pad(44lu, 7)));
EXPECT_EQ(" 11", str(BasicWriter<char>() << pad(11, 7)));
EXPECT_EQ(" 22", str(BasicWriter<char>() << pad(22u, 7)));
EXPECT_EQ(" 33", str(BasicWriter<char>() << pad(33l, 7)));
EXPECT_EQ(" 44", str(BasicWriter<char>() << pad(44lu, 7)));
BasicFormatter<char> f;
BasicWriter<char> 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<char>() << hex(0xcafe)));
EXPECT_EQ("12", str(BasicFormatter<char>() << oct(012)));
EXPECT_EQ("cafe", str(BasicWriter<char>() << hex(0xcafe)));
EXPECT_EQ("12", str(BasicWriter<char>() << oct(012)));
}
TEST(StrTest, BasicFormatterWChar) {
EXPECT_EQ(L"cafe", str(BasicFormatter<wchar_t>() << fmt::hex(0xcafe)));
TEST(StrTest, BasicWriterWChar) {
EXPECT_EQ(L"cafe", str(BasicWriter<wchar_t>() << fmt::hex(0xcafe)));
}
template <typename T>