repeat.tcl

Go to the documentation of this file.
00001 /*  repeat.tcl --*/
00002 /* */
00003 /*  Emulation of string repeat for older*/
00004 /*  revisions of Tcl.*/
00005 /* */
00006 /*  Copyright (c) 2000      by Ajuba Solutions.*/
00007 /*  Copyright (c) 2001-2006 by Andreas Kupries <andreas_kupries@users.sourceforge.net>*/
00008 /* */
00009 /*  See the file "license.terms" for information on usage and redistribution*/
00010 /*  of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00011 /* */
00012 /*  RCS: @(#) $Id: repeat.tcl,v 1.1 2006/04/21 04:42:28 andreas_kupries Exp $*/
00013 
00014 /*  ### ### ### ######### ######### #########*/
00015 /*  Requirements*/
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     /*  Problems with the deactivated code:*/
00029     /*  - Linear in 'num'.*/
00030     /*  - Tests for 'string repeat' in every call!*/
00031     /*    (Ok, just the variable, still a test every call)*/
00032     /*  - Fails for 'num == 0' because of undefined 'str'.*/
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 /*  Data structures*/
00083 
00084 namespace ::textutil::repeat {
00085     namespace export strRepeat blank
00086 }
00087 
00088 /*  ### ### ### ######### ######### #########*/
00089 /*  Ready*/
00090 
00091 package provide textutil::repeat 0.7
00092 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1