From 257a1027899b2e8dc1cfec0d19b966d43d5f9cb2 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 17 Jan 2019 21:38:01 +0000 Subject: Slightly more efficient TclGetIntForIndexM macro, compared to previous commit --- generic/tclInt.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/generic/tclInt.h b/generic/tclInt.h index 53fb629..2fba5bf 100644 --- a/generic/tclInt.h +++ b/generic/tclInt.h @@ -2499,7 +2499,7 @@ typedef struct List { #define TclGetLongFromObj(interp, objPtr, longPtr) \ (((objPtr)->typePtr == &tclIntType \ && (objPtr)->internalRep.wideValue >= (Tcl_WideInt)(LONG_MIN) \ - && (objPtr)->internalRep.wideValue <= (Tcl_WideInt)(LONG_MAX)) \ + && (objPtr)->internalRep.wideValue <= (Tcl_WideInt)(LONG_MAX)) \ ? ((*(longPtr) = (long)(objPtr)->internalRep.wideValue), TCL_OK) \ : Tcl_GetLongFromObj((interp), (objPtr), (longPtr))) #endif @@ -2507,14 +2507,14 @@ typedef struct List { #define TclGetIntFromObj(interp, objPtr, intPtr) \ (((objPtr)->typePtr == &tclIntType \ && (objPtr)->internalRep.wideValue >= (Tcl_WideInt)(INT_MIN) \ - && (objPtr)->internalRep.wideValue <= (Tcl_WideInt)(INT_MAX)) \ + && (objPtr)->internalRep.wideValue <= (Tcl_WideInt)(INT_MAX)) \ ? ((*(intPtr) = (int)(objPtr)->internalRep.wideValue), TCL_OK) \ : Tcl_GetIntFromObj((interp), (objPtr), (intPtr))) #define TclGetIntForIndexM(interp, objPtr, endValue, idxPtr) \ (((objPtr)->typePtr == &tclIntType \ - && (objPtr)->internalRep.wideValue >= -1 \ - && (objPtr)->internalRep.wideValue <= INT_MAX) \ - ? ((*(idxPtr) = (int)(objPtr)->internalRep.wideValue), TCL_OK) \ + && (objPtr)->internalRep.wideValue <= (Tcl_WideInt)(INT_MAX)) \ + ? ((*(idxPtr) = ((objPtr)->internalRep.wideValue >= 0) \ + ? (int)(objPtr)->internalRep.wideValue : -1), TCL_OK) \ : TclGetIntForIndex((interp), (objPtr), (endValue), (idxPtr))) /* -- cgit v0.12 From 3dd30ce854309c3433f211cacf5c430b9e8d0005 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Fri, 18 Jan 2019 22:18:06 +0000 Subject: Clamp output of TclIndexDecode() between -1 (TCL_INDEX_NONE) and INT_MAX. Use this to produce slightly better error-message for lsort -index --- generic/tclCmdIL.c | 13 ++++++++++--- generic/tclUtil.c | 10 +++++++--- tests/cmdIL.test | 5 ++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/generic/tclCmdIL.c b/generic/tclCmdIL.c index 34e9382..a1a7f3e 100644 --- a/generic/tclCmdIL.c +++ b/generic/tclCmdIL.c @@ -4720,9 +4720,16 @@ SelectObjFromSublist( return NULL; } if (currentObj == NULL) { - Tcl_SetObjResult(infoPtr->interp, Tcl_ObjPrintf( - "element %d missing from sublist \"%s\"", - index, TclGetString(objPtr))); + if (index == TCL_INDEX_NONE) { + index = TCL_INDEX_END - infoPtr->indexv[i]; + Tcl_SetObjResult(infoPtr->interp, Tcl_ObjPrintf( + "element end-%d missing from sublist \"%s\"", + index, TclGetString(objPtr))); + } else { + Tcl_SetObjResult(infoPtr->interp, Tcl_ObjPrintf( + "element %d missing from sublist \"%s\"", + index, TclGetString(objPtr))); + } Tcl_SetErrorCode(infoPtr->interp, "TCL", "OPERATION", "LSORT", "INDEXFAILED", NULL); infoPtr->resultCode = TCL_ERROR; diff --git a/generic/tclUtil.c b/generic/tclUtil.c index a2670d5..04fc2af 100644 --- a/generic/tclUtil.c +++ b/generic/tclUtil.c @@ -4159,10 +4159,14 @@ TclIndexDecode( int encoded, /* Value to decode */ int endValue) /* Meaning of "end" to use, > TCL_INDEX_END */ { - if (encoded <= TCL_INDEX_END) { - return (encoded - TCL_INDEX_END) + endValue; + if (encoded > TCL_INDEX_END) { + return encoded; } - return encoded; + endValue += encoded - TCL_INDEX_END; + if (endValue >= 0) { + return endValue; + } + return TCL_INDEX_NONE; } /* diff --git a/tests/cmdIL.test b/tests/cmdIL.test index 360d6b0..e4931a4 100644 --- a/tests/cmdIL.test +++ b/tests/cmdIL.test @@ -232,7 +232,7 @@ test cmdIL-3.5.3 {SortCompare procedure, -index option (out of range, calculated } -returnCodes error -result {index "-2" cannot select an element from any list} test cmdIL-3.5.4 {SortCompare procedure, -index option (out of range, calculated index)} -body { lsort -index end-4 {{1 . c} {2 . b} {3 . a}} -} -returnCodes error -result {element -2 missing from sublist "1 . c"} +} -returnCodes error -result {element end-4 missing from sublist "1 . c"} test cmdIL-3.5.5 {SortCompare procedure, -index option} { lsort -index {} {a b} } {a b} @@ -248,6 +248,9 @@ test cmdIL-3.5.8 {SortCompare procedure, -index option (out of range, calculated test cmdIL-3.5.9 {SortCompare procedure, -index option (out of range, calculated index)} -body { lsort -index end+2 {{1 . c} {2 . b} {3 . a}} } -returnCodes error -result {index "end+2" cannot select an element from any list} +test cmdIL-3.5.10 {SortCompare procedure, -index option (out of range, calculated index)} -body { + lsort -index 0 {{}} +} -returnCodes error -result {element 0 missing from sublist ""} test cmdIL-3.6 {SortCompare procedure, -index option} { lsort -integer -index 2 {{1 15 30} {2 5 25} {3 25 20}} } {{3 25 20} {2 5 25} {1 15 30}} -- cgit v0.12