SDL_vsnprintf: implemented '+' flag for signed integers printing. it is, of course, ignored for %u, %x, %o and %p.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index 94e461b..0fedcf4 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;
}