diff options
author | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2016-07-06 22:29:58 -0400 |
---|---|---|
committer | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2016-07-06 22:29:58 -0400 |
commit | eb261daff5e9a96f294cd806d1fd3e68f06fdbaa (patch) | |
tree | 5f4d05861ac492c52a9f6a0781c69e90be73b60c | |
parent | ce342a04922797cb53557178c54d32c4efafda16 (diff) | |
download | egawk-eb261daff5e9a96f294cd806d1fd3e68f06fdbaa.tar.gz egawk-eb261daff5e9a96f294cd806d1fd3e68f06fdbaa.tar.bz2 egawk-eb261daff5e9a96f294cd806d1fd3e68f06fdbaa.zip |
Modify MAYBE_NUM usage and typeof function to return "strnum" only for actual numeric strings.
-rw-r--r-- | ChangeLog | 18 | ||||
-rw-r--r-- | awk.h | 14 | ||||
-rw-r--r-- | builtin.c | 4 | ||||
-rw-r--r-- | eval.c | 2 | ||||
-rw-r--r-- | int_array.c | 6 | ||||
-rw-r--r-- | mpfr.c | 3 | ||||
-rw-r--r-- | node.c | 9 | ||||
-rw-r--r-- | test/ChangeLog | 8 | ||||
-rw-r--r-- | test/forcenum.awk | 6 | ||||
-rw-r--r-- | test/forcenum.ok | 6 | ||||
-rw-r--r-- | test/rebuild.in | 2 | ||||
-rw-r--r-- | test/rebuild.ok | 2 |
12 files changed, 54 insertions, 26 deletions
@@ -1,5 +1,23 @@ 2016-07-06 Andrew J. Schorr <aschorr@telemetry-investments.com> + * awk.h: Modify comments to indicate that MAYBE_NUM will now be + left enabled to indicate strnum values by the NUMBER|MAYBE_NUM + combination, whereas STRING|MAYBE_NUM indicates a potential strnum. + (fixtype): Modify MAYBE_NUM test to avoid calling force_number if + NUMCUR is already set. + * builtin.c (do_typeof): Call fixtype to resolve argument type. + This forces parsing of numeric strings, so there's a performance + penalty, but we must do this to give a correct result. The meaning + of "strnum" changes from "potential strnum" to "actual strnum". + * eval.c (set_TEXTDOMAIN): Remove some dead code left over from last + patch. + * int_array.c (is_integer): When a MAYBE_NUM is converted successfully + to a NUMBER, leave the MAYBE_NUM flag enabled. + * mpfr.c (mpg_force_number): Ditto. + * node.c (r_force_number): Ditto. + +2016-07-06 Andrew J. Schorr <aschorr@telemetry-investments.com> + * awk.h: Modify stptr comment to indicate that all strings are now NUL-terminated. * builtin.c (do_mktime): Remove unnecessary logic to terminate @@ -406,14 +406,16 @@ typedef struct exp_node { * b = a + 0 # Adds NUMCUR to a, since numeric value * # is now available. But the type hasn't changed! * - * MAYBE_NUM is the joker. It means "this is string data, but - * the user may have really wanted it to be a number. If we have - * to guess, like in a comparison, turn it into a number if the string - * is indeed numeric." + * MAYBE_NUM is the joker. When STRING|MAYBE_NUM is set, it means + * "this is string data, but the user may have really wanted it to be a + * number. If we have to guess, like in a comparison, turn it into a + * number if the string is indeed numeric." * For example, gawk -v a=42 .... * Here, `a' gets STRING|STRCUR|MAYBE_NUM and then when used where * a number is needed, it gets turned into a NUMBER and STRING - * is cleared. + * is cleared. In that case, we leave the MAYBE_NUM in place, so + * the combination NUMBER|MAYBE_NUM means it is a strnum a.k.a. a + * "numeric string". * * WSTRCUR is for efficiency. If in a multibyte locale, and we * need to do something character based (substr, length, etc.) @@ -1865,7 +1867,7 @@ fixtype(NODE *n) { assert(n->type == Node_val || n->type == Node_typedregex); if (n->type == Node_val) { - if ((n->flags & MAYBE_NUM) != 0) + if ((n->flags & (NUMCUR|MAYBE_NUM)) == MAYBE_NUM) return force_number(n); if ((n->flags & INTIND) != 0) return force_string(n); @@ -3951,14 +3951,14 @@ do_typeof(int nargs) break; case Node_val: case Node_var: - switch (arg->flags & (STRING|NUMBER|MAYBE_NUM)) { + switch (fixtype(arg)->flags & (STRING|NUMBER|MAYBE_NUM)) { case STRING: res = "string"; break; case NUMBER: res = "number"; break; - case STRING|MAYBE_NUM: + case NUMBER|MAYBE_NUM: res = "strnum"; break; case NUMBER|STRING: @@ -955,12 +955,10 @@ set_LINT() void set_TEXTDOMAIN() { - int len; NODE *tmp; tmp = TEXTDOMAIN_node->var_value = force_string(TEXTDOMAIN_node->var_value); TEXTDOMAIN = tmp->stptr; - len = tmp->stlen; /* * Note: don't call textdomain(); this value is for * the awk program, not for gawk itself. diff --git a/int_array.c b/int_array.c index 93e96d1f..937a91cf 100644 --- a/int_array.c +++ b/int_array.c @@ -142,7 +142,8 @@ is_integer(NODE *symbol, NODE *subs) if (len == 1 && *cp != '-') { /* single digit */ subs->numbr = (long) (*cp - '0'); if ((subs->flags & MAYBE_NUM) != 0) { - subs->flags &= ~(MAYBE_NUM|STRING); + /* leave MAYBE_NUM set */ + subs->flags &= ~STRING; subs->flags |= NUMBER; } subs->flags |= (NUMCUR|NUMINT); @@ -158,7 +159,8 @@ is_integer(NODE *symbol, NODE *subs) subs->numbr = l; if ((subs->flags & MAYBE_NUM) != 0) { - subs->flags &= ~(MAYBE_NUM|STRING); + /* leave MAYBE_NUM set */ + subs->flags &= ~STRING; subs->flags |= NUMBER; } subs->flags |= NUMCUR; @@ -342,7 +342,8 @@ mpg_force_number(NODE *n) if (force_mpnum(n, (do_non_decimal_data && ! do_traditional), true)) { if ((n->flags & MAYBE_NUM) != 0) { - n->flags &= ~(MAYBE_NUM|STRING); + /* leave MAYBE_NUM set to indicate a strnum */ + n->flags &= ~STRING; n->flags |= NUMBER; } } else @@ -66,9 +66,9 @@ r_force_number(NODE *n) return n; /* - * We should always set NUMCUR and clear MAYBE_NUM, and we may possibly - * change STRING to NUMBER if MAYBE_NUM was set and it's a good numeric - * string. + * We should always set NUMCUR. If MAYBE_NUM is set and it's a + * numeric string, we clear STRING and enable NUMBER, but if it's not + * numeric, we disable MAYBE_NUM. */ /* All the conditionals are an attempt to avoid the expensive strtod */ @@ -166,7 +166,8 @@ badnum: goodnum: if ((n->flags & MAYBE_NUM) != 0) { - n->flags &= ~(MAYBE_NUM|STRING); + /* leave MAYBE_NUM enabled to indicate that this is a strnum */ + n->flags &= ~STRING; n->flags |= NUMBER; } return n; diff --git a/test/ChangeLog b/test/ChangeLog index 5897906e..6821488c 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,11 @@ +2016-07-06 Andrew J. Schorr <aschorr@telemetry-investments.com> + + * forcenum.awk: We no longer need to force the strnum conversion, + since typeof now does this automatically. + * forcenum.ok: Change "number" to "strnum" for the numeric strings. + * rebuild.in: Change input to include a strnum. + * rebuild.ok: Update results. + 2016-07-04 Andrew J. Schorr <aschorr@telemetry-investments.com> * Makefile.am (arrayind3): New test. diff --git a/test/forcenum.awk b/test/forcenum.awk index 54c536c9..1a7ddce7 100644 --- a/test/forcenum.awk +++ b/test/forcenum.awk @@ -1,8 +1,6 @@ BEGIN { - # first, make some strnums + # make some strnums nf = split("|5apple|+NaN| 6|0x1az|011Q|027", f, "|") - for (i = 1; i <= nf; i++) { - x = f[i]+0 # trigger strnum conversion to number or string + for (i = 1; i <= nf; i++) printf "[%s] -> %g (type %s)\n", f[i], f[i], typeof(f[i]) - } } diff --git a/test/forcenum.ok b/test/forcenum.ok index c74eefc7..a379db62 100644 --- a/test/forcenum.ok +++ b/test/forcenum.ok @@ -1,7 +1,7 @@ [] -> 0 (type string) [5apple] -> 5 (type string) -[+NaN] -> nan (type number) -[ 6] -> 6 (type number) +[+NaN] -> nan (type strnum) +[ 6] -> 6 (type strnum) [0x1az] -> 26 (type string) [011Q] -> 9 (type string) -[027] -> 23 (type number) +[027] -> 23 (type strnum) diff --git a/test/rebuild.in b/test/rebuild.in index b2901ea9..2f16a825 100644 --- a/test/rebuild.in +++ b/test/rebuild.in @@ -1 +1 @@ -a b +a 6.3 diff --git a/test/rebuild.ok b/test/rebuild.ok index 29635279..0fe72e23 100644 --- a/test/rebuild.ok +++ b/test/rebuild.ok @@ -1,2 +1,2 @@ -test b +test 6.3 strnum |