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() {
e9fa07
	local	_rla_vnames="${1}" _rla_sep="${2}"	\
e9fa07
		_rla_head="" _rla_tail="" _rla_vname="" _rla_vval="" IFS;
e9fa07
	IFS="${_rla_sep}"; shift 2;
e9fa07
e9fa07
	for _rla_vval in ${@}; do
e9fa07
		if ! rtl_lfirst \$_rla_head \$_rla_tail "${_rla_vnames}" " "; then
Lucio Andrés Illanes Albornoz 0875f5
			return 1;
Lucio Andrés Illanes Albornoz 0875f5
		else
e9fa07
			_rla_vname="${_rla_head}"; _rla_vnames="${_rla_tail}";
e9fa07
			rtl_set_var_unsafe "${_rla_vname}" "${_rla_vval}";
Lucio Andrés Illanes Albornoz 0875f5
		fi;
Lucio Andrés Illanes Albornoz 0875f5
	done;
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz 0875f5
};
Lucio Andrés Illanes Albornoz 0875f5
Lucio Andrés Illanes Albornoz c6d6e0
rtl_lconcat() {
e9fa07
	rtl_lconcat2 "${1}" "${1}" "${2}" "${3:-}";
e9fa07
};
e9fa07
e9fa07
rtl_lconcat2() {
e9fa07
	local	_rlc2_rlist_new="${1#\$}" _rlc2_rlist="${2#\$}" _rlc2_litem_new="${3}"	\
e9fa07
		_rlc2_sep="${4:- }" IFS="${4:-${IFS:- }}";
e9fa07
e9fa07
	if eval [ \"'${'${_rlc2_rlist}':+1}'\" = 1 ]; then
e9fa07
		eval ${_rlc2_rlist_new}='${'"${_rlc2_rlist}"'}${_rlc2_sep}${_rlc2_litem_new}';
Lucio Andrés Illanes Albornoz 2b85d0
	else
e9fa07
		eval ${_rlc2_rlist_new}='${_rlc2_litem_new}';
Lucio Andrés Illanes Albornoz 2b85d0
	fi;
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz c6d6e0
rtl_lfilter() {
e9fa07
	rtl_lfilter2 "${1}" "${1}" "${2}" "${3:-}";
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz 3e5303
rtl_lfilter2() {
e9fa07
	local	_rlf2_rlist="${1#\$}" _rlf2_rlist_new="${2#\$}" _rlf2_filter="${3}" _rlf2_sep="${4:-}"\
e9fa07
		_rlf2_filterfl="" _rlf2_list="" _rlf2_litem="" _rlf2_litem_filter="" _rlf2_lnew="";
e9fa07
e9fa07
	if [ "${_rlf2_sep:+1}" = 1 ]; then
e9fa07
		local IFS="${_rlf2_sep}";
e9fa07
	else
e9fa07
		local _rlf2_sep=" " IFS="${RTL_IFS_ORIG}";
e9fa07
	fi;
e9fa07
e9fa07
	if [ "${_rlf2_filter:+1}" != 1 ]; then
e9fa07
		eval ${_rlf2_rlist_new}=;
e9fa07
	else
e9fa07
		eval _rlf2_list='${'"${_rlf2_rlist}"'}';
e9fa07
		eval ${_rlf2_rlist_new}=;
e9fa07
		for _rlf2_litem in ${_rlf2_list}; do
e9fa07
			_rlf2_filterfl=0;
e9fa07
			for _rlf2_litem_filter in ${_rlf2_filter}; do
e9fa07
				if [ "${_rlf2_litem_filter}" = "${_rlf2_litem}" ]; then
e9fa07
					_rlf2_filterfl=1; break;
Lucio Andrés Illanes Albornoz 3e5303
				fi;
Lucio Andrés Illanes Albornoz 3e5303
			done;
e9fa07
			if [ "${_rlf2_filterfl:-0}" -eq 0 ]; then
e9fa07
				eval ${_rlf2_rlist_new}='${'"${_rlf2_rlist_new}"':+${'"${_rlf2_rlist_new}"'}${_rlf2_sep}}${_rlf2_litem}';
Lucio Andrés Illanes Albornoz 3e5303
			fi;
Lucio Andrés Illanes Albornoz 3e5303
		done;
Lucio Andrés Illanes Albornoz 3e5303
	fi;
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz 3e5303
};
Lucio Andrés Illanes Albornoz 3e5303
Lucio Andrés Illanes Albornoz 0875f5
rtl_lfirst() {
e9fa07
	local _rlf_rhead="${1#\$}" _rlf_rtail="${2#\$}" _rlf_list="${3}" _rlf_sep="${4}" IFS="${4}";
e9fa07
e9fa07
	set -- ${_rlf_list}; eval ${_rlf_rhead}='${1}';
Lucio Andrés Illanes Albornoz 0875f5
	if [ "${#}" -ge 0 ]; then
e9fa07
		shift; eval ${_rlf_rtail}=\"'${*}'\";
Lucio Andrés Illanes Albornoz 0875f5
	else
e9fa07
		eval ${_rlf_rtail}=;
Lucio Andrés Illanes Albornoz 0875f5
	fi;
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz 0875f5
};
Lucio Andrés Illanes Albornoz 0875f5
e9fa07
rtl_lindexV() {
e9fa07
	local	_rliV_rlout="${1#\$}" _rliV_idx="${2}"	\
e9fa07
		_rliV_limit=0; shift 2;
e9fa07
e9fa07
	if [ "${_rliV_idx#-}" != "${_rliV_idx}" ]; then
e9fa07
		_rliV_idx="${_rliV_idx#-}";
e9fa07
		while [ "${#}" -gt "${_rliV_idx}" ]; do
e9fa07
			shift;
e9fa07
		done;
e9fa07
		_rliV_limit=$((${#} + 1));
e9fa07
	else
e9fa07
		_rliV_limit="${#}";
e9fa07
		shift "${_rliV_idx}";
e9fa07
	fi;
e9fa07
e9fa07
	if [ "${#}" -ge 1 ]\
e9fa07
	&& [ "${_rliV_idx}" -lt "${_rliV_limit}" ]; then
e9fa07
		eval ${_rliV_rlout}='${1}';
e9fa07
	else
e9fa07
		eval ${_rliV_rlout}=;
e9fa07
	fi;
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
e9fa07
rtl_llength() {
e9fa07
	local _rll_rlen="${1#\$}" _rll_rlist="${2#\$}" _rll_sep="${3:- }" IFS="${3:-${IFS:- }}";
e9fa07
e9fa07
	eval set -- '${'"${_rll_rlist}"'}'\; ${_rll_rlen}='${#}';
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz 3e5303
};
Lucio Andrés Illanes Albornoz 3e5303
Lucio Andrés Illanes Albornoz 974685
rtl_llift() {
e9fa07
	rtl_llift2 "${1}" "${1}" "${2}" "${3}";
Lucio Andrés Illanes Albornoz 974685
};
Lucio Andrés Illanes Albornoz 974685
Lucio Andrés Illanes Albornoz 3e5303
rtl_llift2() {
e9fa07
	local	_rl2_rlist="${1#\$}" _rl2_rlist_new="${2#\$}" _rl2_sep="${3:-}"	\
e9fa07
		_rl2_sep_new="${4}";
Lucio Andrés Illanes Albornoz 3e5303
e9fa07
	if [ "${_rl2_sep:+1}" = 1 ]; then
e9fa07
		local IFS="${_rl2_sep}";
e9fa07
	else
e9fa07
		local _rl2_sep=" " IFS="${RTL_IFS_ORIG}";
e9fa07
	fi;
Lucio Andrés Illanes Albornoz 3e5303
e9fa07
	eval set -- '${'"${_rl2_rlist}"'}'; IFS="${_rl2_sep_new}";
e9fa07
	eval ${_rl2_rlist_new}='"${*}"';
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz 3e5303
};
Lucio Andrés Illanes Albornoz 3e5303
Lucio Andrés Illanes Albornoz c6d6e0
rtl_lmatch() {
e9fa07
	local _rlm_rlist="${1#\$}" _rlm_item="${2}" _rlm_sep="${3:-}" _rlm_list_="";
e9fa07
e9fa07
	rtl_lsearch2 "${_rlm_rlist}" \$_rlm_list_ "${_rlm_item}" "${_rlm_sep}";
e9fa07
	[ "${_rlm_list_:+1}" = 1 ];
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz b6a9a1
rtl_lmax() {
e9fa07
	local	_rlm2_rlist="${1#\$}" _rlm2_rmax="${2#\$}"	\
e9fa07
		_rlm2_len=0 _rlm2_len_max=0;
e9fa07
e9fa07
	eval set -- '${'"${_rlm2_rlist}"'}';
Lucio Andrés Illanes Albornoz b6a9a1
	while [ "${#}" -gt 0 ]; do
e9fa07
		_rlm2_len="${#1}";
e9fa07
		if [ "${_rlm2_len}" -gt "${_rlm2_len_max}" ]; then
e9fa07
			_rlm2_len_max="${_rlm2_len}";
Lucio Andrés Illanes Albornoz b6a9a1
		fi; shift;
e9fa07
	done;
e9fa07
	eval ${_rlm2_rmax}='${_rlm2_len_max}';
e9fa07
	return 0;
e9fa07
};
e9fa07
e9fa07
rtl_lrangeV() {
e9fa07
	local	_rlrV_rlout="${1#\$}" _rlrV_idx0="${2}" _rlrV_idx1="${3}" _rlrV_sep="${4}"	\
e9fa07
		_rlrV_limit=0; shift 4;
e9fa07
e9fa07
	if [ "${_rlrV_idx0#-}" != "${_rlrV_idx0}" ]; then
e9fa07
		eval ${_rlrV_rlout}=;
e9fa07
e9fa07
		return 1;
e9fa07
	elif [ "${_rlrV_idx1#-}" != "${_rlrV_idx1}" ]; then
e9fa07
		_rlrV_idx1="${_rlrV_idx1#-}";
e9fa07
e9fa07
		_rlrV_limit="${#}";
e9fa07
		shift "${_rlrV_idx0}";
e9fa07
e9fa07
		eval ${_rlrV_rlout}=;
e9fa07
		while [ "${#}" -gt 0 ]\
e9fa07
		   && [ "${_rlrV_idx1}" -lt "${#}" ];
e9fa07
		do
e9fa07
			eval ${_rlrV_rlout}='${'"${_rlrV_rlout}"':+${'"${_rlrV_rlout}"'}${_rlrV_sep}}${1}';
e9fa07
			shift;
e9fa07
		done;
e9fa07
e9fa07
		return 0;
e9fa07
	elif [ "${_rlrV_idx1}" -ge "${_rlrV_idx0}" ]; then
e9fa07
		_rlrV_limit="${#}";
e9fa07
		shift "${_rlrV_idx0}";
e9fa07
		: $((_rlrV_idx1 -= ${_rlrV_idx0}));
e9fa07
		: $((_rlrV_idx1 += 1));
e9fa07
e9fa07
		eval ${_rlrV_rlout}=;
e9fa07
		while [ "${_rlrV_idx1}" -gt 0 ]\
e9fa07
		   && [ "${#}" -gt 0 ];
e9fa07
		do
e9fa07
			eval ${_rlrV_rlout}='${'"${_rlrV_rlout}"':+${'"${_rlrV_rlout}"'}${_rlrV_sep}}${1}';
e9fa07
			: $((_rlrV_idx1 -= 1));
e9fa07
			shift;
e9fa07
		done;
e9fa07
e9fa07
		return 0;
e9fa07
	fi;
Lucio Andrés Illanes Albornoz b6a9a1
};
Lucio Andrés Illanes Albornoz b6a9a1
Lucio Andrés Illanes Albornoz c6d6e0
rtl_lsearch() {
e9fa07
	rtl_lsearch2 "${1}" "${1}" "${2}" "${3:-}";
e9fa07
}
e9fa07
e9fa07
rtl_lsearch2() {
e9fa07
	local	_rls2_rlist="${1#\$}" _rls2_rlist_new="${2#\$}"		\
e9fa07
		_rls2_filter="${3}" _rls2_sep="${4:-}"			\
e9fa07
		_rls2_list="" _rls2_litem="" _rls2_litem_filter=""	\
e9fa07
		_rls2_lnew="";
e9fa07
e9fa07
	if [ "${_rls2_sep:+1}" = 1 ]; then
e9fa07
		local IFS="${_rls2_sep}";
e9fa07
	else
e9fa07
		local _rls2_sep=" " IFS="${RTL_IFS_ORIG}";
e9fa07
	fi;
e9fa07
	if [ "${_rls2_filter:+1}" != 1 ]; then
e9fa07
		eval ${_rls2_rlist_new}='${_rls2_list}'; return 0;
e9fa07
	else
e9fa07
		eval _rls2_list='${'"${_rls2_rlist}"'}';
e9fa07
		for _rls2_litem in ${_rls2_list}; do
e9fa07
			for _rls2_litem_filter in ${_rls2_filter}; do
e9fa07
				if [ "${_rls2_litem_filter}" = "${_rls2_litem}" ]; then
e9fa07
					_rls2_lnew="${_rls2_lnew:+${_rls2_lnew}${_rls2_sep}}${_rls2_litem}";
e9fa07
					break;
e9fa07
				fi;
e9fa07
			done;
Lucio Andrés Illanes Albornoz 2b85d0
		done;
e9fa07
	fi;
e9fa07
e9fa07
	eval ${_rls2_rlist_new}='${_rls2_lnew}';
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz 2b85d0
};
Lucio Andrés Illanes Albornoz 2b85d0
Lucio Andrés Illanes Albornoz b1d432
rtl_lsearch_patternl() {
e9fa07
	rtl_lsearch_patternl2 "${1}" "${1}" "${2}" "${3:-}";
e9fa07
};
e9fa07
e9fa07
rtl_lsearch_patternl2() {
e9fa07
	local	_rlsp2_rlist="${1#\$}" _rlsp2_rlist_new="${2#\$}"	\
e9fa07
		_rlsp2_pattern="${3}" _rlsp2_sep="${4:- }"		\
e9fa07
		IFS="${4:-${IFS:- }}"					\
e9fa07
		_rlsp2_litem="" _rlsp2_lnew="";
e9fa07
e9fa07
	if [ "${_rlsp2_pattern:+1}" = 1 ]; then
e9fa07
		eval _rlsp2_list='${'"${_rlsp2_rlist}"'}';
e9fa07
		for _rlsp2_litem in ${_rlsp2_list}; do
e9fa07
			if [ "${_rlsp2_litem#${_rlsp2_pattern}}" != "${_rlsp2_litem}" ]; then
e9fa07
				_rlsp2_lnew="${_rlsp2_lnew:+${_rlsp2_lnew}${_rlsp2_sep}}${_rlsp2_litem}";
Lucio Andrés Illanes Albornoz b1d432
			fi;
Lucio Andrés Illanes Albornoz b1d432
		done;
Lucio Andrés Illanes Albornoz b1d432
	fi;
e9fa07
	eval ${_rlsp2_rlist_new}='${_rlsp2_lnew}';
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz b1d432
};
Lucio Andrés Illanes Albornoz b1d432
Lucio Andrés Illanes Albornoz 60fba6
rtl_lsort() {
e9fa07
	printf "%s" "${*}" | tr " " "\n" | sort | paste -s -d " ";
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz 60fba6
};
Lucio Andrés Illanes Albornoz 60fba6
e9fa07
RTL_LUNFOLD_DEPENDS_LEVEL=0;
Lucio Andrés Illanes Albornoz c6d6e0
rtl_lunfold_depends() {
e9fa07
	local	_rld_vname_template="${1}" _rld_rlist="${2#\$}"		\
e9fa07
		_rld_depends="" _rld_list="" _rld_name="" _rld_names=""	\
e9fa07
		_rld_vname_template_="";
e9fa07
		shift 2;
e9fa07
e9fa07
	for _rld_name in "${@}"; do
e9fa07
		eval _rld_vname_template_=\"${_rld_vname_template}\";
e9fa07
		if rtl_get_var_unsafe \$_rld_depends -u "${_rld_vname_template_}"\
e9fa07
		&& [ "${_rld_depends:+1}" = 1 ]; then
e9fa07
			: $((RTL_LUNFOLD_DEPENDS_LEVEL+=1));
e9fa07
			eval _rld_depends${RTL_LUNFOLD_DEPENDS_LEVEL}='${_rld_depends}';
e9fa07
			rtl_lunfold_depends "${_rld_vname_template}" \$_rld_depends${RTL_LUNFOLD_DEPENDS_LEVEL} ${_rld_depends};
e9fa07
			eval _rld_depends='${_rld_depends'"${RTL_LUNFOLD_DEPENDS_LEVEL}"'}';
e9fa07
			unset '_rld_depends'"${RTL_LUNFOLD_DEPENDS_LEVEL}";
e9fa07
			: $((RTL_LUNFOLD_DEPENDS_LEVEL-=1));
e9fa07
			rtl_lconcat \$_rld_names "${_rld_depends}";
Lucio Andrés Illanes Albornoz 763157
		fi;
e9fa07
		rtl_lconcat \$_rld_names "${_rld_name}";
Lucio Andrés Illanes Albornoz 763157
	done;
e9fa07
	eval ${_rld_rlist}='${_rld_names}';
e9fa07
	return 0;
Lucio Andrés Illanes Albornoz 763157
};
Lucio Andrés Illanes Albornoz 763157
64844b
# vim:filetype=sh textwidth=0