trim.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::trim {}
00020
00021
00022
00023
00024 ret ::textutil::trim::trimleft (type text , optional trim ="[ \t]+") {
00025 regsub -line -all -- [MakeStr $trim left] $text {} text
00026 return $text
00027 }
00028
00029 ret ::textutil::trim::trimright (type text , optional trim ="[ \t]+") {
00030 regsub -line -all -- [MakeStr $trim right] $text {} text
00031 return $text
00032 }
00033
00034 ret ::textutil::trim::trim (type text , optional trim ="[ \t]+") {
00035 regsub -line -all -- [MakeStr $trim left] $text {} text
00036 regsub -line -all -- [MakeStr $trim right] $text {} text
00037 return $text
00038 }
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 ret ::textutil::trim::trimPrefix (type text , type prefix) {
00052 if {[string first $prefix $text] == 0} {
00053 return [string range $text [string length $prefix] end]
00054 } else {
00055 return $text
00056 }
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 ret ::textutil::trim::trimEmptyHeading (type text) {
00069 regsub -- "^(\[ \t\]*\n)*" $text {} text
00070 return $text
00071 }
00072
00073
00074
00075
00076 ret ::textutil::trim::MakeStr ( type string , type pos ) {
00077 variable StrU
00078 variable StrR
00079 variable StrL
00080
00081 if { "$string" != "$StrU" } {
00082 set StrU $string
00083 set StrR "(${StrU})\$"
00084 set StrL "^(${StrU})"
00085 }
00086 if { "$pos" == "left" } {
00087 return $StrL
00088 }
00089 if { "$pos" == "right" } {
00090 return $StrR
00091 }
00092
00093 return -code error "Panic, illegal position key \"$pos\""
00094 }
00095
00096
00097
00098
00099 namespace ::textutil::trim {
00100 variable StrU "\[ \t\]+"
00101 variable StrR "(${StrU})\$"
00102 variable StrL "^(${StrU})"
00103
00104 namespace export \
00105 trim trimright trimleft \
00106 trimPrefix trimEmptyHeading
00107 }
00108
00109
00110
00111
00112 package provide textutil::trim 0.7
00113