[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[SCM] gawk branch, stable/printf-rework, updated. gawk-4.1.0-5514-g8700b
From: |
Arnold Robbins |
Subject: |
[SCM] gawk branch, stable/printf-rework, updated. gawk-4.1.0-5514-g8700b5dd |
Date: |
Sat, 20 Jul 2024 15:42:06 -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 8700b5dd514cc70fc29c3df5c40d2a8935c43307 (commit)
from 8d1495bf9b1b502a69e3ce593c4e1bbf30ccff1f (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=8700b5dd514cc70fc29c3df5c40d2a8935c43307
commit 8700b5dd514cc70fc29c3df5c40d2a8935c43307
Author: Arnold D. Robbins <arnold@skeeve.com>
Date: Sat Jul 20 22:41:52 2024 +0300
More printf fixes.
diff --git a/ChangeLog b/ChangeLog
index 0d439b29..5922ceb9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2024-07-20 Arnold D. Robbins <arnold@skeeve.com>
+
+ * printf.c: Continuing fixes and cleanups.
+
2024-07-17 Arnold D. Robbins <arnold@skeeve.com>
* NEWS: Updated.
diff --git a/printf.c b/printf.c
index ebc63eb2..13c05d26 100644
--- a/printf.c
+++ b/printf.c
@@ -54,13 +54,13 @@ static void reverse(char *str);
static bool compute_zero_flag(struct flags *flags);
static void adjust_flags(struct flags *flags);
static const char *add_thousands(const char *original);
-static const char *format_integer_digits(NODE *arg, struct flags *flags);
+static const char *format_integer_digits(NODE *arg, struct flags *flags, bool
*used_float);
static const char *format_mpg_integer_digits(NODE *arg, struct flags *flags,
bool *used_float);
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);
+static char *fill_to_field_width(char *startval, struct flags *flags, int
fill);
#ifdef HAVE_MPFR
@@ -881,7 +881,7 @@ do_printf(int nargs, int redirtype)
/* format_integer_digits --- format just the actual value of an integer.
caller frees return value */
static const char *
-format_integer_digits(NODE *arg, struct flags *flags)
+format_integer_digits(NODE *arg, struct flags *flags, bool *used_float)
{
#define VALUE_SIZE 40
char *buf = NULL;
@@ -908,6 +908,7 @@ format_integer_digits(NODE *arg, struct flags *flags)
buflen = VALUE_SIZE;
cp = buf;
+ *used_float = false;
tmpval = double_to_int(arg->numbr);
if (flags->base == 10 && flags->format != 'u') {
// signed decimal
@@ -927,7 +928,7 @@ format_integer_digits(NODE *arg, struct flags *flags)
erealloc(buf, char *, buflen, "format_args");
}
} else {
- // octal or hex
+ // octal or hex or unsigned decimal
chbuf = (flags->format == 'X' ? Uchbuf : lchbuf);
if (tmpval < 0) {
@@ -935,6 +936,8 @@ format_integer_digits(NODE *arg, struct flags *flags)
if ((AWKNUM)(intmax_t)uval != double_to_int(tmpval)) {
flags->format = 'g';
free((void *) buf);
+ *used_float = true;
+
return format_float(arg, flags);
}
} else {
@@ -942,6 +945,8 @@ format_integer_digits(NODE *arg, struct flags *flags)
if ((AWKNUM)uval != double_to_int(tmpval)) {
flags->format = 'g';
free((void *) buf);
+ *used_float = true;
+
return format_float(arg, flags);
}
}
@@ -987,7 +992,7 @@ format_signed_integer(NODE *arg, struct flags *flags)
if (is_mpg_integer(arg) || is_mpg_float(arg))
number_value = format_mpg_integer_digits(arg, flags, &
used_float);
else
- number_value = format_integer_digits(arg, flags); // just
digits, possible leading '-'
+ number_value = format_integer_digits(arg, flags, & used_float);
// just digits, possible leading '-'
if (used_float)
return number_value;
@@ -1183,7 +1188,7 @@ format_unsigned_integer(NODE *arg, struct flags *flags)
if (is_mpg_integer(arg) || is_mpg_float(arg))
number_value = format_mpg_integer_digits(arg, flags, &
used_float);
else
- number_value = format_integer_digits(arg, flags); // just
digits
+ number_value = format_integer_digits(arg, flags, & used_float);
// just digits
if (used_float)
return number_value;
@@ -1279,7 +1284,7 @@ format_unsigned_integer(NODE *arg, struct flags *flags)
val_len = strlen(buf1);
if (val_len < flags->field_width) {
- buf1 = (char *) fill_to_field_width(buf1, flags, fill);
+ buf1 = fill_to_field_width(buf1, flags, fill);
val_len = strlen(buf1);
}
} else
@@ -1347,7 +1352,7 @@ format_out_of_range(NODE *arg, struct flags *flags)
if (flags->field_width > len) {
char *buf = estrdup(nan_inf_val, len);
- buf = fill_to_field_width(buf, flags, ' ');
+ buf = fill_to_field_width(buf, flags, ' '); // frees
original
return buf;
}
@@ -1377,7 +1382,7 @@ compute_zero_flag(struct flags *flags)
return zero_flag;
}
-/* format_mpg_integer --- format an MPZ or MPFR integer. caller frees return
value */
+/* format_mpg_integer_digits --- format an MPZ or MPFR integer. caller frees
return value */
/*
* It seems that mpfr_snprintf() doesn't exactly mimic snprintf() with respect
@@ -1389,16 +1394,20 @@ static const char *
format_mpg_integer_digits(NODE *arg, struct flags *flags, bool *used_float)
{
#ifdef HAVE_MPFR
- mpz_ptr zi;
- mpfr_ptr mf;
+ mpz_ptr zi = NULL;
+ mpfr_ptr mf = NULL;
char *buf;
size_t buflen;
char cpbuf[30];
int nc;
- bool need_to_add_thousands = (strchr("diu", flags->format) != NULL);
+ bool quote_flag = false;
*used_float = false;
+#if defined(HAVE_LOCALE_H)
+ quote_flag = (flags->quote && loc.thousands_sep[0] != '\0');
+#endif
+
if (is_zero(arg))
return estrdup("0", 1);
@@ -1434,6 +1443,7 @@ mpf1:
if (mpfr_sgn(mf) <= 0) {
if (! mpfr_fits_intmax_p(mf, ROUND_MODE)) {
*used_float = true;
+
return format_float(arg, flags);
}
goto fmt0;
@@ -1450,7 +1460,7 @@ fmt0:
#if defined(LC_NUMERIC)
- if (flags->quote && ! use_lc_numeric)
+ if (quote_flag && ! use_lc_numeric)
setlocale(LC_NUMERIC, "");
#endif
@@ -1461,11 +1471,11 @@ fmt0:
}
#if defined(LC_NUMERIC)
- if (flags->quote && ! use_lc_numeric)
+ if (quote_flag && ! use_lc_numeric)
setlocale(LC_NUMERIC, "C");
#endif
- if (flags->quote && flags->base == 10 && need_to_add_thousands) {
+ if (quote_flag && flags->base == 10) {
const char *new_text = add_thousands(buf);
free((void *) buf);
@@ -1545,6 +1555,13 @@ adjust_flags(struct flags *flags)
/* format_float --- format a floating point number. caller frees return value
*/
+/*
+ * We let the C and MPFR libraries handle the quote flag, since they
+ * know how to do it for the floating point formats. Thus here we do NOT
+ * set up a separate quote_flag variable based on the contents of the
+ * struct locale loc.
+ */
+
static const char *
format_float(NODE *arg, struct flags *flags)
{
@@ -1557,7 +1574,6 @@ format_float(NODE *arg, struct flags *flags)
#ifdef HAVE_MPFR
mpfr_ptr mf = NULL;
#endif
- bool quote_flag = false;
int nc;
bool mpfr_format = false;
@@ -1583,9 +1599,6 @@ format_float(NODE *arg, struct flags *flags)
if (! flags->have_prec)
flags->precision = DEFAULT_G_PRECISION;
-#if defined(HAVE_LOCALE_H)
- quote_flag = (flags->quote && loc.thousands_sep[0] != 0);
-#endif
buflen = flags->field_width + flags->precision + 11; /* 11 == slop */
emalloc(buf, char *, buflen, "format_float");
@@ -1606,15 +1619,14 @@ format_float(NODE *arg, struct flags *flags)
*cp++ = '#';
if (flags->zero)
*cp++ = '0';
- if (quote_flag)
+ if (flags->quote)
*cp++ = '\'';
#if defined(LC_NUMERIC)
- if (quote_flag && ! use_lc_numeric)
+ if (flags->quote && ! use_lc_numeric)
setlocale(LC_NUMERIC, "");
#endif
- bool need_to_add_thousands = false;
if (mpfr_format) {
#ifdef HAVE_MPFR
sprintf(cp, "*.*R*%c", flags->format);
@@ -1649,16 +1661,10 @@ format_float(NODE *arg, struct flags *flags)
}
#if defined(LC_NUMERIC)
- if (quote_flag && ! use_lc_numeric)
+ if (flags->quote && ! use_lc_numeric)
setlocale(LC_NUMERIC, "C");
#endif
- if (quote_flag && need_to_add_thousands) {
- const char *new_text = add_thousands(buf);
-
- free((void *) buf);
- buf = (char *) new_text;
- }
return buf;
}
@@ -1869,8 +1875,8 @@ add_thousands(const char *original)
/* 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)
+static char *
+fill_to_field_width(char *startval, struct flags *flags, int fill)
{
size_t l = strlen(startval);
char *buf;
-----------------------------------------------------------------------
Summary of changes:
ChangeLog | 4 ++++
printf.c | 68 ++++++++++++++++++++++++++++++++++-----------------------------
2 files changed, 41 insertions(+), 31 deletions(-)
hooks/post-receive
--
gawk
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [SCM] gawk branch, stable/printf-rework, updated. gawk-4.1.0-5514-g8700b5dd,
Arnold Robbins <=