Patch-ID: bash41-003
[bash.git] / examples / scripts.noah / string.bash
blobd80ebe80f136a57da3291a715b9ed59fc0d3cc28
1 # string.bash --- bash emulation of string(3) library routines
2 # Author: Noah Friedman <friedman@prep.ai.mit.edu>
3 # Created: 1992-07-01
4 # Last modified: 1993-09-29
5 # Public domain
7 # Conversion to bash v2 syntax done by Chet Ramey
9 # Commentary:
10 # Code:
12 #:docstring strcat:
13 # Usage: strcat s1 s2
15 # Strcat appends the value of variable s2 to variable s1.
17 # Example:
18 # a="foo"
19 # b="bar"
20 # strcat a b
21 # echo $a
22 # => foobar
24 #:end docstring:
26 ###;;;autoload
27 function strcat ()
29 local s1_val s2_val
31 s1_val=${!1} # indirect variable expansion
32 s2_val=${!2}
33 eval "$1"=\'"${s1_val}${s2_val}"\'
36 #:docstring strncat:
37 # Usage: strncat s1 s2 $n
39 # Line strcat, but strncat appends a maximum of n characters from the value
40 # of variable s2. It copies fewer if the value of variabl s2 is shorter
41 # than n characters. Echoes result on stdout.
43 # Example:
44 # a=foo
45 # b=barbaz
46 # strncat a b 3
47 # echo $a
48 # => foobar
50 #:end docstring:
52 ###;;;autoload
53 function strncat ()
55 local s1="$1"
56 local s2="$2"
57 local -i n="$3"
58 local s1_val s2_val
60 s1_val=${!s1} # indirect variable expansion
61 s2_val=${!s2}
63 if [ ${#s2_val} -gt ${n} ]; then
64 s2_val=${s2_val:0:$n} # substring extraction
67 eval "$s1"=\'"${s1_val}${s2_val}"\'
70 #:docstring strcmp:
71 # Usage: strcmp $s1 $s2
73 # Strcmp compares its arguments and returns an integer less than, equal to,
74 # or greater than zero, depending on whether string s1 is lexicographically
75 # less than, equal to, or greater than string s2.
76 #:end docstring:
78 ###;;;autoload
79 function strcmp ()
81 [ "$1" = "$2" ] && return 0
83 [ "${1}" '<' "${2}" ] > /dev/null && return -1
85 return 1
88 #:docstring strncmp:
89 # Usage: strncmp $s1 $s2 $n
91 # Like strcmp, but makes the comparison by examining a maximum of n
92 # characters (n less than or equal to zero yields equality).
93 #:end docstring:
95 ###;;;autoload
96 function strncmp ()
98 if [ -z "${3}" ] || [ "${3}" -le "0" ]; then
99 return 0
102 if [ ${3} -ge ${#1} ] && [ ${3} -ge ${#2} ]; then
103 strcmp "$1" "$2"
104 return $?
105 else
106 s1=${1:0:$3}
107 s2=${2:0:$3}
108 strcmp $s1 $s2
109 return $?
113 #:docstring strlen:
114 # Usage: strlen s
116 # Strlen returns the number of characters in string literal s.
117 #:end docstring:
119 ###;;;autoload
120 function strlen ()
122 eval echo "\${#${1}}"
125 #:docstring strspn:
126 # Usage: strspn $s1 $s2
128 # Strspn returns the length of the maximum initial segment of string s1,
129 # which consists entirely of characters from string s2.
130 #:end docstring:
132 ###;;;autoload
133 function strspn ()
135 # Unsetting IFS allows whitespace to be handled as normal chars.
136 local IFS=
137 local result="${1%%[!${2}]*}"
139 echo ${#result}
142 #:docstring strcspn:
143 # Usage: strcspn $s1 $s2
145 # Strcspn returns the length of the maximum initial segment of string s1,
146 # which consists entirely of characters not from string s2.
147 #:end docstring:
149 ###;;;autoload
150 function strcspn ()
152 # Unsetting IFS allows whitspace to be handled as normal chars.
153 local IFS=
154 local result="${1%%[${2}]*}"
156 echo ${#result}
159 #:docstring strstr:
160 # Usage: strstr s1 s2
162 # Strstr echoes a substring starting at the first occurrence of string s2 in
163 # string s1, or nothing if s2 does not occur in the string. If s2 points to
164 # a string of zero length, strstr echoes s1.
165 #:end docstring:
167 ###;;;autoload
168 function strstr ()
170 # if s2 points to a string of zero length, strstr echoes s1
171 [ ${#2} -eq 0 ] && { echo "$1" ; return 0; }
173 # strstr echoes nothing if s2 does not occur in s1
174 case "$1" in
175 *$2*) ;;
176 *) return 1;;
177 esac
179 # use the pattern matching code to strip off the match and everything
180 # following it
181 first=${1/$2*/}
183 # then strip off the first unmatched portion of the string
184 echo "${1##$first}"
187 #:docstring strtok:
188 # Usage: strtok s1 s2
190 # Strtok considers the string s1 to consist of a sequence of zero or more
191 # text tokens separated by spans of one or more characters from the
192 # separator string s2. The first call (with a non-empty string s1
193 # specified) echoes a string consisting of the first token on stdout. The
194 # function keeps track of its position in the string s1 between separate
195 # calls, so that subsequent calls made with the first argument an empty
196 # string will work through the string immediately following that token. In
197 # this way subsequent calls will work through the string s1 until no tokens
198 # remain. The separator string s2 may be different from call to call.
199 # When no token remains in s1, an empty value is echoed on stdout.
200 #:end docstring:
202 ###;;;autoload
203 function strtok ()
208 #:docstring strtrunc:
209 # Usage: strtrunc $n $s1 {$s2} {$...}
211 # Used by many functions like strncmp to truncate arguments for comparison.
212 # Echoes the first n characters of each string s1 s2 ... on stdout.
213 #:end docstring:
215 ###;;;autoload
216 function strtrunc ()
218 n=$1 ; shift
219 for z; do
220 echo "${z:0:$n}"
221 done
224 provide string
226 # string.bash ends here