diff options
-rw-r--r-- | ChangeLog | 17 | ||||
-rw-r--r-- | array.c | 9 | ||||
-rw-r--r-- | awk.h | 12 | ||||
-rw-r--r-- | builtin.c | 2 | ||||
-rw-r--r-- | field.c | 2 | ||||
-rw-r--r-- | gawkapi.c | 2 | ||||
-rw-r--r-- | int_array.c | 4 | ||||
-rw-r--r-- | mpfr.c | 4 | ||||
-rw-r--r-- | node.c | 10 | ||||
-rw-r--r-- | str_array.c | 2 | ||||
-rw-r--r-- | symbol.c | 2 |
11 files changed, 51 insertions, 15 deletions
@@ -1,3 +1,20 @@ +2016-06-30 Andrew J. Schorr <aschorr@telemetry-investments.com> + + * awk.h (STFMT_UNUSED): New define indicating that the string + representation does not depend on CONVFMT or OFMT. + (force_string): Use STFMT_UNUSED to improve code clarity. + * array.c (value_info): Fix stfmt logic. + * builtin.c (do_print): Use STFMT_UNUSED to improve code clarity. + * field.c (set_record): Ditto. + * gawkapi.c (api_sym_update_scalar): Ditto. + * int_array.c (is_integer): Check stfmt equals STFMT_UNUSED before + bothering to inspect the string. + * mpfr.c (mpg_format_val): Use STFMT_UNUSED to improve code clarity. + Remove buggy cast to char in stfmt assignment. + * node.c (r_format_val): Ditto. + * str_array.c (str_lookup): Use STFMT_UNUSED to improve code clarity. + * symbol.c (check_param_names): Ditto. + 2016-06-29 Andrew J. Schorr <aschorr@telemetry-investments.com> * node.c (r_force_number): Optimize by trimming leading and trailing @@ -704,7 +704,14 @@ value_info(NODE *n) if ((n->flags & (STRING|STRCUR)) == STRCUR) { fprintf(output_fp, "]["); fprintf(output_fp, "stfmt=%d, ", n->stfmt); - fprintf(output_fp, "CONVFMT=\"%s\"", n->stfmt <= -1 ? "%ld" + /* + * If not STFMT_UNUSED, could be CONVFMT or OFMT if last + * used in a print statement. If immutable, could be that it + * was originally set as a string, or it's a number that has + * an integer value. + */ + fprintf(output_fp, "FMT=\"%s\"", + n->stfmt == STFMT_UNUSED ? "%s" : fmt_list[n->stfmt]->stptr); } @@ -497,6 +497,16 @@ typedef struct exp_node { #define numbr sub.val.fltnum #endif +/* + * If stfmt is set to STFMT_UNUSED, it means that the string representation + * stored in stptr is not a function of the value of CONVFMT or OFMT. That + * indicates that either the string value was explicitly assigned, or it + * was converted from a NUMBER that has an integer value. When stfmt is not + * set to STFMT_UNUSED, it is an offset into the fmt_list array of distinct + * CONVFMT and OFMT node pointers. + */ +#define STFMT_UNUSED -1 + /* Node_arrayfor */ #define for_list sub.nodep.r.av #define for_list_size sub.nodep.reflags @@ -1798,7 +1808,7 @@ force_string(NODE *s) return dupnode(s->re_exp); if ((s->flags & STRCUR) != 0 - && (s->stfmt == -1 || s->stfmt == CONVFMTidx) + && (s->stfmt == STFMT_UNUSED || s->stfmt == CONVFMTidx) ) return s; return format_val(CONVFMT, CONVFMTidx, s); @@ -2204,7 +2204,7 @@ do_print(int nargs, int redirtype) if (tmp->type == Node_typedregex) args_array[i] = force_string(tmp); else if (!((tmp->flags & STRCUR) != 0 - && (tmp->stfmt == -1 || tmp->stfmt == OFMTidx))) + && (tmp->stfmt == STFMT_UNUSED || tmp->stfmt == OFMTidx))) args_array[i] = format_val(OFMT, OFMTidx, tmp); } @@ -288,7 +288,7 @@ set_record(const char *buf, int cnt) n->stlen = cnt; n->valref = 1; n->type = Node_val; - n->stfmt = -1; + n->stfmt = STFMT_UNUSED; n->flags = (STRING|STRCUR|MAYBE_NUM|FIELD); fields_arr[0] = n; @@ -686,7 +686,7 @@ api_sym_update_scalar(awk_ext_id_t id, /* make_str_node(s, l, ALREADY_MALLOCED): */ r->numbr = 0; r->flags = (MALLOC|STRING|STRCUR); - r->stfmt = -1; + r->stfmt = STFMT_UNUSED; r->stptr = value->str_value.str; r->stlen = value->str_value.len; return awk_true; diff --git a/int_array.c b/int_array.c index f17bddef..e7913dea 100644 --- a/int_array.c +++ b/int_array.c @@ -92,8 +92,10 @@ is_integer(NODE *symbol, NODE *subs) /* * Protect against MAYBE_NUM values where the string may not regenerate * correctly. There could be white space and/or a non-decimal value. + * If stfmt is not STFMT_UNUSED, it means that the string value was + * generated using CONVFMT or OFMT, so there is no info there. */ - if ((subs->flags & STRCUR) != 0) { + if ((subs->flags & STRCUR) != 0 && subs->stfmt == STFMT_UNUSED) { char *cp = subs->stptr; if ( subs->stlen == 0 @@ -370,11 +370,11 @@ mpg_format_val(const char *format, int index, NODE *s) if (is_mpg_integer(s) || mpfr_integer_p(s->mpg_numbr)) { /* integral value, use %d */ r = format_tree("%d", 2, dummy, 2); - s->stfmt = -1; + s->stfmt = STFMT_UNUSED; } else { r = format_tree(format, fmt_list[index]->stlen, dummy, 2); assert(r != NULL); - s->stfmt = (char) index; + s->stfmt = index; } s->flags = oflags; s->stlen = r->stlen; @@ -226,7 +226,7 @@ r_format_val(const char *format, int index, NODE *s) * Once upon a time, we just blindly did this: * sprintf(sp, format, s->numbr); * s->stlen = strlen(sp); - * s->stfmt = (char) index; + * s->stfmt = index; * but that's no good if, e.g., OFMT is %s. So we punt, * and just always format the value ourselves. */ @@ -241,11 +241,11 @@ r_format_val(const char *format, int index, NODE *s) if (val == s->numbr) { /* integral value, but outside range of %ld, use %.0f */ r = format_tree("%.0f", 4, dummy, 2); - s->stfmt = -1; + s->stfmt = STFMT_UNUSED; } else { r = format_tree(format, fmt_list[index]->stlen, dummy, 2); assert(r != NULL); - s->stfmt = (char) index; + s->stfmt = index; } s->flags = oflags; s->stlen = r->stlen; @@ -268,7 +268,7 @@ r_format_val(const char *format, int index, NODE *s) (void) sprintf(sp, "%ld", num); s->stlen = strlen(sp); } - s->stfmt = -1; + s->stfmt = STFMT_UNUSED; if ((s->flags & INTIND) != 0) { s->flags &= ~(INTIND|NUMBER); s->flags |= STRING; @@ -385,7 +385,7 @@ make_str_node(const char *s, size_t len, int flags) r->numbr = 0; r->flags = (MALLOC|STRING|STRCUR); r->valref = 1; - r->stfmt = -1; + r->stfmt = STFMT_UNUSED; r->wstptr = NULL; r->wstlen = 0; diff --git a/str_array.c b/str_array.c index d9b5d2a4..f66b22cc 100644 --- a/str_array.c +++ b/str_array.c @@ -168,7 +168,7 @@ str_lookup(NODE *symbol, NODE *subs) * flag on it since other variables could be using the same * reference-counted value. */ - if (subs->stfmt != -1 || (subs->flags & MAYBE_NUM) != 0) { + if (subs->stfmt != STFMT_UNUSED || (subs->flags & MAYBE_NUM) != 0) { NODE *tmp; /* @@ -648,7 +648,7 @@ check_param_names(void) memset(& n, 0, sizeof n); n.type = Node_val; n.flags = STRING|STRCUR; - n.stfmt = -1; + n.stfmt = STFMT_UNUSED; /* * assoc_list() returns an array with two elements per awk array |