Version 4 of string concat

Updated 2009-01-26 04:22:44 by andy

Note: This is a renaming of string append.

This extends the command string to accept a new sub-command concat:

 if {[catch {string concat}]} then {
    rename string STRING_ORIGINAL
    proc string {cmd args} {
        switch -regexp -- $cmd {
            ^con(c(a(t)?)?)?$ {
                uplevel [list join $args {}]
            }
            default {
                if {[catch {
                    set result\
                        [uplevel [list STRING_ORIGINAL $cmd] $args]
                } err]} then {
                    return -code error\
                        [STRING_ORIGINAL map\
                             [list\
                                  STRING_ORIGINAL string\
                                  ", compare, equal,"\
                                  ", compare, concat, equal,"]\
                             $err]
                } else {
                    set result
                }
            }
        }
    }
 }

Test if it does as expected:

 % string concat hully gully
 hullygully
 %

Yeah. Check an original sub-cmd:

 % string match -nocase hully gully
 0
 % 

Works. Great. Now some erraneous situation:

 % string match -nocase hully gully bully
 wrong # args: should be "string match ?-nocase? pattern string"
 % 

The err msg hides the STRING_ORIGINAL and shows up string instead. Great again. Now another situation:

 % string what'dya'mean?
 bad option "what'dya'mean?": must be bytelength, compare, concat, equal, (...)
 % 

The err msg shows up all sub-cmds inclusive concat. Yep. That's it. Errp.


AMG: Why is there no [string concat] in the core? It would be helpful for concatenating strings that are quoted in different ways. For example:

string concat "dict with Sproc-[list $name]" {([list [namespace tail [lindex [info level 0] 0]]])} \{$script\}

is quite a bit easier to read (in my opinion) than:

"dict with Sproc-[list $name](\[list \[namespace tail \[lindex \[info level 0\] 0\]\]\]) {$script}"