mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 02:37:36 +02:00
Implement fill.
This commit is contained in:
131
format.cc
131
format.cc
@ -104,8 +104,8 @@ void Formatter::ReportError(const char *s, const std::string &message) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
void Formatter::FormatInt(T value, const FormatSpec &spec) {
|
||||||
int size = 0;
|
unsigned size = 0;
|
||||||
char sign = 0;
|
char sign = 0;
|
||||||
typedef typename IntTraits<T>::UnsignedType UnsignedType;
|
typedef typename IntTraits<T>::UnsignedType UnsignedType;
|
||||||
UnsignedType abs_value = value;
|
UnsignedType abs_value = value;
|
||||||
@ -113,20 +113,19 @@ void Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
|||||||
sign = '-';
|
sign = '-';
|
||||||
++size;
|
++size;
|
||||||
abs_value = -value;
|
abs_value = -value;
|
||||||
} else if ((flags & PLUS_FLAG) != 0) {
|
} else if ((spec.flags & PLUS_FLAG) != 0) {
|
||||||
sign = '+';
|
sign = '+';
|
||||||
++size;
|
++size;
|
||||||
}
|
}
|
||||||
char fill = (flags & ZERO_FLAG) != 0 ? '0' : ' ';
|
|
||||||
size_t start = buffer_.size();
|
size_t start = buffer_.size();
|
||||||
char *p = 0;
|
char *p = 0;
|
||||||
switch (type) {
|
switch (spec.type) {
|
||||||
case 0: case 'd': {
|
case 0: case 'd': {
|
||||||
UnsignedType n = abs_value;
|
UnsignedType n = abs_value;
|
||||||
do {
|
do {
|
||||||
++size;
|
++size;
|
||||||
} while ((n /= 10) != 0);
|
} while ((n /= 10) != 0);
|
||||||
width = std::max(width, size);
|
unsigned width = std::max(spec.width, size);
|
||||||
p = GrowBuffer(width) + width - 1;
|
p = GrowBuffer(width) + width - 1;
|
||||||
n = abs_value;
|
n = abs_value;
|
||||||
do {
|
do {
|
||||||
@ -136,20 +135,21 @@ void Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
|||||||
}
|
}
|
||||||
case 'x': case 'X': {
|
case 'x': case 'X': {
|
||||||
UnsignedType n = abs_value;
|
UnsignedType n = abs_value;
|
||||||
bool print_prefix = (flags & HEX_PREFIX_FLAG) != 0;
|
bool print_prefix = (spec.flags & HEX_PREFIX_FLAG) != 0;
|
||||||
if (print_prefix) size += 2;
|
if (print_prefix) size += 2;
|
||||||
do {
|
do {
|
||||||
++size;
|
++size;
|
||||||
} while ((n >>= 4) != 0);
|
} while ((n >>= 4) != 0);
|
||||||
width = std::max(width, size);
|
unsigned width = std::max(spec.width, size);
|
||||||
p = GrowBuffer(width) + width - 1;
|
p = GrowBuffer(width) + width - 1;
|
||||||
n = abs_value;
|
n = abs_value;
|
||||||
const char *digits = type == 'x' ? "0123456789abcdef" : "0123456789ABCDEF";
|
const char *digits = spec.type == 'x' ?
|
||||||
|
"0123456789abcdef" : "0123456789ABCDEF";
|
||||||
do {
|
do {
|
||||||
*p-- = digits[n & 0xf];
|
*p-- = digits[n & 0xf];
|
||||||
} while ((n >>= 4) != 0);
|
} while ((n >>= 4) != 0);
|
||||||
if (print_prefix) {
|
if (print_prefix) {
|
||||||
*p-- = type;
|
*p-- = spec.type;
|
||||||
*p-- = '0';
|
*p-- = '0';
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -159,7 +159,7 @@ void Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
|||||||
do {
|
do {
|
||||||
++size;
|
++size;
|
||||||
} while ((n >>= 3) != 0);
|
} while ((n >>= 3) != 0);
|
||||||
width = std::max(width, size);
|
unsigned width = std::max(spec.width, size);
|
||||||
p = GrowBuffer(width) + width - 1;
|
p = GrowBuffer(width) + width - 1;
|
||||||
n = abs_value;
|
n = abs_value;
|
||||||
do {
|
do {
|
||||||
@ -168,22 +168,22 @@ void Formatter::FormatInt(T value, unsigned flags, int width, char type) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
ReportUnknownType(type, "integer");
|
ReportUnknownType(spec.type, "integer");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (sign) {
|
if (sign) {
|
||||||
if ((flags & ZERO_FLAG) != 0)
|
if ((spec.flags & ZERO_FLAG) != 0)
|
||||||
buffer_[start++] = sign;
|
buffer_[start++] = sign;
|
||||||
else
|
else
|
||||||
*p-- = sign;
|
*p-- = sign;
|
||||||
}
|
}
|
||||||
std::fill(&buffer_[start], p + 1, fill);
|
std::fill(&buffer_[start], p + 1, spec.fill);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Formatter::FormatDouble(
|
void Formatter::FormatDouble(T value, const FormatSpec &spec, int precision) {
|
||||||
T value, unsigned flags, int width, int precision, char type) {
|
|
||||||
// Check type.
|
// Check type.
|
||||||
|
char type = spec.type;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 0:
|
case 0:
|
||||||
type = 'g';
|
type = 'g';
|
||||||
@ -205,12 +205,13 @@ void Formatter::FormatDouble(
|
|||||||
enum { MAX_FORMAT_SIZE = 9}; // longest format: %+0*.*Lg
|
enum { MAX_FORMAT_SIZE = 9}; // longest format: %+0*.*Lg
|
||||||
char format[MAX_FORMAT_SIZE];
|
char format[MAX_FORMAT_SIZE];
|
||||||
char *format_ptr = format;
|
char *format_ptr = format;
|
||||||
|
unsigned width = spec.width;
|
||||||
*format_ptr++ = '%';
|
*format_ptr++ = '%';
|
||||||
if ((flags & PLUS_FLAG) != 0)
|
if ((spec.flags & PLUS_FLAG) != 0)
|
||||||
*format_ptr++ = '+';
|
*format_ptr++ = '+';
|
||||||
if ((flags & ZERO_FLAG) != 0)
|
if ((spec.flags & ZERO_FLAG) != 0)
|
||||||
*format_ptr++ = '0';
|
*format_ptr++ = '0';
|
||||||
if (width > 0)
|
if (width != 0)
|
||||||
*format_ptr++ = '*';
|
*format_ptr++ = '*';
|
||||||
if (precision >= 0) {
|
if (precision >= 0) {
|
||||||
*format_ptr++ = '.';
|
*format_ptr++ = '.';
|
||||||
@ -226,16 +227,19 @@ void Formatter::FormatDouble(
|
|||||||
for (;;) {
|
for (;;) {
|
||||||
size_t size = buffer_.capacity() - offset;
|
size_t size = buffer_.capacity() - offset;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
if (width <= 0) {
|
char *start = &buffer_[offset];
|
||||||
|
if (width == 0) {
|
||||||
n = precision < 0 ?
|
n = precision < 0 ?
|
||||||
snprintf(&buffer_[offset], size, format, value) :
|
snprintf(start, size, format, value) :
|
||||||
snprintf(&buffer_[offset], size, format, precision, value);
|
snprintf(start, size, format, precision, value);
|
||||||
} else {
|
} else {
|
||||||
n = precision < 0 ?
|
n = precision < 0 ?
|
||||||
snprintf(&buffer_[offset], size, format, width, value) :
|
snprintf(start, size, format, width, value) :
|
||||||
snprintf(&buffer_[offset], size, format, width, precision, value);
|
snprintf(start, size, format, width, precision, value);
|
||||||
}
|
}
|
||||||
if (n >= 0 && offset + n < buffer_.capacity()) {
|
if (n >= 0 && offset + n < buffer_.capacity()) {
|
||||||
|
while (*start == ' ')
|
||||||
|
*start++ = spec.fill;
|
||||||
GrowBuffer(n);
|
GrowBuffer(n);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -285,12 +289,21 @@ void Formatter::DoFormat() {
|
|||||||
|
|
||||||
const Arg &arg = ParseArgIndex(s);
|
const Arg &arg = ParseArgIndex(s);
|
||||||
|
|
||||||
unsigned flags = 0;
|
FormatSpec spec = {};
|
||||||
int width = 0;
|
spec.fill = ' ';
|
||||||
int precision = -1;
|
int precision = -1;
|
||||||
char type = 0;
|
|
||||||
if (*s == ':') {
|
if (*s == ':') {
|
||||||
++s;
|
++s;
|
||||||
|
if (char c = *s) {
|
||||||
|
switch (s[1]) {
|
||||||
|
case '<': case '>': case '=': case '^':
|
||||||
|
if (c != '{' && c != '}') {
|
||||||
|
s += 2;
|
||||||
|
spec.fill = c;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (*s == '+') {
|
if (*s == '+') {
|
||||||
++s;
|
++s;
|
||||||
if (arg.type > LAST_NUMERIC_TYPE)
|
if (arg.type > LAST_NUMERIC_TYPE)
|
||||||
@ -299,13 +312,14 @@ void Formatter::DoFormat() {
|
|||||||
ReportError(s,
|
ReportError(s,
|
||||||
"format specifier '+' requires signed argument");
|
"format specifier '+' requires signed argument");
|
||||||
}
|
}
|
||||||
flags |= PLUS_FLAG;
|
spec.flags |= PLUS_FLAG;
|
||||||
}
|
}
|
||||||
if (*s == '0') {
|
if (*s == '0') {
|
||||||
++s;
|
++s;
|
||||||
if (arg.type > LAST_NUMERIC_TYPE)
|
if (arg.type > LAST_NUMERIC_TYPE)
|
||||||
ReportError(s, "format specifier '0' requires numeric argument");
|
ReportError(s, "format specifier '0' requires numeric argument");
|
||||||
flags |= ZERO_FLAG;
|
spec.flags |= ZERO_FLAG;
|
||||||
|
spec.fill = '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse width.
|
// Parse width.
|
||||||
@ -313,7 +327,7 @@ void Formatter::DoFormat() {
|
|||||||
unsigned value = ParseUInt(s);
|
unsigned value = ParseUInt(s);
|
||||||
if (value > INT_MAX)
|
if (value > INT_MAX)
|
||||||
ReportError(s, "number is too big in format");
|
ReportError(s, "number is too big in format");
|
||||||
width = value;
|
spec.width = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse precision.
|
// Parse precision.
|
||||||
@ -367,7 +381,7 @@ void Formatter::DoFormat() {
|
|||||||
|
|
||||||
// Parse type.
|
// Parse type.
|
||||||
if (*s != '}' && *s)
|
if (*s != '}' && *s)
|
||||||
type = *s++;
|
spec.type = *s++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*s++ != '}')
|
if (*s++ != '}')
|
||||||
@ -377,35 +391,35 @@ void Formatter::DoFormat() {
|
|||||||
// Format argument.
|
// Format argument.
|
||||||
switch (arg.type) {
|
switch (arg.type) {
|
||||||
case INT:
|
case INT:
|
||||||
FormatInt(arg.int_value, flags, width, type);
|
FormatInt(arg.int_value, spec);
|
||||||
break;
|
break;
|
||||||
case UINT:
|
case UINT:
|
||||||
FormatInt(arg.uint_value, flags, width, type);
|
FormatInt(arg.uint_value, spec);
|
||||||
break;
|
break;
|
||||||
case LONG:
|
case LONG:
|
||||||
FormatInt(arg.long_value, flags, width, type);
|
FormatInt(arg.long_value, spec);
|
||||||
break;
|
break;
|
||||||
case ULONG:
|
case ULONG:
|
||||||
FormatInt(arg.ulong_value, flags, width, type);
|
FormatInt(arg.ulong_value, spec);
|
||||||
break;
|
break;
|
||||||
case DOUBLE:
|
case DOUBLE:
|
||||||
FormatDouble(arg.double_value, flags, width, precision, type);
|
FormatDouble(arg.double_value, spec, precision);
|
||||||
break;
|
break;
|
||||||
case LONG_DOUBLE:
|
case LONG_DOUBLE:
|
||||||
FormatDouble(arg.long_double_value, flags, width, precision, type);
|
FormatDouble(arg.long_double_value, spec, precision);
|
||||||
break;
|
break;
|
||||||
case CHAR: {
|
case CHAR: {
|
||||||
if (type && type != 'c')
|
if (spec.type && spec.type != 'c')
|
||||||
ReportUnknownType(type, "char");
|
ReportUnknownType(spec.type, "char");
|
||||||
char *out = GrowBuffer(std::max(width, 1));
|
char *out = GrowBuffer(std::max(spec.width, 1u));
|
||||||
*out++ = arg.int_value;
|
*out++ = arg.int_value;
|
||||||
if (width > 1)
|
if (spec.width > 1)
|
||||||
std::fill_n(out, width - 1, ' ');
|
std::fill_n(out, spec.width - 1, spec.fill);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case STRING: {
|
case STRING: {
|
||||||
if (type && type != 's')
|
if (spec.type && spec.type != 's')
|
||||||
ReportUnknownType(type, "string");
|
ReportUnknownType(spec.type, "string");
|
||||||
const char *str = arg.string.value;
|
const char *str = arg.string.value;
|
||||||
size_t size = arg.string.size;
|
size_t size = arg.string.size;
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
@ -414,22 +428,23 @@ void Formatter::DoFormat() {
|
|||||||
if (*str)
|
if (*str)
|
||||||
size = std::strlen(str);
|
size = std::strlen(str);
|
||||||
}
|
}
|
||||||
char *out = GrowBuffer(std::max<size_t>(width, size));
|
char *out = GrowBuffer(std::max<size_t>(spec.width, size));
|
||||||
out = std::copy(str, str + size, out);
|
out = std::copy(str, str + size, out);
|
||||||
if (static_cast<unsigned>(width) > size)
|
if (spec.width > size)
|
||||||
std::fill_n(out, width - size, ' ');
|
std::fill_n(out, spec.width - size, spec.fill);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case POINTER:
|
case POINTER:
|
||||||
if (type && type != 'p')
|
if (spec.type && spec.type != 'p')
|
||||||
ReportUnknownType(type, "pointer");
|
ReportUnknownType(spec.type, "pointer");
|
||||||
FormatInt(reinterpret_cast<uintptr_t>(
|
spec.flags = HEX_PREFIX_FLAG;
|
||||||
arg.pointer_value), HEX_PREFIX_FLAG, width, 'x');
|
spec.type = 'x';
|
||||||
|
FormatInt(reinterpret_cast<uintptr_t>(arg.pointer_value), spec);
|
||||||
break;
|
break;
|
||||||
case CUSTOM:
|
case CUSTOM:
|
||||||
if (type)
|
if (spec.type)
|
||||||
ReportUnknownType(type, "object");
|
ReportUnknownType(spec.type, "object");
|
||||||
(this->*arg.custom.format)(arg.custom.value, width);
|
(this->*arg.custom.format)(arg.custom.value, spec);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(false);
|
assert(false);
|
||||||
@ -440,9 +455,9 @@ void Formatter::DoFormat() {
|
|||||||
buffer_.resize(buffer_.size() - 1); // Don't count the terminating zero.
|
buffer_.resize(buffer_.size() - 1); // Don't count the terminating zero.
|
||||||
}
|
}
|
||||||
|
|
||||||
void Formatter::Write(const std::string &s, unsigned width) {
|
void Formatter::Write(const std::string &s, const FormatSpec &spec) {
|
||||||
char *out = GrowBuffer(std::max<std::size_t>(width, s.size()));
|
char *out = GrowBuffer(std::max<std::size_t>(spec.width, s.size()));
|
||||||
std::copy(s.begin(), s.end(), out);
|
std::copy(s.begin(), s.end(), out);
|
||||||
if (width > s.size())
|
if (spec.width > s.size())
|
||||||
std::fill_n(out + s.size(), width - s.size(), ' ');
|
std::fill_n(out + s.size(), spec.width - s.size(), spec.fill);
|
||||||
}
|
}
|
||||||
|
31
format.h
31
format.h
@ -123,6 +123,13 @@ class FormatError : public std::runtime_error {
|
|||||||
: std::runtime_error(message) {}
|
: std::runtime_error(message) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct FormatSpec {
|
||||||
|
unsigned flags;
|
||||||
|
unsigned width;
|
||||||
|
char type;
|
||||||
|
char fill;
|
||||||
|
};
|
||||||
|
|
||||||
// Formatter provides string formatting functionality similar to Python's
|
// Formatter provides string formatting functionality similar to Python's
|
||||||
// str.format. The output is stored in a memory buffer that grows dynamically.
|
// str.format. The output is stored in a memory buffer that grows dynamically.
|
||||||
// Usage:
|
// Usage:
|
||||||
@ -149,7 +156,8 @@ class Formatter {
|
|||||||
CHAR, STRING, WSTRING, POINTER, CUSTOM
|
CHAR, STRING, WSTRING, POINTER, CUSTOM
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef void (Formatter::*FormatFunc)(const void *arg, unsigned width);
|
typedef void (Formatter::*FormatFunc)(
|
||||||
|
const void *arg, const FormatSpec &spec);
|
||||||
|
|
||||||
// A format argument.
|
// A format argument.
|
||||||
class Arg {
|
class Arg {
|
||||||
@ -258,16 +266,15 @@ class Formatter {
|
|||||||
|
|
||||||
// Formats an integer.
|
// Formats an integer.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void FormatInt(T value, unsigned flags, int width, char type);
|
void FormatInt(T value, const FormatSpec &spec);
|
||||||
|
|
||||||
// Formats a floating point number (double or long double).
|
// Formats a floating point number (double or long double).
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void FormatDouble(
|
void FormatDouble(T value, const FormatSpec &spec, int precision);
|
||||||
T value, unsigned flags, int width, int precision, char type);
|
|
||||||
|
|
||||||
// Formats an argument of a custom type, such as a user-defined class.
|
// Formats an argument of a custom type, such as a user-defined class.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void FormatCustomArg(const void *arg, unsigned width);
|
void FormatCustomArg(const void *arg, const FormatSpec &spec);
|
||||||
|
|
||||||
unsigned ParseUInt(const char *&s) const;
|
unsigned ParseUInt(const char *&s) const;
|
||||||
|
|
||||||
@ -306,7 +313,7 @@ class Formatter {
|
|||||||
|
|
||||||
// Writes a string to the output buffer padding with spaces if
|
// Writes a string to the output buffer padding with spaces if
|
||||||
// necessary to achieve the desired width.
|
// necessary to achieve the desired width.
|
||||||
void Write(const std::string &s, unsigned width);
|
void Write(const std::string &s, const FormatSpec &spec);
|
||||||
};
|
};
|
||||||
|
|
||||||
// A reference to a string. It can be constructed from a C string,
|
// A reference to a string. It can be constructed from a C string,
|
||||||
@ -443,23 +450,23 @@ class ArgFormatter {
|
|||||||
public:
|
public:
|
||||||
explicit ArgFormatter(Formatter &f) : formatter_(f) {}
|
explicit ArgFormatter(Formatter &f) : formatter_(f) {}
|
||||||
|
|
||||||
void Write(const std::string &s, unsigned width) {
|
void Write(const std::string &s, const FormatSpec &spec) {
|
||||||
formatter_.Write(s, width);
|
formatter_.Write(s, spec);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// The default formatting function.
|
// The default formatting function.
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Format(ArgFormatter &af, unsigned width, const T &value) {
|
void Format(ArgFormatter &af, const FormatSpec &spec, const T &value) {
|
||||||
std::ostringstream os;
|
std::ostringstream os;
|
||||||
os << value;
|
os << value;
|
||||||
af.Write(os.str(), width);
|
af.Write(os.str(), spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void Formatter::FormatCustomArg(const void *arg, unsigned width) {
|
void Formatter::FormatCustomArg(const void *arg, const FormatSpec &spec) {
|
||||||
ArgFormatter af(*this);
|
ArgFormatter af(*this);
|
||||||
Format(af, width, *static_cast<const T*>(arg));
|
Format(af, spec, *static_cast<const T*>(arg));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline internal::ArgInserter Formatter::operator()(const char *format) {
|
inline internal::ArgInserter Formatter::operator()(const char *format) {
|
||||||
|
@ -257,6 +257,21 @@ TEST(FormatterTest, EmptySpecs) {
|
|||||||
EXPECT_EQ("42", str(Format("{0:}") << 42));
|
EXPECT_EQ("42", str(Format("{0:}") << 42));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(FormatterTest, Fill) {
|
||||||
|
EXPECT_EQ("**42", str(Format("{0:*>4}") << 42));
|
||||||
|
EXPECT_EQ("**-42", str(Format("{0:*>5}") << -42));
|
||||||
|
EXPECT_EQ("***42", str(Format("{0:*>5}") << 42u));
|
||||||
|
EXPECT_EQ("**-42", str(Format("{0:*>5}") << -42l));
|
||||||
|
EXPECT_EQ("***42", str(Format("{0:*>5}") << 42ul));
|
||||||
|
EXPECT_EQ("**-42", str(Format("{0:*>5}") << -42.0));
|
||||||
|
EXPECT_EQ("**-42", str(Format("{0:*>5}") << -42.0l));
|
||||||
|
EXPECT_EQ("c****", str(Format("{0:*<5}") << 'c'));
|
||||||
|
EXPECT_EQ("abc**", str(Format("{0:*<5}") << "abc"));
|
||||||
|
EXPECT_EQ("**0xface", str(Format("{0:*>8}")
|
||||||
|
<< reinterpret_cast<void*>(0xface)));
|
||||||
|
EXPECT_EQ("def**", str(Format("{0:*<5}") << TestString("def")));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(FormatterTest, PlusFlag) {
|
TEST(FormatterTest, PlusFlag) {
|
||||||
EXPECT_EQ("+42", str(Format("{0:+}") << 42));
|
EXPECT_EQ("+42", str(Format("{0:+}") << 42));
|
||||||
EXPECT_EQ("-42", str(Format("{0:+}") << -42));
|
EXPECT_EQ("-42", str(Format("{0:+}") << -42));
|
||||||
@ -484,7 +499,7 @@ void CheckUnknownTypes(
|
|||||||
for (int i = CHAR_MIN; i <= CHAR_MAX; ++i) {
|
for (int i = CHAR_MIN; i <= CHAR_MAX; ++i) {
|
||||||
char c = i;
|
char c = i;
|
||||||
if (std::strchr(types, c) || std::strchr(special, c) || !c) continue;
|
if (std::strchr(types, c) || std::strchr(special, c) || !c) continue;
|
||||||
sprintf(format, "{0:1%c}", c);
|
sprintf(format, "{0:10%c}", c);
|
||||||
if (std::isprint(static_cast<unsigned char>(c)))
|
if (std::isprint(static_cast<unsigned char>(c)))
|
||||||
sprintf(message, "unknown format code '%c' for %s", c, type_name);
|
sprintf(message, "unknown format code '%c' for %s", c, type_name);
|
||||||
else
|
else
|
||||||
@ -629,22 +644,32 @@ TEST(FormatterTest, FormatString) {
|
|||||||
|
|
||||||
TEST(FormatterTest, Write) {
|
TEST(FormatterTest, Write) {
|
||||||
Formatter format;
|
Formatter format;
|
||||||
format.Write("12", 2);
|
fmt::FormatSpec spec = {};
|
||||||
|
spec.fill = ' ';
|
||||||
|
spec.width = 2;
|
||||||
|
format.Write("12", spec);
|
||||||
EXPECT_EQ("12", format.str());
|
EXPECT_EQ("12", format.str());
|
||||||
format.Write("34", 4);
|
spec.width = 4;
|
||||||
|
format.Write("34", spec);
|
||||||
EXPECT_EQ("1234 ", format.str());
|
EXPECT_EQ("1234 ", format.str());
|
||||||
format.Write("56", 0);
|
spec.width = 0;
|
||||||
|
format.Write("56", spec);
|
||||||
EXPECT_EQ("1234 56", format.str());
|
EXPECT_EQ("1234 56", format.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ArgFormatterTest, Write) {
|
TEST(ArgFormatterTest, Write) {
|
||||||
Formatter formatter;
|
Formatter formatter;
|
||||||
fmt::ArgFormatter format(formatter);
|
fmt::ArgFormatter format(formatter);
|
||||||
format.Write("12", 2);
|
fmt::FormatSpec spec = {};
|
||||||
|
spec.fill = ' ';
|
||||||
|
spec.width = 2;
|
||||||
|
format.Write("12", spec);
|
||||||
EXPECT_EQ("12", formatter.str());
|
EXPECT_EQ("12", formatter.str());
|
||||||
format.Write("34", 4);
|
spec.width = 4;
|
||||||
|
format.Write("34", spec);
|
||||||
EXPECT_EQ("1234 ", formatter.str());
|
EXPECT_EQ("1234 ", formatter.str());
|
||||||
format.Write("56", 0);
|
spec.width = 0;
|
||||||
|
format.Write("56", spec);
|
||||||
EXPECT_EQ("1234 56", formatter.str());
|
EXPECT_EQ("1234 56", formatter.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -669,8 +694,8 @@ TEST(FormatterTest, FormatUsingIOStreams) {
|
|||||||
|
|
||||||
class Answer {};
|
class Answer {};
|
||||||
|
|
||||||
void Format(fmt::ArgFormatter &af, unsigned width, Answer) {
|
void Format(fmt::ArgFormatter &af, const fmt::FormatSpec &spec, Answer) {
|
||||||
af.Write("42", width);
|
af.Write("42", spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatterTest, CustomFormat) {
|
TEST(FormatterTest, CustomFormat) {
|
||||||
|
Reference in New Issue
Block a user