Skip to content

expr: add support for =* and =~ #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) || defined(DEBUG_SHOW_SUBST)
#if defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND) || defined(DEBUG_SHOW_SUBST) || 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 @@ -8247,6 +8248,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 @@ -8255,13 +8259,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 @@ -8270,7 +8274,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 @@ -8836,7 +8840,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 @@ -8881,11 +8903,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 @@ -9016,6 +9049,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 @@ -9284,7 +9319,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(DEBUG_SHOW_SUBST)) && !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 stringmatch-8.1 {string match operator =*} {
expr {"abc" =* "a*"}
} 1

test stringmatch-8.2 {string match operator =*} {
expr {"abc" =* "b*"}
} 0

test stringmatch-8.3 {string match operator =*} {
expr {"abc" =* {*[bB]c}}
} 1

testreport
Loading