Skip to content

Commit 4a888a1

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

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

jim.c

+40-5
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) || defined(DEBUG_SHOW_SUBST)
112+
#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)
113+
#define JIM_TT_NAME
113114
static const char *jim_tt_name(int type);
114115
#endif
115116

@@ -8247,6 +8248,9 @@ enum
82478248
/* Binary operators (strings) */
82488249
JIM_EXPROP_STREQ, /* 43 */
82498250
JIM_EXPROP_STRNE,
8251+
JIM_EXPROP_STRGLOB,
8252+
JIM_EXPROP_STRRE,
8253+
82508254
JIM_EXPROP_STRIN,
82518255
JIM_EXPROP_STRNI,
82528256
JIM_EXPROP_STRLT,
@@ -8255,13 +8259,13 @@ enum
82558259
JIM_EXPROP_STRGE,
82568260

82578261
/* Unary operators (numbers) */
8258-
JIM_EXPROP_NOT, /* 51 */
8262+
JIM_EXPROP_NOT, /* 53 */
82598263
JIM_EXPROP_BITNOT,
82608264
JIM_EXPROP_UNARYMINUS,
82618265
JIM_EXPROP_UNARYPLUS,
82628266

82638267
/* Functions */
8264-
JIM_EXPROP_FUNC_INT, /* 55 */
8268+
JIM_EXPROP_FUNC_INT, /* 57 */
82658269
JIM_EXPROP_FUNC_WIDE,
82668270
JIM_EXPROP_FUNC_ABS,
82678271
JIM_EXPROP_FUNC_DOUBLE,
@@ -8270,7 +8274,7 @@ enum
82708274
JIM_EXPROP_FUNC_SRAND,
82718275

82728276
/* math functions from libm */
8273-
JIM_EXPROP_FUNC_SIN, /* 69 */
8277+
JIM_EXPROP_FUNC_SIN, /* 71 */
82748278
JIM_EXPROP_FUNC_COS,
82758279
JIM_EXPROP_FUNC_TAN,
82768280
JIM_EXPROP_FUNC_ASIN,
@@ -8836,7 +8840,25 @@ static int JimSearchList(Jim_Interp *interp, Jim_Obj *listObjPtr, Jim_Obj *valOb
88368840
return 0;
88378841
}
88388842

8843+
static int JimRegexpMatch(Jim_Interp *interp, Jim_Obj *patternObj, Jim_Obj *objPtr)
8844+
{
8845+
Jim_Obj *argv[3];
8846+
int argc = 0;
8847+
long eq;
8848+
int rc;
88398849

8850+
argv[argc++] = Jim_NewStringObj(interp, "regexp", -1);
8851+
argv[argc++] = patternObj;
8852+
argv[argc++] = objPtr;
8853+
8854+
rc = Jim_EvalObjVector(interp, argc, argv);
8855+
8856+
if (rc != JIM_OK || Jim_GetLong(interp, Jim_GetResult(interp), &eq) != JIM_OK) {
8857+
eq = -rc;
8858+
}
8859+
8860+
return eq;
8861+
}
88408862

88418863
static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprNode *node)
88428864
{
@@ -8881,11 +8903,22 @@ static int JimExprOpStrBin(Jim_Interp *interp, struct JimExprNode *node)
88818903
case JIM_EXPROP_STRNI:
88828904
wC = !JimSearchList(interp, B, A);
88838905
break;
8906+
case JIM_EXPROP_STRGLOB:
8907+
wC = Jim_StringMatchObj(interp, B, A, 0);
8908+
break;
8909+
case JIM_EXPROP_STRRE:
8910+
wC = JimRegexpMatch(interp, B, A);
8911+
if (wC < 0) {
8912+
rc = JIM_ERR;
8913+
goto error;
8914+
}
8915+
break;
88848916
default:
88858917
abort();
88868918
}
88878919
Jim_SetResultInt(interp, wC);
88888920

8921+
error:
88898922
Jim_DecrRefCount(interp, A);
88908923
Jim_DecrRefCount(interp, B);
88918924

@@ -9016,6 +9049,8 @@ static const struct Jim_ExprOperator Jim_ExprOperators[] = {
90169049

90179050
OPRINIT("eq", 60, 2, JimExprOpStrBin),
90189051
OPRINIT("ne", 60, 2, JimExprOpStrBin),
9052+
OPRINIT("=*", 60, 2, JimExprOpStrBin),
9053+
OPRINIT("=~", 60, 2, JimExprOpStrBin),
90199054

90209055
OPRINIT("in", 55, 2, JimExprOpStrBin),
90219056
OPRINIT("ni", 55, 2, JimExprOpStrBin),
@@ -9284,7 +9319,7 @@ static int JimParseExprOperator(struct JimParserCtx *pc)
92849319
return JIM_OK;
92859320
}
92869321

9287-
#if (defined(DEBUG_SHOW_SCRIPT) || defined(DEBUG_SHOW_SCRIPT_TOKENS) || defined(JIM_DEBUG_COMMAND) || defined(DEBUG_SHOW_SUBST)) && !defined(JIM_BOOTSTRAP)
9322+
#if defined(JIM_TT_NAME) && !defined(JIM_BOOTSTRAP)
92889323
static const char *jim_tt_name(int type)
92899324
{
92909325
static const char * const tt_names[JIM_TT_EXPR_OP] =

tests/regexp2.test

+16
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

+12
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)