home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 5 / hacker05 / 05_HACK_05.ISO / programacao / freewrap / TCLLIBsampleApp.exe / sample / tcllib / tcllib1.0 / textutil / trim.tcl < prev   
Encoding:
Text File  |  2001-08-17  |  1.5 KB  |  73 lines

  1. namespace eval ::textutil {
  2.     
  3.     namespace eval trim {
  4.     
  5.     variable StrU "\[ \t\]+"
  6.     variable StrR "(${StrU})\$"
  7.     variable StrL "^(${StrU})"
  8.  
  9.     namespace export trim trimright trimleft
  10.  
  11.     # This will be redefined later. We need it just to let
  12.     # a chance for the next import subcommand to work
  13.     #
  14.     proc trimleft  { text { trim "[ \t]+" } } { }
  15.     proc trimright { text { trim "[ \t]+" } } { }
  16.     proc trim      { text { trim "[ \t]+" } } { }
  17.  
  18.     }
  19.  
  20.     namespace import -force trim::trim trim::trimleft trim::trimright
  21.     namespace export trim trimleft trimright
  22.  
  23. }
  24.  
  25. proc ::textutil::trim::trimleft { text { trim "[ \t]+" } } {
  26.     
  27.     set trim "[ MakeStr $trim left ]"
  28.     regsub -line -all -- $trim $text {} text
  29.  
  30.     return $text
  31. }
  32.  
  33. proc ::textutil::trim::trimright { text { trim "[ \t]+" } } {
  34.     
  35.     set trim "[ MakeStr $trim right ]"
  36.     regsub -line -all -- $trim $text {} text
  37.  
  38.     
  39.     return $text
  40. }
  41.  
  42. proc ::textutil::trim::trim { text { trim "[ \t]+" } } {
  43.     
  44.     set triml "[ MakeStr $trim left ]"
  45.     regsub -line -all -- $triml $text {} text
  46.     set trimr "[ MakeStr $trim right ]"
  47.     regsub -line -all -- $trimr $text {} text
  48.     
  49.     return $text
  50. }
  51.  
  52. proc ::textutil::trim::MakeStr { string pos }  {
  53.     variable StrU
  54.     variable StrR
  55.     variable StrL
  56.  
  57.     if { "$string" != "$StrU" } then {
  58.         set StrU $string
  59.         set StrR "(${StrU})\$"
  60.         set StrL "^(${StrU})"
  61.     }
  62.  
  63.     if { "$pos" == "left" } then {
  64.         return $StrL
  65.     }
  66.  
  67.     if { "$pos" == "right" } then {
  68.         return $StrR
  69.     }
  70.  
  71.     error "Panic, illegal position key \"$pos\""
  72. }
  73.