From 36133281324cdcf34e12857f39847fd84bbac936 Mon Sep 17 00:00:00 2001
From: Dirk Zimoch <dirk.zimoch@psi.ch>
Date: Wed, 2 May 2018 15:13:29 +0200
Subject: [PATCH] add # (string length) operator

---
 expr.c      | 15 ++++++++++++---
 teststrings | 11 +++++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/expr.c b/expr.c
index 40d1c300..f1a00c63 100644
--- a/expr.c
+++ b/expr.c
@@ -29,7 +29,7 @@ static int parseValue(const char **pp, long *v)
     o = *p;
     if (memchr("+-~!", o, 4))
     {
-        /* handle unary operators */
+        /* unary operators */
         p++;
         if (!parseValue(&p, &val)) return 0; /* no valid value */
         if (exprDebug) printf("parseValue: %c %ld\n", o, val);
@@ -39,15 +39,24 @@ static int parseValue(const char **pp, long *v)
     }
     else if (o == '(')
     {
-        /* handle sub-expression */
+        /* sub-expression */
         p++;
         if (parseExpr(&p, &val) < 0) return 0; /* no valid expression */
         skipSpace(p);
         if (*p++ != ')') return 0; /* missing ) */
     }
+    else if (o == '#')
+    {
+        /* string length operator */
+        p++;
+        if (exprDebug) printf("parseValue: string length of %s\n", p);
+        if (*p == '"' || *p == '\'')
+            val = parseString(&p, NULL);
+        else return 0;
+    }
     else
     {
-        /* get number */
+        /* number */
         char *e;
         val = strtol(p, &e, 0);
         if (e == p) return 0; /* no number */
diff --git a/teststrings b/teststrings
index 7366646b..9d6b4085 100644
--- a/teststrings
+++ b/teststrings
@@ -78,3 +78,14 @@ x="Hello World"[7,-1]
 
 x='\"\\x'[2]
 # $(x) should be: x
+
+x=#"Hello World"
+# $(x) should be: 11
+
+x=#"Hello World"[2:8]
+# $(x) should be: 6
+
+x=#Hello World
+# $(x) should be: #Hello World
+
+
-- 
GitLab