summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--generic/tclBasic.c1
-rw-r--r--generic/tclCmdIL.c117
-rw-r--r--generic/tclInt.h3
-rw-r--r--tests/lreplace.test295
4 files changed, 416 insertions, 0 deletions
diff --git a/generic/tclBasic.c b/generic/tclBasic.c
index a0c5a91..f7e0929 100644
--- a/generic/tclBasic.c
+++ b/generic/tclBasic.c
@@ -324,6 +324,7 @@ static const CmdInfo builtInCmds[] = {
{"lsearch", Tcl_LsearchObjCmd, NULL, NULL, CMD_IS_SAFE},
{"lset", Tcl_LsetObjCmd, TclCompileLsetCmd, NULL, CMD_IS_SAFE},
{"lsort", Tcl_LsortObjCmd, NULL, NULL, CMD_IS_SAFE},
+ {"lsubst", Tcl_LsubstObjCmd, NULL, NULL, CMD_IS_SAFE},
{"package", Tcl_PackageObjCmd, NULL, TclNRPackageObjCmd, CMD_IS_SAFE},
{"proc", Tcl_ProcObjCmd, NULL, NULL, CMD_IS_SAFE},
{"regexp", Tcl_RegexpObjCmd, TclCompileRegexpCmd, NULL, CMD_IS_SAFE},
diff --git a/generic/tclCmdIL.c b/generic/tclCmdIL.c
index cdc302c..7776c78 100644
--- a/generic/tclCmdIL.c
+++ b/generic/tclCmdIL.c
@@ -4486,6 +4486,123 @@ Tcl_LsortObjCmd(
/*
*----------------------------------------------------------------------
*
+ * Tcl_LsubstObjCmd --
+ *
+ * This procedure is invoked to process the "lsubst" Tcl command. See the
+ * user documentation for details on what it does.
+ *
+ * Results:
+ * A standard Tcl result.
+ *
+ * Side effects:
+ * See the user documentation.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+Tcl_LsubstObjCmd(
+ TCL_UNUSED(ClientData),
+ Tcl_Interp *interp, /* Current interpreter. */
+ int objc, /* Number of arguments. */
+ Tcl_Obj *const objv[]) /* Argument values. */
+{
+ Tcl_Obj *listPtr; /* Pointer to the list being altered. */
+ Tcl_Obj *finalValuePtr; /* Value finally assigned to the variable. */
+ int createdNewObj;
+ int result;
+ int first;
+ int last;
+ int listLen;
+ int numToDelete;
+
+ if (objc < 4) {
+ Tcl_WrongNumArgs(interp, 1, objv,
+ "listVar first last ?element ...?");
+ return TCL_ERROR;
+ }
+
+ listPtr = Tcl_ObjGetVar2(interp, objv[1], NULL, TCL_LEAVE_ERR_MSG);
+ if (listPtr == NULL) {
+ return TCL_ERROR;
+ }
+
+ /*
+ * TODO - refactor the index extraction into a common function shared
+ * by Tcl_{Lrange,Lreplace,Lsubst}ObjCmd
+ */
+
+ result = TclListObjLengthM(interp, listPtr, &listLen);
+ if (result != TCL_OK) {
+ return result;
+ }
+
+ result = TclGetIntForIndexM(interp, objv[2], /*end*/ listLen-1, &first);
+ if (result != TCL_OK) {
+ return result;
+ }
+
+ result = TclGetIntForIndexM(interp, objv[3], /*end*/ listLen-1, &last);
+ if (result != TCL_OK) {
+ return result;
+ }
+
+ if (first == TCL_INDEX_NONE) {
+ first = 0;
+ } else if (first > listLen) {
+ first = listLen;
+ }
+
+ if (last >= listLen) {
+ last = listLen - 1;
+ }
+ if (first <= last) {
+ numToDelete = last - first + 1;
+ } else {
+ numToDelete = 0;
+ }
+
+ if (Tcl_IsShared(listPtr)) {
+ listPtr = TclListObjCopy(NULL, listPtr);
+ createdNewObj = 1;
+ } else {
+ createdNewObj = 0;
+ }
+
+ result =
+ Tcl_ListObjReplace(interp, listPtr, first, numToDelete, objc - 4, objv + 4);
+ if (result != TCL_OK) {
+ if (createdNewObj) {
+ Tcl_DecrRefCount(listPtr);
+ }
+ return result;
+ }
+
+ /*
+ * Tcl_ObjSetVar2 mau return a value different from listPtr in the
+ * presence of traces etc.. Note that finalValuePtr will always have a
+ * reference count of at least 1 corresponding to the reference from the
+ * var. If it is same as listPtr, then ref count will be at least 2
+ * since we are incr'ing the latter below (safer when calling
+ * Tcl_ObjSetVar2 which can release it in some cases). Note that we
+ * leave the incrref of listPtr this late because we want to pass it as
+ * unshared to Tcl_ListObjReplace above if possible.
+ */
+ Tcl_IncrRefCount(listPtr);
+ finalValuePtr =
+ Tcl_ObjSetVar2(interp, objv[1], NULL, listPtr, TCL_LEAVE_ERR_MSG);
+ Tcl_DecrRefCount(listPtr); /* safe irrespective of createdNewObj */
+ if (finalValuePtr == NULL) {
+ return TCL_ERROR;
+ }
+
+ Tcl_SetObjResult(interp, finalValuePtr);
+ return TCL_OK;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
* MergeLists -
*
* This procedure combines two sorted lists of SortElement structures
diff --git a/generic/tclInt.h b/generic/tclInt.h
index 06ec2ad..562140c 100644
--- a/generic/tclInt.h
+++ b/generic/tclInt.h
@@ -3711,6 +3711,9 @@ MODULE_SCOPE int Tcl_LsetObjCmd(ClientData clientData,
MODULE_SCOPE int Tcl_LsortObjCmd(ClientData clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
+MODULE_SCOPE int Tcl_LsubstObjCmd(ClientData clientData,
+ Tcl_Interp *interp, int objc,
+ Tcl_Obj *const objv[]);
MODULE_SCOPE Tcl_Command TclInitNamespaceCmd(Tcl_Interp *interp);
MODULE_SCOPE int TclNamespaceEnsembleCmd(ClientData dummy,
Tcl_Interp *interp, int objc,
diff --git a/tests/lreplace.test b/tests/lreplace.test
index 0b26e86..4204c2f 100644
--- a/tests/lreplace.test
+++ b/tests/lreplace.test
@@ -236,6 +236,301 @@ apply {{} {
}
}}
+# Essentially same tests as above but for lsubst
+test lsubst-1.1 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 0 0 a] $l
+} {{a 2 3 4 5} {a 2 3 4 5}}
+test lsubst-1.2 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 1 1 a] $l
+} {{1 a 3 4 5} {1 a 3 4 5}}
+test lsubst-1.3 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 2 2 a] $l
+} {{1 2 a 4 5} {1 2 a 4 5}}
+test lsubst-1.4 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 3 3 a] $l
+} {{1 2 3 a 5} {1 2 3 a 5}}
+test lsubst-1.5 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 4 4 a] $l
+} {{1 2 3 4 a} {1 2 3 4 a}}
+test lsubst-1.6 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 4 5 a] $l
+} {{1 2 3 4 a} {1 2 3 4 a}}
+test lsubst-1.7 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l -1 -1 a] $l
+} {{a 1 2 3 4 5} {a 1 2 3 4 5}}
+test lsubst-1.8 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 2 end a b c d] $l
+} {{1 2 a b c d} {1 2 a b c d}}
+test lsubst-1.9 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 0 3] $l
+} {5 5}
+test lsubst-1.10 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 0 4] $l
+} {{} {}}
+test lsubst-1.11 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 0 1] $l
+} {{3 4 5} {3 4 5}}
+test lsubst-1.12 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 2 3] $l
+} {{1 2 5} {1 2 5}}
+test lsubst-1.13 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l 3 end] $l
+} {{1 2 3} {1 2 3}}
+test lsubst-1.14 {lsubst command} {
+ set l {1 2 3 4 5}
+ list [lsubst l -1 4 a b c] $l
+} {{a b c} {a b c}}
+test lsubst-1.15 {lsubst command} {
+ set l {a b "c c" d e f}
+ list [lsubst l 3 3] $l
+} {{a b {c c} e f} {a b {c c} e f}}
+test lsubst-1.16 {lsubst command} {
+ set l { 1 2 3 4 5}
+ list [lsubst l 0 0 a] $l
+} {{a 2 3 4 5} {a 2 3 4 5}}
+test lsubst-1.17 {lsubst command} {
+ set l {1 2 3 4 "5 6"}
+ list [lsubst l 4 4 a] $l
+} {{1 2 3 4 a} {1 2 3 4 a}}
+test lsubst-1.18 {lsubst command} {
+ set l {1 2 3 4 {5 6}}
+ list [lsubst l 4 4 a] $l
+} {{1 2 3 4 a} {1 2 3 4 a}}
+test lsubst-1.19 {lsubst command} {
+ set l {1 2 3 4}
+ list [lsubst l 2 end x y z] $l
+} {{1 2 x y z} {1 2 x y z}}
+test lsubst-1.20 {lsubst command} {
+ set l {1 2 3 4}
+ list [lsubst l end end a] $l
+} {{1 2 3 a} {1 2 3 a}}
+test lsubst-1.21 {lsubst command} {
+ set l {1 2 3 4}
+ list [lsubst l end 3 a] $l
+} {{1 2 3 a} {1 2 3 a}}
+test lsubst-1.22 {lsubst command} {
+ set l {1 2 3 4}
+ list [lsubst l end end] $l
+} {{1 2 3} {1 2 3}}
+test lsubst-1.23 {lsubst command} {
+ set l {1 2 3 4}
+ list [lsubst l 2 -1 xy] $l
+} {{1 2 xy 3 4} {1 2 xy 3 4}}
+test lsubst-1.24 {lsubst command} {
+ set l {1 2 3 4}
+ list [lsubst l end -1 z] $l
+} {{1 2 3 z 4} {1 2 3 z 4}}
+test lsubst-1.25 {lsubst command} {
+ set l {\}\ hello}
+ concat \"[lsubst l end end]\" $l
+} {"\}\ " \}\ }
+test lsubst-1.26 {lsubst command} {
+ catch {unset foo}
+ set foo {a b}
+ list [lsubst foo end end] $foo \
+ [lsubst foo end end] $foo \
+ [lsubst foo end end] $foo
+} {a a {} {} {} {}}
+test lsubst-1.27 {lsubset command} -body {
+ set l x
+ list [lsubst l 1 1] $l
+} -result {x x}
+test lsubst-1.28 {lsubst command} -body {
+ set l x
+ list [lsubst l 1 1 y] $l
+} -result {{x y} {x y}}
+test lsubst-1.29 {lsubst command} -body {
+ set l x
+ lsubst l 1 1 [error foo]
+} -returnCodes 1 -result {foo}
+test lsubst-1.30 {lsubst command} -body {
+ set l {not {}alist}
+ lsubst l 0 0 [error foo]
+} -returnCodes 1 -result {foo}
+test lsubst-1.31 {lsubst command} -body {
+ unset -nocomplain arr
+ set arr(x) {a b}
+ list [lsubst arr(x) 0 0 c] $arr(x)
+} -result {{c b} {c b}}
+
+test lsubst-2.1 {lsubst errors} -body {
+ list [catch lsubst msg] $msg
+} -result {1 {wrong # args: should be "lsubst listVar first last ?element ...?"}}
+test lsubst-2.2 {lsubst errors} -body {
+ unset -nocomplain x
+ list [catch {lsubst l b} msg] $msg
+} -result {1 {wrong # args: should be "lsubst listVar first last ?element ...?"}}
+test lsubst-2.3 {lsubst errors} -body {
+ set x {}
+ list [catch {lsubst x a 10} msg] $msg
+} -result {1 {bad index "a": must be integer?[+-]integer? or end?[+-]integer?}}
+test lsubst-2.4 {lsubst errors} -body {
+ set l {}
+ list [catch {lsubst l 10 x} msg] $msg
+} -result {1 {bad index "x": must be integer?[+-]integer? or end?[+-]integer?}}
+test lsubst-2.5 {lsubst errors} -body {
+ set l {}
+ list [catch {lsubst l 10 1x} msg] $msg
+} -result {1 {bad index "1x": must be integer?[+-]integer? or end?[+-]integer?}}
+test lsubst-2.6 {lsubst errors} -body {
+ set l x
+ list [catch {lsubst l 3 2} msg] $msg
+} -result {0 x}
+test lsubst-2.7 {lsubst errors} -body {
+ set l x
+ list [catch {lsubst l 2 2} msg] $msg
+} -result {0 x}
+test lsubst-2.8 {lsubst errors} -body {
+ unset -nocomplain l
+ lsubst l 0 0 x
+} -returnCodes error -result {can't read "l": no such variable}
+test lsubst-2.9 {lsubst errors} -body {
+ unset -nocomplain arr
+ lsubst arr(x) 0 0 x
+} -returnCodes error -result {can't read "arr(x)": no such variable}
+test lsubst-2.10 {lsubst errors} -body {
+ unset -nocomplain arr
+ set arr(y) y
+ lsubst arr(x) 0 0 x
+} -returnCodes error -result {can't read "arr(x)": no such element in array}
+
+test lsubst-3.1 {lsubst won't modify shared argument objects} {
+ proc p {} {
+ set l "a b c"
+ lsubst l 1 1 "x y"
+ # The literal in locals table should be unmodified
+ return [list "a b c" $l]
+ }
+ p
+} {{a b c} {a {x y} c}}
+
+# Following bugs were in lreplace. Make sure lsubst does not have them
+test lsubst-4.1 {Bug ccc2c2cc98: lreplace edge case} {
+ set l {}
+ list [lsubst l 1 1] $l
+} {{} {}}
+test lsubst-4.2 {Bug ccc2c2cc98: lreplace edge case} {
+ set l { }
+ list [lsubst l 1 1] $l
+} {{} {}}
+test lsubst-4.3 {lreplace edge case} {
+ set l {1 2 3}
+ lsubst l 2 0
+} {1 2 3}
+test lsubst-4.4 {lsubst edge case} {
+ set l {1 2 3 4 5}
+ list [lsubst l 3 1] $l
+} {{1 2 3 4 5} {1 2 3 4 5}}
+test lreplace-4.5 {lreplace edge case} {
+ lreplace {1 2 3 4 5} 3 0 _
+} {1 2 3 _ 4 5}
+test lsubst-4.6 {lsubst end-x: bug a4cb3f06c4} {
+ set l {0 1 2 3 4}
+ list [lsubst l 0 end-2] $l
+} {{3 4} {3 4}}
+test lsubst-4.6.1 {lsubst end-x: bug a4cb3f06c4} {
+ set l {0 1 2 3 4}
+ list [lsubst l 0 end-2 a b c] $l
+} {{a b c 3 4} {a b c 3 4}}
+test lsubst-4.7 {lsubst with two end-indexes: increasing} {
+ set l {0 1 2 3 4}
+ list [lsubst l end-2 end-1] $l
+} {{0 1 4} {0 1 4}}
+test lsubst-4.7.1 {lsubst with two end-indexes: increasing} {
+ set l {0 1 2 3 4}
+ list [lsubst l end-2 end-1 a b c] $l
+} {{0 1 a b c 4} {0 1 a b c 4}}
+test lsubst-4.8 {lsubst with two end-indexes: equal} {
+ set l {0 1 2 3 4}
+ list [lsubst l end-2 end-2] $l
+} {{0 1 3 4} {0 1 3 4}}
+test lsubst-4.8.1 {lsubst with two end-indexes: equal} {
+ set l {0 1 2 3 4}
+ list [lsubst l end-2 end-2 a b c] $l
+} {{0 1 a b c 3 4} {0 1 a b c 3 4}}
+test lsubst-4.9 {lsubst with two end-indexes: decreasing} {
+ set l {0 1 2 3 4}
+ list [lsubst l end-2 end-3] $l
+} {{0 1 2 3 4} {0 1 2 3 4}}
+test lsubst-4.9.1 {lsubst with two end-indexes: decreasing} {
+ set l {0 1 2 3 4}
+ list [lsubst l end-2 end-3 a b c] $l
+} {{0 1 a b c 2 3 4} {0 1 a b c 2 3 4}}
+test lsubst-4.10 {lsubst with two equal indexes} {
+ set l {0 1 2 3 4}
+ list [lsubst l 2 2] $l
+} {{0 1 3 4} {0 1 3 4}}
+test lsubst-4.10.1 {lsubst with two equal indexes} {
+ set l {0 1 2 3 4}
+ list [lsubst l 2 2 a b c] $l
+} {{0 1 a b c 3 4} {0 1 a b c 3 4}}
+test lsubst-4.11 {lsubst end index first} {
+ set l {0 1 2 3 4}
+ list [lsubst l end-2 1 a b c] $l
+} {{0 1 a b c 2 3 4} {0 1 a b c 2 3 4}}
+test lsubst-4.12 {lsubst end index first} {
+ set l {0 1 2 3 4}
+ list [lsubst l end-2 2 a b c] $l
+} {{0 1 a b c 3 4} {0 1 a b c 3 4}}
+test lsubst-4.13 {lsubst empty list} {
+ set l {}
+ list [lsubst l 1 1 1] $l
+} {1 1}
+test lsubst-4.14 {lsubst empty list} {
+ set l {}
+ list [lsubst l 2 2 2] $l
+} {2 2}
+
+test lsubst-5.1 {compiled lreplace: Bug 47ac84309b} {
+ apply {x {
+ lsubst x end 0
+ }} {a b c}
+} {a b c}
+test lsubst-5.2 {compiled lreplace: Bug 47ac84309b} {
+ apply {x {
+ lsubst x end 0 A
+ }} {a b c}
+} {a b A c}
+
+# Testing for compiled behaviour. Far too many variations to check with
+# spelt-out tests. Note that this *just* checks whether the compiled version
+# and the interpreted version are the same, not whether the interpreted
+# version is correct.
+apply {{} {
+ set lss {{} {a} {a b c} {a b c d}}
+ set ins {{} A {A B}}
+ set idxs {-2 -1 0 1 2 3 end-3 end-2 end-1 end end+1 end+2}
+ set lreplace lreplace
+
+ foreach ls $lss {
+ foreach a $idxs {
+ foreach b $idxs {
+ foreach i $ins {
+ set expected [list [catch {$lreplace $ls $a $b {*}$i} m] $m]
+ set tester [list lsubst ls $a $b {*}$i]
+ set script [list catch $tester m]
+ set script "list \[$script\] \$m"
+ test lsubst-6.[incr n] {lsubst battery} -body \
+ [list apply [list {ls} $script] $ls] -result $expected
+ }
+ }
+ }
+ }
+}}
+
# cleanup
catch {unset foo}
::tcltest::cleanupTests