string.tcl

Go to the documentation of this file.
00001 /*  string.tcl --*/
00002 /* */
00003 /*  Utilities for manipulating strings, words, single lines,*/
00004 /*  paragraphs, ...*/
00005 /* */
00006 /*  Copyright (c) 2000      by Ajuba Solutions.*/
00007 /*  Copyright (c) 2000      by Eric Melski <ericm@ajubasolutions.com>*/
00008 /*  Copyright (c) 2002      by Joe English <jenglish@users.sourceforge.net>*/
00009 /*  Copyright (c) 2001-2006 by Andreas Kupries <andreas_kupries@users.sourceforge.net>*/
00010 /* */
00011 /*  See the file "license.terms" for information on usage and redistribution*/
00012 /*  of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00013 /*  */
00014 /*  RCS: @(#) $Id: string.tcl,v 1.1 2006/04/21 04:42:28 andreas_kupries Exp $*/
00015 
00016 /*  ### ### ### ######### ######### #########*/
00017 /*  Requirements*/
00018 
00019 package require Tcl 8.2
00020 
00021 namespace ::textutil::string {}
00022 
00023 /*  ### ### ### ######### ######### #########*/
00024 /*  API implementation*/
00025 
00026 /*  @c Removes the last character from the given <a string>.*/
00027 /* */
00028 /*  @a string: The string to manipulate.*/
00029 /* */
00030 /*  @r The <a string> without its last character.*/
00031 /* */
00032 /*  @i chopping*/
00033 
00034 ret  ::textutil::string::chop (type string) {
00035     return [string range $string 0 [expr {[string length $string]-2}]]
00036 }
00037 
00038 /*  @c Removes the first character from the given <a string>.*/
00039 /*  @c Convenience procedure.*/
00040 /* */
00041 /*  @a string: string to manipulate.*/
00042 /* */
00043 /*  @r The <a string> without its first character.*/
00044 /* */
00045 /*  @i tail*/
00046 
00047 ret  ::textutil::string::tail (type string) {
00048     return [string range $string 1 end]
00049 }
00050 
00051 /*  @c Capitalizes first character of the given <a string>.*/
00052 /*  @c Complementary procedure to <p ::textutil::uncap>.*/
00053 /* */
00054 /*  @a string: string to manipulate.*/
00055 /* */
00056 /*  @r The <a string> with its first character capitalized.*/
00057 /* */
00058 /*  @i capitalize*/
00059 
00060 ret  ::textutil::string::cap (type string) {
00061     return [string toupper [string index $string 0]][string range $string 1 end]
00062 }
00063 
00064 /*  @c unCapitalizes first character of the given <a string>.*/
00065 /*  @c Complementary procedure to <p ::textutil::cap>.*/
00066 /* */
00067 /*  @a string: string to manipulate.*/
00068 /* */
00069 /*  @r The <a string> with its first character uncapitalized.*/
00070 /* */
00071 /*  @i uncapitalize*/
00072 
00073 ret  ::textutil::string::uncap (type string) {
00074     return [string tolower [string index $string 0]][string range $string 1 end]
00075 }
00076 
00077 /*  Compute the longest string which is common to all strings given to*/
00078 /*  the command, and at the beginning of said strings, i.e. a prefix. If*/
00079 /*  only one argument is specified it is treated as a list of the*/
00080 /*  strings to look at. If more than one argument is specified these*/
00081 /*  arguments are the strings to be looked at. If only one string is*/
00082 /*  given, in either form, the string is returned, as it is its own*/
00083 /*  longest common prefix.*/
00084 
00085 ret  ::textutil::string::longestCommonPrefix (type args) {
00086     return [longestCommonPrefixList $args]
00087 }
00088 
00089 ret  ::textutil::string::longestCommonPrefixList (type list) {
00090     if {[llength $list] == 0} {
00091     return ""
00092     } elseif {[llength $list] == 1} {
00093     return [lindex $list 0]
00094     }
00095 
00096     set list [lsort  $list]
00097     set min  [lindex $list 0]
00098     set max  [lindex $list end]
00099 
00100     # Min and max are the two strings which are most different. If
00101     # they have a common prefix, it will also be the common prefix for
00102     # all of them.
00103 
00104     # Fast bailouts for common cases.
00105 
00106     set n [string length $min]
00107     if {$n == 0}                         {return ""}
00108     if {0 == [string compare $min $max]} {return $min}
00109 
00110     set prefix ""
00111     for {set i 0} {$i < $n} {incr i} {
00112     if {0 == [string compare [set x [string range $min 0 $i]] [string range $max 0 $i]]} {
00113         set prefix $x
00114         continue
00115     }
00116     break
00117     }
00118     return $prefix
00119 }
00120 
00121 /*  ### ### ### ######### ######### #########*/
00122 /*  Data structures*/
00123 
00124 namespace ::textutil::string {
00125     /*  Export the imported commands*/
00126 
00127     namespace export chop tail cap uncap
00128     namespace export longestCommonPrefix
00129     namespace export longestCommonPrefixList
00130 }
00131 
00132 /*  ### ### ### ######### ######### #########*/
00133 /*  Ready*/
00134 
00135 package provide textutil::string 0.7
00136 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1