Improve compatibility with older gcc and update tests

This commit is contained in:
Victor Zverovich 2017-01-22 11:48:02 -08:00
parent 8485027710
commit aea5d3ab00
2 changed files with 25 additions and 12 deletions

View File

@ -1656,7 +1656,10 @@ class format_spec {
}; };
template <typename Char> template <typename Char>
using fill_spec = format_spec<Char, fill_tag>; class fill_spec : public format_spec<Char, fill_tag> {
public:
explicit fill_spec(Char value) : format_spec<Char, fill_tag>(value) {}
};
using width_spec = format_spec<unsigned, width_tag>; using width_spec = format_spec<unsigned, width_tag>;
using type_spec = format_spec<char, type_tag>; using type_spec = format_spec<char, type_tag>;

View File

@ -418,25 +418,34 @@ public:
template <typename Char> template <typename Char>
friend basic_writer<Char> &operator<<( friend basic_writer<Char> &operator<<(
basic_writer<Char> &w, const ISO8601DateFormatter &d) { basic_writer<Char> &w, const ISO8601DateFormatter &d) {
w << pad(d.date_->year(), 4, '0'); using namespace fmt;
w.write(d.date_->year(), width=4, fill='0');
w.write('-'); w.write('-');
w << pad(d.date_->month(), 2, '0'); w.write(d.date_->month(), width=2, fill='0');
w.write('-'); w.write('-');
w << pad(d.date_->day(), 2, '0'); w.write(d.date_->day(), width=2, fill='0');
return w; return w;
} }
}; };
ISO8601DateFormatter iso8601(const Date &d) { return ISO8601DateFormatter(d); } ISO8601DateFormatter iso8601(const Date &d) { return ISO8601DateFormatter(d); }
template <typename... T>
std::string write_str(T... args) {
MemoryWriter writer;
using namespace fmt;
writer.write(args...);
return writer.str();
}
TEST(WriterTest, pad) { TEST(WriterTest, pad) {
using fmt::hex; using namespace fmt;
EXPECT_EQ(" cafe", (MemoryWriter() << pad(hex(0xcafe), 8)).str()); EXPECT_EQ(" cafe", write_str(0xcafe, width=8, type='x'));
EXPECT_EQ(" babe", (MemoryWriter() << pad(hex(0xbabeu), 8)).str()); EXPECT_EQ(" babe", write_str(0xbabeu, width=8, type='x'));
EXPECT_EQ(" dead", (MemoryWriter() << pad(hex(0xdeadl), 8)).str()); EXPECT_EQ(" dead", write_str(0xdeadl, width=8, type='x'));
EXPECT_EQ(" beef", (MemoryWriter() << pad(hex(0xbeeful), 8)).str()); EXPECT_EQ(" beef", write_str(0xbeeful, width=8, type='x'));
EXPECT_EQ(" dead", (MemoryWriter() << pad(hex(0xdeadll), 8)).str()); EXPECT_EQ(" dead", write_str(0xdeadll, width=8, type='x'));
EXPECT_EQ(" beef", (MemoryWriter() << pad(hex(0xbeefull), 8)).str()); EXPECT_EQ(" beef", write_str(0xbeefull, width=8, type='x'));
EXPECT_EQ(" 11", (MemoryWriter() << pad(11, 7)).str()); EXPECT_EQ(" 11", (MemoryWriter() << pad(11, 7)).str());
EXPECT_EQ(" 22", (MemoryWriter() << pad(22u, 7)).str()); EXPECT_EQ(" 22", (MemoryWriter() << pad(22u, 7)).str());
@ -447,7 +456,8 @@ TEST(WriterTest, pad) {
MemoryWriter w; MemoryWriter w;
w.clear(); w.clear();
w << pad(42, 5, '0'); using namespace fmt;
w.write(42, width=5, fill='0');
EXPECT_EQ("00042", w.str()); EXPECT_EQ("00042", w.str());
w.clear(); w.clear();
w << Date(2012, 12, 9); w << Date(2012, 12, 9);