[Commits] SDL: SDL_vsnprintf: implement precision for the integral value p...

libsdl.org revision control commits-owner at libsdl.org
Wed Sep 26 14:32:23 PDT 2018


details:   https://hg.libsdl.org/SDL/rev/d3b8ea488be8
changeset: 12232:d3b8ea488be8
user:      Ozkan Sezer <sezeroz at gmail.com>
date:      Thu Sep 27 00:32:15 2018 +0300
description:
SDL_vsnprintf: implement precision for the integral value printers.

diffstat:

 src/stdlib/SDL_string.c |  27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)

diffs (64 lines):

diff -r e653f009e6c0 -r d3b8ea488be8 src/stdlib/SDL_string.c
--- a/src/stdlib/SDL_string.c	Wed Sep 26 11:17:43 2018 -0700
+++ b/src/stdlib/SDL_string.c	Thu Sep 27 00:32:15 2018 +0300
@@ -1413,12 +1413,36 @@
     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 @@
     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 @@
     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 @@
     char num[130];
 
     SDL_ulltoa(value, num, info ? info->radix : 10);
+    SDL_IntPrecisionAdjust(num, maxlen, info);
     return SDL_PrintString(text, maxlen, info, num);
 }
 


More information about the commits mailing list