changeset 368:51a04243a5f6

Some cleanup work on th_printf_vput_int(). Passes more tests, but it's not perfect.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 29 Feb 2016 13:16:49 +0200
parents dfd1e7b3bf2a
children f26290f8d35e
files th_string.c
diffstat 1 files changed, 20 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Mon Feb 29 12:00:23 2016 +0200
+++ b/th_string.c	Mon Feb 29 13:16:49 2016 +0200
@@ -136,20 +136,20 @@
 
 
 static int th_printf_pad_pre(th_printf_ctx *ctx, th_printf_vputch vputch,
-    int f_width, const int f_flags, const char ch)
+    int f_width, const int f_flags)
 {
     if (f_width > 0 && (f_flags & TH_PF_LEFT) == 0)
-        return th_printf_vput_repch(ctx, vputch, f_width, ch);
+        return th_printf_vput_repch(ctx, vputch, f_width, ' ');
     else
         return 0;
 }
 
 
 static int th_printf_pad_post(th_printf_ctx *ctx, th_printf_vputch vputch,
-    int f_width, const int f_flags, const char ch)
+    int f_width, const int f_flags)
 {
     if (f_width > 0 && (f_flags & TH_PF_LEFT))
-        return th_printf_vput_repch(ctx, vputch, f_width, ch);
+        return th_printf_vput_repch(ctx, vputch, f_width, ' ');
     else
         return 0;
 }
@@ -205,24 +205,27 @@
         (f_neg ? '-' : ((f_flags & TH_PF_SPACE) ? ' ' : 0)));
 
     // Calculate necessary padding, etc
+    if (f_sign)
+        f_width--;
+
+    if ((f_flags & TH_PF_ZERO) && f_prec < 0 && (f_flags & TH_PF_LEFT) == 0)
+        f_prec = f_width;
+
     f_prec = (f_prec > f_len) ? f_prec - f_len : 0;
-    nwidth = f_width - f_len - f_prec - (f_sign ? 1 : 0) - (f_altstr ? strlen(f_altstr) : 0);
+    nwidth = f_width - f_len - f_prec - (f_altstr ? strlen(f_altstr) : 0);
 
     // Prefix padding?
-    if (f_sign && (f_flags & TH_PF_ZERO) && (ret = vputch(ctx, f_sign)) == EOF)
+    if ((ret = th_printf_pad_pre(ctx, vputch, nwidth, f_flags)) == EOF)
         return ret;
 
-    if ((ret = th_printf_pad_pre(ctx, vputch, nwidth, f_flags, ' ')) == EOF)
+    // Do we want a sign prefix? Not for unsigned values
+    if (f_sign && (ret = vputch(ctx, f_sign)) == EOF)
         return ret;
 
     if (f_altstr && (ret = th_printf_vput_pstr(ctx, vputch, f_altstr)) == EOF)
         return ret;
 
-    // Do we want a sign prefix? Not for unsigned values
-    if (f_sign && (f_flags & TH_PF_ZERO) == 0 && (ret = vputch(ctx, f_sign)) == EOF)
-        return ret;
-
-    if ((ret = th_printf_vput_repch(ctx, vputch, f_prec, '0')) == EOF)
+    if (f_prec > 0 && (ret = th_printf_vput_repch(ctx, vputch, f_prec, '0')) == EOF)
         return ret;
 
     // Output the value
@@ -233,7 +236,7 @@
     }
 
     // Postfix padding?
-    return th_printf_pad_post(ctx, vputch, nwidth, f_flags, ' ');
+    return th_printf_pad_post(ctx, vputch, nwidth, f_flags);
 }
 
 
@@ -253,7 +256,7 @@
     nwidth = f_width - f_len;
 
     // Prefix padding?
-    if ((ret = th_printf_pad_pre(ctx, vputch, nwidth, f_flags, ' ')) == EOF)
+    if ((ret = th_printf_pad_pre(ctx, vputch, nwidth, f_flags)) == EOF)
         return ret;
 
     while (*str && f_len--)
@@ -263,10 +266,7 @@
     }
 
     // Postfix padding?
-    if ((ret = th_printf_pad_post(ctx, vputch, nwidth, f_flags, ' ')) == EOF)
-        return ret;
-
-    return ret;
+    return th_printf_pad_post(ctx, vputch, nwidth, f_flags);
 }
 
 
@@ -382,11 +382,11 @@
                     return -104;
 
                 case 'c':
-                    if ((ret = th_printf_pad_pre(ctx, vputch, f_width - 1, f_flags, ' ')) == EOF)
+                    if ((ret = th_printf_pad_pre(ctx, vputch, f_width - 1, f_flags)) == EOF)
                         goto out;
                     if ((ret = vputch(ctx, va_arg(ap, int))) == EOF)
                         goto out;
-                    if ((ret = th_printf_pad_post(ctx, vputch, f_width - 1, f_flags, ' ')) == EOF)
+                    if ((ret = th_printf_pad_post(ctx, vputch, f_width - 1, f_flags)) == EOF)
                         goto out;
                     break;