Skip to content

Commit

Permalink
(cherry-pick): Bug [5a1aaa20]: lsearch -stride with -subindices and -…
Browse files Browse the repository at this point in the history
…inline -all gives unexpected result
  • Loading branch information
jan.nijtmans committed Nov 26, 2024
1 parent 9271512 commit 8b83e05
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
27 changes: 13 additions & 14 deletions generic/tclCmdIL.c
Original file line number Diff line number Diff line change
Expand Up @@ -2246,7 +2246,7 @@ Tcl_JoinObjCmd(
joinObjPtr = (objc == 2) ? Tcl_NewStringObj(" ", 1) : objv[2];
Tcl_IncrRefCount(joinObjPtr);

(void) TclGetStringFromObj(joinObjPtr, &length);
(void)TclGetStringFromObj(joinObjPtr, &length);
if (length == 0) {
resObjPtr = TclStringCat(interp, listLen, elemPtrs, 0);
} else {
Expand Down Expand Up @@ -2663,8 +2663,7 @@ Tcl_LpopObjCmd(
/* empty list, throw the same error as with index "end" */
Tcl_SetObjResult(interp, Tcl_NewStringObj(
"index \"end\" out of range", -1));
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX"
"OUTOFRANGE", (char *)NULL);
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX", "OUTOFRANGE", (char *)NULL);
return TCL_ERROR;
}
elemPtr = elemPtrs[listLen - 1];
Expand Down Expand Up @@ -3203,7 +3202,7 @@ Tcl_LreverseObjCmd(
}

if (Tcl_IsShared(objv[1])
|| ListObjRepIsShared(objv[1])) { /* Bug 1675044 */
|| ListObjRepIsShared(objv[1])) { /* Bug 1675044 */
Tcl_Obj *resultObj, **dataArray;
ListRep listRep;

Expand Down Expand Up @@ -3504,8 +3503,7 @@ Tcl_LsearchObjCmd(
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"index \"%s\" out of range",
TclGetString(indices[j])));
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX"
"OUTOFRANGE", (char *)NULL);
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX", "OUTOFRANGE", (char *)NULL);
result = TCL_ERROR;
}
if (result == TCL_ERROR) {
Expand Down Expand Up @@ -3734,7 +3732,7 @@ Tcl_LsearchObjCmd(
} else {
itemPtr = listv[i+groupOffset];
}
switch ((enum datatypes) dataType) {
switch ((enum datatypes)dataType) {
case ASCII:
bytes = TclGetString(itemPtr);
match = strCmpFn(patternBytes, bytes);
Expand Down Expand Up @@ -3839,7 +3837,7 @@ Tcl_LsearchObjCmd(
switch (mode) {
case SORTED:
case EXACT:
switch ((enum datatypes) dataType) {
switch ((enum datatypes)dataType) {
case ASCII:
bytes = TclGetStringFromObj(itemPtr, &elemLen);
if (length == elemLen) {
Expand Down Expand Up @@ -3925,6 +3923,10 @@ Tcl_LsearchObjCmd(
itemPtr = SelectObjFromSublist(listv[i+groupOffset],
&sortInfo);
Tcl_ListObjAppendElement(interp, listPtr, itemPtr);
} else if (returnSubindices && (sortInfo.indexc == 0) && (groupSize > 1)) {
Tcl_BounceRefCount(itemPtr);
itemPtr = listv[i + groupOffset];
Tcl_ListObjAppendElement(interp, listPtr, itemPtr);
} else if (groupSize > 1) {
Tcl_ListObjReplace(interp, listPtr, LIST_MAX, 0,
groupSize, &listv[i]);
Expand Down Expand Up @@ -4612,8 +4614,7 @@ Tcl_LsortObjCmd(
Tcl_SetObjResult(interp, Tcl_ObjPrintf(
"index \"%s\" out of range",
TclGetString(indexv[j])));
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX"
"OUTOFRANGE", (char *)NULL);
Tcl_SetErrorCode(interp, "TCL", "VALUE", "INDEX", "OUTOFRANGE", (char *)NULL);
result = TCL_ERROR;
}
if (result == TCL_ERROR) {
Expand Down Expand Up @@ -4880,8 +4881,7 @@ Tcl_LsortObjCmd(
} else if (sortMode == SORTMODE_REAL) {
double a;

if (Tcl_GetDoubleFromObj(sortInfo.interp, indexPtr,
&a) != TCL_OK) {
if (Tcl_GetDoubleFromObj(sortInfo.interp, indexPtr, &a) != TCL_OK) {
sortInfo.resultCode = TCL_ERROR;
goto done;
}
Expand Down Expand Up @@ -5089,7 +5089,7 @@ Tcl_LeditObjCmd(
}

/*
* Tcl_ObjSetVar2 mau return a value different from listPtr in the
* Tcl_ObjSetVar2 may 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
Expand Down Expand Up @@ -5274,7 +5274,6 @@ SortCompare(
return 0;
}


objPtr1 = elemPtr1->collationKey.objValuePtr;
objPtr2 = elemPtr2->collationKey.objValuePtr;

Expand Down
6 changes: 6 additions & 0 deletions tests/lsearch.test
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,12 @@ test lsearch-27.5 {lsearch -stride + -subindices option} {
test lsearch-27.6 {lsearch -stride + -subindices option} {
lsearch -inline -stride 3 -subindices -all -index {1 0} -exact {{a c} {a b} {d a} {x c} {a b} {d a}} a
} {a a}
test lsearch-27.7 {lsearch -stride + -subindices option single index} {
lsearch -inline -stride 3 -subindices -all -index 1 {{x a} {y b} {x c} {xx a} {xx b} {xx c} {xxx a} {y b} {xxx c}} {y b}
} {{y b} {y b}}
test lsearch-27.8 {lsearch -stride + -subindices option single index} {
lsearch -inline -stride 3 -subindices -all -index end {{x a} {y b} {xc} {xx a} {xx b} {xx c} {xxx a} {y b} {}} *
} {xc {xx c} {}}

test lsearch-28.1 {lsearch -sorted with -stride} -body {
lsearch -sorted -stride 2 {5 3 7 8 9 2} 5
Expand Down

0 comments on commit 8b83e05

Please sign in to comment.