gawk-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SCM] gawk branch, stable/printf-rework, updated. gawk-4.1.0-5513-g8d149


From: Arnold Robbins
Subject: [SCM] gawk branch, stable/printf-rework, updated. gawk-4.1.0-5513-g8d1495bf
Date: Wed, 17 Jul 2024 12:13:38 -0400 (EDT)

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gawk".

The branch, stable/printf-rework has been updated
       via  8d1495bf9b1b502a69e3ce593c4e1bbf30ccff1f (commit)
      from  7fdd89f3b30bd6158d7e5e19966e8b0cb72e0077 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.sv.gnu.org/cgit/gawk.git/commit/?id=8d1495bf9b1b502a69e3ce593c4e1bbf30ccff1f

commit 8d1495bf9b1b502a69e3ce593c4e1bbf30ccff1f
Author: Arnold D. Robbins <arnold@skeeve.com>
Date:   Wed Jul 17 19:13:18 2024 +0300

    Start on simplification.

diff --git a/ChangeLog b/ChangeLog
index bc732156..0d439b29 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,8 @@
 2024-07-17         Arnold D. Robbins     <arnold@skeeve.com>
 
        * NEWS: Updated.
+       * printf.c (fill_to_field_width): New function.
+       (format_out_of_range, format_unsigned_integer): Start to use it.
 
 2024-07-16         Arnold D. Robbins     <arnold@skeeve.com>
 
diff --git a/printf.c b/printf.c
index c40280e4..ebc63eb2 100644
--- a/printf.c
+++ b/printf.c
@@ -60,6 +60,7 @@ static const char *format_signed_integer(NODE *arg, struct 
flags *flags);
 static const char *format_unsigned_integer(NODE *arg, struct flags *flags);
 static const char *format_float(NODE *arg, struct flags *flags);
 static const char *format_out_of_range(NODE *arg, struct flags *flags);
+static const char *fill_to_field_width(const char *startval, struct flags 
*flags, int fill);
 
 #ifdef HAVE_MPFR
 
@@ -1278,24 +1279,7 @@ format_unsigned_integer(NODE *arg, struct flags *flags)
                val_len = strlen(buf1);
 
                if (val_len < flags->field_width) {
-                       emalloc(buf2, char *, flags->field_width + 1, 
"format_unsigned_integer");
-                       cp = buf2;
-
-                       fw = flags->field_width;
-                       if (flags->left_just) {
-                               strcpy(cp, buf1);
-                               cp += val_len;
-                               fw -= val_len;
-                               for (; fw > 0; fw--)
-                                       *cp++ = fill;
-                       } else {
-                               for (; fw > val_len; fw--)
-                                       *cp++ = fill;
-                               strcpy(cp, buf1);
-                       }
-
-                       free((void *) buf1);
-                       buf1 = buf2;
+                       buf1 = (char *) fill_to_field_width(buf1, flags, fill);
                        val_len = strlen(buf1);
                }
        } else
@@ -1361,28 +1345,14 @@ format_out_of_range(NODE *arg, struct flags *flags)
        // A NaN or Inf, deal with a field width, if any
        size_t len = strlen(nan_inf_val);
        if (flags->field_width > len) {
-               char *cp = NULL, *buf = NULL;
-               int fw = flags->field_width;
+               char *buf = estrdup(nan_inf_val, len);
 
-               emalloc(buf, char *, fw + 1, "format_out_of_range");
-               cp = buf;
-               if (flags->left_just) {
-                       strcpy(cp, nan_inf_val);
-                       cp += len;
-                       fw -= len;
-                       for (; fw > 0; fw--)
-                               *cp++ = ' ';
-                       *cp = '\0';
-               } else {
-                       for (cp = buf; fw > len; fw--)
-                               *cp++ = ' ';
-                       strcpy(cp, nan_inf_val);
-               }
+               buf = fill_to_field_width(buf, flags, ' ');
 
                return buf;
        }
 
-       return estrdup(nan_inf_val, strlen(nan_inf_val));
+       return estrdup(nan_inf_val, len);
 }
 
 /* compute_zero_flag --- return true if we want to fill with zeros */
@@ -1896,3 +1866,36 @@ add_thousands(const char *original)
 
        return newbuf;
 }
+
+/* fill_to_field_width --- handle justification and padding, frees startval if 
necessary, caller frees return */
+
+static const char *
+fill_to_field_width(const char *startval, struct flags *flags, int fill)
+{
+       size_t l = strlen(startval);
+       char *buf;
+       char *cp;
+       int fw = flags->field_width;
+
+       if (l > fw)     // nothing to do
+               return startval;
+
+       emalloc(buf, char *, l + 1, "fill_to_field_width");
+       cp = buf;
+
+       if (flags->left_just) {
+               strcpy(buf, startval);
+               cp += l;
+               fw -= l;
+               for (; fw > 0; fw--)
+                       *cp++ = fill;
+       } else {
+               for (; fw > l; fw--)
+                       *cp++ = fill;
+               strcpy(cp, startval);
+       }
+
+       free((void *) startval);
+
+       return buf;
+}

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog |  2 ++
 printf.c  | 73 +++++++++++++++++++++++++++++++++------------------------------
 2 files changed, 40 insertions(+), 35 deletions(-)


hooks/post-receive
-- 
gawk



reply via email to

[Prev in Thread] Current Thread [Next in Thread]