changeset 280:454f0d37d94b

Implement th_printf_vput_pad() helper and use it where needed.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 22 Feb 2016 11:53:47 +0200
parents 15e2ee6c7bfc
children 0c70dcfb6796
files th_string.c
diffstat 1 files changed, 19 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Mon Feb 22 11:49:05 2016 +0200
+++ b/th_string.c	Mon Feb 22 11:53:47 2016 +0200
@@ -124,6 +124,17 @@
 //
 // Simple implementations of printf() type functions
 //
+static int th_printf_vput_pad(th_printf_ctx *ctx, th_printf_vputch vputch, int nwidth, const char padChar)
+{
+    while (nwidth--)
+    {
+        int ret;
+        if ((ret = vputch(ctx, padChar)) == EOF)
+            return ret;
+    }
+    return 0;
+}
+
 static int th_printf_vput_int(th_printf_ctx *ctx, th_printf_vputch vputch,
     int pval, const int radix, const char padMode, const char padChar,
     const int width, const BOOL unsig, const BOOL upcase, const BOOL sign)
@@ -174,14 +185,8 @@
     int nwidth = width - pos;
 
     // Prefix padding?
-    if (padMode != '-' && nwidth > 0)
-    {
-        while (nwidth--)
-        {
-            if ((ret = vputch(ctx, padMode)) == EOF)
-                goto out;
-        }
-    }
+    if (padMode != '-' && nwidth > 0 && (ret = th_printf_vput_pad(ctx, vputch, nwidth, padMode)) == EOF)
+        goto out;
 
     // Output the value
     while (pos--)
@@ -191,14 +196,8 @@
     }
 
     // Postfix padding?
-    if (padMode == '-' && nwidth > 0)
-    {
-        while (nwidth--)
-        {
-            if ((ret = vputch(ctx, ' ')) == EOF)
-                goto out;
-        }
-    }
+    if (padMode == '-' && nwidth > 0 && (ret = th_printf_vput_pad(ctx, vputch, nwidth, ' ')) == EOF)
+        goto out;
 
 out:
     return ret;
@@ -218,14 +217,8 @@
     nwidth = width - strlen(str);
 
     // Prefix padding?
-    if (padMode != '-' && nwidth > 0)
-    {
-        while (nwidth--)
-        {
-            if ((ret = vputch(ctx, padMode)) == EOF)
-                goto out;
-        }
-    }
+    if (padMode != '-' && nwidth > 0 && (ret = th_printf_vput_pad(ctx, vputch, nwidth, padMode)) == EOF)
+        goto out;
 
     while (*str)
     {
@@ -234,14 +227,8 @@
     }
 
     // Postfix padding?
-    if (padMode == '-' && nwidth > 0)
-    {
-        while (nwidth--)
-        {
-            if ((ret = vputch(ctx, ' ')) == EOF)
-                goto out;
-        }
-    }
+    if (padMode == '-' && nwidth > 0 && (ret = th_printf_vput_pad(ctx, vputch, nwidth, ' ')) == EOF)
+        goto out;
 
 out:
     return ret;