Subject: Request Tcl 8.1 Updated string command, take 2 - DN [1]
scripticsBugForm@auto.genned.post - 05 May 1999 - comp.lang.tcl
Tcl 8.1 Request: Generated by Scriptics' bug entry form at
http://www.scriptics.com/support/bugForm.html
Responses to this post are encouraged.
------
Submitted by: Jeffrey Hobbs
OperatingSystem: Sun Solaris
OperatingSystemVersion: 2.5.1
Synopsis: Updated string command, take 2
DesiredBehavior:
This is related to the previous feature request for an updated string
command, with one new method (string is) and patched against 8.1.0.
To cover it all again, I have made the following mods:
* string compare now takes optional length arg
(for strncmp behavior)
* added string equal (just a few lines of code blended
in with string compare)
* added string icompare/iequal for case-insensitive comparisons
* string index's index can now be ?end[+-]?expression
I made this change in the private TclGetIntForIndex,
which means that the list commands also benefit, as
well as string range, et al.
* added string is <class> ?-strict? ?-failindex varname? string
The following are valid classes:
alnum, alpha, ascii, boolean, digit, double, false,
integer, lower, space, true, upper, wordchar
Most of the correspond to the std is*() C functions. Those
that are missing (like iscntrl()) are due to a lack of
Tcl_UniCharIs*() equivalent, but could be added. I chose
not to mess with adding them, as that means going in and
setting things in the stub tables and such. However, they
are not complicated (zB, *IsControl() is a one liner).
Those that do not correspond to is*() (like boolean, int,
...) are intelligently handled to return the correct value,
but the command is written such that no type conversion of
the passed object occurs (a good thing, I believe). For
integer and double, this was tricky, but the test suite is
full, and the C code is well-commented with the purpose.
With the exception of int/double, which are inherently in
Tcl not unicode safe (still calls to strto*), all of these
are unicode safe (with tests).
* added [string repeat string count]
Repeats given string <count> number of times
* added string replace, quasi string equiv to lreplace
(quasi opposite of string range):
string replace first last ?string?
Example of use, replacing end of string with ...
should the string be more than 16 chars long:
string replace $string 16 end "..."
This just returns the string len < 16, so it
will only affect the long strings.
* added optional first and last args to string to*
This allows you to just affect certain regions of
a string with the command (like just capping the
first letter). I found the original totitle to
be too draconian to be useful.
* added [string map charMap string]
where charMap is a {from to from to} list that equates to
what one might get from [array get]. Each <from> and <to>
can be multiple chars (or none at all). For Tcl/CGI users,
this is a MAJOR speed booster.
Patch:
The patch comes to 100KB with full doc and test suite changes
against the Tcl 8.1.0 sources.
http://www.hobbs.wservice.com/tcl/capp/Tcl81-string.txt
PatchFiles:
C: tclUtil.c && tclCmdMZ.c
doc: string.n
tests: string.test cmd*.test l*.test error.test
Comments:
The implementations were done to maintain 100% backwards compatibility
with working code (changing the options to an arg can obviously change
what error strings arise), and reduce any loss of speed (the only
method that actually attained a speed loss was string index, as I
throw a Tcl_NumUtfChars call in to support end-1 like indices, and
that is not major).
Last modified
1999-09-27
1999-09-27
(195.108.246.50)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
