trim.tcl

Go to the documentation of this file.
00001 /*  trim.tcl --*/
00002 /* */
00003 /*  Various ways of trimming a string.*/
00004 /* */
00005 /*  Copyright (c) 2000      by Ajuba Solutions.*/
00006 /*  Copyright (c) 2000      by Eric Melski <ericm@ajubasolutions.com>*/
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: trim.tcl,v 1.5 2006/04/21 04:42:28 andreas_kupries Exp $*/
00013 
00014 /*  ### ### ### ######### ######### #########*/
00015 /*  Requirements*/
00016 
00017 package require Tcl 8.2
00018 
00019 namespace ::textutil::trim {}
00020 
00021 /*  ### ### ### ######### ######### #########*/
00022 /*  API implementation*/
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 /*  @c Strips <a prefix> from <a text>, if found at its start.*/
00043 /* */
00044 /*  @a text: The string to check for <a prefix>.*/
00045 /*  @a prefix: The string to remove from <a text>.*/
00046 /* */
00047 /*  @r The <a text>, but without <a prefix>.*/
00048 /* */
00049 /*  @i remove, prefix*/
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 /*  @c Removes the Heading Empty Lines of <a text>.*/
00061 /* */
00062 /*  @a text: The text block to manipulate.*/
00063 /* */
00064 /*  @r The <a text>, but without heading empty lines.*/
00065 /* */
00066 /*  @i remove, empty lines*/
00067 
00068 ret  ::textutil::trim::trimEmptyHeading (type text) {
00069     regsub -- "^(\[ \t\]*\n)*" $text {} text
00070     return $text
00071 }
00072 
00073 /*  ### ### ### ######### ######### #########*/
00074 /*  Helper commands. Internal*/
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 /*  Data structures*/
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 /*  Ready*/
00111 
00112 package provide textutil::trim 0.7
00113 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1