Skip to content

Commit

Permalink
SDL_vsnprintf() updates for zero-padding:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
sezero committed Sep 27, 2018
1 parent 7689162 commit bb5516a
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/stdlib/SDL_string.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit bb5516a

Please sign in to comment.