-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathposix-arrays.sh
671 lines (579 loc) · 19.8 KB
/
posix-arrays.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
#!/bin/sh
# shellcheck disable=SC2154,SC2086
# Copyright: friendly bits
# github.com/friendly-bits
# posix-arrays.sh
# emulates arrays in POSIX shell
# each array element is stored in a variable named in the format '_[x]_[arr_name]_[key/index]'
# where [x] is either 'a' for associative array or 'i' for indexed array
# keys/indices are stored in a variable named in the format '_[x]_[arr_name]_keys' or '_[x]_[arr_name]_indices'
# buffered keys/indices are stored in a variable named in the format '_[x]_[arr_name]_[keys|indices]_b'
# array 'sorted' flag (1=true, 0=false) is stored in variable: $_[x]_[arr_name]_sorted_flag
# the 'sorted' flag is also used as an indicator of whether the array has any elements (for performance reasons)
# for indexed arrays, $_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}\""
}
# unsets all variables used to store an associative array
# 1 - array name
unset_a_arr() {
___me="unset_a_arr"
case $# in 1) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"
_check_vars _arr_name || return 1
_do_unset_a_arr "${_arr_name}"
unset "_a_${_arr_name}_sorted_flag"
}
# backend function
# unsets all variables of an associative array
# 1 - array name
_do_unset_a_arr() {
eval "___keys=\"\${_a_${1}___keys:-}\${_a_${1}___keys_b:-}\""
for ___key in $___keys; do
unset "_a_${1}_${___key}"
done
unset "_a_${1}___keys" "_a_${1}___keys_b"
}
# wrapper function for __sort_a_arr, intended as a user interface
# 1 - array name
sort_a_arr() {
___me="sort_a_arr"
case $# in 1) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"
_check_vars _arr_name || return 1
__sort_a_arr
:
}
# backend function
# sorts keys of an associative array
# assigns the resulting keys to $___keys
# updates the '$_a_${_arr_name}____keys' variable
# sets the 'sorted' flag
__sort_a_arr() {
eval "___sorted_flag=\"\${_a_${_arr_name}_sorted_flag:-}\""
case "$___sorted_flag" in
1) eval "___keys=\"\${_a_${_arr_name}___keys:-}\"" ;;
*) eval "___keys=\"\$(printf %s \"\${_a_${_arr_name}___keys:-}\${_a_${_arr_name}___keys_b:-}\" | sort -u)\"
_a_${_arr_name}___keys=\"\$___keys\"
_a_${_arr_name}___keys_b=''
_a_${_arr_name}_sorted_flag=1"
esac
}
# declare an associative array while populating elements
# resets all previous elements of the array if it already exists
# 1 - array name
# all other args - 'key=value' pairs
declare_a_arr() {
___me="declare_a_arr"
case $# in 0) wrongargs "$@"; return 1; esac
_arr_name="$1"; shift
_check_vars _arr_name || return 1
_do_unset_a_arr "${_arr_name}"
___keys=''
case "$*" in *?* )
for ___pair in "$@"; do
check_pair || return 1
___key="${___pair%%=*}"
___val="$_el_set_flag${___pair#*=}"
_check_vars ___key || return 1
eval "_a_${_arr_name}_${___key}"='$___val'
___keys="$___keys$___nl$___key"
done
esac
eval "_a_${_arr_name}___keys=\"$___keys\"; _a_${_arr_name}_sorted_flag=0"
:
}
# get all values from an associative array (alphabetically sorted by key)
# whitespace-delimited output is set as a value of a global variable
# 1 - array name
# 2 - global variable name for output
get_a_arr_values() {
___me="get_a_arr_values"
case "${1:-}" in "-s") _do_sort=1; shift; esac
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; _out_var="$2"; ___keys=''
_check_vars _arr_name _out_var || return 1
case "$_do_sort" in
'') eval "___keys=\"\${_a_${_arr_name}___keys:-}\${_a_${_arr_name}___keys_b:-}\"" ;;
*) __sort_a_arr
unset _do_sort
esac
___values=''
case "$___keys" in *?* )
___values="$(
for ___key in $___keys; do
eval "___val=\"\${_a_${_arr_name}_${___key}#$_el_set_flag}\""
case "$___val" in *?* ) printf '%s ' "$___val"; esac
done
)"
esac
eval "$_out_var"='${___values% }'
:
}
# get all keys from an associative array (alphabetically sorted)"
# whitespace-delimited output is set as a value of a global variable
# 1 - array name
# 2 - global variable name for output
get_a_arr_keys() {
___me="get_a_arr_keys"
case "${1:-}" in "-s") _do_sort=1; shift; esac
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; _out_var="$2"; ___keys=''
_check_vars _arr_name _out_var || return 1
case "$_do_sort" in
'') eval "___keys=\"\$(printf '%s ' \${_a_${_arr_name}___keys:-}\${_a_${_arr_name}___keys_b:-})\"" ;;
*) __sort_a_arr
unset _do_sort
___keys="$(printf '%s ' $___keys)" # no extra quotes on purpose
esac
eval "$_out_var"='${___keys% }'
:
}
# get the element count of an associative array
# 1 - array name
# 2 - global variable name for output
get_a_arr_el_cnt() {
___me="get_a_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 "___keys=\"\${_a_${_arr_name}___keys:-}\${_a_${_arr_name}___keys_b:-}\""
i=0
for ___key in $___keys; do i=$((i+1)); done
eval "$_out_var"='$i'
:
}
# set an element in an associative array
# if value is an empty string, assings empty string to the key
# 1 - array name
# 2 - 'key=value' pair
set_a_arr_el() {
___me="set_a_arr_el"
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; ___pair="$2"
check_pair || return 1
___key="${___pair%%=*}"
___new_val="${___pair#*=}"
_check_vars _arr_name ___key || return 1
eval "___old_val=\"\${_a_${_arr_name}_${___key}:-}\"
_sorted_flag=\"\${_a_${_arr_name}_sorted_flag:-}\"
_a_${_arr_name}_${___key}"='${_el_set_flag}${___new_val}'
case "$___old_val" in '')
case "$_sorted_flag" in
'') eval "_a_${_arr_name}_sorted_flag=1
_a_${_arr_name}___keys=\"${___nl}${___key}\""
;;
*) eval "_a_${_arr_name}_sorted_flag=0
_a_${_arr_name}___keys_b=\"\${_a_${_arr_name}___keys_b:-}${___nl}${___key}\""
esac
esac
:
}
# unset an element in an associative array
# 1 - array name
# 2 - key
unset_a_arr_el() {
_rm_mid_key() {
eval "_a_${_arr_name}${1}=''
for ___key_tmp in \$${1}"';'" do
case $___key in
\"\$___key_tmp\" ) ;;
*) _a_${_arr_name}${1}=\"\${_a_${_arr_name}${1}}$___nl\$___key_tmp\"
esac
done"
}
___me="unset_a_arr_el"
case $# in 2) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; ___key="$2"
_check_vars _arr_name ___key || return 1
eval "_sorted_flag=\"\${_a_${_arr_name}_sorted_flag:-}\"
___old_val=\"\${_a_${_arr_name}_${___key}:-}\""
case "$___old_val" in *?* )
unset "_a_${_arr_name}_${___key}"
case "$_sorted_flag" in
1) eval "___keys=\"\${_a_${_arr_name}___keys:-}\""
_no_b_keys=1 ;;
0) eval "___keys=\"\${_a_${_arr_name}___keys:-}\"; ___keys_b=\"\${_a_${_arr_name}___keys_b:-}\""
_no_b_keys=''
case "${___keys_b#"$___nl"}" in
"$___key$___nl"* ) eval "_a_${_arr_name}___keys_b=\"\${___keys_b#$___nl$___key}\"" ;;
*"$___nl$___key" ) eval "_a_${_arr_name}___keys_b=\"\${___keys_b%$___nl$___key}\"" ;;
*"$___nl$___key$___nl"* ) _rm_mid_key "___keys_b" ;;
"$___key" ) unset "_a_${_arr_name}___keys_b"; _no_b_keys=1 ;;
'') _no_b_keys=1
esac
;;
'') return 0
esac
case "${___keys#"$___nl"}" in
"$___key" ) unset "_a_${_arr_name}___keys"
case "$_no_b_keys" in 1) unset "_a_${_arr_name}_sorted_flag"; esac ;;
"$___key$___nl"* ) eval "_a_${_arr_name}___keys=\"\${___keys#$___nl$___key}\"" ;;
*"$___nl$___key" ) eval "_a_${_arr_name}___keys=\"\${___keys%$___nl$___key}\"" ;;
*"$___nl$___key$___nl"* ) _rm_mid_key "___keys" ;;
'') case "$_no_b_keys" in 1) unset "_a_${_arr_name}_sorted_flag"; esac
esac
esac
:
}
# get a value from an emulated associative array
# output is set as a value of a global variable
# 1 - array name
# 2 - key
# 3 - global variable name for output
get_a_arr_val() {
___me="get_a_arr_val"
case $# in 3) ;; *) wrongargs "$@"; return 1; esac
_arr_name="$1"; ___key="$2"; _out_var="$3"
_check_vars _arr_name ___key _out_var || return 1
eval "$_out_var=\"\${_a_${_arr_name}_${___key}#$_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; }
check_pair() { case "$___pair" in *=* ) ;; *) echo "$___me: Error: '$___pair' is not a 'key=value' pair." >&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')"}"