Blame subr.rtl/rtl_list.subr

Lucio Andrés Illanes Albornoz 2b85d0
#
Lucio Andrés Illanes Albornoz e1d469
# set +o errexit -o noglob -o nounset is assumed.
Lucio Andrés Illanes Albornoz 370e98
# N.B.	Lists separated by SP, when passed to rtl_l*() w/ explicit IFS,
Lucio Andrés Illanes Albornoz 370e98
#	must specify "<SP><VT>" as IFS.
Lucio Andrés Illanes Albornoz 2b85d0
#
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz 0875f5
rtl_lassign() {
Lucio Andrés Illanes Albornoz 0875f5
	local	_vnames="${1}" _sep="${2}" _vname="" _vval=""\
Lucio Andrés Illanes Albornoz 0875f5
		IFS RTL_LFIRST_HEAD="" RTL_LFIRST_TAIL="";
Lucio Andrés Illanes Albornoz 0875f5
	IFS="${_sep}"; shift 2;
Lucio Andrés Illanes Albornoz 0875f5
	for _vval in ${@}; do
Lucio Andrés Illanes Albornoz 0875f5
		if ! rtl_lfirst "${_vnames}" " "; then
Lucio Andrés Illanes Albornoz 0875f5
			return 1;
Lucio Andrés Illanes Albornoz 0875f5
		else
Lucio Andrés Illanes Albornoz 0875f5
			_vname="${RTL_LFIRST_HEAD}"; _vnames="${RTL_LFIRST_TAIL}";
Lucio Andrés Illanes Albornoz 0875f5
			rtl_set_var_unsafe "${_vname}" "${_vval}";
Lucio Andrés Illanes Albornoz 0875f5
		fi;
Lucio Andrés Illanes Albornoz 0875f5
	done;
Lucio Andrés Illanes Albornoz 0875f5
};
Lucio Andrés Illanes Albornoz 0875f5
Lucio Andrés Illanes Albornoz c6d6e0
rtl_lconcat() {
Lucio Andrés Illanes Albornoz e58399
	local _list="${1}" _litem_new="${2}" _sep="${3:- }" IFS="${3:-${IFS:- }}";
Lucio Andrés Illanes Albornoz 2b85d0
	if [ -n "${_list}" ]; then
Lucio Andrés Illanes Albornoz 2b85d0
		printf "%s%s%s" "${_list}" "${_sep}" "${_litem_new}";
Lucio Andrés Illanes Albornoz 2b85d0
	else
Lucio Andrés Illanes Albornoz 2b85d0
		printf "%s" "${_litem_new}";
Lucio Andrés Illanes Albornoz 2b85d0
	fi;
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz c6d6e0
rtl_lfilter() {
Lucio Andrés Illanes Albornoz e58399
	local	_list="${1}" _filter="${2}" _sep="${3:- }" IFS="${3:-${IFS:- }}"\
Lucio Andrés Illanes Albornoz 2b85d0
		_filterfl="" _litem="" _litem_filter="" _lnew="";
Lucio Andrés Illanes Albornoz 2b85d0
	if [ -z "${_filter}" ]; then
Lucio Andrés Illanes Albornoz b6a9a1
		printf "%s" "${_list}"; return 0;
Lucio Andrés Illanes Albornoz 2b85d0
	else for _litem in ${_list}; do
Lucio Andrés Illanes Albornoz 2b85d0
		_filterfl=0;
Lucio Andrés Illanes Albornoz 2b85d0
		for _litem_filter in ${_filter}; do
Lucio Andrés Illanes Albornoz 2b85d0
			if [ "${_litem_filter}" = "${_litem}" ]; then
Lucio Andrés Illanes Albornoz 2b85d0
				_filterfl=1; break;
Lucio Andrés Illanes Albornoz 2b85d0
			fi;
Lucio Andrés Illanes Albornoz 2b85d0
		done;
Lucio Andrés Illanes Albornoz 2b85d0
		if [ "${_filterfl:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 2b85d0
			_lnew="${_lnew:+${_lnew}${_sep}}${_litem}";
Lucio Andrés Illanes Albornoz 2b85d0
		fi;
Lucio Andrés Illanes Albornoz 2b85d0
	done; fi;
Lucio Andrés Illanes Albornoz b6a9a1
	printf "%s" "${_lnew}";
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz 3e5303
rtl_lfilter2() {
Lucio Andrés Illanes Albornoz 3e5303
	local	_rlist="${1#\$}" _rlist_new="${2#\$}" _filter="${3}" _sep="${4:- }" IFS="${4:-${IFS:- }}"\
Lucio Andrés Illanes Albornoz 3e5303
		_filterfl="" _litem="" _litem_filter="" _lnew="";
Lucio Andrés Illanes Albornoz 3e5303
Lucio Andrés Illanes Albornoz 3e5303
	if [ "${_filter:+1}" != 1 ]; then
Lucio Andrés Illanes Albornoz 3e5303
		eval ${_rlist_new}=; return 0;
Lucio Andrés Illanes Albornoz 3e5303
	else	eval set -- '${'"${_rlist}"'}'\; ${_rlist_new}=;
Lucio Andrés Illanes Albornoz 3e5303
		while [ "${#}" -gt 0 ]; do
Lucio Andrés Illanes Albornoz 3e5303
			_litem="${1}"; shift; _filterfl=0;
Lucio Andrés Illanes Albornoz 3e5303
			for _litem_filter in ${_filter}; do
Lucio Andrés Illanes Albornoz 3e5303
				if [ "${_litem_filter}" = "${_litem}" ]; then
Lucio Andrés Illanes Albornoz 3e5303
					_filterfl=1; break;
Lucio Andrés Illanes Albornoz 3e5303
				fi;
Lucio Andrés Illanes Albornoz 3e5303
			done;
Lucio Andrés Illanes Albornoz 3e5303
			if [ "${_filterfl:-0}" -eq 0 ]; then
Lucio Andrés Illanes Albornoz 3e5303
				eval ${_rlist_new}='${'"${_rlist_new}"':+${'"${_rlist_new}"'}${_sep}}${_litem}';
Lucio Andrés Illanes Albornoz 3e5303
			fi;
Lucio Andrés Illanes Albornoz 3e5303
		done;
Lucio Andrés Illanes Albornoz 3e5303
	fi;
Lucio Andrés Illanes Albornoz 3e5303
};
Lucio Andrés Illanes Albornoz 3e5303
Lucio Andrés Illanes Albornoz 3e5303
rtl_lfilter3() {
Lucio Andrés Illanes Albornoz 3e5303
	rtl_lfilter2 "${1}" "${1}" "${2}" "${3:-}";
Lucio Andrés Illanes Albornoz 3e5303
};
Lucio Andrés Illanes Albornoz 3e5303
Lucio Andrés Illanes Albornoz 0875f5
rtl_lfirst() {
Lucio Andrés Illanes Albornoz 0875f5
	local _list="${1}" _sep="${2}" IFS; IFS="${_sep}";
Lucio Andrés Illanes Albornoz 0875f5
	set -- ${_list}; RTL_LFIRST_HEAD="${1}";
Lucio Andrés Illanes Albornoz 0875f5
	if [ "${#}" -ge 0 ]; then
Lucio Andrés Illanes Albornoz 0875f5
		shift; RTL_LFIRST_TAIL="${*}";
Lucio Andrés Illanes Albornoz 0875f5
	else
Lucio Andrés Illanes Albornoz 0875f5
		RTL_LFIRST_TAIL="";
Lucio Andrés Illanes Albornoz 0875f5
	fi;
Lucio Andrés Illanes Albornoz 0875f5
};
Lucio Andrés Illanes Albornoz 0875f5
Lucio Andrés Illanes Albornoz c6d6e0
rtl_llength() {
Lucio Andrés Illanes Albornoz e58399
	local _list="${1}" _sep="${2:- }" IFS="${2:-${IFS:- }}" _litem="" _llength=0;
Lucio Andrés Illanes Albornoz 2b85d0
	for _litem in ${_list}; do
Lucio Andrés Illanes Albornoz 2b85d0
		: $((_llength+=1));
Lucio Andrés Illanes Albornoz 2b85d0
	done;
Lucio Andrés Illanes Albornoz b6a9a1
	printf "%s" "${_llength}";
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz 3e5303
rtl_llength2() {
Lucio Andrés Illanes Albornoz 3e5303
	local _rlist="${1#\$}" _rlen="${2#\$}" _sep="${3:- }" IFS="${3:-${IFS:- }}";
Lucio Andrés Illanes Albornoz 3e5303
	eval set -- '${'"${_rlist}"'}'\; ${_rlen}='${#}';
Lucio Andrés Illanes Albornoz 3e5303
};
Lucio Andrés Illanes Albornoz 3e5303
Lucio Andrés Illanes Albornoz 974685
rtl_llift() {
Lucio Andrés Illanes Albornoz 974685
	local _list="${1}" _sep="${2}" _sep_new="${3}" IFS; IFS="${_sep}";
Lucio Andrés Illanes Albornoz 974685
	set -- ${_list}; IFS="${_sep_new}";
Lucio Andrés Illanes Albornoz b6a9a1
	printf "%s" "${*}";
Lucio Andrés Illanes Albornoz 974685
};
Lucio Andrés Illanes Albornoz 974685
Lucio Andrés Illanes Albornoz 3e5303
rtl_llift2() {
Lucio Andrés Illanes Albornoz 3e5303
	local	_rlist="${1#\$}" _rlist_new="${2#\$}" _sep="${3}"	\
Lucio Andrés Illanes Albornoz 3e5303
		_sep_new="${4}" IFS; IFS="${_sep}";
Lucio Andrés Illanes Albornoz 3e5303
Lucio Andrés Illanes Albornoz 3e5303
	eval set -- '${'"${_rlist}"'}'; IFS="${_sep_new}";
Lucio Andrés Illanes Albornoz 3e5303
	eval ${_rlist_new}='"${*}"';
Lucio Andrés Illanes Albornoz 3e5303
};
Lucio Andrés Illanes Albornoz 3e5303
Lucio Andrés Illanes Albornoz 3e5303
rtl_llift3() {
Lucio Andrés Illanes Albornoz 3e5303
	rtl_llift2 "${1}" "${1}" "${2}" "${3}";
Lucio Andrés Illanes Albornoz 3e5303
};
Lucio Andrés Illanes Albornoz 3e5303
Lucio Andrés Illanes Albornoz c6d6e0
rtl_lmatch() {
Lucio Andrés Illanes Albornoz e1d469
	local _list="${1}" _item="${2}" _sep="${3:- }";
Lucio Andrés Illanes Albornoz c6d6e0
	[ -n "$(rtl_lsearch "${_list}" "${_item}" "${_sep}")" ];
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz b6a9a1
rtl_lmax() {
Lucio Andrés Illanes Albornoz b6a9a1
	local _len=0 _len_max=0;
Lucio Andrés Illanes Albornoz b6a9a1
	while [ "${#}" -gt 0 ]; do
Lucio Andrés Illanes Albornoz b6a9a1
		_len="${#1}";
Lucio Andrés Illanes Albornoz b6a9a1
		if [ "${_len}" -gt "${_len_max}" ]; then
Lucio Andrés Illanes Albornoz b6a9a1
			_len_max="${_len}";
Lucio Andrés Illanes Albornoz b6a9a1
		fi; shift;
Lucio Andrés Illanes Albornoz b6a9a1
	done; printf "%s" "${_len_max}";
Lucio Andrés Illanes Albornoz b6a9a1
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz c6d6e0
rtl_lsearch() {
Lucio Andrés Illanes Albornoz e58399
	local	_list="${1}" _filter="${2}" _sep="${3:- }" IFS="${3:-${IFS:- }}"\
Lucio Andrés Illanes Albornoz 2b85d0
		_litem="" _litem_filter="" _lnew="";
Lucio Andrés Illanes Albornoz 2b85d0
	if [ -z "${_filter}" ]; then
Lucio Andrés Illanes Albornoz b6a9a1
		printf "%s" "${_list}"; return 0;
Lucio Andrés Illanes Albornoz 2b85d0
	else for _litem in ${_list}; do
Lucio Andrés Illanes Albornoz 2b85d0
		for _litem_filter in ${_filter}; do
Lucio Andrés Illanes Albornoz 2b85d0
			if [ "${_litem_filter}" = "${_litem}" ]; then
Lucio Andrés Illanes Albornoz 2b85d0
				_lnew="${_lnew:+${_lnew}${_sep}}${_litem}";
Lucio Andrés Illanes Albornoz 2b85d0
				break;
Lucio Andrés Illanes Albornoz 2b85d0
			fi;
Lucio Andrés Illanes Albornoz 2b85d0
		done;
Lucio Andrés Illanes Albornoz 2b85d0
	done; fi;
Lucio Andrés Illanes Albornoz b6a9a1
	printf "%s" "${_lnew}";
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz b1d432
rtl_lsearch_patternl() {
Lucio Andrés Illanes Albornoz b1d432
	local	_list="${1}" _pattern="${2}" _sep="${3:- }" IFS="${3:-${IFS:- }}"\
Lucio Andrés Illanes Albornoz b1d432
		_litem="" _lnew="";
Lucio Andrés Illanes Albornoz b1d432
	if [ -n "${_pattern}" ]; then
Lucio Andrés Illanes Albornoz b1d432
		for _litem in ${_list}; do
Lucio Andrés Illanes Albornoz b1d432
			if [ "${_litem#${_pattern}}" != "${_litem}" ]; then
Lucio Andrés Illanes Albornoz b1d432
				_lnew="${_lnew:+${_lnew}${_sep}}${_litem}";
Lucio Andrés Illanes Albornoz b1d432
			fi;
Lucio Andrés Illanes Albornoz b1d432
		done;
Lucio Andrés Illanes Albornoz b1d432
	fi;
Lucio Andrés Illanes Albornoz b1d432
	printf "%s" "${_lnew}";
Lucio Andrés Illanes Albornoz b1d432
};
Lucio Andrés Illanes Albornoz b1d432
Lucio Andrés Illanes Albornoz 60fba6
rtl_lsort() {
Lucio Andrés Illanes Albornoz 60fba6
	local _list="${1}" _sep="${2:- }";
Lucio Andrés Illanes Albornoz 60fba6
	printf "%s" "${_list}" | tr "${_sep}" "\n" | sort | paste -s -d "${_sep}";
Lucio Andrés Illanes Albornoz 60fba6
};
Lucio Andrés Illanes Albornoz 60fba6
Lucio Andrés Illanes Albornoz c6d6e0
rtl_lunfold_depends() {
Lucio Andrés Illanes Albornoz 763157
	local _vname_template="${1}" _depends="" _name="" _names=""; shift;
Lucio Andrés Illanes Albornoz 763157
	for _name in "${@}"; do
Lucio Andrés Illanes Albornoz c6d6e0
		if _depends="$(rtl_get_var_unsafe -u "$(eval printf \"%s\" \""${_vname_template}"\")")"\
Lucio Andrés Illanes Albornoz 763157
		&& [ -n "${_depends}" ]; then
Lucio Andrés Illanes Albornoz c6d6e0
			_depends="$(rtl_lunfold_depends "${_vname_template}" ${_depends})";
Lucio Andrés Illanes Albornoz c6d6e0
			_names="$(rtl_lconcat "${_names}" "${_depends}")";
Lucio Andrés Illanes Albornoz 763157
		fi;
Lucio Andrés Illanes Albornoz c6d6e0
		_names="$(rtl_lconcat "${_names}" "${_name}")";
Lucio Andrés Illanes Albornoz 763157
	done;
Lucio Andrés Illanes Albornoz b6a9a1
	printf "%s" "${_names}";
Lucio Andrés Illanes Albornoz 763157
};
Lucio Andrés Illanes Albornoz 763157
Lucio Andrés Illanes Albornoz 2b85d0
# vim:filetype=sh