Blob Blame History Raw
#
# 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