diff options
author | Kevin B Kenny <kennykb@acm.org> | 2008-10-05 22:12:20 (GMT) |
---|---|---|
committer | Kevin B Kenny <kennykb@acm.org> | 2008-10-05 22:12:20 (GMT) |
commit | c095ba4368ae987448c8141e73b12276031ed168 (patch) | |
tree | 94cff7ec70ea677fcadbc0e90d799558a6e94287 /generic | |
parent | d663ac8e9131b33c795b6a046e1114156c2062b8 (diff) | |
download | tcl-c095ba4368ae987448c8141e73b12276031ed168.zip tcl-c095ba4368ae987448c8141e73b12276031ed168.tar.gz tcl-c095ba4368ae987448c8141e73b12276031ed168.tar.bz2 |
TIP #331 IMPLEMENTATION
* generic/tclListObj.c (TclLsetFlat):
* tests/lset.test: Modified the [lset] command so that it allows
for an index of 'end+1', which has the effect of appending an
element to the list.
Diffstat (limited to 'generic')
-rw-r--r-- | generic/tclListObj.c | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/generic/tclListObj.c b/generic/tclListObj.c index aebaee8..68db503 100644 --- a/generic/tclListObj.c +++ b/generic/tclListObj.c @@ -10,7 +10,7 @@ * See the file "license.terms" for information on usage and redistribution of * this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclListObj.c,v 1.53 2008/09/10 13:03:33 dkf Exp $ + * RCS: @(#) $Id: tclListObj.c,v 1.54 2008/10/05 22:12:20 kennykb Exp $ */ #include "tclInt.h" @@ -1271,7 +1271,7 @@ TclLsetFlat( /* Index args. */ Tcl_Obj *valuePtr) /* Value arg to 'lset'. */ { - int index, result; + int index, result, len; Tcl_Obj *subListPtr, *retValuePtr, *chainPtr; /* @@ -1335,7 +1335,7 @@ TclLsetFlat( } indexArray++; - if (index < 0 || index >= elemCount) { + if (index < 0 || index > elemCount) { /* ...the index points outside the sublist. */ Tcl_SetObjResult(interp, Tcl_NewStringObj("list index out of range", -1)); @@ -1352,7 +1352,11 @@ TclLsetFlat( result = TCL_OK; if (--indexCount) { parentList = subListPtr; - subListPtr = elemPtrs[index]; + if (index == elemCount) { + subListPtr = Tcl_NewObj(); + } else { + subListPtr = elemPtrs[index]; + } if (Tcl_IsShared(subListPtr)) { subListPtr = Tcl_DuplicateObj(subListPtr); } @@ -1366,7 +1370,11 @@ TclLsetFlat( * make and store another copy. */ - TclListObjSetElement(NULL, parentList, index, subListPtr); + if (index == elemCount) { + Tcl_ListObjAppendElement(NULL, parentList, subListPtr); + } else { + TclListObjSetElement(NULL, parentList, index, subListPtr); + } if (Tcl_IsShared(subListPtr)) { subListPtr = Tcl_DuplicateObj(subListPtr); TclListObjSetElement(NULL, parentList, index, subListPtr); @@ -1428,7 +1436,12 @@ TclLsetFlat( } /* Store valuePtr in proper sublist and return */ - TclListObjSetElement(NULL, subListPtr, index, valuePtr); + Tcl_ListObjLength(NULL, subListPtr, &len); + if (index == len) { + Tcl_ListObjAppendElement(NULL, subListPtr, valuePtr); + } else { + TclListObjSetElement(NULL, subListPtr, index, valuePtr); + } Tcl_InvalidateStringRep(subListPtr); Tcl_IncrRefCount(retValuePtr); return retValuePtr; |