| 1 | # string.tcl -- |
|---|
| 2 | # |
|---|
| 3 | # Utilities for manipulating strings, words, single lines, |
|---|
| 4 | # paragraphs, ... |
|---|
| 5 | # |
|---|
| 6 | # Copyright (c) 2000 by Ajuba Solutions. |
|---|
| 7 | # Copyright (c) 2000 by Eric Melski <ericm@ajubasolutions.com> |
|---|
| 8 | # Copyright (c) 2002 by Joe English <jenglish@users.sourceforge.net> |
|---|
| 9 | # Copyright (c) 2001-2006 by Andreas Kupries <andreas_kupries@users.sourceforge.net> |
|---|
| 10 | # |
|---|
| 11 | # See the file "license.terms" for information on usage and redistribution |
|---|
| 12 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
|---|
| 13 | # |
|---|
| 14 | # RCS: @(#) $Id: string.tcl,v 1.1 2006/04/21 04:42:28 andreas_kupries Exp $ |
|---|
| 15 | |
|---|
| 16 | # ### ### ### ######### ######### ######### |
|---|
| 17 | ## Requirements |
|---|
| 18 | |
|---|
| 19 | package require Tcl 8.2 |
|---|
| 20 | |
|---|
| 21 | namespace eval ::textutil::string {} |
|---|
| 22 | |
|---|
| 23 | # ### ### ### ######### ######### ######### |
|---|
| 24 | ## API implementation |
|---|
| 25 | |
|---|
| 26 | # @c Removes the last character from the given <a string>. |
|---|
| 27 | # |
|---|
| 28 | # @a string: The string to manipulate. |
|---|
| 29 | # |
|---|
| 30 | # @r The <a string> without its last character. |
|---|
| 31 | # |
|---|
| 32 | # @i chopping |
|---|
| 33 | |
|---|
| 34 | proc ::textutil::string::chop {string} { |
|---|
| 35 | return [string range $string 0 [expr {[string length $string]-2}]] |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | # @c Removes the first character from the given <a string>. |
|---|
| 39 | # @c Convenience procedure. |
|---|
| 40 | # |
|---|
| 41 | # @a string: string to manipulate. |
|---|
| 42 | # |
|---|
| 43 | # @r The <a string> without its first character. |
|---|
| 44 | # |
|---|
| 45 | # @i tail |
|---|
| 46 | |
|---|
| 47 | proc ::textutil::string::tail {string} { |
|---|
| 48 | return [string range $string 1 end] |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | # @c Capitalizes first character of the given <a string>. |
|---|
| 52 | # @c Complementary procedure to <p ::textutil::uncap>. |
|---|
| 53 | # |
|---|
| 54 | # @a string: string to manipulate. |
|---|
| 55 | # |
|---|
| 56 | # @r The <a string> with its first character capitalized. |
|---|
| 57 | # |
|---|
| 58 | # @i capitalize |
|---|
| 59 | |
|---|
| 60 | proc ::textutil::string::cap {string} { |
|---|
| 61 | return [string toupper [string index $string 0]][string range $string 1 end] |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | # @c unCapitalizes first character of the given <a string>. |
|---|
| 65 | # @c Complementary procedure to <p ::textutil::cap>. |
|---|
| 66 | # |
|---|
| 67 | # @a string: string to manipulate. |
|---|
| 68 | # |
|---|
| 69 | # @r The <a string> with its first character uncapitalized. |
|---|
| 70 | # |
|---|
| 71 | # @i uncapitalize |
|---|
| 72 | |
|---|
| 73 | proc ::textutil::string::uncap {string} { |
|---|
| 74 | return [string tolower [string index $string 0]][string range $string 1 end] |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | # Compute the longest string which is common to all strings given to |
|---|
| 78 | # the command, and at the beginning of said strings, i.e. a prefix. If |
|---|
| 79 | # only one argument is specified it is treated as a list of the |
|---|
| 80 | # strings to look at. If more than one argument is specified these |
|---|
| 81 | # arguments are the strings to be looked at. If only one string is |
|---|
| 82 | # given, in either form, the string is returned, as it is its own |
|---|
| 83 | # longest common prefix. |
|---|
| 84 | |
|---|
| 85 | proc ::textutil::string::longestCommonPrefix {args} { |
|---|
| 86 | return [longestCommonPrefixList $args] |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | proc ::textutil::string::longestCommonPrefixList {list} { |
|---|
| 90 | if {[llength $list] == 0} { |
|---|
| 91 | return "" |
|---|
| 92 | } elseif {[llength $list] == 1} { |
|---|
| 93 | return [lindex $list 0] |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | set list [lsort $list] |
|---|
| 97 | set min [lindex $list 0] |
|---|
| 98 | set max [lindex $list end] |
|---|
| 99 | |
|---|
| 100 | # Min and max are the two strings which are most different. If |
|---|
| 101 | # they have a common prefix, it will also be the common prefix for |
|---|
| 102 | # all of them. |
|---|
| 103 | |
|---|
| 104 | # Fast bailouts for common cases. |
|---|
| 105 | |
|---|
| 106 | set n [string length $min] |
|---|
| 107 | if {$n == 0} {return ""} |
|---|
| 108 | if {0 == [string compare $min $max]} {return $min} |
|---|
| 109 | |
|---|
| 110 | set prefix "" |
|---|
| 111 | for {set i 0} {$i < $n} {incr i} { |
|---|
| 112 | if {0 == [string compare [set x [string range $min 0 $i]] [string range $max 0 $i]]} { |
|---|
| 113 | set prefix $x |
|---|
| 114 | continue |
|---|
| 115 | } |
|---|
| 116 | break |
|---|
| 117 | } |
|---|
| 118 | return $prefix |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | # ### ### ### ######### ######### ######### |
|---|
| 122 | ## Data structures |
|---|
| 123 | |
|---|
| 124 | namespace eval ::textutil::string { |
|---|
| 125 | # Export the imported commands |
|---|
| 126 | |
|---|
| 127 | namespace export chop tail cap uncap |
|---|
| 128 | namespace export longestCommonPrefix |
|---|
| 129 | namespace export longestCommonPrefixList |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | # ### ### ### ######### ######### ######### |
|---|
| 133 | ## Ready |
|---|
| 134 | |
|---|
| 135 | package provide textutil::string 0.7 |
|---|