vsnprintf: Right justified prec modified defaults to fill by 0.

This commit is contained in:
Бранимир Караџић
2024-05-02 20:00:34 -07:00
parent 34ba9c7a5c
commit db8a6d7915
2 changed files with 33 additions and 3 deletions

View File

@@ -758,7 +758,7 @@ namespace bx
len--;
}
int32_t padding = _param.width > len ? _param.width - len - hasSign: 0;
const int32_t padding = _param.width > len ? _param.width - len - hasSign: 0;
if (!_param.left)
{
@@ -769,7 +769,24 @@ namespace bx
sign = '\0';
}
size += writeRep(_writer, _param.fill, max(0, padding), _err);
if (_param.width < _param.prec)
{
size += writeRep(_writer, _param.fill, max(0, padding), _err);
}
else
{
const int32_t maxPrec = max(_param.prec, len);
const int32_t fillLen = max(0, _param.width - maxPrec - hasSign);
size += writeRep(_writer, _param.fill, fillLen, _err);
if ('\0' != sign)
{
size += write(_writer, sign, _err);
sign = '\0';
}
size += writeRep(_writer, '0', max(0, padding-fillLen), _err);
}
}
if ('\0' != sign)

View File

@@ -64,7 +64,16 @@ static bool test(const char* _expected, const char* _format, ...)
if (!result)
{
printf("result (%d) '%s', expected (%d) '%s'\n", len, temp, max-1, _expected);
printf("---\n");
printf("printf format '%s'\n", _format);
printf(" result (%4d) '%s'\n", len, temp);
printf(" expected (%4d) '%s'\n", max-1, _expected);
va_start(argList, _format);
len = ::vsnprintf(temp, max, _format, argList);
va_end(argList);
printf("CRT vsnprintf (%d) '%s'\n", len, temp);
}
return result;
@@ -243,6 +252,10 @@ TEST_CASE("Format modifiers", "[string][printf]")
REQUIRE(test("|1 |", "|%-10.0f|", 1.0f) );
REQUIRE(test("|1. |", "|%#-10.0f|", 1.0f) );
REQUIRE(test("|+1. |", "|%+#-10.0f|", 1.0f) );
REQUIRE(test("| 00013: -00089|", "|%10.5d:%10.5d|", 13, -89) );
REQUIRE(test("| -00013: +00089|", "|%10.5d:%+10.5d|", -13, 89) );
REQUIRE(test("| -00013: -00089|", "|%10.5d:%10.5d|", -13, -89) );
}
TEST_CASE("Format %p", "[string][printf]")