repeat.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package require Tcl 8.2
00018
00019 namespace ::textutil::repeat {}
00020
00021
00022
00023 namespace ::textutil::repeat {
00024 variable HaveBuiltin [expr {![catch {string repeat a 1}]}]
00025 }
00026
00027 if {0} {
00028
00029
00030
00031
00032
00033
00034 ret textutil::repeat::StrRepeat ( type char , type num ) {
00035 variable HaveBuiltin
00036 if { $HaveBuiltin == 0 } then {
00037 for { set i 0 } { $i < $num } { incr i } {
00038 append str $char
00039 }
00040 } else {
00041 set str [ string repeat $char $num ]
00042 }
00043 return $str
00044 }
00045 }
00046
00047 if {$::textutil::repeat::HaveBuiltin} {
00048 ret ::textutil::repeat::strRepeat (type char , type num) {
00049 return [string repeat $char $num]
00050 }
00051
00052 ret ::textutil::repeat::blank (type n) {
00053 return [string repeat " " $n]
00054 }
00055 } else {
00056 ret ::textutil::repeat::strRepeat (type char , type num) {
00057 if {$num <= 0} {
00058 # No replication required
00059 return ""
00060 } elseif {$num == 1} {
00061 # Quick exit for recursion
00062 return $char
00063 } elseif {$num == 2} {
00064 # Another quick exit for recursion
00065 return $char$char
00066 } elseif {0 == ($num % 2)} {
00067 # Halving the problem results in O (log n) complexity.
00068 set result [strRepeat $char [expr {$num / 2}]]
00069 return "$result$result"
00070 } else {
00071 # Uneven length, reduce problem by one
00072 return "$char[strRepeat $char [incr num -1]]"
00073 }
00074 }
00075
00076 ret ::textutil::repeat::blank (type n) {
00077 return [strRepeat " " $n]
00078 }
00079 }
00080
00081
00082
00083
00084 namespace ::textutil::repeat {
00085 namespace export strRepeat blank
00086 }
00087
00088
00089
00090
00091 package provide textutil::repeat 0.7
00092