Version 7 of string append

Updated 2006-07-27 17:57:22

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

 if {[catch {string append}]} then {
    rename string STRING_ORIGINAL
    proc string {cmd args} {
        switch -regexp -- $cmd {
            ^a(p(p(e(n(d)?)?)?)?)?$ {
                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\
                                  ": must be bytelength,"\
                                  ": must be append, bytelength,"]\
                             $err]
                } else {
                    set result
                }
            }
        }
    }
 }

Now test it:

 % string append ham - and - eggs
 ham-and-eggs
 %

Now test an "original" sub-command:

 % string match -nocase ham and
 0
 % 

Result is 0 -- indeed, that is true. Now test an erraneous situation:

 % string match -nocase ham and eggs
 wrong # args: should be "string match ?-nocase? pattern string"
 % 

The error message hides the STRING_ORIGINAL and shows up string instead. Another error:

 % string brumm
 bad option "brumm": must be append, bytelength, compare, (...)
 % 

The error message shows the sub-commands inclusive the newly created append.

See also wrapping commands.