-
Notifications
You must be signed in to change notification settings - Fork 1
/
posix-arrays-indexed.sh
422 lines (363 loc) · 12.6 KB
/
posix-arrays-indexed.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/bin/sh
# shellcheck disable=SC2154,SC2086
# Copyright: friendly bits
# github.com/friendly-bits
# posix-arrays-indexed.sh
# emulates *indexed* arrays in POSIX shell
# each array element is stored in a variable named in the format '_i_[arr_name]_[index]'
# indices are stored in a variable named in the format '_i_[arr_name]_indices'
# buffered indices are stored in a variable named in the format '_i_[arr_name]_indices_b'
# array 'sorted' flag (1=true, 0=false) is stored in variable: $_i_[arr_name]_sorted_flag
# the 'sorted' flag is also used as an indicator of whether the array has any elements (for performance reasons)
# the $_i_[arr_name]_h_index variable holds the highest index in the array if it's known
# unsets all variables used to store an indexed array
# 1 - array name
unset_i_arr() {
___me="unset_i_arr"
case $# in 1) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"
_check_vars _arr_name || return 1
do_unset_i_arr "${_arr_name}"
}
# backend function
# unsets all variables of an indexed array
# 1 - array name
do_unset_i_arr() {
eval "_indices=\"\${_i_${1}_indices:-}\${_i_${1}_indices_b:-}\""
for _index in $_indices; do
unset "_i_${1}_${_index}"
done
unset "_i_${1}_indices" "_i_${1}_indices_b" "_i_${1}_h_index" "_i_${_arr_name}_sorted_flag"
}
# wrapper function for __sort_i_arr, intended as a user interface
# 1 - array name
sort_i_arr() {
___me="sort_i_arr"
case $# in 1) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"
_check_vars _arr_name || return 1
__sort_i_arr
:
}
# backend function
# sorts indices of indexed array
# sets the 'sorted' flag
__sort_i_arr() {
eval "_sorted_flag=\"\${_i_${_arr_name}_sorted_flag:-}\"
_h_index=\"\${_i_${_arr_name}_h_index:-}\""
case "$_sorted_flag" in
1) eval "_indices=\"\${_i_${_arr_name}_indices:-}\"" ;;
'') _h_index="-1" ;;
*) eval "_indices=\"\$(printf %s \"\${_i_${_arr_name}_indices:-}\${_i_${_arr_name}_indices_b:-}\" | sort -n)\"
_i_${_arr_name}_indices=\"\$_indices\"
_i_${_arr_name}_indices_b=''
_i_${_arr_name}_sorted_flag=1"
esac
}
# backend function
# finds the max index and assigns to variables '_h_index' and '_i_${arr_name}_h_index'
_get_h_index() {
case "${_h_index:-}" in '')
: "${_indices:=}"
_h_index="${_indices##*"${___nl}"}"
eval "_i_${_arr_name}_h_index"='$_h_index'
esac
}
# declare an indexed array while populating first elements
# resets all previous elements of the array if it already exists
# 1 - array name
# all other args - new array values
declare_i_arr() {
___me="declare_i_arr"
case $# in 0) wrongargs "$@"; return 1; esac
_arr_name="$1"; shift
_check_vars _arr_name || return 1
do_unset_i_arr "${_arr_name}"
_index=0; _indices=''
for ___val in "$@"; do
eval "_i_${_arr_name}_${_index}"='$_el_set_flag$___val'
_indices="$_indices$___nl$_index"
_index=$((_index + 1))
done
_index=$((_index - 1))
case "$_index" in
"-1") ;;
*) eval "_i_${_arr_name}_h_index"='$_index'"
_i_${_arr_name}_indices"='$_indices'"
_i_${_arr_name}_sorted_flag=1"
esac
:
}
# read lines from input string into an indexed array
# unsets all previous elements of the array if it already exists
# 1 - array name
# 2 - newline-separated string
read_i_arr() {
___me="read_i_arr"
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; ___lines="$2"
_check_vars _arr_name || return 1
do_unset_i_arr "${_arr_name}"
_index=0; _indices=''
IFS_OLD="$IFS"
IFS="$___nl"
for ___line in $___lines; do
eval "_i_${_arr_name}_${_index}"='$_el_set_flag$___line'
_indices="$_indices$___nl$_index"
_index=$((_index + 1))
done
IFS="$IFS_OLD"
_index=$((_index - 1))
case "$_index" in
"-1") ;;
*) eval "_i_${_arr_name}_h_index"='$_index'"
_i_${_arr_name}_indices"='$_indices'"
_i_${_arr_name}_sorted_flag=1"
esac
:
}
# get all values from an indexed array
# whitespace-delimited output is set as a value of a global variable
# (0 - optional: '-s' : sort the array by index and output sorted values)
# 1 - array name
# 2 - global variable name for output
get_i_arr_values() {
___me="get_i_arr_values"
case "${1:-}" in "-s") _do_sort=1; shift; esac
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; _out_var="$2"; ___values=''
_check_vars _arr_name _out_var || return 1
case "$_do_sort" in 1)
__sort_i_arr
unset _do_sort
esac
eval "_indices=\"\${_i_${_arr_name}_indices:-}\${_i_${_arr_name}_indices_b:-}\""
___values="$(
for _index in $_indices; do
eval "___val=\"\${_i_${_arr_name}_${_index}#$_el_set_flag}\""
case "$___val" in '') ;; *) printf '%s ' "$___val"; esac
done
)"
eval "$_out_var"='${___values% }'
:
}
# get all indices from an indexed array
# whitespace-delimited output is set as a value of a global variable
# (0 - optional: '-s' : sort the array and output sorted indices)
# 1 - array name
# 2 - global variable name for output
get_i_arr_indices() {
___me="get_i_arr_indices"
case "${1:-}" in "-s") _do_sort=1; shift; esac
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1" _out_var="$2"
_check_vars _arr_name _out_var || return 1
case "$_do_sort" in 1)
__sort_i_arr
unset _do_sort
esac
eval "_indices=\"\${_i_${_arr_name}_indices:-}\${_i_${_arr_name}_indices_b:-}\""
_indices="$(printf '%s ' $_indices)" # no quotes on purpose
eval "$_out_var"='${_indices% }'
:
}
# add a new element to an indexed array and set its value
# 1 - array name
# 2 - value
add_i_arr_el() {
___me="add_i_arr_el"
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; ___new_val="${2:-}"
_check_vars _arr_name || return 1
eval "_h_index=\"\${_i_${_arr_name}_h_index:-}\""
case "$_h_index" in '') __sort_i_arr; _get_h_index; esac
case "$_h_index" in "-1") eval "_i_${_arr_name}_sorted_flag=1"; esac
_index=$((_h_index + 1))
eval "_i_${_arr_name}_indices=\"\${_i_${_arr_name}_indices:-}${___nl}${_index}\"
_i_${_arr_name}_h_index"='$_index'"
_i_${_arr_name}_${_index}"='$_el_set_flag$___new_val'
:
}
# unset an element of an indexed array
# 1 - array name
# 2 - index
unset_i_arr_el() {
_rm_mid_index() {
eval "___last_ind=\"\${${1}##*$___nl}\"
___first_ind=\"\${${1}#$___nl}\""
case $((___last_ind + ${___first_ind%%"$___nl"*} < 2*_index)) in
1) eval "_i_${_arr_name}${1}=\"\${${1}%$___nl$_index$___nl*}$___nl\${${1}##*$___nl$_index$___nl}\"" ;;
0) eval "_i_${_arr_name}${1}=\"\${${1}%%$___nl$_index$___nl*}$___nl\${${1}#*$___nl$_index$___nl}\""
esac
}
___me="unset_i_arr_el"
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; _index="$2"
_check_vars _arr_name && check_index || return 1
eval "_sorted_flag=\"\${_i_${_arr_name}_sorted_flag:-}\"
_h_index=\"\${_i_${_arr_name}_h_index:-}\"
___old_val=\"\${_i_${_arr_name}_${_index}:-}\""
case "$___old_val" in *?* )
unset "_i_${_arr_name}_${_index}"
case "$_sorted_flag" in
1) eval "_indices=\"\${_i_${_arr_name}_indices:-}\""
case "${_indices#"$___nl"}" in
"$_index" ) unset "_i_${_arr_name}_indices"
unset "_i_${_arr_name}_h_index" "_i_${_arr_name}_sorted_flag" ;;
"$_index$___nl"* ) eval "_i_${_arr_name}_indices=\"\${_indices#$___nl$_index}\"" ;;
*"$___nl$_index" ) eval "_i_${_arr_name}_indices=\"\${_indices%$___nl$_index}\"
_i_${_arr_name}_h_index=\"\${_i_${_arr_name}_indices##*${___nl}}\"" ;;
*"$___nl$_index$___nl"* ) _rm_mid_index "_indices" ;;
'') unset "_i_${_arr_name}_h_index" "_i_${_arr_name}_sorted_flag"
esac
;;
0) eval "_indices=\"\${_i_${_arr_name}_indices:-}\"; _indices_b=\"\${_i_${_arr_name}_indices_b:-}\""
_no_b_ind=''
case "${_indices_b#"$___nl"}" in
"$_index$___nl"* ) eval "_i_${_arr_name}_indices_b=\"\${_indices_b#$___nl$_index}\"" ;;
*"$___nl$_index" ) eval "_i_${_arr_name}_indices_b=\"\${_indices_b%$___nl$_index}\"" ;;
*"$___nl$_index$___nl"* ) _rm_mid_index "_indices_b" ;;
"$_index" ) unset "_i_${_arr_name}_indices_b"; _no_b_ind=1 ;;
'') _no_b_ind=1
esac
case "${_indices#"$___nl"}" in
"$_index" ) unset "_i_${_arr_name}_indices"
case "$_no_b_ind" in 1) unset "_i_${_arr_name}_h_index" "_i_${_arr_name}_sorted_flag"; return 0; esac ;;
"$_index$___nl"* ) eval "_i_${_arr_name}_indices=\"\${_indices#$___nl$_index}\"" ;;
*"$___nl$_index" ) eval "_i_${_arr_name}_indices=\"\${_indices%$___nl$_index}\"" ;;
*"$___nl$_index$___nl"* ) _rm_mid_index "_indices" ;;
'') case "$_no_b_ind" in 1) unset "_i_${_arr_name}_h_index" "_i_${_arr_name}_sorted_flag"; return 0; esac
esac
case "$_index" in "$_h_index" ) unset "_i_${_arr_name}_h_index"; esac
esac
esac
:
}
# get maximum index of an indexed array
# 1 - array name
# 2 - global variable name for output
get_i_arr_max_index() {
___me="get_i_arr_max_index"
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; _out_var="$2"
_check_vars _arr_name _out_var || return 1
eval "_sorted_flag=\"\${_i_${_arr_name}_sorted_flag:-}\"
_h_index=\"\${_i_${_arr_name}_h_index:-}\""
case "$_h_index" in
'') case "${_sorted_flag:-}" in
'') unset "$_out_var" "_i_${_arr_name}_indices" _h_index _out_var
no_elements; return 1 ;;
*) __sort_i_arr; _get_h_index; eval "$_out_var"='${_h_index}'
esac ;;
*) eval "$_out_var"='$_h_index'
esac
:
}
# get value of the last element in an indexed array
# 1 - array name
# 2 - global variable name for output
get_i_arr_last_val() {
___me="get_i_arr_last_val"
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; _out_var="$2"
_check_vars _arr_name _out_var || return 1
eval "_sorted_flag=\"\${_i_${_arr_name}_sorted_flag:-}\"
_h_index=\"\${_i_${_arr_name}_h_index:-}\""
case "$_h_index" in
'') case "$_sorted_flag" in
'') unset "$_out_var" "_i_${_arr_name}_indices" _h_index _out_var
no_elements; return 1 ;;
*) __sort_i_arr; _get_h_index; eval "$_out_var=\"\${_i_${_arr_name}_${_h_index}#$_el_set_flag}\""
esac ;;
*) eval "$_out_var=\"\${_i_${_arr_name}_${_h_index}#$_el_set_flag}\""
esac
:
}
# get the element count of an indexed array
# 1 - array name
# 2 - global variable name for output
get_i_arr_el_cnt() {
___me="get_i_arr_el_cnt"
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; _out_var="$2"
_check_vars _arr_name _out_var || return 1
eval "_indices=\"\${_i_${_arr_name}_indices:-}\${_i_${_arr_name}_indices_b:-}\""
i=0
for _ind in $_indices; do i=$((i+1)); done
eval "$_out_var"='$i'
:
}
# set an element in an indexed array
# 1 - array name
# 2 - index
# 3 - value (if no value, unsets the element)
set_i_arr_el() {
___me="set_i_arr_el"
case $# in 2|3) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; _index="$2"; ___new_val="${3:-}"
_check_vars _arr_name && check_index || return 1
eval "___old_val=\"\${_i_${_arr_name}_${_index}:-}\"
_i_${_arr_name}_${_index}"='$_el_set_flag$___new_val'
case "$___old_val" in '')
eval "_h_index=\"\${_i_${_arr_name}_h_index:-}\"
_sorted_flag=\"\${_i_${_arr_name}_sorted_flag:-}\""
___entry="$___nl$_index"
case "$_sorted_flag" in '') # no existing elements in the array
eval "_i_${_arr_name}_sorted_flag=1
_i_${_arr_name}_h_index=\"$_index\"
_i_${_arr_name}_indices=\"$___entry\""
return 0
esac
case "$_h_index" in *?* )
case $((_index - _h_index)) in 0|-* ) ;; *)
case "$_sorted_flag" in
1) _target_list="_indices" ;;
*) _target_list="_indices_b"
esac
eval "_i_${_arr_name}${_target_list}=\"\${_i_${_arr_name}${_target_list}:-}$___entry\"
_i_${_arr_name}_h_index=$_index"
return 0
esac
esac
eval "_i_${_arr_name}_sorted_flag=0
_i_${_arr_name}_indices_b=\"\${_i_${_arr_name}_indices_b:-}$___entry\""
esac
:
}
# get a value from an indexed array
# output is set as a value of a global variable
# 1 - array name
# 2 - index
# 3 - global variable name for output
get_i_arr_val() {
___me="get_i_arr_val"
case $# in 3) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; _index="$2"; _out_var="$3"
_check_vars _arr_name _out_var && check_index || return 1
eval "$_out_var=\"\${_i_${_arr_name}_${_index}#$_el_set_flag}\""
}
## Backend functions
_check_vars() {
case "${nocheckvars:-}" in *?*) return 0; esac
for ___var in "$@"; do
eval "_var_val=\"\$$___var\""
case "$_var_val" in ''|*[!A-Za-z0-9_]* )
case "$___var" in
___key) _var_desc="key" ;;
_arr_name) _var_desc="array name" ;;
_out_var) _var_desc="output variable name"
esac
printf '%s\n' "$___me: Error: invalid $_var_desc '$_var_val'." >&2
return 1
esac
done
}
no_elements() { echo "$___me: Error: array '$_arr_name' has no elements." >&2; }
check_index() { case "$_index" in *[!0-9]* ) echo "$___me: Error: '$_index' is not a nonnegative integer." >&2; return 1; esac; }
wrongargs() { echo "$___me: Error: '$*': wrong number of arguments '$#'." >&2; }
## Constants
set -f
export LC_ALL=C
___nl='
'
: "${_el_set_flag:="$(printf '\35')"}"