Skip to content

Commit 6d906ad

Browse files
committed
expr: add support for =* and =~
glob match and regexp match operators Signed-off-by: Steve Bennett <[email protected]>
1 parent 0be8ac0 commit 6d906ad

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

jim.c

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@
109109
/* Maximum size of an integer */
110110
#define JIM_INTEGER_SPACE 24
111111

112-
#if defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND)
112+
#if defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND) || defined(DEBUG_SHOW_EXPR_TOKENS) || defined(DEBUG_SHOW_EXPR)
113+
#define JIM_TT_NAME
113114
static const char *jim_tt_name(int type);
114115
#endif
115116

@@ -8235,6 +8236,9 @@ enum
82358236
/* Binary operators (strings) */
82368237
JIM_EXPROP_STREQ, /* 43 */
82378238
JIM_EXPROP_STRNE,
8239+
JIM_EXPROP_STRGLOB,
8240+
JIM_EXPROP_STRRE,
8241+
82388242
JIM_EXPROP_STRIN,
82398243
JIM_EXPROP_STRNI,
82408244
JIM_EXPROP_STRLT,
@@ -8243,13 +8247,13 @@ enum
82438247
JIM_EXPROP_STRGE,
82448248

82458249
/* Unary operators (numbers) */
8246-
JIM_EXPROP_NOT, /* 51 */
8250+
JIM_EXPROP_NOT, /* 53 */
82478251
JIM_EXPROP_BITNOT,
82488252
JIM_EXPROP_UNARYMINUS,
82498253
JIM_EXPROP_UNARYPLUS,
82508254

82518255
/* Functions */
8252-
JIM_EXPROP_FUNC_INT, /* 55 */
8256+
JIM_EXPROP_FUNC_INT, /* 57 */
82538257
JIM_EXPROP_FUNC_WIDE,
82548258
JIM_EXPROP_FUNC_ABS,
82558259
JIM_EXPROP_FUNC_DOUBLE,
@@ -8258,7 +8262,7 @@ enum
82588262
JIM_EXPROP_FUNC_SRAND,
82598263

82608264
/* math functions from libm */
8261-
JIM_EXPROP_FUNC_SIN, /* 69 */
8265+
JIM_EXPROP_FUNC_SIN, /* 71 */
82628266
JIM_EXPROP_FUNC_COS,
82638267
JIM_EXPROP_FUNC_TAN,
82648268
JIM_EXPROP_FUNC_ASIN,
@@ -8824,7 +8828,25 @@ static int JimSearchList(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *valOb
88248828
return 0;
88258829
}
88268830

8831+
static int JimRegexpMatch(Jim_Interp *interp, Jim_Obj *patternObj, Jim_Obj *objPtr)
8832+
{
8833+
Jim_Obj *argv[3];
8834+
int argc = 0;
8835+
long eq;
8836+
int rc;
88278837

8838+
argv[argc++] = Jim_NewStringObj(interp, "regexp", -1);
8839+
argv[argc++] = patternObj;
8840+
argv[argc++] = objPtr;
8841+
8842+
rc = Jim_EvalObjVector(interp, argc, argv);
8843+
8844+
if (rc != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &eq) != JIM_OK) {
8845+
eq = -rc;
8846+
}
8847+
8848+
return eq;
8849+
}
88288850

88298851
static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprNode *node)
88308852
{
@@ -8869,11 +8891,22 @@ static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprNode *node)
88698891
case JIM_EXPROP_STRNI:
88708892
wC = !JimSearchList(interp, B, A);
88718893
break;
8894+
case JIM_EXPROP_STRGLOB:
8895+
wC = Jim_StringMatchObj(interp, B, A, 0);
8896+
break;
8897+
case JIM_EXPROP_STRRE:
8898+
wC = JimRegexpMatch(interp, B, A);
8899+
if (wC < 0) {
8900+
rc = JIM_ERR;
8901+
goto error;
8902+
}
8903+
break;
88728904
default:
88738905
abort();
88748906
}
88758907
Jim_SetResultInt(interp, wC);
88768908

8909+
error:
88778910
Jim_DecrRefCount(interp, A);
88788911
Jim_DecrRefCount(interp, B);
88798912

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

90059038
OPRINIT("eq", 60, 2, JimExprOpStrBin),
90069039
OPRINIT("ne", 60, 2, JimExprOpStrBin),
9040+
OPRINIT("=*", 60, 2, JimExprOpStrBin),
9041+
OPRINIT("=~", 60, 2, JimExprOpStrBin),
90079042

90089043
OPRINIT("in", 55, 2, JimExprOpStrBin),
90099044
OPRINIT("ni", 55, 2, JimExprOpStrBin),
@@ -9268,7 +9303,7 @@ static int JimParseExprOperator(struct JimParserCtx *pc)
92689303
return JIM_OK;
92699304
}
92709305

9271-
#if (defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND)) && !defined(JIM_BOOTSTRAP)
9306+
#if defined(JIM_TT_NAME) && !defined(JIM_BOOTSTRAP)
92729307
static const char *jim_tt_name(int type)
92739308
{
92749309
static const char * const tt_names[JIM_TT_EXPR_OP] =

tests/regexp2.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,4 +943,20 @@ test regexp-25.3 {End of word} {
943943
regexp {\mcd\M} cdef
944944
} 0
945945

946+
test regexp-26.1 {regexp operator =~} {
947+
expr {"abc" =~ "^a"}
948+
} 1
949+
950+
test regexp-26.2 {regexp operator =~} {
951+
expr {"abc" =~ "^b"}
952+
} 0
953+
954+
test regexp-26.2 {regexp operator =~} {
955+
expr {"abc" =~ ".b."}
956+
} 1
957+
958+
test regexp-26.3 {regexp operator =~ invalid regexp} -body {
959+
expr {"abc" =~ {[}}
960+
} -returnCodes error -result {couldn't compile regular expression pattern: brackets [] not balanced}
961+
946962
testreport

tests/stringmatch.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,16 @@ test stringmatch-7.4 {null in pattern} {
230230
string match *b\[\0a\]r* foobar
231231
} 1
232232

233+
test regexp-8.1 {string match operator =*} {
234+
expr {"abc" =* "a*"}
235+
} 1
236+
237+
test regexp-26.2 {regexp operator =~} {
238+
expr {"abc" =* "b*"}
239+
} 0
240+
241+
test regexp-26.2 {regexp operator =~} {
242+
expr {"abc" =* {*[bB]c}}
243+
} 1
244+
233245
testreport

0 commit comments

Comments
 (0)