aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew J. Schorr <aschorr@telemetry-investments.com>2016-07-04 11:20:07 -0400
committerAndrew J. Schorr <aschorr@telemetry-investments.com>2016-07-04 11:20:07 -0400
commitcedd0829b0075533986fce1e699bc6ae511a891e (patch)
treee2a67bf0ee7d67cb7b078f2863233529d5aece02
parentb3fa425feb23dd36e82d4dd71f0bc1e03495a46b (diff)
downloadegawk-cedd0829b0075533986fce1e699bc6ae511a891e.tar.gz
egawk-cedd0829b0075533986fce1e699bc6ae511a891e.tar.bz2
egawk-cedd0829b0075533986fce1e699bc6ae511a891e.zip
Unify force_string handling of CONVFMT and OFMT.
-rw-r--r--ChangeLog9
-rw-r--r--awk.h20
-rw-r--r--builtin.c7
3 files changed, 26 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index feb84d10..2fb4d45d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
2016-07-04 Andrew J. Schorr <aschorr@telemetry-investments.com>
+ * awk.h (force_string_fmt): New inline function to get the string
+ representation in a requested format.
+ (force_string): Reimplement as a macro using force_string_fmt function.
+ (force_string_ofmt): New macro to get a value's OFMT representation.
+ * builtin.c (do_print): Use new force_string_ofmt macro instead of
+ duplicating the logic inline.
+
+2016-07-04 Andrew J. Schorr <aschorr@telemetry-investments.com>
+
* str_array.c (str_lookup): There is no need to worry about the
MAYBE_NUM flag, since the code has been patched to make sure to
preserve the string value of strnum values, and the integer array
diff --git a/awk.h b/awk.h
index 34755581..b99dec70 100644
--- a/awk.h
+++ b/awk.h
@@ -1799,21 +1799,33 @@ dupnode(NODE *n)
}
#endif
-/* force_string --- force a node to have a string value */
+/*
+ * force_string_fmt --- force a node to have a string value in a given format.
+ * The string representation of a number may change due to whether it was most
+ * recently rendered with CONVFMT or OFMT, or due to changes in the CONVFMT
+ * and OFMT values. But if the value entered gawk as a string or strnum, then
+ * stfmt should be set to STFMT_UNUSED, and the string representation should
+ * not change.
+ */
static inline NODE *
-force_string(NODE *s)
+force_string_fmt(NODE *s, const char *fmtstr, int fmtidx)
{
if (s->type == Node_typedregex)
return dupnode(s->re_exp);
if ((s->flags & STRCUR) != 0
- && (s->stfmt == STFMT_UNUSED || s->stfmt == CONVFMTidx)
+ && (s->stfmt == STFMT_UNUSED || s->stfmt == fmtidx)
)
return s;
- return format_val(CONVFMT, CONVFMTidx, s);
+ return format_val(fmtstr, fmtidx, s);
}
+/* conceptually should be force_string_convfmt, but this is the typical case */
+#define force_string(s) force_string_fmt((s), CONVFMT, CONVFMTidx)
+
+#define force_string_ofmt(s) force_string_fmt((s), OFMT, OFMTidx)
+
#ifdef GAWKDEBUG
#define unref r_unref
#define force_number str2number
diff --git a/builtin.c b/builtin.c
index 92ac9e49..a01018e3 100644
--- a/builtin.c
+++ b/builtin.c
@@ -2200,12 +2200,7 @@ do_print(int nargs, int redirtype)
DEREF(args_array[i]);
fatal(_("attempt to use array `%s' in a scalar context"), array_vname(tmp));
}
-
- if (tmp->type == Node_typedregex)
- args_array[i] = force_string(tmp);
- else if (!((tmp->flags & STRCUR) != 0
- && (tmp->stfmt == STFMT_UNUSED || tmp->stfmt == OFMTidx)))
- args_array[i] = format_val(OFMT, OFMTidx, tmp);
+ args_array[i] = force_string_ofmt(tmp);
}
if (redir_exp != NULL) {