[Commits] SDL: SDL_vsnprintf: implemented '+' flag for signed integers pr...

libsdl.org revision control commits-owner at libsdl.org
Fri Sep 28 15:24:22 PDT 2018


details:   https://hg.libsdl.org/SDL/rev/f1ac9de30ee1
changeset: 12254:f1ac9de30ee1
user:      Ozkan Sezer <sezeroz at gmail.com>
date:      Sat Sep 29 01:24:10 2018 +0300
description:
SDL_vsnprintf:  implemented '+' flag for signed integers printing.

it is, of course, ignored for %u, %x, %o and %p.

diffstat:

 src/stdlib/SDL_string.c |  19 ++++++++++++++-----
 1 files changed, 14 insertions(+), 5 deletions(-)

diffs (52 lines):

diff -r 2dbf011db466 -r f1ac9de30ee1 src/stdlib/SDL_string.c
--- a/src/stdlib/SDL_string.c	Sat Sep 29 00:51:24 2018 +0300
+++ b/src/stdlib/SDL_string.c	Sat Sep 29 01:24:10 2018 +0300
@@ -1361,7 +1361,7 @@
 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 @@
 static size_t
 SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value)
 {
-    char num[130];
+    char num[130], *p = num;
 
-    SDL_ltoa(value, num, info ? info->radix : 10);
+    if (info->force_sign && value >= 0L) {
+        *p++ = '+';
+    }
+
+    SDL_ltoa(value, p, info ? info->radix : 10);
     SDL_IntPrecisionAdjust(num, maxlen, info);
     return SDL_PrintString(text, maxlen, info, num);
 }
@@ -1473,9 +1477,13 @@
 static size_t
 SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value)
 {
-    char num[130];
+    char num[130], *p = num;
 
-    SDL_lltoa(value, num, info ? info->radix : 10);
+    if (info->force_sign && value >= (Sint64)0) {
+        *p++ = '+';
+    }
+
+    SDL_lltoa(value, p, info ? info->radix : 10);
     SDL_IntPrecisionAdjust(num, maxlen, info);
     return SDL_PrintString(text, maxlen, info, num);
 }
@@ -1737,6 +1745,7 @@
                     }
                     /* Fall through to unsigned handling */
                 case 'u':
+                    info.force_sign = SDL_FALSE;
                     if (info.precision >= 0) {
                         info.pad_zeroes = SDL_FALSE;
                     }


More information about the commits mailing list