aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2024-10-02 08:47:55 +1000
committerSteve Bennett <steveb@workware.net.au>2025-07-16 09:34:08 +1000
commit972d93d2df4da677d2f8b8e354169b7165d9e171 (patch)
treeb34b3fb0034db7a48b62facc9833e2d324726946
parentf4321af962050cfd3569f1cdf8df96424985604e (diff)
downloadjimtcl-972d93d2df4da677d2f8b8e354169b7165d9e171.zip
jimtcl-972d93d2df4da677d2f8b8e354169b7165d9e171.tar.gz
jimtcl-972d93d2df4da677d2f8b8e354169b7165d9e171.tar.bz2
regexp: assign match vars with -all
To match the documentation and Tcl, if match vars are given with -all, the last match is assigned. Fixes #310 Signed-off-by: Steve Bennett <steveb@workware.net.au>
-rw-r--r--jim-regexp.c14
-rw-r--r--tests/regexp.test5
2 files changed, 12 insertions, 7 deletions
diff --git a/jim-regexp.c b/jim-regexp.c
index 28cede4..22e69eb 100644
--- a/jim-regexp.c
+++ b/jim-regexp.c
@@ -259,10 +259,11 @@ int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
num_matches++;
- if (opt_all && !opt_inline) {
- /* Just count the number of matches, so skip the substitution h */
- goto try_next_match;
- }
+ /* We used to not assign vars for -all if not -inline, since we can't
+ * really assign capture groups for multiple matches, but Tcl does this,
+ * just setting the last value for each capture group, so we will do the
+ * same for compatibility
+ */
/*
* If additional variable names have been specified, return
@@ -270,7 +271,7 @@ int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
*/
j = 0;
- for (i += 2; opt_inline ? j < num_vars : i < argc; i++, j++) {
+ for (j = 0; j < num_vars; j++) {
Jim_Obj *resultObj;
if (opt_indices) {
@@ -304,7 +305,7 @@ int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
}
else {
/* And now set the result variable */
- result = Jim_SetVariable(interp, argv[i], resultObj);
+ result = Jim_SetVariable(interp, argv[i + 2 + j], resultObj);
if (result != JIM_OK) {
Jim_FreeObj(interp, resultObj);
@@ -313,7 +314,6 @@ int Jim_RegexpCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
}
}
- try_next_match:
if (opt_all && (pattern[0] != '^' || (regcomp_flags & REG_NEWLINE)) && *source_str) {
if (pmatch[0].rm_eo) {
offset += utf8_strlen(source_str, pmatch[0].rm_eo);
diff --git a/tests/regexp.test b/tests/regexp.test
index c7f949f..f7c589d 100644
--- a/tests/regexp.test
+++ b/tests/regexp.test
@@ -583,6 +583,11 @@ test regexp-18.12 {regexp -all -inline -indices} {
regexp -all -inline -indices a(b(c)d|e(f)g)h abcdhaefgh
} {{0 4} {1 3} {2 2} {-1 -1} {5 9} {6 8} {-1 -1} {7 7}}
+test regexp-18.13 {regexp -all with match vars} -body {
+ regexp -all a(b(c)d|e(f)g)h abcdhaefgh a b c d e
+ list $a $b $c $d $e
+} -result {aefgh efg {} f {}}
+
test regexp-19.1 {regsub null replacement} {
regsub -all {@} {@hel@lo@} "\0a\0" result
list $result [string length $result]