summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_list.subr
blob: 0debe62c6873d07f59266f1fe064fcd91852d90d (plain)
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
#
# Copyright (c) 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Lucía Andrea Illanes Albornoz <lucia@luciaillanes.de>
# set +o errexit -o noglob -o nounset is assumed.
# N.B.	Lists separated by SP, when passed to rtl_l*() w/ explicit IFS,
#	must specify "<SP><VT>" as IFS.
#

rtl_lassign() {
	local	_rla_vnames="${1}" _rla_sep="${2}"	\
		_rla_head="" _rla_tail="" _rla_vname="" _rla_vval="" IFS;
	IFS="${_rla_sep}"; shift 2;

	for _rla_vval in ${@}; do
		if ! rtl_lfirst \$_rla_head \$_rla_tail "${_rla_vnames}" " "; then
			return 1;
		else
			_rla_vname="${_rla_head}"; _rla_vnames="${_rla_tail}";
			rtl_set_var_unsafe "${_rla_vname}" "${_rla_vval}";
		fi;
	done;
	return 0;
};

rtl_lconcat() {
	rtl_lconcat2 "${1}" "${1}" "${2}" "${3:-}";
};

rtl_lconcat2() {
	local	_rlc2_rlist_new="${1#\$}" _rlc2_rlist="${2#\$}" _rlc2_litem_new="${3}"	\
		_rlc2_sep="${4:- }" IFS="${4:-${IFS:- }}";

	if eval [ \"'${'${_rlc2_rlist}':+1}'\" = 1 ]; then
		eval ${_rlc2_rlist_new}='${'"${_rlc2_rlist}"'}${_rlc2_sep}${_rlc2_litem_new}';
	else
		eval ${_rlc2_rlist_new}='${_rlc2_litem_new}';
	fi;
	return 0;
};

rtl_lfilter() {
	rtl_lfilter2 "${1}" "${1}" "${2}" "${3:-}";
};

rtl_lfilter2() {
	local	_rlf2_rlist="${1#\$}" _rlf2_rlist_new="${2#\$}" _rlf2_filter="${3}" _rlf2_sep="${4:-}"\
		_rlf2_filterfl="" _rlf2_list="" _rlf2_litem="" _rlf2_litem_filter="" _rlf2_lnew="";

	if [ "${_rlf2_sep:+1}" = 1 ]; then
		local IFS="${_rlf2_sep}";
	else
		local _rlf2_sep=" " IFS="${RTL_IFS_ORIG}";
	fi;

	if [ "${_rlf2_filter:+1}" != 1 ]; then
		eval ${_rlf2_rlist_new}=;
	else
		eval _rlf2_list='${'"${_rlf2_rlist}"'}';
		eval ${_rlf2_rlist_new}=;
		for _rlf2_litem in ${_rlf2_list}; do
			_rlf2_filterfl=0;
			for _rlf2_litem_filter in ${_rlf2_filter}; do
				if [ "${_rlf2_litem_filter}" = "${_rlf2_litem}" ]; then
					_rlf2_filterfl=1; break;
				fi;
			done;
			if [ "${_rlf2_filterfl:-0}" -eq 0 ]; then
				eval ${_rlf2_rlist_new}='${'"${_rlf2_rlist_new}"':+${'"${_rlf2_rlist_new}"'}${_rlf2_sep}}${_rlf2_litem}';
			fi;
		done;
	fi;
	return 0;
};

rtl_lfirst() {
	local _rlf_rhead="${1#\$}" _rlf_rtail="${2#\$}" _rlf_list="${3}" _rlf_sep="${4}" IFS="${4}";

	set -- ${_rlf_list}; eval ${_rlf_rhead}='${1}';
	if [ "${#}" -ge 0 ]; then
		shift; eval ${_rlf_rtail}=\"'${*}'\";
	else
		eval ${_rlf_rtail}=;
	fi;
	return 0;
};

rtl_lindexV() {
	local	_rliV_rlout="${1#\$}" _rliV_idx="${2}"	\
		_rliV_limit=0; shift 2;

	if [ "${_rliV_idx#-}" != "${_rliV_idx}" ]; then
		_rliV_idx="${_rliV_idx#-}";
		while [ "${#}" -gt "${_rliV_idx}" ]; do
			shift;
		done;
		_rliV_limit=$((${#} + 1));
	else
		_rliV_limit="${#}";
		shift "${_rliV_idx}";
	fi;

	if [ "${#}" -ge 1 ]\
	&& [ "${_rliV_idx}" -lt "${_rliV_limit}" ]; then
		eval ${_rliV_rlout}='${1}';
	else
		eval ${_rliV_rlout}=;
	fi;
	return 0;
};

rtl_llength() {
	local _rll_rlen="${1#\$}" _rll_rlist="${2#\$}" _rll_sep="${3:- }" IFS="${3:-${IFS:- }}";

	eval set -- '${'"${_rll_rlist}"'}'\; ${_rll_rlen}='${#}';
	return 0;
};

rtl_llift() {
	rtl_llift2 "${1}" "${1}" "${2}" "${3}";
};

rtl_llift2() {
	local	_rl2_rlist="${1#\$}" _rl2_rlist_new="${2#\$}" _rl2_sep="${3:-}"	\
		_rl2_sep_new="${4}";

	if [ "${_rl2_sep:+1}" = 1 ]; then
		local IFS="${_rl2_sep}";
	else
		local _rl2_sep=" " IFS="${RTL_IFS_ORIG}";
	fi;

	eval set -- '${'"${_rl2_rlist}"'}'; IFS="${_rl2_sep_new}";
	eval ${_rl2_rlist_new}='"${*}"';
	return 0;
};

rtl_lmatch() {
	local _rlm_rlist="${1#\$}" _rlm_item="${2}" _rlm_sep="${3:-}" _rlm_list_="";

	rtl_lsearch2 "${_rlm_rlist}" \$_rlm_list_ "${_rlm_item}" "${_rlm_sep}";
	[ "${_rlm_list_:+1}" = 1 ];
};

rtl_lmax() {
	local	_rlm2_rlist="${1#\$}" _rlm2_rmax="${2#\$}"	\
		_rlm2_len=0 _rlm2_len_max=0;

	eval set -- '${'"${_rlm2_rlist}"'}';
	while [ "${#}" -gt 0 ]; do
		_rlm2_len="${#1}";
		if [ "${_rlm2_len}" -gt "${_rlm2_len_max}" ]; then
			_rlm2_len_max="${_rlm2_len}";
		fi; shift;
	done;
	eval ${_rlm2_rmax}='${_rlm2_len_max}';
	return 0;
};

rtl_lrangeV() {
	local	_rlrV_rlout="${1#\$}" _rlrV_idx0="${2}" _rlrV_idx1="${3}" _rlrV_sep="${4}"	\
		_rlrV_limit=0; shift 4;

	if [ "${_rlrV_idx0#-}" != "${_rlrV_idx0}" ]; then
		eval ${_rlrV_rlout}=;

		return 1;
	elif [ "${_rlrV_idx1#-}" != "${_rlrV_idx1}" ]; then
		_rlrV_idx1="${_rlrV_idx1#-}";

		_rlrV_limit="${#}";
		shift "${_rlrV_idx0}";

		eval ${_rlrV_rlout}=;
		while [ "${#}" -gt 0 ]\
		   && [ "${_rlrV_idx1}" -lt "${#}" ];
		do
			eval ${_rlrV_rlout}='${'"${_rlrV_rlout}"':+${'"${_rlrV_rlout}"'}${_rlrV_sep}}${1}';
			shift;
		done;

		return 0;
	elif [ "${_rlrV_idx1}" -ge "${_rlrV_idx0}" ]; then
		_rlrV_limit="${#}";
		shift "${_rlrV_idx0}";
		: $((_rlrV_idx1 -= ${_rlrV_idx0}));
		: $((_rlrV_idx1 += 1));

		eval ${_rlrV_rlout}=;
		while [ "${_rlrV_idx1}" -gt 0 ]\
		   && [ "${#}" -gt 0 ];
		do
			eval ${_rlrV_rlout}='${'"${_rlrV_rlout}"':+${'"${_rlrV_rlout}"'}${_rlrV_sep}}${1}';
			: $((_rlrV_idx1 -= 1));
			shift;
		done;

		return 0;
	fi;
};

rtl_lsearch() {
	rtl_lsearch2 "${1}" "${1}" "${2}" "${3:-}";
}

rtl_lsearch2() {
	local	_rls2_rlist="${1#\$}" _rls2_rlist_new="${2#\$}"		\
		_rls2_filter="${3}" _rls2_sep="${4:-}"			\
		_rls2_list="" _rls2_litem="" _rls2_litem_filter=""	\
		_rls2_lnew="";

	if [ "${_rls2_sep:+1}" = 1 ]; then
		local IFS="${_rls2_sep}";
	else
		local _rls2_sep=" " IFS="${RTL_IFS_ORIG}";
	fi;
	if [ "${_rls2_filter:+1}" != 1 ]; then
		eval ${_rls2_rlist_new}='${_rls2_list}'; return 0;
	else
		eval _rls2_list='${'"${_rls2_rlist}"'}';
		for _rls2_litem in ${_rls2_list}; do
			for _rls2_litem_filter in ${_rls2_filter}; do
				if [ "${_rls2_litem_filter}" = "${_rls2_litem}" ]; then
					_rls2_lnew="${_rls2_lnew:+${_rls2_lnew}${_rls2_sep}}${_rls2_litem}";
					break;
				fi;
			done;
		done;
	fi;

	eval ${_rls2_rlist_new}='${_rls2_lnew}';
	return 0;
};

rtl_lsearch_patternl() {
	rtl_lsearch_patternl2 "${1}" "${1}" "${2}" "${3:-}";
};

rtl_lsearch_patternl2() {
	local	_rlsp2_rlist="${1#\$}" _rlsp2_rlist_new="${2#\$}"	\
		_rlsp2_pattern="${3}" _rlsp2_sep="${4:- }"		\
		IFS="${4:-${IFS:- }}"					\
		_rlsp2_litem="" _rlsp2_lnew="";

	if [ "${_rlsp2_pattern:+1}" = 1 ]; then
		eval _rlsp2_list='${'"${_rlsp2_rlist}"'}';
		for _rlsp2_litem in ${_rlsp2_list}; do
			if [ "${_rlsp2_litem#${_rlsp2_pattern}}" != "${_rlsp2_litem}" ]; then
				_rlsp2_lnew="${_rlsp2_lnew:+${_rlsp2_lnew}${_rlsp2_sep}}${_rlsp2_litem}";
			fi;
		done;
	fi;
	eval ${_rlsp2_rlist_new}='${_rlsp2_lnew}';
	return 0;
};

rtl_lsort() {
	printf "%s" "${*}" | tr " " "\n" | sort | paste -s -d " ";
	return 0;
};

RTL_LUNFOLD_DEPENDS_LEVEL=0;
rtl_lunfold_depends() {
	local	_rld_vname_template="${1}" _rld_rlist="${2#\$}"		\
		_rld_depends="" _rld_list="" _rld_name="" _rld_names=""	\
		_rld_vname_template_="";
		shift 2;

	for _rld_name in "${@}"; do
		eval _rld_vname_template_=\"${_rld_vname_template}\";
		if rtl_get_var_unsafe \$_rld_depends -u "${_rld_vname_template_}"\
		&& [ "${_rld_depends:+1}" = 1 ]; then
			: $((RTL_LUNFOLD_DEPENDS_LEVEL+=1));
			eval _rld_depends${RTL_LUNFOLD_DEPENDS_LEVEL}='${_rld_depends}';
			rtl_lunfold_depends "${_rld_vname_template}" \$_rld_depends${RTL_LUNFOLD_DEPENDS_LEVEL} ${_rld_depends};
			eval _rld_depends='${_rld_depends'"${RTL_LUNFOLD_DEPENDS_LEVEL}"'}';
			unset '_rld_depends'"${RTL_LUNFOLD_DEPENDS_LEVEL}";
			: $((RTL_LUNFOLD_DEPENDS_LEVEL-=1));
			rtl_lconcat \$_rld_names "${_rld_depends}";
		fi;
		rtl_lconcat \$_rld_names "${_rld_name}";
	done;
	eval ${_rld_rlist}='${_rld_names}';
	return 0;
};

# vim:filetype=sh textwidth=0