Index: openacs-4/packages/acs-tcl/tcl/text-html-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/text-html-procs.tcl,v diff -u -r1.82 -r1.83 --- openacs-4/packages/acs-tcl/tcl/text-html-procs.tcl 11 Jun 2018 09:14:55 -0000 1.82 +++ openacs-4/packages/acs-tcl/tcl/text-html-procs.tcl 28 Jun 2018 15:32:37 -0000 1.83 @@ -2342,6 +2342,48 @@ return $string } +ad_proc -public ad_pad { + -left:boolean + -right:boolean + string + length + padstring +} { + Tcl implementation of the pad string function found in many DBMSs.
+ One of the directional flags -left or -right must be specified and + will dictate whether this will be a lpad or a rpad. + + @param left text will be appended left of the original string. + @param right text will be appended right of the original string. + + @arg string String to be padded. + + @arg length length this string will be after padding. If string + this long or longer, will be truncated. + + @arg padstring string that will be repeated until length of + supplied string is equal or greather than length. + + @return padded string +} { + if {!($left_p ^ $right_p)} { + error "Please specifiy a single flag -left or -right" + } + + set slength [string length $string] + set padlength [string length $padstring] + set repetitions [expr {int(($length - $slength) / $padlength) + 1}] + set appended [string repeat $padstring $repetitions] + + if {$left_p} { + set string [string range $appended$string end-[expr {$length - 1}] end] + } else { + set string [string range $string$appended 0 [expr {$length - 1}]] + } + + return $string +} + # Local variables: # mode: tcl # tcl-indent-level: 4 Index: openacs-4/packages/acs-tcl/tcl/test/text-html-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-tcl/tcl/test/text-html-procs.tcl,v diff -u -r1.3 -r1.4 --- openacs-4/packages/acs-tcl/tcl/test/text-html-procs.tcl 15 Mar 2018 12:33:05 -0000 1.3 +++ openacs-4/packages/acs-tcl/tcl/test/text-html-procs.tcl 28 Jun 2018 15:32:37 -0000 1.4 @@ -201,13 +201,50 @@ -allowed_protocols * \ -no_outer_urls \ -validate] - aa_true "$msg with validate?" $valid_p + aa_true "$msg with validate?" $valid_p aa_false $msg? [regexp {<([a-z]\w*)\s+[^>]*(href|src|content|action)="(http|https|//):.*"[^>]*>} $result] } +aa_register_case -cats {api smoke} ad_pad { + Test if it ad_pad is working as expected +} { + + aa_log " ------------ Testing left pad ------------ " + + set string [ad_generate_random_string] + set length [expr {int(rand()*1000)}] + set padstring [ad_generate_random_string] + + aa_log " - string: $string" + aa_log " - length: $length" + aa_log " - padstring: $padstring" + + set result [ad_pad -left $string $length $padstring] + + aa_true " - Result is exactly $length long " {[string length $result] == $length} + aa_true " - String is at right end " [regexp "^.*${string}\$" $result] + + aa_log " ------------ Testing right pad ------------ " + + set string [ad_generate_random_string] + set length [expr {int(rand()*1000)}] + set padstring [ad_generate_random_string] + + aa_log " - string: $string" + aa_log " - length: $length" + aa_log " - padstring: $padstring" + + set result [ad_pad -right $string $length $padstring] + + aa_true " - Result is exactly $length long " {[string length $result] == $length} + aa_true " - String is at left end " [regexp "^${string}.*\$" $result] + +} + + # Local variables: # mode: tcl # tcl-indent-level: 4