| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | |
| 3 | # string.bash --- bash emulation of string(3) library routines |
| 4 | # Author: Noah Friedman <[email protected]> |
| 5 | # ==> Used with his kind permission in this document. |
| 6 | # Created: 1992-07-01 |
| 7 | # Last modified: 1993-09-29 |
| 8 | # Public domain |
| 9 | |
| 10 | # Conversion to bash v2 syntax done by Chet Ramey |
| 11 | |
| 12 | # Commentary: |
| 13 | # Code: |
| 14 | |
| 15 | #:docstring strcat: |
| 16 | # Usage: strcat s1 s2 |
| 17 | # |
| 18 | # Strcat appends the value of variable s2 to variable s1. |
| 19 | # |
| 20 | # Example: |
| 21 | # a="foo" |
| 22 | # b="bar" |
| 23 | # strcat a b |
| 24 | # echo $a |
| 25 | # => foobar |
| 26 | # |
| 27 | #:end docstring: |
| 28 | |
| 29 | ###;;;autoload ==> Autoloading of function commented out. |
| 30 | function strcatscip-shell shell abs-guide 10-4 strcat(). () |
| 31 | { |
| 32 | local s1_val s2_val |
| 33 | |
| 34 | s1_val=${!1} # indirect variable expansion |
| 35 | s2_val=${!2} |
| 36 | eval "$1"=\'"${s1_val}${s2_val}"\' |
| 37 | # ==> eval $1='${s1_val}${s2_val}' avoids problems, |
| 38 | # ==> if one of the variables contains a single quote. |
| 39 | } |
| 40 | |
| 41 | #:docstring strncat: |
| 42 | # Usage: strncat s1 s2 $n |
| 43 | # |
| 44 | # Line strcat, but strncat appends a maximum of n characters from the value |
| 45 | # of variable s2. It copies fewer if the value of variabl s2 is shorter |
| 46 | # than n characters. Echoes result on stdout. |
| 47 | # |
| 48 | # Example: |
| 49 | # a=foo |
| 50 | # b=barbaz |
| 51 | # strncat a b 3 |
| 52 | # echo $a |
| 53 | # => foobar |
| 54 | # |
| 55 | #:end docstring: |
| 56 | |
| 57 | ###;;;autoload |
| 58 | function strncatscip-shell shell abs-guide 10-4 strncat(). () |
| 59 | { |
| 60 | local s1="$1" |
| 61 | local s2="$2" |
| 62 | local -i n="$3" |
| 63 | local s1_val s2_val |
| 64 | |
| 65 | s1_val=${!s1} # ==> indirect variable expansion |
| 66 | s2_val=${!s2} |
| 67 | |
| 68 | if [scip-shell system . . `[`. ${#s2_val} -gt ${n} ]; then |
| 69 | s2_val=${s2_val:0:$n} # ==> substring extraction |
| 70 | fi |
| 71 | |
| 72 | eval "$s1"=\'"${s1_val}${s2_val}"\' |
| 73 | # ==> eval $1='${s1_val}${s2_val}' avoids problems, |
| 74 | # ==> if one of the variables contains a single quote. |
| 75 | } |
| 76 | |
| 77 | #:docstring strcmp: |
| 78 | # Usage: strcmp $s1 $s2 |
| 79 | # |
| 80 | # Strcmp compares its arguments and returns an integer less than, equal to, |
| 81 | # or greater than zero, depending on whether string s1 is lexicographically |
| 82 | # less than, equal to, or greater than string s2. |
| 83 | #:end docstring: |
| 84 | |
| 85 | ###;;;autoload |
| 86 | function strcmpscip-shell shell abs-guide 10-4 strcmp(). () |
| 87 | { |
| 88 | [scip-shell system . . `[`. "$1" = "$2" ] && return 0 |
| 89 | |
| 90 | [scip-shell system . . `[`. "${1}" '<' "${2}" ] > /dev/nullscip-shell filesystem . . `/dev/null`. && return -1 |
| 91 | |
| 92 | return 1 |
| 93 | } |
| 94 | |
| 95 | #:docstring strncmp: |
| 96 | # Usage: strncmp $s1 $s2 $n |
| 97 | # |
| 98 | # Like strcmp, but makes the comparison by examining a maximum of n |
| 99 | # characters (n less than or equal to zero yields equality). |
| 100 | #:end docstring: |
| 101 | |
| 102 | ###;;;autoload |
| 103 | function strncmpscip-shell shell abs-guide 10-4 strncmp(). () |
| 104 | { |
| 105 | if [scip-shell system . . `[`. -z "${3}" -o "${3}" -le "0" ]; then |
| 106 | return 0 |
| 107 | fi |
| 108 | |
| 109 | if [scip-shell system . . `[`. ${3} -ge ${#1} -a ${3} -ge ${#2} ]; then |
| 110 | strcmpscip-shell shell abs-guide 10-4 strcmp(). "$1" "$2" |
| 111 | return $? |
| 112 | else |
| 113 | s1=${1:0:$3} |
| 114 | s2=${2:0:$3} |
| 115 | strcmpscip-shell shell abs-guide 10-4 strcmp(). $s1 $s2 |
| 116 | return $? |
| 117 | fi |
| 118 | } |
| 119 | |
| 120 | #:docstring strlen: |
| 121 | # Usage: strlen s |
| 122 | # |
| 123 | # Strlen returns the number of characters in string literal s. |
| 124 | #:end docstring: |
| 125 | |
| 126 | ###;;;autoload |
| 127 | function strlenscip-shell shell abs-guide 10-4 strlen(). () |
| 128 | { |
| 129 | eval echo "\${#${1}}" |
| 130 | # ==> Returns the length of the value of the variable |
| 131 | # ==> whose name is passed as an argument. |
| 132 | } |
| 133 | |
| 134 | #:docstring strspn: |
| 135 | # Usage: strspn $s1 $s2 |
| 136 | # |
| 137 | # Strspn returns the length of the maximum initial segment of string s1, |
| 138 | # which consists entirely of characters from string s2. |
| 139 | #:end docstring: |
| 140 | |
| 141 | ###;;;autoload |
| 142 | function strspnscip-shell shell abs-guide 10-4 strspn(). () |
| 143 | { |
| 144 | # Unsetting IFS allows whitespace to be handled as normal chars. |
| 145 | local IFS= |
| 146 | local result="${1%%[!${2}]*}" |
| 147 | |
| 148 | echoscip-shell system . . echo. ${#result} |
| 149 | } |
| 150 | |
| 151 | #:docstring strcspn: |
| 152 | # Usage: strcspn $s1 $s2 |
| 153 | # |
| 154 | # Strcspn returns the length of the maximum initial segment of string s1, |
| 155 | # which consists entirely of characters not from string s2. |
| 156 | #:end docstring: |
| 157 | |
| 158 | ###;;;autoload |
| 159 | function strcspnscip-shell shell abs-guide 10-4 strcspn(). () |
| 160 | { |
| 161 | # Unsetting IFS allows whitspace to be handled as normal chars. |
| 162 | local IFS= |
| 163 | local result="${1%%[${2}]*}" |
| 164 | |
| 165 | echoscip-shell system . . echo. ${#result} |
| 166 | } |
| 167 | |
| 168 | #:docstring strstr: |
| 169 | # Usage: strstr s1 s2 |
| 170 | # |
| 171 | # Strstr echoes a substring starting at the first occurrence of string s2 in |
| 172 | # string s1, or nothing if s2 does not occur in the string. If s2 points to |
| 173 | # a string of zero length, strstr echoes s1. |
| 174 | #:end docstring: |
| 175 | |
| 176 | ###;;;autoload |
| 177 | function strstrscip-shell shell abs-guide 10-4 strstr(). () |
| 178 | { |
| 179 | # if s2 points to a string of zero length, strstr echoes s1 |
| 180 | [scip-shell system . . `[`. ${#2} -eq 0 ] && { echoscip-shell system . . echo. "$1" ; return 0; } |
| 181 | |
| 182 | # strstr echoes nothing if s2 does not occur in s1 |
| 183 | case "$1" in |
| 184 | *$2*) ;; |
| 185 | *) return 1;; |
| 186 | esac |
| 187 | |
| 188 | # use the pattern matching code to strip off the match and everything |
| 189 | # following it |
| 190 | first=${1/$2*/} |
| 191 | |
| 192 | # then strip off the first unmatched portion of the string |
| 193 | echoscip-shell system . . echo. "${1##$first}" |
| 194 | } |
| 195 | |
| 196 | #:docstring strtok: |
| 197 | # Usage: strtok s1 s2 |
| 198 | # |
| 199 | # Strtok considers the string s1 to consist of a sequence of zero or more |
| 200 | # text tokens separated by spans of one or more characters from the |
| 201 | # separator string s2. The first call (with a non-empty string s1 |
| 202 | # specified) echoes a string consisting of the first token on stdout. The |
| 203 | # function keeps track of its position in the string s1 between separate |
| 204 | # calls, so that subsequent calls made with the first argument an empty |
| 205 | # string will work through the string immediately following that token. In |
| 206 | # this way subsequent calls will work through the string s1 until no tokens |
| 207 | # remain. The separator string s2 may be different from call to call. |
| 208 | # When no token remains in s1, an empty value is echoed on stdout. |
| 209 | #:end docstring: |
| 210 | |
| 211 | ###;;;autoload |
| 212 | function strtokscip-shell shell abs-guide 10-4 strtok(). () |
| 213 | { |
| 214 | : |
| 215 | } |
| 216 | |
| 217 | #:docstring strtrunc: |
| 218 | # Usage: strtrunc $n $s1 {$s2} {$...} |
| 219 | # |
| 220 | # Used by many functions like strncmp to truncate arguments for comparison. |
| 221 | # Echoes the first n characters of each string s1 s2 ... on stdout. |
| 222 | #:end docstring: |
| 223 | |
| 224 | ###;;;autoload |
| 225 | function strtruncscip-shell shell abs-guide 10-4 strtrunc(). () |
| 226 | { |
| 227 | n=$1 ; shift |
| 228 | for z; do |
| 229 | echoscip-shell system . . echo. "${z:0:$n}" |
| 230 | done |
| 231 | } |
| 232 | |
| 233 | # provide string |
| 234 | |
| 235 | # string.bash ends here |
| 236 | |
| 237 | |
| 238 | # ========================================================================== # |
| 239 | # ==> Everything below here added by the document author. |
| 240 | |
| 241 | # ==> Suggested use of this script is to delete everything below here, |
| 242 | # ==> and "source" this file into your own scripts. |
| 243 | |
| 244 | # strcat |
| 245 | string0=one |
| 246 | string1=two |
| 247 | echoscip-shell system . . echo. |
| 248 | echoscip-shell system . . echo. "Testing \"strcat\" function:" |
| 249 | echoscip-shell system . . echo. "Original \"string0\" = $string0" |
| 250 | echoscip-shell system . . echo. "\"string1\" = $string1" |
| 251 | strcatscip-shell shell abs-guide 10-4 strcat(). string0 string1 |
| 252 | echoscip-shell system . . echo. "New \"string0\" = $string0" |
| 253 | echoscip-shell system . . echo. |
| 254 | |
| 255 | # strlen |
| 256 | echoscip-shell system . . echo. |
| 257 | echoscip-shell system . . echo. "Testing \"strlen\" function:" |
| 258 | str=123456789 |
| 259 | echoscip-shell system . . echo. "\"str\" = $str" |
| 260 | echoscip-shell system . . echo. -n "Length of \"str\" = " |
| 261 | strlenscip-shell shell abs-guide 10-4 strlen(). str |
| 262 | echoscip-shell system . . echo. |
| 263 | |
| 264 | |
| 265 | |
| 266 | # Exercise: |
| 267 | # -------- |
| 268 | # Add code to test all the other string functions above. |
| 269 | |
| 270 | |
| 271 | exit 0 |