SDL_vsnprintf: implement precision for the integral value printers.
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 54 55 56 57 58 59 60 61 62 63 64 65
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index 1f55b51..5ca6526 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -1413,12 +1413,36 @@ SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *str
return length;
}
+static void
+SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *info)
+{/* left-pad num with zeroes, if needed. */
+ size_t sz, pad, i;
+
+ if (!info || info->precision < 0)
+ return;
+
+ if (*num == '-')
+ ++num;
+ sz = SDL_strlen(num);
+ if (sz < (size_t)info->precision) {
+ pad = (size_t)info->precision - sz;
+ if (pad + sz + 1 <= maxlen) { /* otherwise ignore the precision */
+ SDL_memmove(num + pad, num, sz + 1);
+ for(i = 0; i < pad; ++i) {
+ num[i] = '0';
+ }
+ }
+ }
+ info->precision = -1;/* so that SDL_PrintString() doesn't make a mess. */
+}
+
static size_t
SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value)
{
char num[130];
SDL_ltoa(value, num, info ? info->radix : 10);
+ SDL_IntPrecisionAdjust(num, maxlen, info);
return SDL_PrintString(text, maxlen, info, num);
}
@@ -1428,6 +1452,7 @@ SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned
char num[130];
SDL_ultoa(value, num, info ? info->radix : 10);
+ SDL_IntPrecisionAdjust(num, maxlen, info);
return SDL_PrintString(text, maxlen, info, num);
}
@@ -1437,6 +1462,7 @@ SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value)
char num[130];
SDL_lltoa(value, num, info ? info->radix : 10);
+ SDL_IntPrecisionAdjust(num, maxlen, info);
return SDL_PrintString(text, maxlen, info, num);
}
@@ -1446,6 +1472,7 @@ SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Uint6
char num[130];
SDL_ulltoa(value, num, info ? info->radix : 10);
+ SDL_IntPrecisionAdjust(num, maxlen, info);
return SDL_PrintString(text, maxlen, info, num);
}