[Commits] SDL: SDL_vsnprintf: string printer now honors the precision. (bu...

libsdl.org revision control commits-owner at libsdl.org
Wed Sep 26 00:43:33 PDT 2018


details:   https://hg.libsdl.org/SDL/rev/3c5adcfe014e
changeset: 12227:3c5adcfe014e
user:      Ozkan Sezer <sezeroz at gmail.com>
date:      Wed Sep 26 10:40:02 2018 +0300
description:
SDL_vsnprintf: string printer now honors the precision. (bug #4263.)

diffstat:

 src/stdlib/SDL_string.c |  16 +++++++++++++---
 1 files changed, 13 insertions(+), 3 deletions(-)

diffs (39 lines):

diff -r 4cbf7e663cb2 -r 3c5adcfe014e src/stdlib/SDL_string.c
--- a/src/stdlib/SDL_string.c	Wed Sep 26 10:38:40 2018 +0300
+++ b/src/stdlib/SDL_string.c	Wed Sep 26 10:40:02 2018 +0300
@@ -1374,15 +1374,18 @@
 SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *string)
 {
     size_t length = 0;
-    size_t slen;
+    size_t slen, sz;
 
     if (string == NULL) {
         string = "(null)";
     }
 
-    if (info && info->width && (size_t)info->width > SDL_strlen(string)) {
+    sz = SDL_strlen(string);
+    if (info && info->width > 0 && (size_t)info->width > sz) {
         char fill = info->pad_zeroes ? '0' : ' ';
-        size_t width = info->width - SDL_strlen(string);
+        size_t width = info->width - sz;
+        if (info->precision >= 0 && (size_t)info->precision < sz)
+            width += sz - (size_t)info->precision;
         while (width-- > 0 && maxlen > 0) {
             *text++ = fill;
             ++length;
@@ -1394,6 +1397,13 @@
     length += SDL_min(slen, maxlen);
 
     if (info) {
+        if (info->precision >= 0 && info->precision < sz) {
+            slen = info->precision;
+            if (slen < maxlen) {
+                text[slen] = 0;
+                length -= (sz - slen);
+            }
+        }
         if (info->force_case == SDL_CASE_LOWER) {
             SDL_strlwr(text);
         } else if (info->force_case == SDL_CASE_UPPER) {


More information about the commits mailing list