mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-26 00:21:13 +00:00
Add support for negative width.
This commit is contained in:
parent
91e6bc8b97
commit
4099a1269b
35
format.cc
35
format.cc
@ -609,18 +609,43 @@ unsigned fmt::BasicWriter<Char>::PrintfParser::ParseHeader(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Parse flags and width.
|
|
||||||
ParseFlags(spec, s);
|
ParseFlags(spec, s);
|
||||||
|
// Parse width.
|
||||||
if (*s >= '0' && *s <= '9') {
|
if (*s >= '0' && *s <= '9') {
|
||||||
spec.width_ = internal::ParseNonnegativeInt(s, error);
|
spec.width_ = internal::ParseNonnegativeInt(s, error);
|
||||||
} else if (*s == '*') {
|
} else if (*s == '*') {
|
||||||
++s;
|
++s;
|
||||||
const ArgInfo &arg = HandleArgIndex(UINT_MAX, error);
|
const ArgInfo &arg = HandleArgIndex(UINT_MAX, error);
|
||||||
if (arg.type <= LAST_INTEGER_TYPE)
|
ULongLong width = 0;
|
||||||
spec.width_ = GetIntValue(arg);
|
switch (arg.type) {
|
||||||
else if (!error)
|
case INT:
|
||||||
|
width = arg.int_value;
|
||||||
|
if (arg.int_value < 0) {
|
||||||
|
spec.align_ = ALIGN_LEFT;
|
||||||
|
width = -width;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case UINT:
|
||||||
|
width = arg.uint_value;
|
||||||
|
break;
|
||||||
|
case LONG_LONG:
|
||||||
|
width = arg.long_long_value;
|
||||||
|
if (arg.long_long_value < 0) {
|
||||||
|
spec.align_ = ALIGN_LEFT;
|
||||||
|
width = -width;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ULONG_LONG:
|
||||||
|
width = arg.ulong_long_value;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (!error)
|
||||||
error = "width is not integer";
|
error = "width is not integer";
|
||||||
// TODO: check for negative width
|
}
|
||||||
|
if (width <= INT_MAX)
|
||||||
|
spec.width_ = static_cast<unsigned>(width);
|
||||||
|
else if (!error)
|
||||||
|
error = "number is too big in format";
|
||||||
}
|
}
|
||||||
return arg_index;
|
return arg_index;
|
||||||
}
|
}
|
||||||
|
@ -221,6 +221,7 @@ TEST(PrintfTest, Width) {
|
|||||||
|
|
||||||
TEST(PrintfTest, DynamicWidth) {
|
TEST(PrintfTest, DynamicWidth) {
|
||||||
EXPECT_EQ(" 42", str(fmt::sprintf("%*d", 5, 42)));
|
EXPECT_EQ(" 42", str(fmt::sprintf("%*d", 5, 42)));
|
||||||
|
EXPECT_EQ("42 ", str(fmt::sprintf("%*d", -5, 42)));
|
||||||
EXPECT_THROW_MSG(fmt::sprintf("%*d", 5.0, 42), FormatError,
|
EXPECT_THROW_MSG(fmt::sprintf("%*d", 5.0, 42), FormatError,
|
||||||
"width is not integer");
|
"width is not integer");
|
||||||
EXPECT_THROW_MSG(fmt::sprintf("%*d"), FormatError,
|
EXPECT_THROW_MSG(fmt::sprintf("%*d"), FormatError,
|
||||||
|
Loading…
Reference in New Issue
Block a user