Author: Jan Nijtmans <[email protected]>
State: Draft
Type: Project
Vote: Pending
Tcl-Version: 9.1
Tcl-Branch: tip-615
Abstract
This TIP proposes a new command string is index. It returns 1 if the provided
value is a valid index (such as 15 or end-2), 0 otherwise. See also
TIP #502.
Also, it prepares the way to handle the empty string as allowed index value,
and use it to bring consistency in commands which have first/last
arguments. All first and last arguments become optional or simply
can be filled with the empty string (for example if followed by other arguments
which need to be provided). If last is omitted or filled as the empty string,
it will be handled as if it contained the same value as first. So, if you
want to replace a single character in a string, you no longer need to write.
% string replace $string $index $index $newchar
you can simply write:
% string replace $string $index {} $newchar
This is usefull when $index is something more complex than only a variable reference.
It is also usefull, when the $index comes from Tk (e.g. by .b cget -underline, where
.b is a button). In Tk, -underline (or any other option which is an index) can
be the empty string if it is not configured. See also this bug,
which wouldn't have occurred with this TIP in place. Then string index [$child cget -text] [$child cget -underline]
would just return the empty string (as it did in Tk 8.6) in stead of giving an error.
Specification
In Tcl 9.0, Tcl_GetIntForIndex() returns TCL_ERROR when the empty string is input. In Tcl 9.1,
it will return TCL_OK, with value TCL_INDEX_NONE, in that case. If you want to
distinguish between the empty string and value -1, use Tcl_IsEmpty().
New function:
- string is ?-strict? index
This will return 1 if Tcl_GetIntForIndex() returns TCL_OK, 0 otherwise. With
the -strict argument, the empty string will return 0, otherwise the
empty string will return 1 (as other string is -strict variants do as well).
All commands which have an index or first argument, now accept the empty string
as well, which means "not provided". The command will handle it as if the argument wasn't
there. For index this normally means "out of range". For first it normally means that
the range starts at the beginning. Some functions (like lindex) accept a list of indices
(in an argument named list). Those will interpret the empty string as empty list, as before.
All commands which have a last argument, now accept the empty string
as well. The command will handle it as if it had the same value as first,
except if both first and last are the empty string, which would indicate
the full range.
Other Changed commands:
Old:
- ledit listVar first last ?value value ...?
- lrange list first last
- lreplace list first last ?element element ...?
- string range string first last
- string replace string first last ?newstring?
New:
- ledit listVar ?first? ?last? ?value value ...?
- lrange list ?first? ?last?
- lreplace list ?first? ?last? ?element element ...?
- string range string ?first? ?last?
- string replace string ?first? ?last? ?newstring?
The string tolower, string toupper and string toupper already have
?first? ?last? arguments. Those are enhanced to handle the empty string
as index value, no other changes are done.
Example:
% set l [list a b c]
a b c
% ledit l 1 {} d; #same as: ledit l 1 1 d
a d c
% lrange $l 1; #same as: lrange $l 1 1
d
% lreplace $l 1 {} x; #same as: lreplace $l 1 1 x
a x c
% string range $l 2; #same as: string range $l 2 2
x
% string range $l 2 {}; #same as: string range $l 2 2
x
% string replace $l 2 {} y; #same as: string replace $l 2 2 y
a y c
Implementation
Implementation is in Tcl branch "tip-615"
The Tcl byte-compiler is not fully optimized yet for the new behavior, since new shortcuts are now possible. That will be implemented after this TIP is accepted.
Compatibility
This is 100% upwards compatible with Tcl 9.0.
Copyright
This document has been placed in the public domain.
