Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Updated string command, take 2

Closed / Fixed218751ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 01:04:56
OriginalBugID: 1992 RFE
Version: 8.1
SubmitDate: '1999-05-05'
LastModified: '1999-05-07'
Severity: LOW
Status: Released
Submitter: pat
ChangedBy: stanton
OS: Solaris
OSVersion: 2.5.1
Machine: Other
FixedDate: '1999-05-07'
FixedInVersion: 8.1.1
ClosedDate: '2000-10-25'


Name:
Jeffrey Hobbs

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).

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
welch Closed ticket [218751ffff]. [History diff]2000-10-26 08:28:13
dgp Open ticket [218751ffff]. [History diff]2001-04-18 06:54:21
dgp Closed ticket [218751ffff]. [History diff]2001-04-18 06:54:53
Log in (anonymous is fine) to add a comment.