if [llength [info commands p7proc]] { set Compiling true } else { set Compiling false source testing.tcl } if $Compiling { p7proc (int)is_nice_char { (int)c } { k ( '0'<=$c && $c<='9' || 'A'<=$c && $c<='Z' || 'a'<=$c && $c<='z' || $c=='.' || $c=='_' || $c=='-' ) } p7proc (int)nybble_to_hexchar { (int)x } { if ( 0<=$x && $x<=9 ) { return ($x+'0') } elseif ( 10<=$x && $x<=15 ) { return ($x+'A'-10) } error "bad int for nybble_to_hexchar: $x" } p7proc (utf*)Percentize { (utf*)s } { set (int)slen [len $s] set (int)zlen 0 loop (int)i $slen { if ([is_nice_char $s($i)] || $s($i)==' ' ) { incr zlen 1 } else { incr zlen 3 } } new (utf*)z $zlen #::puts zlen=$zlen set (int)j 0 loop i $slen { set (int)c $s($i) #::puts i=$i,,,c=$c if ([is_nice_char $c]) { set z($j) $c incr j } elseif (' ' == $c) { set z($j) ('+') incr j } else { set z($j) ('%') incr j set z($j) [nybble_to_hexchar (15&($c>>4))] incr j set z($j) [nybble_to_hexchar (15&$c)] incr j } } set z } p7proc (utf*)Plusify2 { (utf*)s } { set (int)slen [len $s] new (utf*)z $slen loop (int)i $slen { set (int)c $s($i) if ( $c == ' ' ) { set z($i) ('+') } else { set z($i) $s($i) } } set z } p7proc (utf*)Plusify1 { (utf*)s } { set (int)slen [len $s] for {set (int)i 0} ($i<$slen) {incr i} { set (int)c $s($i) if ( $c == ' ' ) { set s($i) ('+') } } set s } p7proc (utf*)PlusifyL { (utf*)s } { loop (int)i [len $s] { set (int)c $s($i) if ( $c == ' ' ) { set s($i) ('+') } } set s } p7proc (utf*)Plusify0 { (utf*)s } { for {set (int)i 0} ($i<[len $s]) {incr i} { set (int)c $s($i) if ( $c == ' ' ) { set s($i) ('+') } } set s } p7proc (utf*)to_lower { (utf*)x } { loop (int)i [len $x] { set (int)c $x(i) if ( 'A'<=c && c<='Z' ) { set x($i) (c-'A'+'a') } } set x } p7proc (byt*)to_lower_bytes { (byt*)x } { loop (int)i [len $x] { set (int)c $x(i) if ( 'A'<=c && c<='Z' ) { set x($i) (c-'A'+'a') } } set x } p7class Apple { (int)seed (tcl)core } p7class Banana { (int)skin (tcl)peel (Apple)apple } p7proc (Apple)new_Apple { } { new (Apple) } p7proc (Banana)new_Banana { } { new (Banana)b ; set b(apple) [new_Apple]; set b } p7proc (Apple)get_apple { (Banana)b } { set b(apple) } p7proc (tcl)tcl_peek { (tcl)x } { tclpeek $x } p7proc (int*)explode_uni { (uni*)x } { new (int*)z [len $x] loop (int)i [len $x] { set z($i) $x($i) } set z } p7proc (int*)explode_utf { (utf*)x } { new (int*)z [len $x] loop (int)i [len $x] { set z($i) $x($i) } set z } p7proc (int*)explode_byt { (byt*)x } { new (int*)z [len $x] loop (int)i [len $x] { set z($i) $x($i) } set z } p7proc (uni*)implode_uni { (int*)x } { new (uni*)z [len $x] loop (int)i [len $x] { set z($i) $x($i) } set z } p7proc (utf*)implode_utf { (int*)x } { new (utf*)z [len $x] loop (int)i [len $x] { set z($i) $x($i) } set z } p7proc (byt*)implode_byt { (int*)x } { new (byt*)z [len $x] loop (int)i [len $x] { set z($i) $x($i) } set z } } else { # tesing is_nice_char and implode_uni for {set i 0} {$i<999} {incr i} { if [is_nice_char $i] { AssertTrue {[regexp {^[-A-Za-z0-9_.]$} [implode_uni $i]]} } else { AssertFalse {[regexp {^[-A-Za-z0-9_.]$} [implode_uni $i]]} } } # testing nybble_to_hexchar for {set i 0} {$i<16} {incr i} { AssertEq [scan [string toupper [format %x $i]] %c c; set c] {nybble_to_hexchar $i} } # testing Percentize AssertEq %21abc+%3Dxyz { Percentize "!abc =xyz" } AssertEq %C2%80-%C3%BF-%E5%95%95-%C0%80 { Percentize "\u0080-\u00ff-\u5555-\u0000" } # testing Plusify{012L} foreach w { "a b c d e f g" "jwfijwifjow" "wiefo ewif i iewjo" } { AssertEq [string map { " " "+" } $w] { Plusify0 $w } AssertEq [string map { " " "+" } $w] { Plusify1 $w } AssertEq [string map { " " "+" } $w] { Plusify2 $w } AssertEq [string map { " " "+" } $w] { PlusifyL $w } } # testing to_lower and to_lower_bytes foreach w { AppleSauce f00b@R "hUnGeR fOrCe" } { AssertEq [string tolower $w] { to_lower $w } AssertEq [string tolower $w] { to_lower_bytes $w } } # test tcl_peek AssertMatch "Apple@*" { new_Apple } AssertMatch "Banana@*" { new_Banana } AssertMatch "p7class Apple addr=*" { tcl_peek [new_Apple] } AssertMatch "p7class Banana addr=*" { tcl_peek [new_Banana] } AssertMatch "p7class Apple addr=*" { tcl_peek [get_apple [new_Banana]] } AssertMatch int* { tcl_peek [expr {3+4} ] } AssertMatch double* { tcl_peek [expr { sin(1) } ] } AssertMatch regexp* { tcl_peek [ set x {ABC} ; regexp $x xxx ; set x ] } AssertMatch bytearray* { tcl_peek [ set f [open /dev/zero] ; fconfigure $f -encoding binary ; set x [read $f 32] ; close $f ; set x ] } foreach p [info commands ::* ] { AssertEq $p { implode_byt [ explode_byt $p ] } AssertEq $p { implode_utf [ explode_utf $p ] } AssertEq $p { implode_uni [ explode_uni $p ] } } ########## TIMING TESTS ############# set tc [read [open /etc/termcap]] puts lo=[time { set z [string tolower $tc] } 1 ] puts lo=[time { set z [string tolower $tc] } 10 ] puts rs=[time { set z [regsub -all { } $tc {+} y] } 1 ] puts rs=[time { set z [regsub -all { } $tc {+} y] } 10 ] puts lo=[time { set z [to_lower $tc] } 1 ] puts lo=[time { set z [to_lower $tc] } 10 ] puts p0=[time { set z [Plusify0 $tc] } 1 ] puts p0=[time { set z [Plusify0 $tc] } 10 ] puts p1=[time { set z [Plusify1 $tc] } 1 ] puts p1=[time { set z [Plusify1 $tc] } 10 ] puts pL=[time { set z [PlusifyL $tc] } 1 ] puts pL=[time { set z [PlusifyL $tc] } 10 ] puts p2=[time { set z [Plusify2 $tc] } 1 ] puts p2=[time { set z [Plusify2 $tc] } 10 ] puts %=[time { set z [Percentize $tc] } 1 ] puts %=[time { set z [Percentize $tc] } 10 ] puts lo=[time { set z [string tolower $tc] } 1 ] puts lo=[time { set z [string tolower $tc] } 10 ] puts rs=[time { set z [regsub -all { } $tc {+} y] } 1 ] puts rs=[time { set z [regsub -all { } $tc {+} y] } 10 ] Okay }