Make precision and alignment work together.
This commit is contained in:
parent
f430516d3f
commit
6238f2daa1
24
format.h
24
format.h
@ -1310,21 +1310,28 @@ typename fmt::BasicWriter<Char>::CharPtr
|
|||||||
unsigned num_digits, const Spec &spec,
|
unsigned num_digits, const Spec &spec,
|
||||||
const char *prefix, unsigned prefix_size) {
|
const char *prefix, unsigned prefix_size) {
|
||||||
unsigned width = spec.width();
|
unsigned width = spec.width();
|
||||||
|
Alignment align = spec.align();
|
||||||
if (spec.precision() > static_cast<int>(num_digits)) {
|
if (spec.precision() > static_cast<int>(num_digits)) {
|
||||||
// Octal prefix '0' is counted as a digit, so ignore it if precision
|
// Octal prefix '0' is counted as a digit, so ignore it if precision
|
||||||
// is specified.
|
// is specified.
|
||||||
if (prefix_size > 0 && prefix[prefix_size - 1] == '0')
|
if (prefix_size > 0 && prefix[prefix_size - 1] == '0')
|
||||||
--prefix_size;
|
--prefix_size;
|
||||||
unsigned number_size = prefix_size + spec.precision();
|
unsigned number_size = prefix_size + spec.precision();
|
||||||
if (number_size < width) {
|
AlignSpec subspec(number_size, '0', ALIGN_NUMERIC);
|
||||||
buffer_.reserve(width);
|
if (number_size >= width)
|
||||||
unsigned size = width - number_size;
|
return PrepareBufferForInt(num_digits, subspec, prefix, prefix_size);
|
||||||
CharPtr p = GrowBuffer(size);
|
buffer_.reserve(width);
|
||||||
std::fill(p, p + size, spec.fill());
|
unsigned fill_size = width - number_size;
|
||||||
// TODO: take alignment into account
|
if (align != ALIGN_LEFT) {
|
||||||
|
CharPtr p = GrowBuffer(fill_size);
|
||||||
|
std::fill(p, p + fill_size, spec.fill());
|
||||||
}
|
}
|
||||||
return PrepareBufferForInt(num_digits,
|
CharPtr result = PrepareBufferForInt(num_digits, subspec, prefix, prefix_size);
|
||||||
AlignSpec(number_size, '0', ALIGN_NUMERIC), prefix, prefix_size);
|
if (align == ALIGN_LEFT) {
|
||||||
|
CharPtr p = GrowBuffer(fill_size);
|
||||||
|
std::fill(p, p + fill_size, spec.fill());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
unsigned size = prefix_size + num_digits;
|
unsigned size = prefix_size + num_digits;
|
||||||
if (width <= size) {
|
if (width <= size) {
|
||||||
@ -1334,7 +1341,6 @@ typename fmt::BasicWriter<Char>::CharPtr
|
|||||||
}
|
}
|
||||||
CharPtr p = GrowBuffer(width);
|
CharPtr p = GrowBuffer(width);
|
||||||
CharPtr end = p + width;
|
CharPtr end = p + width;
|
||||||
Alignment align = spec.align();
|
|
||||||
// TODO: error if fill is not convertible to Char
|
// TODO: error if fill is not convertible to Char
|
||||||
Char fill = static_cast<Char>(spec.fill());
|
Char fill = static_cast<Char>(spec.fill());
|
||||||
if (align == ALIGN_LEFT) {
|
if (align == ALIGN_LEFT) {
|
||||||
|
@ -241,7 +241,11 @@ TEST(PrintfTest, Precision) {
|
|||||||
EXPECT_PRINTF(" 00042", "%7.5o", 042);
|
EXPECT_PRINTF(" 00042", "%7.5o", 042);
|
||||||
EXPECT_PRINTF(" 00042", "%#10.5o", 042);
|
EXPECT_PRINTF(" 00042", "%#10.5o", 042);
|
||||||
|
|
||||||
// TODO: test left alignment
|
EXPECT_PRINTF("00042 ", "%-7.5d", 42);
|
||||||
|
EXPECT_PRINTF("00042 ", "%-7.5x", 0x42);
|
||||||
|
EXPECT_PRINTF("0x00042 ", "%-#10.5x", 0x42);
|
||||||
|
EXPECT_PRINTF("00042 ", "%-7.5o", 042);
|
||||||
|
EXPECT_PRINTF("00042 ", "%-#10.5o", 042);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: test length and type specifier
|
// TODO: test length and type specifier
|
||||||
|
Loading…
Reference in New Issue
Block a user