changeset 320:0362ea9872f0

Some work on sign handling.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 23 Feb 2016 01:42:34 +0200
parents f2af6049d958
children f7d72b7bebf3
files th_printf1.c th_string.c
diffstat 2 files changed, 11 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/th_printf1.c	Tue Feb 23 01:39:49 2016 +0200
+++ b/th_printf1.c	Tue Feb 23 01:42:34 2016 +0200
@@ -20,7 +20,7 @@
 {
     char buf[64];
     size_t pos = 0;
-    int ret = 0;
+    char f_sign = 0;
 #ifdef TH_PFUNC_SIGNED
     BOOL neg = FALSE;
 #endif
@@ -59,20 +59,11 @@
     // Do we want a sign prefix? Not for unsigned values
 #ifdef TH_PFUNC_SIGNED
     if (!unsig)
-    {
-        char ch = (f_flags & TH_PF_SIGN) ? (neg ? '-' : '+') : (neg ? '-' : ((f_flags & TH_PF_SPACE) ? ' ' : 0));
-        if (ch && (ret = vputch(ctx, ch)) == EOF)
-            goto out;
-    }
+        f_sign = (f_flags & TH_PF_SIGN) ? (neg ? '-' : '+') : (neg ? '-' : ((f_flags & TH_PF_SPACE) ? ' ' : 0));
 #endif
 
     // Output the data
-    return th_printf_vput_intstr(ctx, vputch, buf, f_width, pos, f_prec, f_flags);
-    
-#ifdef TH_PFUNC_SIGNED
-out:
-#endif
-    return ret;
+    return th_printf_vput_intstr(ctx, vputch, buf, f_width, pos, f_prec, f_flags, f_sign);
 }
 
 
--- a/th_string.c	Tue Feb 23 01:39:49 2016 +0200
+++ b/th_string.c	Tue Feb 23 01:42:34 2016 +0200
@@ -159,11 +159,15 @@
 
 
 int th_printf_vput_intstr(th_printf_ctx *ctx, th_printf_vputch vputch,
-    const char *buf, const int f_width, int pos, int f_prec, int f_flags)
+    const char *buf, const int f_width, int pos, int f_prec, int f_flags,
+    const char f_sign)
 {
     // Calculate necessary padding, if any
     int ret, nwidth = f_width - pos;
 
+    if (f_sign)
+        nwidth--;
+
     if (f_prec > 0 && f_prec > pos)
     {
         nwidth = nwidth - f_prec + 1;
@@ -174,6 +178,9 @@
     if ((ret = th_printf_pad_pre(ctx, vputch, nwidth, f_flags)) == EOF)
         return ret;
 
+    if (f_sign && (ret = vputch(ctx, f_sign)) == EOF)
+        return ret;
+
     if (f_prec > 0 && f_prec > pos)
     {
         while (--f_prec)