From 31596f23cec7f20e83e91f8cc8393c512a1d8a77 Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Sat, 29 Sep 2018 01:24:10 +0300 Subject: [PATCH] SDL_vsnprintf: implemented '+' flag for signed integers printing. it is, of course, ignored for %u, %x, %o and %p. --- src/stdlib/SDL_string.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c index 94e461bbf4c2a..0fedcf4355e10 100644 --- a/src/stdlib/SDL_string.c +++ b/src/stdlib/SDL_string.c @@ -1361,7 +1361,7 @@ typedef enum typedef struct { SDL_bool left_justify; /* for now: ignored. */ - SDL_bool force_sign; /* for now: used only by float printer, ignored otherwise. */ + SDL_bool force_sign; SDL_bool force_type; /* for now: used only by float printer, ignored otherwise. */ SDL_bool pad_zeroes; SDL_letter_case force_case; @@ -1453,9 +1453,13 @@ SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *info) static size_t SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value) { - char num[130]; + char num[130], *p = num; + + if (info->force_sign && value >= 0L) { + *p++ = '+'; + } - SDL_ltoa(value, num, info ? info->radix : 10); + SDL_ltoa(value, p, info ? info->radix : 10); SDL_IntPrecisionAdjust(num, maxlen, info); return SDL_PrintString(text, maxlen, info, num); } @@ -1473,9 +1477,13 @@ SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned static size_t SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value) { - char num[130]; + char num[130], *p = num; + + if (info->force_sign && value >= (Sint64)0) { + *p++ = '+'; + } - SDL_lltoa(value, num, info ? info->radix : 10); + SDL_lltoa(value, p, info ? info->radix : 10); SDL_IntPrecisionAdjust(num, maxlen, info); return SDL_PrintString(text, maxlen, info, num); } @@ -1737,6 +1745,7 @@ SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, } /* Fall through to unsigned handling */ case 'u': + info.force_sign = SDL_FALSE; if (info.precision >= 0) { info.pad_zeroes = SDL_FALSE; }