Enhanced Syntax for Pair-Wise IndicesAndreas Leitgeb$Revision: 1.7 $Tcl lrange lreplace
This TIP proposes adding another anchor s (for usage exclusively in end-indices) that refers to the respective start-index.
Currently, most commands that accept an index also accept the local keyword end as well as simple arithmetics involving this keyword.
Commands that accept a pair of indices for a range, require an explicit start and (inclusive)end index for the requested subrange.
In other languages (C++/stl, java, ...) it appears to be more common to specify ranges by start and length. Since the length has a higher tendency of being constant, this seems actually the better idea, anyway. For compatibility reasons, we just cannot simply change all the commands to take offset/length pairs rather than start/end.
However we could add a new anchor s to be used in the end-index, and allow simple arithmetic on it just like on the other index forms.
Specifying a subrange of length 1, one would then simply specify s as end-index. For a pair, "s+1" as end index does it.
If the start position of a subrange is obtained as result of a command, currently one typically does in tcl:
IHNldCBvZmZzIFsuLi5dIC4uLiBbbHJhbmdlICRsaXN0ICRvZmZzIFtleHByIHskb2ZmcysxfV1dIC4uLg==
A previous TIP has added support to do the following instead:
IHNldCBvZmZzIFsuLi5dIC4uLiBbbHJhbmdlICRsaXN0ICRvZmZzICRvZmZzKzFdIC4uLg==
This TIP aims at making this the new idiom:
IC4uLiBbbHJhbmdlICRsaXN0IFsuLi5dIHMrMV0=
Currently there is a function TclGetIntForIndex, which deals with the current end anchor. It could be accompanied with a second function (e.g., TclGetIntForIndex2) that would accept another argument for the previously calculated index (from the range-start argument), and use that in case the new anchor s occurs in the range-end index.
No change at all is proposed for each range's start index.
None, at the moment.
For a sketch see previous section. In bytecode, it could be done such, that if an s-prefix is seen for an (eventually bytecoded range-related command), then the current stack-top (the corresponding start-index) is dup'ed and then incremented or decremented according to any offset (s+#, s-#).
For lreplace, s-1 as an end-index would actually make it an linsert operation.
This TIP covers all tcl commands that accept a pair of indices to specify a range, including (but not limited to) lrange, lreplace, string range, plus any third-party command written such as to use the proposed TclGetIntForIndex2 function for parsing end-indices.
This document has been placed in the public domain.