Merge branch 'master' of https://github.com/cppformat/cppformat
Conflicts: format.cc test/format-test.cc
This commit is contained in:
commit
eb7e92e015
64
format.cc
64
format.cc
@ -1073,44 +1073,44 @@ const Char *fmt::BasicFormatter<Char>::format(
|
|||||||
|
|
||||||
// Parse zero flag.
|
// Parse zero flag.
|
||||||
if (*s == '0') {
|
if (*s == '0') {
|
||||||
require_numeric_argument(arg, '0');
|
require_numeric_argument(arg, '0');
|
||||||
spec.align_ = ALIGN_NUMERIC;
|
spec.align_ = ALIGN_NUMERIC;
|
||||||
spec.fill_ = '0';
|
spec.fill_ = '0';
|
||||||
++s;
|
++s;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse width.
|
// Parse width.
|
||||||
if ('0' <= *s && *s <= '9') {
|
if ('0' <= *s && *s <= '9') {
|
||||||
spec.width_ = parse_nonnegative_int(s);
|
spec.width_ = parse_nonnegative_int(s);
|
||||||
} else if (*s == '{') {
|
} else if (*s == '{') {
|
||||||
++s;
|
++s;
|
||||||
const Arg &width_arg = is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
|
const Arg &width_arg = is_name_start(*s) ? parse_arg_name(s) : parse_arg_index(s);
|
||||||
if (*s++ != '}')
|
if (*s++ != '}')
|
||||||
FMT_THROW(FormatError("invalid format string"));
|
FMT_THROW(FormatError("invalid format string"));
|
||||||
ULongLong value = 0;
|
ULongLong value = 0;
|
||||||
switch (width_arg.type) {
|
switch (width_arg.type) {
|
||||||
case Arg::INT:
|
case Arg::INT:
|
||||||
if (width_arg.int_value < 0)
|
if (width_arg.int_value < 0)
|
||||||
FMT_THROW(FormatError("negative width"));
|
FMT_THROW(FormatError("negative width"));
|
||||||
value = width_arg.int_value;
|
value = width_arg.int_value;
|
||||||
break;
|
break;
|
||||||
case Arg::UINT:
|
case Arg::UINT:
|
||||||
value = width_arg.uint_value;
|
value = width_arg.uint_value;
|
||||||
break;
|
break;
|
||||||
case Arg::LONG_LONG:
|
case Arg::LONG_LONG:
|
||||||
if (width_arg.long_long_value < 0)
|
if (width_arg.long_long_value < 0)
|
||||||
FMT_THROW(FormatError("negative width"));
|
FMT_THROW(FormatError("negative width"));
|
||||||
value = width_arg.long_long_value;
|
value = width_arg.long_long_value;
|
||||||
break;
|
break;
|
||||||
case Arg::ULONG_LONG:
|
case Arg::ULONG_LONG:
|
||||||
value = width_arg.ulong_long_value;
|
value = width_arg.ulong_long_value;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
FMT_THROW(FormatError("width is not integer"));
|
FMT_THROW(FormatError("width is not integer"));
|
||||||
}
|
}
|
||||||
if (value > INT_MAX)
|
if (value > INT_MAX)
|
||||||
FMT_THROW(FormatError("number is too big"));
|
FMT_THROW(FormatError("number is too big"));
|
||||||
spec.width_ = static_cast<int>(value);
|
spec.width_ = static_cast<int>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse precision.
|
// Parse precision.
|
||||||
|
|||||||
@ -609,20 +609,20 @@ TEST(FormatterTest, ManyArgs) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEST(FormatterTest, NamedArg) {
|
TEST(FormatterTest, NamedArg) {
|
||||||
char a = 'A', b = 'B', c = 'C';
|
char a = 'A', b = 'B', c = 'C';
|
||||||
EXPECT_EQ("BBAACC", format("{1}{b}{0}{a}{2}{c}", FMT_CAPTURE(a, b, c)));
|
EXPECT_EQ("BBAACC", format("{1}{b}{0}{a}{2}{c}", FMT_CAPTURE(a, b, c)));
|
||||||
EXPECT_EQ(" A", format("{a:>2}", FMT_CAPTURE(a)));
|
EXPECT_EQ(" A", format("{a:>2}", FMT_CAPTURE(a)));
|
||||||
EXPECT_THROW_MSG(format("{a+}", FMT_CAPTURE(a)), FormatError, "missing '}' in format string");
|
EXPECT_THROW_MSG(format("{a+}", FMT_CAPTURE(a)), FormatError, "missing '}' in format string");
|
||||||
EXPECT_THROW_MSG(format("{a}"), FormatError, "argument not found");
|
EXPECT_THROW_MSG(format("{a}"), FormatError, "argument not found");
|
||||||
EXPECT_THROW_MSG(format("{d}", FMT_CAPTURE(a, b, c)), FormatError, "argument not found");
|
EXPECT_THROW_MSG(format("{d}", FMT_CAPTURE(a, b, c)), FormatError, "argument not found");
|
||||||
EXPECT_THROW_MSG(format("{a}{}", FMT_CAPTURE(a)),
|
EXPECT_THROW_MSG(format("{a}{}", FMT_CAPTURE(a)),
|
||||||
FormatError, "cannot switch from manual to automatic argument indexing");
|
FormatError, "cannot switch from manual to automatic argument indexing");
|
||||||
EXPECT_THROW_MSG(format("{}{a}", FMT_CAPTURE(a)),
|
EXPECT_THROW_MSG(format("{}{a}", FMT_CAPTURE(a)),
|
||||||
FormatError, "cannot switch from automatic to manual argument indexing");
|
FormatError, "cannot switch from automatic to manual argument indexing");
|
||||||
EXPECT_EQ(" -42", format("{0:{width}}", -42, fmt::arg("width", 4)));
|
EXPECT_EQ(" -42", format("{0:{width}}", -42, fmt::arg("width", 4)));
|
||||||
EXPECT_EQ("st", format("{0:.{precision}}", "str", fmt::arg("precision", 2)));
|
EXPECT_EQ("st", format("{0:.{precision}}", "str", fmt::arg("precision", 2)));
|
||||||
int n = 100;
|
int n = 100;
|
||||||
EXPECT_EQ(L"n=100", format(L"n={n}", FMT_CAPTURE_W(n)));
|
EXPECT_EQ(L"n=100", format(L"n={n}", FMT_CAPTURE_W(n)));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatterTest, AutoArgIndex) {
|
TEST(FormatterTest, AutoArgIndex) {
|
||||||
@ -920,61 +920,35 @@ TEST(FormatterTest, Width) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatterTest, RuntimeWidth) {
|
TEST(FormatterTest, RuntimeWidth) {
|
||||||
char format_str[BUFFER_SIZE];
|
char format_str[BUFFER_SIZE];
|
||||||
safe_sprintf(format_str, "{0:{%u", UINT_MAX);
|
safe_sprintf(format_str, "{0:{%u", UINT_MAX);
|
||||||
increment(format_str + 4);
|
increment(format_str + 4);
|
||||||
EXPECT_THROW_MSG(format(format_str, 0), FormatError, "number is too big");
|
EXPECT_THROW_MSG(format(format_str, 0), FormatError, "number is too big");
|
||||||
std::size_t size = std::strlen(format_str);
|
std::size_t size = std::strlen(format_str);
|
||||||
format_str[size] = '}';
|
format_str[size] = '}';
|
||||||
format_str[size + 1] = 0;
|
format_str[size + 1] = 0;
|
||||||
EXPECT_THROW_MSG(format(format_str, 0), FormatError, "number is too big");
|
EXPECT_THROW_MSG(format(format_str, 0), FormatError, "number is too big");
|
||||||
format_str[size + 1] = '}';
|
format_str[size + 1] = '}';
|
||||||
format_str[size + 2] = 0;
|
format_str[size + 2] = 0;
|
||||||
EXPECT_THROW_MSG(format(format_str, 0), FormatError, "number is too big");
|
EXPECT_THROW_MSG(format(format_str, 0), FormatError, "number is too big");
|
||||||
|
|
||||||
EXPECT_THROW_MSG(format("{0:{", 0),
|
EXPECT_THROW_MSG(format("{0:{1}}", 0, '0'),
|
||||||
FormatError, "invalid format string");
|
FormatError, "width is not integer");
|
||||||
EXPECT_THROW_MSG(format("{0:{}", 0),
|
EXPECT_THROW_MSG(format("{0:{1}}", 0, 0.0),
|
||||||
FormatError, "cannot switch from manual to automatic argument indexing");
|
FormatError, "width is not integer");
|
||||||
EXPECT_THROW_MSG(format("{0:{?}}", 0),
|
|
||||||
FormatError, "invalid format string");
|
|
||||||
EXPECT_THROW_MSG(format("{0:{1}}", 0),
|
|
||||||
FormatError, "argument index out of range");
|
|
||||||
|
|
||||||
EXPECT_THROW_MSG(format("{0:{0:}}", 0),
|
EXPECT_EQ(" -42", format("{0:{1}}", -42, 4));
|
||||||
FormatError, "invalid format string");
|
EXPECT_EQ(" 42", format("{0:{1}}", 42u, 5));
|
||||||
|
EXPECT_EQ(" -42", format("{0:{1}}", -42l, 6));
|
||||||
EXPECT_THROW_MSG(format("{0:{1}}", 0, -1),
|
EXPECT_EQ(" 42", format("{0:{1}}", 42ul, 7));
|
||||||
FormatError, "negative width");
|
EXPECT_EQ(" -42", format("{0:{1}}", -42ll, 6));
|
||||||
EXPECT_THROW_MSG(format("{0:{1}}", 0, (INT_MAX + 1u)),
|
EXPECT_EQ(" 42", format("{0:{1}}", 42ull, 7));
|
||||||
FormatError, "number is too big");
|
EXPECT_EQ(" -1.23", format("{0:{1}}", -1.23, 8));
|
||||||
EXPECT_THROW_MSG(format("{0:{1}}", 0, -1l),
|
EXPECT_EQ(" -1.23", format("{0:{1}}", -1.23l, 9));
|
||||||
FormatError, "negative width");
|
EXPECT_EQ(" 0xcafe", format("{0:{1}}", reinterpret_cast<void*>(0xcafe), 10));
|
||||||
if (fmt::internal::check(sizeof(long) > sizeof(int))) {
|
EXPECT_EQ("x ", format("{0:{1}}", 'x', 11));
|
||||||
long value = INT_MAX;
|
EXPECT_EQ("str ", format("{0:{1}}", "str", 12));
|
||||||
EXPECT_THROW_MSG(format("{0:{1}}", 0, (value + 1)),
|
EXPECT_EQ("test ", format("{0:{1}}", TestString("test"), 13));
|
||||||
FormatError, "number is too big");
|
|
||||||
}
|
|
||||||
EXPECT_THROW_MSG(format("{0:{1}}", 0, (INT_MAX + 1ul)),
|
|
||||||
FormatError, "number is too big");
|
|
||||||
|
|
||||||
EXPECT_THROW_MSG(format("{0:{1}}", 0, '0'),
|
|
||||||
FormatError, "width is not integer");
|
|
||||||
EXPECT_THROW_MSG(format("{0:{1}}", 0, 0.0),
|
|
||||||
FormatError, "width is not integer");
|
|
||||||
|
|
||||||
EXPECT_EQ(" -42", format("{0:{1}}", -42, 4));
|
|
||||||
EXPECT_EQ(" 42", format("{0:{1}}", 42u, 5));
|
|
||||||
EXPECT_EQ(" -42", format("{0:{1}}", -42l, 6));
|
|
||||||
EXPECT_EQ(" 42", format("{0:{1}}", 42ul, 7));
|
|
||||||
EXPECT_EQ(" -42", format("{0:{1}}", -42ll, 6));
|
|
||||||
EXPECT_EQ(" 42", format("{0:{1}}", 42ull, 7));
|
|
||||||
EXPECT_EQ(" -1.23", format("{0:{1}}", -1.23, 8));
|
|
||||||
EXPECT_EQ(" -1.23", format("{0:{1}}", -1.23l, 9));
|
|
||||||
EXPECT_EQ(" 0xcafe", format("{0:{1}}", reinterpret_cast<void*>(0xcafe), 10));
|
|
||||||
EXPECT_EQ("x ", format("{0:{1}}", 'x', 11));
|
|
||||||
EXPECT_EQ("str ", format("{0:{1}}", "str", 12));
|
|
||||||
EXPECT_EQ("test ", format("{0:{1}}", TestString("test"), 13));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(FormatterTest, Precision) {
|
TEST(FormatterTest, Precision) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user