From bb5516ac8edcd52edb41e216ebb496983fd4ff8e Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Thu, 27 Sep 2018 09:37:36 +0300 Subject: [PATCH] SDL_vsnprintf() updates for zero-padding: - remove force-enabling of pad_zeroes for %u for compatibility (was added in https://hg.libsdl.org/SDL/rev/701f4a25df89) - ignore pad_zeroes for %s and %S - ignore pad_zeroes for %d, %i and %u if a precision is given --- src/stdlib/SDL_string.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c index b65af5497..a41dc2534 100644 --- a/src/stdlib/SDL_string.c +++ b/src/stdlib/SDL_string.c @@ -1415,7 +1415,7 @@ SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *str static void SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *info) -{/* left-pad num with zeroes, if needed. */ +{/* left-pad num with zeroes. */ size_t sz, pad; if (!info || info->precision < 0) @@ -1681,6 +1681,9 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, break; case 'i': case 'd': + if (info.precision >= 0) { + info.pad_zeroes = SDL_FALSE; + } switch (inttype) { case DO_INT: len = SDL_PrintLong(text, left, &info, @@ -1718,7 +1721,9 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, } /* Fall through to unsigned handling */ case 'u': - info.pad_zeroes = SDL_TRUE; + if (info.precision >= 0) { + info.pad_zeroes = SDL_FALSE; + } switch (inttype) { case DO_INT: len = SDL_PrintUnsignedLong(text, left, &info, @@ -1745,12 +1750,14 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, /* In practice this is used on Windows for WCHAR strings */ wchar_t *wide_arg = va_arg(ap, wchar_t *); char *arg = SDL_iconv_string("UTF-8", "UTF-16LE", (char *)(wide_arg), (SDL_wcslen(wide_arg)+1)*sizeof(*wide_arg)); + info.pad_zeroes = SDL_FALSE; len = SDL_PrintString(text, left, &info, arg); SDL_free(arg); done = SDL_TRUE; } break; case 's': + info.pad_zeroes = SDL_FALSE; len = SDL_PrintString(text, left, &info, va_arg(ap, char *)); done = SDL_TRUE; break;