-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc-list.sh
More file actions
284 lines (263 loc) · 6.42 KB
/
func-list.sh
File metadata and controls
284 lines (263 loc) · 6.42 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
# Copyright © 2021 qualIP Software
#
# This file is part of shfuncs:
#
# https://github.com/qualIP/shfuncs
#
# shfuncs is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3.0 of the License, or (at your option) any later
# version.
#
# shfuncs is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# shfuncs; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301 USA
if [ -z "${BASH_VERSION:-}${ZSH_VERSION:-}" ] ; then echo Not running bash or zsh! >&2 ; exit 1 ; fi
typeset -f make_comma_list > /dev/null && return
## split sep data... ...
#
# Splits all the data strings using the specified separator(s).
split() {
local old_IFS="$IFS"
local IFS="$1" ; shift
# shellcheck disable=SC2068
set -- $@
IFS="$old_IFS"
echo "$@"
}
## make_comma_list data... ...
#
# Arguments are split on spaces and commas then joined with commas ','.
make_comma_list() {
local IFS=",$IFS"
# shellcheck disable=SC2048,SC2086
set -- $*
echo "$*"
}
## make_space_list data... ...
#
# Arguments are split on spaces and commas then joined with spaces ' '.
make_space_list() {
local IFS=" $IFS,"
# shellcheck disable=SC2048,SC2086
set -- $*
echo "$*"
}
## make_newline_list data... ...
#
# Arguments are split on spaces and commas then joined with newlines '\n'.
make_newline_list() {
local IFS=$'\n'"$IFS,"
# shellcheck disable=SC2048,SC2086
set -- $*
echo "$*"
}
## make_colon_list data... ...
#
# Arguments are split on spaces and commas then joined with colons ':'.
make_colon_list() {
local IFS=":$IFS,"
# shellcheck disable=SC2048,SC2086
set -- $*
echo "$*"
}
## make_semicolon_list data... ...
#
# Arguments are split on spaces and commas then joined with semicolons ';'.
make_semicolon_list() {
local IFS=";$IFS,"
# shellcheck disable=SC2048,SC2086
set -- $*
echo "$*"
}
## join_list sep args...
#
# Arguments are joined with the specified separator.
join_list() {
# fudging IFS only works with a single-character separator
local sep="$1" ; shift
local ret=
local v
for v in "$@" ; do
ret="$ret${ret:+$sep}$v"
done
echo "$ret"
}
## lprepend var args...
#
# Modify the specified variable by prepending it with all other arguments
if [[ -n "${ZSH_VERSION:-}" ]] ; then
lprepend() {
local _lprepend_var="$1" ; shift 1
set -- "$@" ${(P)_lprepend_var}
eval "$_lprepend_var"=\$\*
}
else
lprepend() {
local _lprepend_var="$1" ; shift 1
# shellcheck disable=SC2086
set -- "$@" ${!_lprepend_var}
eval "$_lprepend_var"=\$\*
}
fi
## lpush var args...
#
# Synonym for lprepend.
lpush() {
lprepend "$@"
}
## lappend var args...
#
# Modify the specified variable by appending it with all other arguments
if [[ -n "${ZSH_VERSION:-}" ]] ; then
lappend() {
local _lappend_var="$1" ; shift 1
set -- ${(P)_lappend_var} "$@"
eval "$_lappend_var"=\$\*
}
else
lappend() {
local _lappend_var="$1" ; shift 1
# shellcheck disable=SC2086
set -- ${!_lappend_var} "$@"
eval "$_lappend_var"=\$\*
}
fi
## lpop var [outvar=lpop_value]
#
# Modify the specified variable by popping the first element
if [[ -n "${ZSH_VERSION:-}" ]] ; then
lpop() {
local _lpop_var="$1" ; shift
if (( $# )) ; then
local _lpop_outvar="$1" ; shift
else
local _lpop_outvar=lpop_value
fi
set -- ${(P)_lpop_var}
(( $# )) || return 1
eval "$_lpop_outvar"=\$1 ; shift 1
eval "$_lpop_var"=\$\*
}
else
lpop() {
local _lpop_var="$1" ; shift
if (( $# )) ; then
local _lpop_outvar="$1" ; shift
else
local _lpop_outvar=lpop_value
fi
# shellcheck disable=SC2086
set -- ${!_lpop_var}
(( $# )) || return 1
eval "$_lpop_outvar"=\$1 ; shift 1
eval "$_lpop_var"=\$\*
}
fi
## lindex idx args...
#
# Returns the idx-nth index of the argument list.
lindex() {
local shift_cnt=$1 ; shift
case "$shift_cnt" in
end|end-[0-9]*)
# shellcheck disable=SC2034
local end=$(( $# - 1 ))
# shellcheck disable=SC2004
local shift_cnt=$(( $shift_cnt ))
;;
*)
:
;;
esac
if (( $shift_cnt >= 0 )) && (( $shift_cnt < $# )) ; then
shift $shift_cnt
echo "$1"
fi
}
## lcontain needle args...
#
# Tests if needle is an element of the argument list.
lcontain() {
local el="$1" ; shift
local v
for v in "$@" ; do [[ "$v" != "$el" ]] || return 0 ; done
return 1
}
## lrmdupes args...
#
# Returns all unique arguments, in first appearance order.
lrmdupes() {
local l='' v
for v in "$@" ; do
# shellcheck disable=SC2086
lcontain "$v" $l || l="$l $v"
done
# shellcheck disable=SC2086
echo $l
}
## lsubst from to args...
#
# Substitute all elements equal to `from` with `to` in the arguments list.
lsubst() {
local from="$1" ; shift
local to="$1" ; shift
local ret='' v
for v in "$@" ; do
[[ "$v" = "$from" ]] && v="$to"
ret="$ret $v"
done
# shellcheck disable=SC2086
echo $ret
}
## lorder list1 list2
#
# Return all elements of list1 in the order presented in list2, with unique
# elements of l1 at the end.
lorder() {
local unorderedl="$1" ; shift
local l2="$1" ; shift
local tmpl=
local orderredl=
local e
local lpop_value
for e in $l2 ; do
tmpl=
while lpop unorderedl ; do
if [[ "$lpop_value" = "$e" ]] ; then
orderredl="$orderredl $e"
break
fi
tmpl="$tmpl $lpop_value"
done
unorderedl="$tmpl $unorderedl"
done
# shellcheck disable=SC2086
echo $orderredl $unorderedl
}
## lsort args...
#
# Returns the sorted list of arguments.
lsort() {
local old_IFS="$IFS"
local IFS=$'\n'
local v ; v=$(sort <<<"$*")
IFS="$old_IFS"
# shellcheck disable=SC2086
echo $v
}
## lmap func args...
#
# Maps a function to each list argument.
lmap() {
local func=$1 ; shift
local e
for e in "$@" ; do
$func "$e"
done
}
# vim: ft=bash