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-5511-g6cb66


From: Arnold Robbins
Subject: [SCM] gawk branch, stable/printf-rework, updated. gawk-4.1.0-5511-g6cb66078
Date: Tue, 16 Jul 2024 13:52:01 -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  6cb660788af1d2e1a04b00f84c5c4af638a85ab3 (commit)
      from  74f3b30538985e5d66cf2932cec33ff044170127 (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=6cb660788af1d2e1a04b00f84c5c4af638a85ab3

commit 6cb660788af1d2e1a04b00f84c5c4af638a85ab3
Author: Arnold D. Robbins <arnold@skeeve.com>
Date:   Tue Jul 16 20:51:33 2024 +0300

    Continue cleaning up printf code.

diff --git a/ChangeLog b/ChangeLog
index db90f750..ce92fdaf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2024-07-16         Arnold D. Robbins     <arnold@skeeve.com>
+
+       * printf.c: Continue cleaning up code and removing now-dead code.
+
 2024-07-16         Arnold D. Robbins     <arnold@skeeve.com>
 
        * awk.h (format_args): Renamed from format_tree. Remove
diff --git a/printf.c b/printf.c
index 820d4cb2..c40280e4 100644
--- a/printf.c
+++ b/printf.c
@@ -99,14 +99,14 @@ mpz2mpfr(mpz_ptr zi)
 
 /*
  * format_args() formats arguments of sprintf,
- * and accordingly to a fmt_string providing a format like in
- * printf family from C library.  Returns a string node which value
- * is a formatted string.  Called by  sprintf function.
+ * and according to a fmt_string providing a format like in
+ * printf family from C library.  Returns a string node whose value
+ * is a formatted string.  Called by sprintf function.
  *
  * It is one of the uglier parts of gawk.  Thanks to Michal Jaegermann
  * for taming this beast and making it compatible with ANSI C.
  *
- * July 2024: Refactored into something manageable.
+ * July 2024: Refactored into something (more) manageable.
  */
 
 NODE *
@@ -171,21 +171,12 @@ format_args(
        bool space_flag;
        bool plus_flag;
 
-       /*
-        * Although this is an array, the elements serve two different
-        * purposes. The first element is the general buffer meant
-        * to hold the entire result string.  The second one is a
-        * temporary buffer for large floating point values. They
-        * could just as easily be separate variables, and the
-        * code might arguably be clearer.
-        */
        struct {
                char *buf;
                size_t bufsize;
                char stackbuf[30];
-       } cpbufs[2];
-#define cpbuf  cpbufs[0].buf
-       char *cend = &cpbufs[0].stackbuf[sizeof(cpbufs[0].stackbuf)];
+       } cpbuf;
+       char *cend = &cpbuf.stackbuf[sizeof(cpbuf.stackbuf)];
        char *cp;
        const char *fill;
        char signchar = '\0';
@@ -212,33 +203,8 @@ format_args(
 
        cur_arg = 1;
 
-       {
-               size_t k;
-               for (k = 0; k < sizeof(cpbufs)/sizeof(cpbufs[0]); k++) {
-                       cpbufs[k].bufsize = sizeof(cpbufs[k].stackbuf);
-                       cpbufs[k].buf = cpbufs[k].stackbuf;
-               }
-       }
-
-       /*
-        * The point of this goop is to grow the buffer
-        * holding the converted number, so that large
-        * values don't overflow a fixed length buffer.
-        */
-#define PREPEND(CH) do {       \
-       if (cp == cpbufs[0].buf) {      \
-               char *prev = cpbufs[0].buf;     \
-               emalloc(cpbufs[0].buf, char *, 2*cpbufs[0].bufsize, \
-                       "format_args"); \
-               memcpy((cp = cpbufs[0].buf+cpbufs[0].bufsize), prev,    \
-                      cpbufs[0].bufsize);      \
-               cpbufs[0].bufsize *= 2; \
-               if (prev != cpbufs[0].stackbuf) \
-                       efree(prev);    \
-               cend = cpbufs[0].buf+cpbufs[0].bufsize; \
-       }       \
-       *--cp = (CH);   \
-} while(0)
+       cpbuf.bufsize = sizeof(cpbuf.stackbuf);
+       cpbuf.buf = cpbuf.stackbuf;
 
        /*
         * Check first for use of `count$'.
@@ -464,7 +430,7 @@ check_pos:
                                        /* 'space' flag is ignored */
                                        /* if '+' already present  */
                        space_flag = true;
-                       if (signchar != false)
+                       if (signchar != '\0')
                                goto check_pos;
                        signchar = cs1;
                        goto check_pos;
@@ -570,9 +536,9 @@ check_pos:
                                                goto out0;
                                        }
 
-                                       memcpy(cpbuf, buf, count);
+                                       memcpy(cpbuf.buf, buf, count);
                                        prec = count;
-                                       cp = cpbuf;
+                                       cp = cpbuf.buf;
                                        goto pr_tail;
                                }
 out0:
@@ -580,13 +546,13 @@ out0:
                                /* else,
                                        fall through */
 
-                               cpbuf[0] = uval;
+                               cpbuf.buf[0] = uval;
                                prec = 1;
-                               cp = cpbuf;
+                               cp = cpbuf.buf;
                                goto pr_tail;
                        }
                        /*
-                        * As per POSIX, only output first character of a
+                        * As per POSIX, only output the first character of a
                         * string value.  Thus, we ignore any provided
                         * precision, forcing it to 1.  (Didn't this
                         * used to work? 6/2003.)
@@ -638,7 +604,7 @@ out0:
                                ;
                        else if (gawk_mb_cur_max > 1) {
                                if (cs1 == 's') {
-                                       assert(cp == arg->stptr || cp == cpbuf);
+                                       assert(cp == arg->stptr || cp == 
cpbuf.buf);
                                        copy_count = mbc_byte_count(arg->stptr, 
prec);
                                }
                                /* prec was set by code for %c */
@@ -723,6 +689,7 @@ out0:
                                lintwarn(_("%%%c format is POSIX standard but 
not portable to other awks"), cs1);
                        }
                        base = 6;
+                       goto format_float;
                }
 #endif
                case 'F':
@@ -735,6 +702,7 @@ out0:
                case 'e':
                case 'f':
                case 'E':
+       format_float:
                        base += 10;
                        need_format = false;
                        parse_next_arg();
@@ -782,16 +750,11 @@ out0:
        r = make_str_node(obuf, olen_final, ALREADY_MALLOCED);
        obuf = NULL;
 out:
-       {
-               size_t k;
-               size_t count = sizeof(cpbufs)/sizeof(cpbufs[0]);
-               for (k = 0; k < count; k++) {
-                       if (cpbufs[k].buf != cpbufs[k].stackbuf)
-                               efree(cpbufs[k].buf);
-               }
-               if (obuf != NULL)
-                       efree(obuf);
-       }
+       if (cpbuf.buf != cpbuf.stackbuf)
+               efree(cpbuf.buf);
+
+       if (obuf != NULL)
+               efree(obuf);
 
        if (r == NULL)
                gawk_exit(EXIT_FATAL);
@@ -1014,7 +977,7 @@ format_signed_integer(NODE *arg, struct flags *flags)
        const char *number_value;
        size_t val_len;
        char *buf1 = NULL;
-       char fill[] = " ";
+       char fill = ' ';
        bool used_float = false;
 
        if (out_of_range(arg))
@@ -1106,7 +1069,7 @@ format_signed_integer(NODE *arg, struct flags *flags)
                        emalloc(buf2, char *, count, "format_signed_integer");
                        cp = buf2;
                        for (; fw > val_len; fw--)
-                               *cp++ = fill[0];
+                               *cp++ = fill;
                        strcpy(cp, buf1);
                        free((void *) buf1);
 
@@ -1123,7 +1086,7 @@ format_signed_integer(NODE *arg, struct flags *flags)
                                "format_signed_integer");
 
                if (compute_zero_flag(flags))
-                       fill[0] = '0';
+                       fill = '0';
 
                cp = buf1;
                src = number_value;
@@ -1144,7 +1107,7 @@ format_signed_integer(NODE *arg, struct flags *flags)
                        strcpy(cp, src);
                        cp += val_len;
                        for (; fw > val_len; fw--)
-                               *cp++ = fill[0];
+                               *cp++ = fill;
                        *cp = '\0';
                } else {
                        strcpy(buf1, number_value);
@@ -1157,12 +1120,12 @@ format_signed_integer(NODE *arg, struct flags *flags)
                                val_len--;
                        }
 
-                       if (fill[0] == '0') {
+                       if (fill == '0') {
                                // leave room for the extra character
                                int stop = val_len + (negative || flags->plus 
|| flags->space);
 
                                for (; fw > stop; fw--)
-                                       *cp++ = fill[0];
+                                       *cp++ = fill;
                        }
 
                        if (negative) {
@@ -1176,9 +1139,9 @@ format_signed_integer(NODE *arg, struct flags *flags)
                                fw--;
                        }
 
-                       if (fill[0] == ' ') {
+                       if (fill == ' ') {
                                for (; fw > val_len; fw--)
-                                       *cp++ = fill[0];
+                                       *cp++ = fill;
                        }
                        *cp = '\0';
 
@@ -1209,7 +1172,7 @@ format_unsigned_integer(NODE *arg, struct flags *flags)
        const char *number_value;
        char *buf1 = NULL;
        char *buf2 = NULL;
-       char fill[] = " ";
+       char fill = ' ';
        size_t val_len;
        bool used_float = false;
 
@@ -1287,20 +1250,18 @@ format_unsigned_integer(NODE *arg, struct flags *flags)
                src = number_value;
 
                if (compute_zero_flag(flags))
-                       fill[0] = '0';
+                       fill = '0';
 
                if (flags->alt) {
                        if (flags->base == 16 && ! is_zero(arg)) {
-                               if (fill[0] == '0') {
+                               if (fill == '0') {
                                        *cp++ = '0';
                                        *cp++ = flags->format;
                                        fw -= 2;
                                        for (; fw > val_len; fw--)
-                                               *cp++ = fill[0];
+                                               *cp++ = fill;
                                } else {
                                        fw -= 2;
-                                       // for (; fw > val_len; fw--)
-                                       //      *cp++ = fill[0];
                                        *cp++ = '0';
                                        *cp++ = flags->format;
                                }
@@ -1326,10 +1287,10 @@ format_unsigned_integer(NODE *arg, struct flags *flags)
                                cp += val_len;
                                fw -= val_len;
                                for (; fw > 0; fw--)
-                                       *cp++ = fill[0];
+                                       *cp++ = fill;
                        } else {
                                for (; fw > val_len; fw--)
-                                       *cp++ = fill[0];
+                                       *cp++ = fill;
                                strcpy(cp, buf1);
                        }
 
@@ -1368,8 +1329,6 @@ format_unsigned_integer(NODE *arg, struct flags *flags)
        return buf1;
 }
 
-////////////////// below here all is good /////////////////////////////////
-
 /* format_out_of_range --- format an out of range value as %g. caller frees 
return value */
 
 static const char *
@@ -1460,7 +1419,6 @@ static const char *
 format_mpg_integer_digits(NODE *arg, struct flags *flags, bool *used_float)
 {
 #ifdef HAVE_MPFR
-#undef cpbuf
        mpz_ptr zi;
        mpfr_ptr mf;
        char *buf;
@@ -1611,13 +1569,6 @@ adjust_flags(struct flags *flags)
        if (flags->plus)
                flags->space = false;
 
-/*
-       if (flags->format == 'u') {
-               flags->plus = false;
-               flags->space = false;
-       }
-*/
-
        if (strchr("diouxX", flags->format) != NULL && flags->have_prec)
                flags->zero = false;
 }
@@ -1630,7 +1581,6 @@ format_float(NODE *arg, struct flags *flags)
        char *buf;
        size_t buflen;
        char *cp;
-#undef cpbuf
        char cpbuf[100];
        
        double tmpval;
@@ -1821,19 +1771,17 @@ format_nan_inf(NODE *n, char format)
 {
        static char buf[100];
        double val = n->numbr;
+       enum possible { PLUS_NAN, MINUS_NAN, PLUS_INF, MINUS_INF, NONE } result 
= NONE;
+       static const char *strings[] = { "+nan", "-nan", "+inf", "-inf" };
 
 #ifdef HAVE_MPFR
        if (is_mpg_integer(n))
                return NULL;
        else if (is_mpg_float(n)) {
                if (mpfr_nan_p(n->mpg_numbr)) {
-                       strcpy(buf, mpfr_signbit(n->mpg_numbr) != 0 ? "-nan" : 
"+nan");
-
-                       goto fmt;
+                       result = (mpfr_signbit(n->mpg_numbr) != 0 ? MINUS_NAN : 
PLUS_NAN);
                } else if (mpfr_inf_p(n->mpg_numbr)) {
-                       strcpy(buf, mpfr_signbit(n->mpg_numbr) ? "-inf" : 
"+inf");
-
-                       goto fmt;
+                       result = (mpfr_signbit(n->mpg_numbr) != 0 ? MINUS_INF : 
PLUS_INF);
                } else
                        return NULL;
        }
@@ -1841,26 +1789,24 @@ format_nan_inf(NODE *n, char format)
                fallthrough */
 #endif
 
-       if (isnan(val)) {
-               strcpy(buf, signbit(val) != 0 ? "-nan" : "+nan");
-
-               // fall through to end
-       } else if (isinf(val)) {
-               strcpy(buf, val < 0 ? "-inf" : "+inf");
+       if (result == NONE) {
+               if (isnan(val)) {
+                       result = (signbit(val) != 0 ? MINUS_NAN : PLUS_NAN);
+               } else if (isinf(val)) {
+                       result = (val < 0 ? MINUS_INF : PLUS_INF);
+               } else
+                       return NULL;
+       }
 
-               // fall through to end
-       } else
-               return NULL;
+       strcpy(buf, strings[(int) result]);
 
-#ifdef HAVE_MPFR
-fmt:
-#endif
        if (isupper(format)) {
                int i;
 
                for (i = 0; buf[i] != '\0'; i++)
                        buf[i] = toupper(buf[i]);
        }
+
        return buf;
 }
 
@@ -1880,7 +1826,7 @@ reverse(char *str)
        }
 }
 
-/* add_thousands --- add the thousands separator. caller free the return value 
*/
+/* add_thousands --- add the thousands separator. caller frees the return 
value */
 
 /*
  * Copy the source string into the destination string, backwards,

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

Summary of changes:
 ChangeLog |   4 ++
 printf.c  | 156 ++++++++++++++++++++------------------------------------------
 2 files changed, 55 insertions(+), 105 deletions(-)


hooks/post-receive
-- 
gawk



reply via email to

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