aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jim-regexp.c31
-rw-r--r--test.tcl16
2 files changed, 26 insertions, 21 deletions
diff --git a/jim-regexp.c b/jim-regexp.c
index e161d37..3c36ae9 100644
--- a/jim-regexp.c
+++ b/jim-regexp.c
@@ -318,7 +318,7 @@ int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
long offset = 0;
regex_t *regex;
const char *p;
- int result = JIM_ERR;
+ int result;
regmatch_t pmatch[MAX_SUB_MATCHES + 1];
int num_matches = 0;
@@ -330,9 +330,9 @@ int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
const char *replace_str;
const char *pattern;
- if (argc < 5) {
+ if (argc < 4) {
wrongNumArgs:
- Jim_WrongNumArgs(interp, 1, argv, "?-nocase? ?-all? exp string subSpec varName");
+ Jim_WrongNumArgs(interp, 1, argv, "?-nocase? ?-all? exp string subSpec ?varName?");
return JIM_ERR;
}
@@ -367,7 +367,7 @@ int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
break;
}
}
- if (argc - i != 4) {
+ if (argc - i != 3 && argc - i != 4) {
goto wrongNumArgs;
}
@@ -413,7 +413,7 @@ int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
regerror(match, regex, buf, sizeof(buf));
Jim_SetResultString(interp, "", 0);
Jim_AppendStrings(interp, Jim_GetResult(interp), "error while matching pattern: ", buf, NULL);
- goto done;
+ return JIM_ERR;
}
if (match == REG_NOMATCH) {
break;
@@ -479,19 +479,24 @@ int Jim_RegsubCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
*/
Jim_AppendString(interp, resultObj, p, -1);
- /* And now set the result variable */
- result = Jim_SetVariable(interp, varname, resultObj);
+ /* And now set or return the result variable */
+ if (argc - i == 4) {
+ result = Jim_SetVariable(interp, varname, resultObj);
- if (result == JIM_OK) {
- Jim_SetResultInt(interp, num_matches);
+ if (result == JIM_OK) {
+ Jim_SetResultInt(interp, num_matches);
+ }
+ else {
+ Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
+ Jim_AppendStrings(interp, Jim_GetResult(interp), "couldn't set variable \"", Jim_GetString(varname, NULL), "\"", NULL);
+ Jim_FreeObj(interp, resultObj);
+ }
}
else {
- Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
- Jim_AppendStrings(interp, Jim_GetResult(interp), "couldn't set variable \"", Jim_GetString(varname, NULL), "\"", NULL);
- Jim_FreeObj(interp, resultObj);
+ Jim_SetResult(interp, resultObj);
+ result = JIM_OK;
}
- done:
return result;
}
diff --git a/test.tcl b/test.tcl
index 62d2d9f..ff98d3b 100644
--- a/test.tcl
+++ b/test.tcl
@@ -3959,24 +3959,24 @@ test regexp-10.3 {newline sensitivity in regsub} {
xb}}
test regexp-11.1 {regsub errors} {
- list [catch {regsub a b c} msg] $msg
-} {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec varName"}}
+ list [catch {regsub a b} msg] $msg
+} {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec ?varName?"}}
test regexp-11.2 {regsub errors} {
- list [catch {regsub -nocase a b c} msg] $msg
-} {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec varName"}}
+ list [catch {regsub -nocase a b} msg] $msg
+} {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec ?varName?"}}
test regexp-11.3 {regsub errors} {
- list [catch {regsub -nocase -all a b c} msg] $msg
-} {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec varName"}}
+ list [catch {regsub -nocase -all a b} msg] $msg
+} {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec ?varName?"}}
test regexp-11.4 {regsub errors} {
list [catch {regsub a b c d e f} msg] $msg
-} {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec varName"}}
+} {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec ?varName?"}}
test regexp-11.5 {regsub errors} {
list [catch {regsub -gorp a b c} msg] $msg
-} {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec varName"}}
+} {1 {wrong # args: should be "regsub ?-nocase? ?-all? exp string subSpec ?varName?"}}
test regexp-11.6 {regsub errors} {
list [catch {regsub -nocase a( b c d} msg] $msg