changeset 324:0084618812ee

Fix th_printf_vput_intstr() so that most tests pass. Not certain how "correct" the implementation is yet, tho.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 23 Feb 2016 12:15:51 +0200
parents 63c5b1bf8b98
children 40ce4106f4ad
files th_string.c
diffstat 1 files changed, 12 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Tue Feb 23 11:23:32 2016 +0200
+++ b/th_string.c	Tue Feb 23 12:15:51 2016 +0200
@@ -163,36 +163,30 @@
     const char f_sign)
 {
     // Calculate necessary padding, if any
-    int ret, nwidth = f_width - f_len;
-
-    if (f_sign)
-        nwidth--;
+    int ret, nwidth;
 
-    if (f_prec > 0 && f_prec > f_len)
-    {
-        nwidth = nwidth - f_prec + 1;
-        f_flags &= ~TH_PF_ZERO;
-    }
+    f_prec = (f_prec > f_len) ? f_prec - f_len : 0;
+    nwidth = f_width - f_len - f_prec - (f_sign ? 1 : 0);
 
     // Prefix padding?
+    if (f_sign && (f_flags & TH_PF_ZERO) && (ret = vputch(ctx, f_sign)) == EOF)
+        return ret;
+
     if ((ret = th_printf_pad_pre(ctx, vputch, nwidth, f_flags)) == EOF)
         return ret;
 
-    if (f_sign && (ret = vputch(ctx, f_sign)) == EOF)
+    if (f_sign && (f_flags & TH_PF_ZERO) == 0 && (ret = vputch(ctx, f_sign)) == EOF)
         return ret;
 
-    if (f_prec > 0 && f_prec > f_len)
+    while (f_prec-- > 0)
     {
-        while (--f_prec)
-        {
-            int ret;
-            if ((ret = vputch(ctx, '0')) == EOF)
-                return ret;
-        }
+        int ret;
+        if ((ret = vputch(ctx, '0')) == EOF)
+            return ret;
     }
 
     // Output the value
-    while (f_len--)
+    while (f_len-- > 0)
     {
         if ((ret = vputch(ctx, buf[f_len])) == EOF)
             return ret;