From 984a1029210b912eeac4f77c559f66fb74643ca2 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 28 Jan 2017 04:26:48 -0800 Subject: [PATCH] Remove IntFormatSpec and StrFormatSpec --- fmt/format.h | 101 +------------------------------------------- test/format-test.cc | 100 ++++++++++++++++++------------------------- 2 files changed, 43 insertions(+), 158 deletions(-) diff --git a/fmt/format.h b/fmt/format.h index f26e5cb0..dcf71c13 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -1779,81 +1779,6 @@ class FormatSpec : public AlignSpec { char type() const { return type_; } }; -// An integer format specifier. -template , typename Char = char> -class IntFormatSpec : public SpecT { - private: - T value_; - - public: - IntFormatSpec(T val, const SpecT &spec = SpecT()) - : SpecT(spec), value_(val) {} - - T value() const { return value_; } -}; - -// A string format specifier. -template -class StrFormatSpec : public AlignSpec { - private: - const Char *str_; - - public: - template - StrFormatSpec(const Char *str, unsigned width, FillChar fill) - : AlignSpec(width, fill), str_(str) { - internal::CharTraits::convert(FillChar()); - } - - const Char *str() const { return str_; } -}; - -/** - Returns an integer format specifier to format the value in base 2. - */ -IntFormatSpec > bin(int value); - -/** - Returns an integer format specifier to format the value in base 8. - */ -IntFormatSpec > oct(int value); - -/** - Returns an integer format specifier to format the value in base 16 using - lower-case letters for the digits above 9. - */ -IntFormatSpec > hex(int value); - -/** - Returns an integer formatter format specifier to format in base 16 using - upper-case letters for the digits above 9. - */ -IntFormatSpec > hexu(int value); - -#define FMT_DEFINE_INT_FORMATTERS(TYPE) \ -inline IntFormatSpec > bin(TYPE value) { \ - return IntFormatSpec >(value, TypeSpec<'b'>()); \ -} \ - \ -inline IntFormatSpec > oct(TYPE value) { \ - return IntFormatSpec >(value, TypeSpec<'o'>()); \ -} \ - \ -inline IntFormatSpec > hex(TYPE value) { \ - return IntFormatSpec >(value, TypeSpec<'x'>()); \ -} \ - \ -inline IntFormatSpec > hexu(TYPE value) { \ - return IntFormatSpec >(value, TypeSpec<'X'>()); \ -} - -FMT_DEFINE_INT_FORMATTERS(int) -FMT_DEFINE_INT_FORMATTERS(long) -FMT_DEFINE_INT_FORMATTERS(unsigned) -FMT_DEFINE_INT_FORMATTERS(unsigned long) -FMT_DEFINE_INT_FORMATTERS(LongLong) -FMT_DEFINE_INT_FORMATTERS(ULongLong) - namespace internal { template @@ -2442,28 +2367,18 @@ class basic_writer { void write(int value) { write_decimal(value); } - void write(unsigned value) { - *this << IntFormatSpec(value); - } void write(long value) { write_decimal(value); } - void write(unsigned long value) { - *this << IntFormatSpec(value); - } void write(LongLong value) { write_decimal(value); } /** \rst - Formats *value* and writes it to the stream. + Formats *value* and writes it to the buffer. \endrst */ - void write(ULongLong value) { - *this << IntFormatSpec(value); - } - template typename std::enable_if::value, void>::type write(T value, FormatSpecs... specs) { @@ -2515,20 +2430,6 @@ class basic_writer { write_str(str, FormatSpec(specs...)); } - template - basic_writer &operator<<(IntFormatSpec spec) { - internal::CharTraits::convert(FillChar()); - write_int(spec.value(), spec); - return *this; - } - - template - basic_writer &operator<<(const StrFormatSpec &spec) { - const StrChar *s = spec.str(); - write_str(s, std::char_traits::length(s), spec); - return *this; - } - void clear() FMT_NOEXCEPT { buffer_.clear(); } Buffer &buffer() FMT_NOEXCEPT { return buffer_; } diff --git a/test/format-test.cc b/test/format-test.cc index 12c9c5b9..656551cb 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -353,52 +353,59 @@ TEST(WriterTest, WriteWideString) { //fmt::WMemoryWriter() << "abc"; } +template +std::string write_str(T... args) { + MemoryWriter writer; + using namespace fmt; + writer.write(args...); + return writer.str(); +} + +template +std::wstring write_wstr(T... args) { + WMemoryWriter writer; + using namespace fmt; + writer.write(args...); + return writer.str(); +} + TEST(WriterTest, bin) { - using fmt::bin; - EXPECT_EQ("1100101011111110", (MemoryWriter() << bin(0xcafe)).str()); - EXPECT_EQ("1011101010111110", (MemoryWriter() << bin(0xbabeu)).str()); - EXPECT_EQ("1101111010101101", (MemoryWriter() << bin(0xdeadl)).str()); - EXPECT_EQ("1011111011101111", (MemoryWriter() << bin(0xbeeful)).str()); + EXPECT_EQ("1100101011111110", write_str(0xcafe, type='b')); + EXPECT_EQ("1011101010111110", write_str(0xbabeu, type='b')); + EXPECT_EQ("1101111010101101", write_str(0xdeadl, type='b')); + EXPECT_EQ("1011111011101111", write_str(0xbeeful, type='b')); EXPECT_EQ("11001010111111101011101010111110", - (MemoryWriter() << bin(0xcafebabell)).str()); + write_str(0xcafebabell, type='b')); EXPECT_EQ("11011110101011011011111011101111", - (MemoryWriter() << bin(0xdeadbeefull)).str()); + write_str(0xdeadbeefull, type='b')); } TEST(WriterTest, oct) { - using fmt::oct; - EXPECT_EQ("12", (MemoryWriter() << oct(static_cast(012))).str()); - EXPECT_EQ("12", (MemoryWriter() << oct(012)).str()); - EXPECT_EQ("34", (MemoryWriter() << oct(034u)).str()); - EXPECT_EQ("56", (MemoryWriter() << oct(056l)).str()); - EXPECT_EQ("70", (MemoryWriter() << oct(070ul)).str()); - EXPECT_EQ("1234", (MemoryWriter() << oct(01234ll)).str()); - EXPECT_EQ("5670", (MemoryWriter() << oct(05670ull)).str()); + EXPECT_EQ("12", write_str(static_cast(012), type='o')); + EXPECT_EQ("12", write_str(012, type='o')); + EXPECT_EQ("34", write_str(034u, type='o')); + EXPECT_EQ("56", write_str(056l, type='o')); + EXPECT_EQ("70", write_str(070ul, type='o')); + EXPECT_EQ("1234", write_str(01234ll, type='o')); + EXPECT_EQ("5670", write_str(05670ull, type='o')); } TEST(WriterTest, hex) { - using fmt::hex; - fmt::IntFormatSpec > (*phex)(int value) = hex; - phex(42); - // This shouldn't compile: - //fmt::IntFormatSpec > (*phex2)(short value) = hex; - - EXPECT_EQ("cafe", (MemoryWriter() << hex(0xcafe)).str()); - EXPECT_EQ("babe", (MemoryWriter() << hex(0xbabeu)).str()); - EXPECT_EQ("dead", (MemoryWriter() << hex(0xdeadl)).str()); - EXPECT_EQ("beef", (MemoryWriter() << hex(0xbeeful)).str()); - EXPECT_EQ("cafebabe", (MemoryWriter() << hex(0xcafebabell)).str()); - EXPECT_EQ("deadbeef", (MemoryWriter() << hex(0xdeadbeefull)).str()); + EXPECT_EQ("cafe", write_str(0xcafe, type='x')); + EXPECT_EQ("babe", write_str(0xbabeu, type='x')); + EXPECT_EQ("dead", write_str(0xdeadl, type='x')); + EXPECT_EQ("beef", write_str(0xbeeful, type='x')); + EXPECT_EQ("cafebabe", write_str(0xcafebabell, type='x')); + EXPECT_EQ("deadbeef", write_str(0xdeadbeefull, type='x')); } TEST(WriterTest, hexu) { - using fmt::hexu; - EXPECT_EQ("CAFE", (MemoryWriter() << hexu(0xcafe)).str()); - EXPECT_EQ("BABE", (MemoryWriter() << hexu(0xbabeu)).str()); - EXPECT_EQ("DEAD", (MemoryWriter() << hexu(0xdeadl)).str()); - EXPECT_EQ("BEEF", (MemoryWriter() << hexu(0xbeeful)).str()); - EXPECT_EQ("CAFEBABE", (MemoryWriter() << hexu(0xcafebabell)).str()); - EXPECT_EQ("DEADBEEF", (MemoryWriter() << hexu(0xdeadbeefull)).str()); + EXPECT_EQ("CAFE", write_str(0xcafe, type='X')); + EXPECT_EQ("BABE", write_str(0xbabeu, type='X')); + EXPECT_EQ("DEAD", write_str(0xdeadl, type='X')); + EXPECT_EQ("BEEF", write_str(0xbeeful, type='X')); + EXPECT_EQ("CAFEBABE", write_str(0xcafebabell, type='X')); + EXPECT_EQ("DEADBEEF", write_str(0xdeadbeefull, type='X')); } template @@ -431,22 +438,6 @@ public: ISO8601DateFormatter iso8601(const Date &d) { return ISO8601DateFormatter(d); } -template -std::string write_str(T... args) { - MemoryWriter writer; - using namespace fmt; - writer.write(args...); - return writer.str(); -} - -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')); @@ -485,13 +476,6 @@ TEST(WriterTest, PadWString) { EXPECT_EQ(L"test******", write_wstr(L"test", width=10, fill=L'*')); } -TEST(WriterTest, NoConflictWithIOManip) { - using namespace std; - using namespace fmt; - EXPECT_EQ("cafe", (MemoryWriter() << hex(0xcafe)).str()); - EXPECT_EQ("12", (MemoryWriter() << oct(012)).str()); -} - TEST(WriterTest, Format) { MemoryWriter w; w.format("part{0}", 1); @@ -507,7 +491,7 @@ TEST(WriterTest, Format) { } TEST(WriterTest, WWriter) { - EXPECT_EQ(L"cafe", (fmt::WMemoryWriter() << fmt::hex(0xcafe)).str()); + EXPECT_EQ(L"cafe", write_wstr(0xcafe, type='x')); } TEST(ArrayWriterTest, Ctor) {