Added basic support for %g snprintf format specifier

main
Sam Lantinga 2022-12-29 20:42:07 -08:00
parent cefbeb582f
commit ead4f122e4
2 changed files with 43 additions and 0 deletions

View File

@ -1675,6 +1675,29 @@ SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg)
return length;
}
static size_t
SDL_TrimTrailingFractionalZeroes(char *text, size_t start, size_t length)
{
size_t i, j;
for (i = start; i < length; ++i) {
if (text[i] == '.' || text[i] == ',') {
for (j = length - 1; j > i; --j) {
if (text[j] == '0') {
--length;
} else {
break;
}
}
if (j == i) {
--length;
}
break;
}
}
return length;
}
/* NOLINTNEXTLINE(readability-non-const-parameter) */
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
{
@ -1856,6 +1879,14 @@ int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *f
length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double));
done = SDL_TRUE;
break;
case 'g':
{
size_t starting_length = length;
length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double));
length = SDL_TrimTrailingFractionalZeroes(text, starting_length, length);
done = SDL_TRUE;
break;
}
case 'S':
{
/* In practice this is used on Windows for WCHAR strings */

View File

@ -153,6 +153,18 @@ int stdlib_snprintf(void *arg)
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
result = SDL_snprintf(text, sizeof(text), "%g", 100.0);
expected = "100";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%g\", 100.0)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
SDLTest_AssertCheck(result == 3, "Check result value, expected: 3, got: %d", result);
result = SDL_snprintf(text, sizeof(text), "%g", 100.75);
expected = "100.75";
SDLTest_AssertPass("Call to SDL_snprintf(\"%%g\", 100.75)");
SDLTest_AssertCheck(SDL_strcmp(text, expected) == 0, "Check text, expected: '%s', got: '%s'", expected, text);
SDLTest_AssertCheck(result == 6, "Check result value, expected: 6, got: %d", result);
size = 64;
result = SDL_snprintf(text, sizeof(text), "%zu %s", size, "test");
expected = "64 test";