summaryrefslogtreecommitdiffhomepage
path: root/subr/rtl_list.subr
blob: 479b0fd4acf97bc84ff163d8f829570ec0a19c12 (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
#
# set +o errexit -o noglob is assumed.
#

rtl_lconcat() {
	local _list="${1}" _litem_new="${2}" _sep="${3:- }" IFS="${3:-${IFS}}";
	if [ -n "${_list}" ]; then
		printf "%s%s%s" "${_list}" "${_sep}" "${_litem_new}";
	else
		printf "%s" "${_litem_new}";
	fi;
};

rtl_lfilter() {
	local	_list="${1}" _filter="${2}" _sep="${3:- }" IFS="${3:-${IFS}}"	\
		_filterfl="" _litem="" _litem_filter="" _lnew="";
	if [ -z "${_filter}" ]; then
		echo "${_list}"; return 0;
	else for _litem in ${_list}; do
		_filterfl=0;
		for _litem_filter in ${_filter}; do
			if [ "${_litem_filter}" = "${_litem}" ]; then
				_filterfl=1; break;
			fi;
		done;
		if [ "${_filterfl:-0}" -eq 0 ]; then
			_lnew="${_lnew:+${_lnew}${_sep}}${_litem}";
		fi;
	done; fi;
	echo "${_lnew}";
};

rtl_llength() {
	local _list="${1}" _sep="${2:- }" IFS="${2:-${IFS}}" _litem="" _llength=0;
	for _litem in ${_list}; do
		: $((_llength+=1));
	done;
	echo "${_llength}";
};

rtl_llift() {
	local _list="${1}" _sep="${2}" _sep_new="${3}" IFS; IFS="${_sep}";
	set -- ${_list}; IFS="${_sep_new}";
	echo "${*}";
};

rtl_lmatch() {
	local _list="${1}" _item="${2}" _sep="${3}";
	[ -n "$(rtl_lsearch "${_list}" "${_item}" "${_sep}")" ];
};

rtl_lsearch() {
	local	_list="${1}" _filter="${2}" _sep="${3:- }" IFS="${3:-${IFS}}"	\
		_litem="" _litem_filter="" _lnew="";
	if [ -z "${_filter}" ]; then
		echo "${_list}"; return 0;
	else for _litem in ${_list}; do
		for _litem_filter in ${_filter}; do
			if [ "${_litem_filter}" = "${_litem}" ]; then
				_lnew="${_lnew:+${_lnew}${_sep}}${_litem}";
				break;
			fi;
		done;
	done; fi;
	echo "${_lnew}";
};

rtl_lunfold_depends() {
	local _vname_template="${1}" _depends="" _name="" _names=""; shift;
	for _name in "${@}"; do
		if _depends="$(rtl_get_var_unsafe -u "$(eval printf \"%s\" \""${_vname_template}"\")")"\
		&& [ -n "${_depends}" ]; then
			_depends="$(rtl_lunfold_depends "${_vname_template}" ${_depends})";
			_names="$(rtl_lconcat "${_names}" "${_depends}")";
		fi;
		_names="$(rtl_lconcat "${_names}" "${_name}")";
	done;
	echo "${_names}";
};

# vim:filetype=sh