Version 0 of string forward compatibility

Updated 2001-04-24 15:51:00

# Tcl 8.1 introduced new string commands, notably [string repeat].

 package require Tcl
 if {[package vcompare [package provide Tcl] 8.1] < 0} {
      rename string Tcl8.1_string
      proc string {cmd args} {
           switch -exact -- $cmd {
                repeat {
                        if {[llength $args] != 2} {
                                return -code error \
                                        "wrong # args, should be string repeat string count"
                        }
                        foreach {str n} $args break
                        strrep $str $n
                }
           }
           uplevel [list Tcl8.1_string $cmd] $args
      }
      proc strrep {text n} {
            if {$n <= 0} {
                return ""
            } elseif {$n == 1} {
                return $text
            } elseif {$n == 2} {
                return $text$text
            } elseif {0 == ($n % 2)} {
                set result [strrep $text [expr {$n / 2}]]
                return "$result$result"
            }
            return "$text[strrep $text [incr n -1]]"
      }
  }