-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathto-array.sh
More file actions
executable file
·376 lines (309 loc) · 10.9 KB
/
to-array.sh
File metadata and controls
executable file
·376 lines (309 loc) · 10.9 KB
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
#!/usr/bin/env bash
__to_array__version__='0.0.1'
##
# Removes leading characters from variable reference
# @parameter {Reference} $1 _reference
# @parameter {number | string} $2 _character
# @example
# string="''foo bar'''"
# to_array__strip_leading_character 'string' "'"
#
# echo "${string}"
# #> foo bar'''
# @author S0AndS0
# @license AGPL-3.0
to_array__strip_leading_character() {
local -n _reference="${1}"
local _character="${2}"
while [[ "${_reference}" =~ ^${_character}[[:print:]]* ]]; do
_reference="${_reference:1}"
done
}
##
# Removes trailing characters from variable reference
# @parameter {Reference} $1 _reference
# @parameter {number | string} $2 _character
# @example
# string="''foo bar'''"
# to_array__strip_trailing_character 'string' "'"
#
# echo "${string}"
# #> ''foo bar
# @author S0AndS0
# @license AGPL-3.0
to_array__strip_trailing_character() {
local -n _reference="${1}"
local _character="${2}"
while [[ "${_reference}" =~ [[:print:]]${_character}$ ]]; do
_reference="${_reference::-1}"
done
}
##
# Appends to variable reference a value with optional separator
# @parameter {StringReference} $1 _string_reference
# @parameter {number | string} $2 _value
# @parameter {number | string} $3 _separator
# @example
# string=""
#
# to_array__append_to_string string "one" ', '
# to_array__append_to_string string "two" ', '
# to_array__append_to_string string "three" ', '
#
# printf 'string -> %s\n' "${string}"
# #> string -> one, two, three
# @author S0AndS0
# @license AGPL-3.0
to_array__append_to_string() {
local -n _string_reference="${1}"
local _value="${2}"
local _separator="${3}"
if (( ${#_string_reference} )); then
_string_reference+="${_separator}${_value}"
else
_string_reference="${_value}"
fi
}
##
# Appends value to array reference
# @parameter {ArrayReference} $1 _array_reference
# @parameter {number | string} $2 _value
# @example
# array=( zero one )
#
# to_array__append_to_array 'array' 'two and one half'
#
# printf 'array[*] -> ( %s )\n' "${array[*]@Q}"
# #> array[*] -> ( 'zero' 'one' 'two and one half' )
# @author S0AndS0
# @license AGPL-3.0
to_array__append_to_array() {
local -n _array_reference="${1}"
local _value="${2}"
local -a _array_reference__index_list=( "${!_array_reference[@]}" )
local _array_reference__index_last=0
if (( ${#_array_reference__index_list[@]} )); then
_array_reference__index_last="$(( ${_array_reference__index_list[-1]} + 1))"
fi
_array_reference[${_array_reference__index_last}]="${_value}"
}
##
# Prints to STDERR the calling function prefixed to passed message lines
# @parameter {number[] | string[]} $@
# @example
# fn_caller() {
# to_array__debug 'Ground control to major Tom' 'come in major Tom'
# }
#
# fn_caller
#
# #> fn_caller: Ground control to major Tom
# #> come in major Tom
to_array__debug() {
local -a _message_lines=( "${@}" )
local _calling_function="${FUNCNAME[1]}"
local _message_lines__first="${_message_lines[0]}"
local -a _message_lines__remaining=( "${_message_lines[@]:1}" )
(( ${#_message_lines__first} )) && {
printf >&2 '%s: %s\n' "${_calling_function}" "${_message_lines__first}"
}
(( ${#_message_lines__remaining[@]} )) && {
local _padding_limit="$(( ${#_calling_function} + 2 ))"
local _padding=''
local i
for i in $(seq 1 "${_padding_limit}"); do
_padding+=' '
done
printf >&2 '%s%s\n' "${_padding}" "${_message_lines__remaining[@]}"
}
}
##
# Assigns array reference with string as an array
# @example
# target=()
#
# to_array --strip-quotes -t target -i 'spam "flavored ham" in a can'
#
# printf 'target[*] -> ( %s )\n' "${target[*]@Q}"
# #> target[*] -> ( 'spam' 'flavored ham' 'in' 'a' 'can' )
# @author S0AndS0
# @license AGPL-3.0
to_array() {
##
# Initialize locally scoped variables
local _help=0
local _verbose=0
local _strip__double_quotes=0
local _strip__single_quotes=0
local _strip__quotes=0
local _element__separator=' '
local _target_reference_name
local _input
local _version
##
# Parse parameters
local -a _arguments=( "${@:?No arguments provided to ${FUNCNAME[0]}}" )
local i
for i in "${!_arguments[@]}"; do
case "${_arguments[${i}]}" in
--target|-t)
(( i++ )) || { true; }
local -n _target_reference="${_arguments[${i}]}"
_target_reference_name="${_arguments[${i}]}"
;;
--input|-i)
(( i++ )) || { true; }
_input="${_arguments[${i}]}"
;;
--separator|-s)
(( i++ )) || { true; }
_element__separator="${_arguments[${i}]}"
;;
--strip-double-quotes)
_strip__double_quotes=1
;;
--strip-single-quotes)
_strip__single_quotes=1
;;
--strip-quotes)
_strip__quotes=1
;;
--verbose|-v)
(( _verbose++ )) || { true; }
;;
--version|-V)
_version=1
;;
--help|-h)
_help=1
;;
esac
done
##
# Populate usage variable with parsed parameters
local __usage__
read -rd '' __usage__ <<EOF
Appends to array reference with parsed string elements
## Parameters
-t --target <ArrayReferance> ${_target_reference_name:-array_name}
{Required} - Target array reference to append element(s) to
-i --input <number | string>
{Required} - String to convert to array
-s --separator <number | string> ${_element__separator@Q}
{Optional} - Character inserted between elements
--strip-double-quotes <Boolean> ${_strip__double_quotes:-0}
{Optional} - If true, then strip double quotes during conversion
--strip-single-quotes <Boolean> ${_strip__single_quotes:-0}
{Optional} - If true, then strip single quotes during conversion
--strip-quotes <Boolean> ${_strip__quotes:-0}
{Optional} - If true, then strip double, and single, quotes during conversion
-v --verbose <Counter | Boolean> ${_verbose:-0}
{Optional} - Prints parsed options and results if flag is present
-h --help <Boolean> ${_help:-0}
{Optional} - Prints this message if flag is present
## Example
target=()
to_array -t target -i 'spam "flavored ham" in a can'
printf 'target[*] -> ( %s )\\n' "\${target[*]@Q}"
#> target[*] -> ( 'spam' '"flavored ham"' 'in' 'a' 'can' )
EOF
##
# Detect premature exit states
if (( _version )); then
printf '%s\n' "${__to_array__version__}"
return 0
fi
if (( _help )); then
printf '%s\n' "${__usage__}"
return 0
fi
local -a _element__words
IFS=' ' read -ra _element__words <<<"${_input}"
local _element__word
local _within__single_quotes
local _within__double_quotes
local _element__accumulator
for _element__word in "${_element__words[@]}"; do
if (( _within__double_quotes )); then
if [[ "${_element__word}" =~ '"'$ ]]; then
to_array__append_to_string '_element__accumulator' "${_element__word}" "${_element__separator}"
if (( _strip__double_quotes )); then
to_array__strip_leading_character '_element__accumulator' '"'
to_array__strip_trailing_character '_element__accumulator' '"'
(( _verbose )) && {
to_array__debug 'Stripping double-quotes'
}
elif (( _strip__quotes )); then
to_array__strip_leading_character '_element__accumulator' "(\"|')"
to_array__strip_trailing_character '_element__accumulator' "(\"|')"
(( _verbose )) && {
to_array__debug 'Stripping double and/or single quotes'
}
fi
to_array__append_to_array '_target_reference' "${_element__accumulator}"
_element__accumulator=''
_within__double_quotes=0
(( _verbose )) && {
to_array__debug 'Finished double-quote block'
}
else
to_array__append_to_string '_element__accumulator' "${_element__word}" "${_element__separator}"
fi
elif (( _within__single_quotes )); then
if [[ "${_element__word}" =~ "'"$ ]]; then
to_array__append_to_string '_element__accumulator' "${_element__word}" "${_element__separator}"
if (( _strip__single_quotes )); then
to_array__strip_leading_character '_element__accumulator' "'"
to_array__strip_trailing_character '_element__accumulator' "'"
(( _verbose )) && {
to_array__debug 'Stripping single quotes'
}
elif (( _strip__quotes )); then
to_array__strip_leading_character '_element__accumulator' "(\"|')"
to_array__strip_trailing_character '_element__accumulator' "(\"|')"
(( _verbose )) && {
to_array__debug 'Stripping double and/or single quotes'
}
fi
to_array__append_to_array '_target_reference' "${_element__accumulator}"
_element__accumulator=''
_within__single_quotes=0
(( _verbose )) && {
to_array__debug 'Finished single-quote block'
}
else
to_array__append_to_string '_element__accumulator' "${_element__word}" "${_element__separator}"
fi
else
if [[ "${_element__word}" =~ ^'"' ]]; then
(( _verbose )) && {
to_array__debug 'Started double-quote block'
}
_within__double_quotes=1
_element__accumulator="${_element__word}"
elif [[ "${_element__word}" =~ ^"'" ]]; then
(( _verbose )) && {
to_array__debug 'Started single-quote block'
}
_within__single_quotes=1
_element__accumulator="${_element__word}"
else
to_array__append_to_array '_target_reference' "${_element__word}"
_element__accumulator=''
fi
fi
done
(( _verbose )) && {
to_array__debug "${_target_reference_name}[*] -> ( ${_target_reference[*]@Q} )"
}
}
##
# Enable help and version options if run as a script
case "$@" in
-h|--help)
to_array -h
;;
-V|--version)
to_array -V
;;
esac