awk: fix length(array)
authorDenys Vlasenko <vda.linux@googlemail.com>
Sat, 12 Oct 2013 02:51:54 +0000 (04:51 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sat, 12 Oct 2013 02:51:54 +0000 (04:51 +0200)
function                                             old     new   delta
evaluate                                            3526    3548     +22

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
editors/awk.c
testsuite/awk.tests

index 77784dfc166e1a447d9429bc7f3fed3b34335219..8848d94a5b67f6d627cc147013fb8392060c924e 100644 (file)
@@ -2784,8 +2784,16 @@ static var *evaluate(node *op, var *res)
                                break;
 
                        case F_le:
-                               if (!op1)
+                               debug_printf_eval("length: L.s:'%s'\n", L.s);
+                               if (!op1) {
                                        L.s = getvar_s(intvar[F0]);
+                                       debug_printf_eval("length: L.s='%s'\n", L.s);
+                               }
+                               else if (L.v->type & VF_ARRAY) {
+                                       R_d = L.v->x.array->nel;
+                                       debug_printf_eval("length: array_len:%d\n", L.v->x.array->nel);
+                                       break;
+                               }
                                R_d = strlen(L.s);
                                break;
 
index 6af6072b8bf2ebe08747d02f95936282e579a050..a02302405ec5fcc7f09eda93fd598c28c4862103 100755 (executable)
@@ -274,6 +274,11 @@ testing "awk large integer" \
        "2147483647 2147483647 0 2147483648 2147483648 0\n" \
        "" ""
 
+testing "awk length(array)" \
+       "awk 'BEGIN{ A[1]=2; A["qwe"]="asd"; print length(A)}'" \
+       "2\n" \
+       "" ""
+
 # testing "description" "command" "result" "infile" "stdin"
 
 exit $FAILCOUNT