mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-05 20:41:06 +00:00
Remove IntFormatSpec and StrFormatSpec
This commit is contained in:
parent
4863730eca
commit
984a102921
101
fmt/format.h
101
fmt/format.h
@ -1779,81 +1779,6 @@ class FormatSpec : public AlignSpec {
|
||||
char type() const { return type_; }
|
||||
};
|
||||
|
||||
// An integer format specifier.
|
||||
template <typename T, typename SpecT = TypeSpec<0>, 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 <typename Char>
|
||||
class StrFormatSpec : public AlignSpec {
|
||||
private:
|
||||
const Char *str_;
|
||||
|
||||
public:
|
||||
template <typename FillChar>
|
||||
StrFormatSpec(const Char *str, unsigned width, FillChar fill)
|
||||
: AlignSpec(width, fill), str_(str) {
|
||||
internal::CharTraits<Char>::convert(FillChar());
|
||||
}
|
||||
|
||||
const Char *str() const { return str_; }
|
||||
};
|
||||
|
||||
/**
|
||||
Returns an integer format specifier to format the value in base 2.
|
||||
*/
|
||||
IntFormatSpec<int, TypeSpec<'b'> > bin(int value);
|
||||
|
||||
/**
|
||||
Returns an integer format specifier to format the value in base 8.
|
||||
*/
|
||||
IntFormatSpec<int, TypeSpec<'o'> > 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<int, TypeSpec<'x'> > hex(int value);
|
||||
|
||||
/**
|
||||
Returns an integer formatter format specifier to format in base 16 using
|
||||
upper-case letters for the digits above 9.
|
||||
*/
|
||||
IntFormatSpec<int, TypeSpec<'X'> > hexu(int value);
|
||||
|
||||
#define FMT_DEFINE_INT_FORMATTERS(TYPE) \
|
||||
inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
|
||||
return IntFormatSpec<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
|
||||
} \
|
||||
\
|
||||
inline IntFormatSpec<TYPE, TypeSpec<'o'> > oct(TYPE value) { \
|
||||
return IntFormatSpec<TYPE, TypeSpec<'o'> >(value, TypeSpec<'o'>()); \
|
||||
} \
|
||||
\
|
||||
inline IntFormatSpec<TYPE, TypeSpec<'x'> > hex(TYPE value) { \
|
||||
return IntFormatSpec<TYPE, TypeSpec<'x'> >(value, TypeSpec<'x'>()); \
|
||||
} \
|
||||
\
|
||||
inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
|
||||
return IntFormatSpec<TYPE, TypeSpec<'X'> >(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 <typename Context>
|
||||
@ -2442,28 +2367,18 @@ class basic_writer {
|
||||
void write(int value) {
|
||||
write_decimal(value);
|
||||
}
|
||||
void write(unsigned value) {
|
||||
*this << IntFormatSpec<unsigned>(value);
|
||||
}
|
||||
void write(long value) {
|
||||
write_decimal(value);
|
||||
}
|
||||
void write(unsigned long value) {
|
||||
*this << IntFormatSpec<unsigned long>(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<ULongLong>(value);
|
||||
}
|
||||
|
||||
template <typename T, typename... FormatSpecs>
|
||||
typename std::enable_if<std::is_integral<T>::value, void>::type
|
||||
write(T value, FormatSpecs... specs) {
|
||||
@ -2515,20 +2430,6 @@ class basic_writer {
|
||||
write_str(str, FormatSpec(specs...));
|
||||
}
|
||||
|
||||
template <typename T, typename Spec, typename FillChar>
|
||||
basic_writer &operator<<(IntFormatSpec<T, Spec, FillChar> spec) {
|
||||
internal::CharTraits<Char>::convert(FillChar());
|
||||
write_int(spec.value(), spec);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename StrChar>
|
||||
basic_writer &operator<<(const StrFormatSpec<StrChar> &spec) {
|
||||
const StrChar *s = spec.str();
|
||||
write_str(s, std::char_traits<Char>::length(s), spec);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void clear() FMT_NOEXCEPT { buffer_.clear(); }
|
||||
|
||||
Buffer<Char> &buffer() FMT_NOEXCEPT { return buffer_; }
|
||||
|
@ -353,52 +353,59 @@ TEST(WriterTest, WriteWideString) {
|
||||
//fmt::WMemoryWriter() << "abc";
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
std::string write_str(T... args) {
|
||||
MemoryWriter writer;
|
||||
using namespace fmt;
|
||||
writer.write(args...);
|
||||
return writer.str();
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
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<short>(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<short>(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<int, fmt::TypeSpec<'x'> > (*phex)(int value) = hex;
|
||||
phex(42);
|
||||
// This shouldn't compile:
|
||||
//fmt::IntFormatSpec<short, fmt::TypeSpec<'x'> > (*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 <typename Char>
|
||||
@ -431,22 +438,6 @@ public:
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
template <typename... T>
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user