summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_list.subr
blob: 07d401b81f198f2d8ca7c1cdb45c2fbbd73fb3dc (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
#
# 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	_vnames="${1}" _sep="${2}" _vname="" _vval=""\
		IFS RTL_LFIRST_HEAD="" RTL_LFIRST_TAIL="";
	IFS="${_sep}"; shift 2;
	for _vval in ${@}; do
		if ! rtl_lfirst "${_vnames}" " "; then
			return 1;
		else
			_vname="${RTL_LFIRST_HEAD}"; _vnames="${RTL_LFIRST_TAIL}";
			rtl_set_var_unsafe "${_vname}" "${_vval}";
		fi;
	done;
};

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
		printf "%s" "${_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;
	printf "%s" "${_lnew}";
};

rtl_lfilter2() {
	local	_rlist="${1#\$}" _rlist_new="${2#\$}" _filter="${3}" _sep="${4:- }" IFS="${4:-${IFS:- }}"\
		_filterfl="" _litem="" _litem_filter="" _lnew="";

	if [ "${_filter:+1}" != 1 ]; then
		eval ${_rlist_new}=; return 0;
	else	eval set -- '${'"${_rlist}"'}'\; ${_rlist_new}=;
		while [ "${#}" -gt 0 ]; do
			_litem="${1}"; shift; _filterfl=0;
			for _litem_filter in ${_filter}; do
				if [ "${_litem_filter}" = "${_litem}" ]; then
					_filterfl=1; break;
				fi;
			done;
			if [ "${_filterfl:-0}" -eq 0 ]; then
				eval ${_rlist_new}='${'"${_rlist_new}"':+${'"${_rlist_new}"'}${_sep}}${_litem}';
			fi;
		done;
	fi;
};

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

rtl_lfirst() {
	local _list="${1}" _sep="${2}" IFS; IFS="${_sep}";
	set -- ${_list}; RTL_LFIRST_HEAD="${1}";
	if [ "${#}" -ge 0 ]; then
		shift; RTL_LFIRST_TAIL="${*}";
	else
		RTL_LFIRST_TAIL="";
	fi;
};

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

rtl_llength2() {
	local _rlist="${1#\$}" _rlen="${2#\$}" _sep="${3:- }" IFS="${3:-${IFS:- }}";
	eval set -- '${'"${_rlist}"'}'\; ${_rlen}='${#}';
};

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

rtl_llift2() {
	local	_rlist="${1#\$}" _rlist_new="${2#\$}" _sep="${3}"	\
		_sep_new="${4}" IFS; IFS="${_sep}";

	eval set -- '${'"${_rlist}"'}'; IFS="${_sep_new}";
	eval ${_rlist_new}='"${*}"';
};

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

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

rtl_lmax() {
	local _len=0 _len_max=0;
	while [ "${#}" -gt 0 ]; do
		_len="${#1}";
		if [ "${_len}" -gt "${_len_max}" ]; then
			_len_max="${_len}";
		fi; shift;
	done; printf "%s" "${_len_max}";
};

rtl_lsearch() {
	local	_list="${1}" _filter="${2}" _sep="${3:- }" IFS="${3:-${IFS:- }}"\
		_litem="" _litem_filter="" _lnew="";
	if [ -z "${_filter}" ]; then
		printf "%s" "${_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;
	printf "%s" "${_lnew}";
};

rtl_lsearch_patternl() {
	local	_list="${1}" _pattern="${2}" _sep="${3:- }" IFS="${3:-${IFS:- }}"\
		_litem="" _lnew="";
	if [ -n "${_pattern}" ]; then
		for _litem in ${_list}; do
			if [ "${_litem#${_pattern}}" != "${_litem}" ]; then
				_lnew="${_lnew:+${_lnew}${_sep}}${_litem}";
			fi;
		done;
	fi;
	printf "%s" "${_lnew}";
};

rtl_lsort() {
	local _list="${1}" _sep="${2:- }";
	printf "%s" "${_list}" | tr "${_sep}" "\n" | sort | paste -s -d "${_sep}";
};

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;
	printf "%s" "${_names}";
};

# vim:filetype=sh