-
Notifications
You must be signed in to change notification settings - Fork 12
/
string-funcs-sh
executable file
·231 lines (196 loc) · 4.79 KB
/
string-funcs-sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
_sub()
{
_SUB=$1
[ -n "$2" ] || return 1 ## nothing to search for: error
s_srch=${2} ## pattern to replace
rep=$3 ## replacement string
case $_SUB in *$s_srch*) ## if pattern exists in the string
sr1=${_SUB%%$s_srch*} ## take the string preceding the first match
sr2=${_SUB#*$s_srch} ## and the string following the first match
_SUB=$sr1$rep$sr2 ## and sandwich the replacement string between them
;;
*) return 1 ;; ## if the pattern does not exist, return an error code
esac
}
sub()
{
_sub "$@" && printf "%s\n" "$_SUB"
}
_rsub()
{
str=$1
[ "$2" ] || return 1
s_srch=${2}
rep=$3
case $str in *$s_srch*)
sr1=${str%$s_srch*}
sr2=${str##*$s_srch}
_RSUB=$sr1$rep$sr2
;;
*) _RSUB=
false ;;
esac
}
rsub()
{
_rsub "$@" && printf "%s\n" "$_RSUB"
}
_gsub()
{
## assign the string to sr2; this will be gobbled up with each substitution
sr2=$1
## return an error if there is no pattern specified
[ -n "$2" ] || return 1
## assign the search pattern to s_srch
s_srch=${2}
## assign the replacement text, if any, to rep
rep=$3
## sr1 will hold the portion of the string that has been processed
sr1=
## loop until sr2 no longer contains the search pattern
while :
do
case $sr2 in
*$s_srch*)
## add the string up to the match,
## and the replacement text, to sr1
sr1=$sr1${sr2%%$s_srch*}$rep
## remove up to the match from sr2
sr2=${sr2#*$s_srch}
;;
*) break ;;
esac
done
_GSUB=$sr1$sr2
}
gsub()
{
_gsub "$@" && printf "%s\n" "$_GSUB"
}
_repeat()
{
## If the first argument is -n, repeat the string N times
## otherwise repeat it to a length of N characters
case $1 in
-n) shift
r_num=$(( ${#1} * $2 ))
;;
*) r_num=$2
;;
esac
_REPEAT=$1
while [ ${#_REPEAT} -lt ${r_num} ]
do
_REPEAT=$_REPEAT$_REPEAT$_REPEAT
done
while [ ${#_REPEAT} -gt $r_num ]
do
_REPEAT=${_REPEAT%?}
done
}
repeat()
{
_repeat "$@" && printf "%s\n" "${_REPEAT}"
}
_index()
{
case $1 in
*$2*) ## extract up to beginning of the matching portion
idx=${1%%$2*}
## the starting position is one more than the length
_INDEX=$(( ${#idx} + 1 )) ;;
*) _INDEX=0; return 1 ;;
esac
}
index()
{
_index "$@" && printf "%d\n" "$_INDEX"
}
_rindex()
{
case $1 in
*$2*) ## extract up to beginning of the last matching portion
idx=${1%$2*}
## the starting position is one more than the length
_RINDEX=$(( ${#idx} + 1 )) ;;
*) _RINDEX=0; return 1 ;;
esac
}
rindex()
{
_rindex "$@" && printf "%d\n" "$_RINDEX"
}
_substr()
{
_SUBSTR=
## store the parameters
ss_str=$1
ss_first=$2
ss_length=${3:-${#ss_str}}
## return an error if the first character wanted is beyond end of string
if [ $ss_first -gt ${#ss_str} ]
then
return 1
fi
if [ $ss_first -gt 1 ]
then
## build a string of question marks to use as a wildcard pattern
_repeat "?" $(( $ss_first - 1 ))
## remove the beginning of string
ss_str=${ss_str#$_REPEAT}
elif [ ${ss_first} -lt 0 ] ## ${#ss_str} ]
then
## count from end of string
_repeat "?" ${ss_first#-}
## remove the beginning
ss_wild=$_REPEAT
ss_str=${ss_str#${ss_str%$ss_wild}}
fi
## ss_str now begins at the point we want to start extracting
## so print the desired number of characters
if [ ${#ss_str} -gt $ss_length ]
then
_repeat "${ss_wild:-??}" $ss_length
ss_wild=$_REPEAT
_SUBSTR=${ss_str%${ss_str#$ss_wild}}
else
_SUBSTR=${ss_str}
fi
}
substr()
{
_substr "$@" && printf "%s\n" "$_SUBSTR"
}
_insert_str()
{
_string=$1
i_string=$2
i_c=${3:-2} ## use default if position not supplied
i_1=${_string:0:i_c-1}
i_2=${_string:$i_c}
_INSERT_STR=$i_1$i_string$i_2
}
_insert_str()
{
_string=$1 ## The (soon-to-be) container string
i_string=$2 ## The string to be inserted
_pos=${3:-2} ## default to inserting after first character (position 2)
## Store the string up to the position of the insert
_substr "$_string" 1 $(( $_pos - 1 ))
i_1=$_SUBSTR
## Store the string that will go after the insert
_substr "$_string" $_pos
i_2=$_SUBSTR
## Sandwich the insert between the two pieces
_INSERT_STR=$i_1$i_string$i_2
}
insert_str()
{
_insert_str "$@" && printf "%s\n" "$_INSERT_STR"
}
case $BASH_VERSION in
[2-9]*)
. bash-funcs ## faster bash versions of some functions
;;
*)
esac