From 4863730eca8041b4d247cb0c666f55984f39d288 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 22 Jan 2017 19:11:47 -0800 Subject: [PATCH] Remove pad --- fmt/format.h | 74 +++++---------------------------------------- test/format-test.cc | 42 ++++++++++++++----------- 2 files changed, 31 insertions(+), 85 deletions(-) diff --git a/fmt/format.h b/fmt/format.h index 2a411d94..f26e5cb0 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -1741,7 +1741,8 @@ struct AlignTypeSpec : AlignSpec { // A full format specifier. class FormatSpec : public AlignSpec { private: - void set(fill_spec fill) { + template + void set(fill_spec fill) { fill_ = fill.value(); } @@ -1829,24 +1830,6 @@ IntFormatSpec > hex(int value); */ IntFormatSpec > hexu(int value); -/** - \rst - Returns an integer format specifier to pad the formatted argument with the - fill character to the specified width using the default (right) numeric - alignment. - - **Example**:: - - MemoryWriter out; - out << pad(hex(0xcafe), 8, '0'); - // out.str() == "0000cafe" - - \endrst - */ -template -IntFormatSpec, Char> pad( - int value, unsigned width, Char fill = ' '); - #define FMT_DEFINE_INT_FORMATTERS(TYPE) \ inline IntFormatSpec > bin(TYPE value) { \ return IntFormatSpec >(value, TypeSpec<'b'>()); \ @@ -1862,31 +1845,6 @@ inline IntFormatSpec > hex(TYPE value) { \ \ inline IntFormatSpec > hexu(TYPE value) { \ return IntFormatSpec >(value, TypeSpec<'X'>()); \ -} \ - \ -template \ -inline IntFormatSpec > pad( \ - IntFormatSpec > f, unsigned width) { \ - return IntFormatSpec >( \ - f.value(), AlignTypeSpec(width, ' ')); \ -} \ - \ -/* For compatibility with older compilers we provide two overloads for pad, */ \ -/* one that takes a fill character and one that doesn't. In the future this */ \ -/* can be replaced with one overload making the template argument Char */ \ -/* default to char (C++11). */ \ -template \ -inline IntFormatSpec, Char> pad( \ - IntFormatSpec, Char> f, \ - unsigned width, Char fill) { \ - return IntFormatSpec, Char>( \ - f.value(), AlignTypeSpec(width, fill)); \ -} \ - \ -inline IntFormatSpec > pad( \ - TYPE value, unsigned width) { \ - return IntFormatSpec >( \ - value, AlignTypeSpec<0>(width, ' ')); \ } FMT_DEFINE_INT_FORMATTERS(int) @@ -1896,29 +1854,6 @@ FMT_DEFINE_INT_FORMATTERS(unsigned long) FMT_DEFINE_INT_FORMATTERS(LongLong) FMT_DEFINE_INT_FORMATTERS(ULongLong) -/** - \rst - Returns a string formatter that pads the formatted argument with the fill - character to the specified width using the default (left) string alignment. - - **Example**:: - - std::string s = str(MemoryWriter() << pad("abc", 8)); - // s == "abc " - - \endrst - */ -template -inline StrFormatSpec pad( - const Char *str, unsigned width, Char fill = ' ') { - return StrFormatSpec(str, width, fill); -} - -inline StrFormatSpec pad( - const wchar_t *str, unsigned width, char fill = ' ') { - return StrFormatSpec(str, width, fill); -} - namespace internal { template @@ -2575,6 +2510,11 @@ class basic_writer { buffer_.append(str, str + value.size()); } + template + void write(BasicStringRef str, FormatSpecs... specs) { + write_str(str, FormatSpec(specs...)); + } + template basic_writer &operator<<(IntFormatSpec spec) { internal::CharTraits::convert(FillChar()); diff --git a/test/format-test.cc b/test/format-test.cc index 378e8249..12c9c5b9 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -77,7 +77,9 @@ using fmt::StringRef; using fmt::CStringRef; using fmt::MemoryWriter; using fmt::WMemoryWriter; -using fmt::pad; +using fmt::fill; +using fmt::type; +using fmt::width; namespace { @@ -418,7 +420,6 @@ public: template friend basic_writer &operator<<( basic_writer &w, const ISO8601DateFormatter &d) { - using namespace fmt; w.write(d.date_->year(), width=4, fill='0'); w.write('-'); w.write(d.date_->month(), width=2, fill='0'); @@ -438,8 +439,15 @@ std::string write_str(T... args) { return writer.str(); } -TEST(WriterTest, pad) { +template +std::wstring write_wstr(T... args) { + WMemoryWriter writer; using namespace fmt; + writer.write(args...); + return writer.str(); +} + +TEST(WriterTest, pad) { EXPECT_EQ(" cafe", write_str(0xcafe, width=8, type='x')); EXPECT_EQ(" babe", write_str(0xbabeu, width=8, type='x')); EXPECT_EQ(" dead", write_str(0xdeadl, width=8, type='x')); @@ -447,17 +455,16 @@ TEST(WriterTest, pad) { EXPECT_EQ(" dead", write_str(0xdeadll, width=8, type='x')); EXPECT_EQ(" beef", write_str(0xbeefull, width=8, type='x')); - EXPECT_EQ(" 11", (MemoryWriter() << pad(11, 7)).str()); - EXPECT_EQ(" 22", (MemoryWriter() << pad(22u, 7)).str()); - EXPECT_EQ(" 33", (MemoryWriter() << pad(33l, 7)).str()); - EXPECT_EQ(" 44", (MemoryWriter() << pad(44ul, 7)).str()); - EXPECT_EQ(" 33", (MemoryWriter() << pad(33ll, 7)).str()); - EXPECT_EQ(" 44", (MemoryWriter() << pad(44ull, 7)).str()); + EXPECT_EQ(" 11", write_str(11, width=7)); + EXPECT_EQ(" 22", write_str(22u, width=7)); + EXPECT_EQ(" 33", write_str(33l, width=7)); + EXPECT_EQ(" 44", write_str(44ul, width=7)); + EXPECT_EQ(" 33", write_str(33ll, width=7)); + EXPECT_EQ(" 44", write_str(44ull, width=7)); MemoryWriter w; w.clear(); - using namespace fmt; - w.write(42, width=5, fill='0'); + w.write(42, fmt::width=5, fmt::fill='0'); EXPECT_EQ("00042", w.str()); w.clear(); w << Date(2012, 12, 9); @@ -468,14 +475,14 @@ TEST(WriterTest, pad) { } TEST(WriterTest, PadString) { - EXPECT_EQ("test ", (MemoryWriter() << pad("test", 8)).str()); - EXPECT_EQ("test******", (MemoryWriter() << pad("test", 10, '*')).str()); + EXPECT_EQ("test ", write_str("test", width=8)); + EXPECT_EQ("test******", write_str("test", width=10, fill='*')); } TEST(WriterTest, PadWString) { - EXPECT_EQ(L"test ", (WMemoryWriter() << pad(L"test", 8)).str()); - EXPECT_EQ(L"test******", (WMemoryWriter() << pad(L"test", 10, '*')).str()); - EXPECT_EQ(L"test******", (WMemoryWriter() << pad(L"test", 10, L'*')).str()); + EXPECT_EQ(L"test ", write_wstr(L"test", width=8)); + EXPECT_EQ(L"test******", write_wstr(L"test", width=10, fill='*')); + EXPECT_EQ(L"test******", write_wstr(L"test", width=10, fill=L'*')); } TEST(WriterTest, NoConflictWithIOManip) { @@ -1414,8 +1421,7 @@ TEST(FormatterTest, FormatStringFromSpeedTest) { } TEST(FormatterTest, FormatExamples) { - using fmt::hex; - EXPECT_EQ("0000cafe", (MemoryWriter() << pad(hex(0xcafe), 8, '0')).str()); + EXPECT_EQ("0000cafe", write_str(0xcafe, width=8, fill='0', type='x')); std::string message = format("The answer is {}", 42); EXPECT_EQ("The answer is 42", message);