mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 02:37:36 +02:00
Remove pad
This commit is contained in:
74
fmt/format.h
74
fmt/format.h
@ -1741,7 +1741,8 @@ struct AlignTypeSpec : AlignSpec {
|
|||||||
// A full format specifier.
|
// A full format specifier.
|
||||||
class FormatSpec : public AlignSpec {
|
class FormatSpec : public AlignSpec {
|
||||||
private:
|
private:
|
||||||
void set(fill_spec<char> fill) {
|
template <typename Char>
|
||||||
|
void set(fill_spec<Char> fill) {
|
||||||
fill_ = fill.value();
|
fill_ = fill.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1829,24 +1830,6 @@ IntFormatSpec<int, TypeSpec<'x'> > hex(int value);
|
|||||||
*/
|
*/
|
||||||
IntFormatSpec<int, TypeSpec<'X'> > hexu(int value);
|
IntFormatSpec<int, TypeSpec<'X'> > 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 <char TYPE_CODE, typename Char>
|
|
||||||
IntFormatSpec<int, AlignTypeSpec<TYPE_CODE>, Char> pad(
|
|
||||||
int value, unsigned width, Char fill = ' ');
|
|
||||||
|
|
||||||
#define FMT_DEFINE_INT_FORMATTERS(TYPE) \
|
#define FMT_DEFINE_INT_FORMATTERS(TYPE) \
|
||||||
inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
|
inline IntFormatSpec<TYPE, TypeSpec<'b'> > bin(TYPE value) { \
|
||||||
return IntFormatSpec<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
|
return IntFormatSpec<TYPE, TypeSpec<'b'> >(value, TypeSpec<'b'>()); \
|
||||||
@ -1862,31 +1845,6 @@ inline IntFormatSpec<TYPE, TypeSpec<'x'> > hex(TYPE value) { \
|
|||||||
\
|
\
|
||||||
inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
|
inline IntFormatSpec<TYPE, TypeSpec<'X'> > hexu(TYPE value) { \
|
||||||
return IntFormatSpec<TYPE, TypeSpec<'X'> >(value, TypeSpec<'X'>()); \
|
return IntFormatSpec<TYPE, TypeSpec<'X'> >(value, TypeSpec<'X'>()); \
|
||||||
} \
|
|
||||||
\
|
|
||||||
template <char TYPE_CODE> \
|
|
||||||
inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> > pad( \
|
|
||||||
IntFormatSpec<TYPE, TypeSpec<TYPE_CODE> > f, unsigned width) { \
|
|
||||||
return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE> >( \
|
|
||||||
f.value(), AlignTypeSpec<TYPE_CODE>(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 <char TYPE_CODE, typename Char> \
|
|
||||||
inline IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char> pad( \
|
|
||||||
IntFormatSpec<TYPE, TypeSpec<TYPE_CODE>, Char> f, \
|
|
||||||
unsigned width, Char fill) { \
|
|
||||||
return IntFormatSpec<TYPE, AlignTypeSpec<TYPE_CODE>, Char>( \
|
|
||||||
f.value(), AlignTypeSpec<TYPE_CODE>(width, fill)); \
|
|
||||||
} \
|
|
||||||
\
|
|
||||||
inline IntFormatSpec<TYPE, AlignTypeSpec<0> > pad( \
|
|
||||||
TYPE value, unsigned width) { \
|
|
||||||
return IntFormatSpec<TYPE, AlignTypeSpec<0> >( \
|
|
||||||
value, AlignTypeSpec<0>(width, ' ')); \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FMT_DEFINE_INT_FORMATTERS(int)
|
FMT_DEFINE_INT_FORMATTERS(int)
|
||||||
@ -1896,29 +1854,6 @@ FMT_DEFINE_INT_FORMATTERS(unsigned long)
|
|||||||
FMT_DEFINE_INT_FORMATTERS(LongLong)
|
FMT_DEFINE_INT_FORMATTERS(LongLong)
|
||||||
FMT_DEFINE_INT_FORMATTERS(ULongLong)
|
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 <typename Char>
|
|
||||||
inline StrFormatSpec<Char> pad(
|
|
||||||
const Char *str, unsigned width, Char fill = ' ') {
|
|
||||||
return StrFormatSpec<Char>(str, width, fill);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline StrFormatSpec<wchar_t> pad(
|
|
||||||
const wchar_t *str, unsigned width, char fill = ' ') {
|
|
||||||
return StrFormatSpec<wchar_t>(str, width, fill);
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
template <typename Context>
|
template <typename Context>
|
||||||
@ -2575,6 +2510,11 @@ class basic_writer {
|
|||||||
buffer_.append(str, str + value.size());
|
buffer_.append(str, str + value.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename... FormatSpecs>
|
||||||
|
void write(BasicStringRef<Char> str, FormatSpecs... specs) {
|
||||||
|
write_str(str, FormatSpec(specs...));
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T, typename Spec, typename FillChar>
|
template <typename T, typename Spec, typename FillChar>
|
||||||
basic_writer &operator<<(IntFormatSpec<T, Spec, FillChar> spec) {
|
basic_writer &operator<<(IntFormatSpec<T, Spec, FillChar> spec) {
|
||||||
internal::CharTraits<Char>::convert(FillChar());
|
internal::CharTraits<Char>::convert(FillChar());
|
||||||
|
@ -77,7 +77,9 @@ using fmt::StringRef;
|
|||||||
using fmt::CStringRef;
|
using fmt::CStringRef;
|
||||||
using fmt::MemoryWriter;
|
using fmt::MemoryWriter;
|
||||||
using fmt::WMemoryWriter;
|
using fmt::WMemoryWriter;
|
||||||
using fmt::pad;
|
using fmt::fill;
|
||||||
|
using fmt::type;
|
||||||
|
using fmt::width;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@ -418,7 +420,6 @@ 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) {
|
||||||
using namespace fmt;
|
|
||||||
w.write(d.date_->year(), width=4, fill='0');
|
w.write(d.date_->year(), width=4, fill='0');
|
||||||
w.write('-');
|
w.write('-');
|
||||||
w.write(d.date_->month(), width=2, fill='0');
|
w.write(d.date_->month(), width=2, fill='0');
|
||||||
@ -438,8 +439,15 @@ std::string write_str(T... args) {
|
|||||||
return writer.str();
|
return writer.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(WriterTest, pad) {
|
template <typename... T>
|
||||||
|
std::wstring write_wstr(T... args) {
|
||||||
|
WMemoryWriter writer;
|
||||||
using namespace fmt;
|
using namespace fmt;
|
||||||
|
writer.write(args...);
|
||||||
|
return writer.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(WriterTest, pad) {
|
||||||
EXPECT_EQ(" cafe", write_str(0xcafe, width=8, type='x'));
|
EXPECT_EQ(" cafe", write_str(0xcafe, width=8, type='x'));
|
||||||
EXPECT_EQ(" babe", write_str(0xbabeu, width=8, type='x'));
|
EXPECT_EQ(" babe", write_str(0xbabeu, width=8, type='x'));
|
||||||
EXPECT_EQ(" dead", write_str(0xdeadl, 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(" dead", write_str(0xdeadll, width=8, type='x'));
|
||||||
EXPECT_EQ(" beef", write_str(0xbeefull, width=8, type='x'));
|
EXPECT_EQ(" beef", write_str(0xbeefull, width=8, type='x'));
|
||||||
|
|
||||||
EXPECT_EQ(" 11", (MemoryWriter() << pad(11, 7)).str());
|
EXPECT_EQ(" 11", write_str(11, width=7));
|
||||||
EXPECT_EQ(" 22", (MemoryWriter() << pad(22u, 7)).str());
|
EXPECT_EQ(" 22", write_str(22u, width=7));
|
||||||
EXPECT_EQ(" 33", (MemoryWriter() << pad(33l, 7)).str());
|
EXPECT_EQ(" 33", write_str(33l, width=7));
|
||||||
EXPECT_EQ(" 44", (MemoryWriter() << pad(44ul, 7)).str());
|
EXPECT_EQ(" 44", write_str(44ul, width=7));
|
||||||
EXPECT_EQ(" 33", (MemoryWriter() << pad(33ll, 7)).str());
|
EXPECT_EQ(" 33", write_str(33ll, width=7));
|
||||||
EXPECT_EQ(" 44", (MemoryWriter() << pad(44ull, 7)).str());
|
EXPECT_EQ(" 44", write_str(44ull, width=7));
|
||||||
|
|
||||||
MemoryWriter w;
|
MemoryWriter w;
|
||||||
w.clear();
|
w.clear();
|
||||||
using namespace fmt;
|
w.write(42, fmt::width=5, fmt::fill='0');
|
||||||
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);
|
||||||
@ -468,14 +475,14 @@ TEST(WriterTest, pad) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(WriterTest, PadString) {
|
TEST(WriterTest, PadString) {
|
||||||
EXPECT_EQ("test ", (MemoryWriter() << pad("test", 8)).str());
|
EXPECT_EQ("test ", write_str("test", width=8));
|
||||||
EXPECT_EQ("test******", (MemoryWriter() << pad("test", 10, '*')).str());
|
EXPECT_EQ("test******", write_str("test", width=10, fill='*'));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(WriterTest, PadWString) {
|
TEST(WriterTest, PadWString) {
|
||||||
EXPECT_EQ(L"test ", (WMemoryWriter() << pad(L"test", 8)).str());
|
EXPECT_EQ(L"test ", write_wstr(L"test", width=8));
|
||||||
EXPECT_EQ(L"test******", (WMemoryWriter() << pad(L"test", 10, '*')).str());
|
EXPECT_EQ(L"test******", write_wstr(L"test", width=10, fill='*'));
|
||||||
EXPECT_EQ(L"test******", (WMemoryWriter() << pad(L"test", 10, L'*')).str());
|
EXPECT_EQ(L"test******", write_wstr(L"test", width=10, fill=L'*'));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(WriterTest, NoConflictWithIOManip) {
|
TEST(WriterTest, NoConflictWithIOManip) {
|
||||||
@ -1414,8 +1421,7 @@ TEST(FormatterTest, FormatStringFromSpeedTest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatterTest, FormatExamples) {
|
TEST(FormatterTest, FormatExamples) {
|
||||||
using fmt::hex;
|
EXPECT_EQ("0000cafe", write_str(0xcafe, width=8, fill='0', type='x'));
|
||||||
EXPECT_EQ("0000cafe", (MemoryWriter() << pad(hex(0xcafe), 8, '0')).str());
|
|
||||||
|
|
||||||
std::string message = format("The answer is {}", 42);
|
std::string message = format("The answer is {}", 42);
|
||||||
EXPECT_EQ("The answer is 42", message);
|
EXPECT_EQ("The answer is 42", message);
|
||||||
|
Reference in New Issue
Block a user