Skip to content

Commit

Permalink
expr: add support for =* and =~
Browse files Browse the repository at this point in the history
glob match and regexp match operators

Signed-off-by: Steve Bennett <steveb@workware.net.au>
  • Loading branch information
msteveb committed Sep 10, 2023
1 parent 0be8ac0 commit 6d906ad
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
45 changes: 40 additions & 5 deletions jim.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
/* Maximum size of an integer */
#define JIM_INTEGER_SPACE 24

#if defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND)
#if defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND) || defined(DEBUG_SHOW_EXPR_TOKENS) || defined(DEBUG_SHOW_EXPR)
#define JIM_TT_NAME
static const char *jim_tt_name(int type);
#endif

Expand Down Expand Up @@ -8235,6 +8236,9 @@ enum
/* Binary operators (strings) */
JIM_EXPROP_STREQ, /* 43 */
JIM_EXPROP_STRNE,
JIM_EXPROP_STRGLOB,
JIM_EXPROP_STRRE,

JIM_EXPROP_STRIN,
JIM_EXPROP_STRNI,
JIM_EXPROP_STRLT,
Expand All @@ -8243,13 +8247,13 @@ enum
JIM_EXPROP_STRGE,

/* Unary operators (numbers) */
JIM_EXPROP_NOT, /* 51 */
JIM_EXPROP_NOT, /* 53 */
JIM_EXPROP_BITNOT,
JIM_EXPROP_UNARYMINUS,
JIM_EXPROP_UNARYPLUS,

/* Functions */
JIM_EXPROP_FUNC_INT, /* 55 */
JIM_EXPROP_FUNC_INT, /* 57 */
JIM_EXPROP_FUNC_WIDE,
JIM_EXPROP_FUNC_ABS,
JIM_EXPROP_FUNC_DOUBLE,
Expand All @@ -8258,7 +8262,7 @@ enum
JIM_EXPROP_FUNC_SRAND,

/* math functions from libm */
JIM_EXPROP_FUNC_SIN, /* 69 */
JIM_EXPROP_FUNC_SIN, /* 71 */
JIM_EXPROP_FUNC_COS,
JIM_EXPROP_FUNC_TAN,
JIM_EXPROP_FUNC_ASIN,
Expand Down Expand Up @@ -8824,7 +8828,25 @@ static int JimSearchList(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *valOb
return 0;
}

static int JimRegexpMatch(Jim_Interp *interp, Jim_Obj *patternObj, Jim_Obj *objPtr)
{
Jim_Obj *argv[3];
int argc = 0;
long eq;
int rc;

argv[argc++] = Jim_NewStringObj(interp, "regexp", -1);
argv[argc++] = patternObj;
argv[argc++] = objPtr;

rc = Jim_EvalObjVector(interp, argc, argv);

if (rc != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &eq) != JIM_OK) {
eq = -rc;
}

return eq;
}

static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprNode *node)
{
Expand Down Expand Up @@ -8869,11 +8891,22 @@ static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprNode *node)
case JIM_EXPROP_STRNI:
wC = !JimSearchList(interp, B, A);
break;
case JIM_EXPROP_STRGLOB:
wC = Jim_StringMatchObj(interp, B, A, 0);
break;
case JIM_EXPROP_STRRE:
wC = JimRegexpMatch(interp, B, A);
if (wC < 0) {
rc = JIM_ERR;
goto error;
}
break;
default:
abort();
}
Jim_SetResultInt(interp, wC);

error:
Jim_DecrRefCount(interp, A);
Jim_DecrRefCount(interp, B);

Expand Down Expand Up @@ -9004,6 +9037,8 @@ static const struct Jim_ExprOperator Jim_ExprOperators[] = {

OPRINIT("eq", 60, 2, JimExprOpStrBin),
OPRINIT("ne", 60, 2, JimExprOpStrBin),
OPRINIT("=*", 60, 2, JimExprOpStrBin),
OPRINIT("=~", 60, 2, JimExprOpStrBin),

OPRINIT("in", 55, 2, JimExprOpStrBin),
OPRINIT("ni", 55, 2, JimExprOpStrBin),
Expand Down Expand Up @@ -9268,7 +9303,7 @@ static int JimParseExprOperator(struct JimParserCtx *pc)
return JIM_OK;
}

#if (defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND)) && !defined(JIM_BOOTSTRAP)
#if defined(JIM_TT_NAME) && !defined(JIM_BOOTSTRAP)
static const char *jim_tt_name(int type)
{
static const char * const tt_names[JIM_TT_EXPR_OP] =
Expand Down
16 changes: 16 additions & 0 deletions tests/regexp2.test
Original file line number Diff line number Diff line change
Expand Up @@ -943,4 +943,20 @@ test regexp-25.3 {End of word} {
regexp {\mcd\M} cdef
} 0

test regexp-26.1 {regexp operator =~} {
expr {"abc" =~ "^a"}
} 1

test regexp-26.2 {regexp operator =~} {
expr {"abc" =~ "^b"}
} 0

test regexp-26.2 {regexp operator =~} {
expr {"abc" =~ ".b."}
} 1

test regexp-26.3 {regexp operator =~ invalid regexp} -body {
expr {"abc" =~ {[}}
} -returnCodes error -result {couldn't compile regular expression pattern: brackets [] not balanced}

testreport
12 changes: 12 additions & 0 deletions tests/stringmatch.test
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,16 @@ test stringmatch-7.4 {null in pattern} {
string match *b\[\0a\]r* foobar
} 1

test regexp-8.1 {string match operator =*} {
expr {"abc" =* "a*"}
} 1

test regexp-26.2 {regexp operator =~} {
expr {"abc" =* "b*"}
} 0

test regexp-26.2 {regexp operator =~} {
expr {"abc" =* {*[bB]c}}
} 1

testreport

0 comments on commit 6d906ad

Please sign in to comment.