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.
#

#
# rtl_isnumber() - check if string is number
# @_s:		string to check
#
# Returns:	zero (0) if string is number, non-zero (>0) if string is not number
#
rtl_isnumber() {
	local	_ri_s="${1}"	\
		_ri_rc=0;

	while [ "${_ri_s:+1}" = 1 ]; do
	case "${_ri_s}" in
	[0-9]*)	_ri_s="${_ri_s#[0-9]}"; ;;
	*)	_ri_rc=1; break; ;;
	esac; done;

	return "${_ri_rc}";
};

#
# rtl_match() - match pattern against string
# @_s:		input string
# @_find:	pattern to match against string
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_match() {
	local _rm_s="${1}" _rm_find="${2}";

	if [ "${_rm_s#${_rm_find}}" != "${_rm_s}" ]; then
		return 0;
	else
		return 1;
	fi;
};

#
# rtl_matchr() - match pattern against string from right-hand side
# @_s:		input string
# @_find:	pattern to match against string
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_matchr() {
	local _rmr_s="${1}" _rmr_find="${2}";

	if [ "${_rmr_s%${_rmr_find}}" != "${_rmr_s}" ]; then
		return 0;
	else
		return 1;
	fi;
};

#
# rtl_remove_postfix() - remove longest postfix from string w/ pattern
# @_pattern:	pattern to match against input string
# @_s:		input string
# @_rs_out:	out reference to output string
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_remove_postfix() {
	local _rh_pattern="${1}" _rh_s="${2}" _rh_rs_out="${3#\$}";

	while true; do
		if [ "${_rh_s%%${_rh_pattern}}" = "${_rh_s}" ]; then
			break;
		else
			_rh_s="${_rh_s%%${_rh_pattern}}";
		fi;
	done;

	eval ${_rh_rs_out}='${_rh_s}';
	return 0;
};

#
# rtl_setrstatus() - set status string
# @_rstatus:	out reference to status string
# @_status:	new status string
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_setrstatus() {
	local _rsrs_rstatus="${1#\$}" _rsrs_status="${2}";
	eval ${_rsrs_rstatus}=\"${_rsrs_status}\";
	return 0;
};

#
# rtl_subst() - substitute in string
# @_rs:		inout reference to string
# @_find:	pattern to match against input string
# @_replace:	replacement string
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_subst() {
	rtl_subst2 "${1}" "${1}" "${2}" "${3}";
};

#
# rtl_subst2() - substitute in string
# @_rs:		in reference to string
# @_rs_out:	out reference to new string
# @_find:	pattern to match against input string
# @_replace:	replacement string
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_subst2() {
	local	_rs2_rs="${1#\$}" _rs2_rs_out="${2#\$}" _rs2_find="${3}" _rs2_replace="${4}"	\
		_rs2_prefix="" _rs2_s="" _rs2_s_new="";

	eval _rs2_s="\${${_rs2_rs}}";
	while [ "${_rs2_s:+1}" = 1 ]; do
	case "${_rs2_s}" in
	*${_rs2_find}*)	_rs2_prefix="${_rs2_s%%${_rs2_find}*}"; _rs2_s="${_rs2_s#*${_rs2_find}}";
			_rs2_s_new="${_rs2_s_new:+${_rs2_s_new}}${_rs2_prefix}${_rs2_replace}"; ;;
	*)		_rs2_s_new="${_rs2_s_new:+${_rs2_s_new}}${_rs2_s}"; _rs2_s=""; ;;
	esac; done;
	eval ${_rs2_rs_out}='${_rs2_s_new}';
	return 0;
};

#
# rtl_tolower() - convert string to lower case
# @_rs:		in reference to string
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_tolower() {
	rtl_tolower2 "${1}" "${1}";
};

#
# rtl_tolower2() - convert string to lower case
# @_rs:		in reference to string
# @_rs_out:	out reference to new string
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_tolower2() {
	local	_rtl2_rs="${1#\$}" _rtl2_rs_out="${2#\$}"	\
		_rtl2_s="" _rtl2_s_new="";

	eval _rtl2_s="\${${_rtl2_rs}}";

	while [ "${_rtl2_s:+1}" = 1 ]; do
	case "${_rtl2_s}" in
	A*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}a"; _rtl2_s="${_rtl2_s#A}"; ;;
	B*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}b"; _rtl2_s="${_rtl2_s#B}"; ;;
	C*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}c"; _rtl2_s="${_rtl2_s#C}"; ;;
	D*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}d"; _rtl2_s="${_rtl2_s#D}"; ;;
	E*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}e"; _rtl2_s="${_rtl2_s#E}"; ;;
	F*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}f"; _rtl2_s="${_rtl2_s#F}"; ;;
	G*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}g"; _rtl2_s="${_rtl2_s#G}"; ;;
	H*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}h"; _rtl2_s="${_rtl2_s#H}"; ;;
	I*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}i"; _rtl2_s="${_rtl2_s#I}"; ;;
	J*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}j"; _rtl2_s="${_rtl2_s#J}"; ;;
	K*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}k"; _rtl2_s="${_rtl2_s#K}"; ;;
	L*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}l"; _rtl2_s="${_rtl2_s#L}"; ;;
	M*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}m"; _rtl2_s="${_rtl2_s#M}"; ;;
	N*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}n"; _rtl2_s="${_rtl2_s#N}"; ;;
	O*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}o"; _rtl2_s="${_rtl2_s#O}"; ;;
	P*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}p"; _rtl2_s="${_rtl2_s#P}"; ;;
	Q*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}q"; _rtl2_s="${_rtl2_s#Q}"; ;;
	R*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}r"; _rtl2_s="${_rtl2_s#R}"; ;;
	S*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}s"; _rtl2_s="${_rtl2_s#S}"; ;;
	T*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}t"; _rtl2_s="${_rtl2_s#T}"; ;;
	U*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}u"; _rtl2_s="${_rtl2_s#U}"; ;;
	V*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}v"; _rtl2_s="${_rtl2_s#V}"; ;;
	W*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}w"; _rtl2_s="${_rtl2_s#W}"; ;;
	X*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}x"; _rtl2_s="${_rtl2_s#X}"; ;;
	Y*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}y"; _rtl2_s="${_rtl2_s#Y}"; ;;
	Z*)     _rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}z"; _rtl2_s="${_rtl2_s#Z}"; ;;
	[!ABCDEFGHIJKLMNOPQRSTUVWXYZ]*)
		_rtl2_s_new="${_rtl2_s_new:+${_rtl2_s_new}}${_rtl2_s%%[ABCDEFGHIJKLMNOPQRSTUVWXYZ]*}";
		while [ "${_rtl2_s#[!ABCDEFGHIJKLMNOPQRSTUVWXYZ]}" != "${_rtl2_s}" ]; do
			_rtl2_s="${_rtl2_s#[!ABCDEFGHIJKLMNOPQRSTUVWXYZ]}";
		done; ;;
	esac; done;
	eval ${_rtl2_rs_out}='${_rtl2_s_new}';

	return 0;
};

#
# rtl_toupper() - convert string to upper case
# @_rs:		in reference to string
# @_rs_out:	out reference to new string
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_toupper() {
	rtl_toupper2 "${1}" "${1}";
};

#
# rtl_toupper2() - convert string to upper case
# @_rs:		in reference to string
# @_rs_out:	out reference to new string
#
# Returns:	zero (0) on success, non-zero (>0) on failure
#
rtl_toupper2() {
	local	_rtu2_rs="${1#\$}" _rtu2_rs_out="${2#\$}"	\
		_rtu2_s="" _rtu2_s_new="";

	eval _rtu2_s="\${${_rtu2_rs}}";

	while [ "${_rtu2_s:+1}" = 1 ]; do
	case "${_rtu2_s}" in
	a*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}A"; _rtu2_s="${_rtu2_s#a}"; ;;
	b*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}B"; _rtu2_s="${_rtu2_s#b}"; ;;
	c*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}C"; _rtu2_s="${_rtu2_s#c}"; ;;
	d*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}D"; _rtu2_s="${_rtu2_s#d}"; ;;
	e*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}E"; _rtu2_s="${_rtu2_s#e}"; ;;
	f*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}F"; _rtu2_s="${_rtu2_s#f}"; ;;
	g*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}G"; _rtu2_s="${_rtu2_s#g}"; ;;
	h*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}H"; _rtu2_s="${_rtu2_s#h}"; ;;
	i*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}I"; _rtu2_s="${_rtu2_s#i}"; ;;
	j*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}J"; _rtu2_s="${_rtu2_s#j}"; ;;
	k*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}K"; _rtu2_s="${_rtu2_s#k}"; ;;
	l*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}L"; _rtu2_s="${_rtu2_s#l}"; ;;
	m*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}M"; _rtu2_s="${_rtu2_s#m}"; ;;
	n*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}N"; _rtu2_s="${_rtu2_s#n}"; ;;
	o*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}O"; _rtu2_s="${_rtu2_s#o}"; ;;
	p*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}P"; _rtu2_s="${_rtu2_s#p}"; ;;
	q*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}Q"; _rtu2_s="${_rtu2_s#q}"; ;;
	r*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}R"; _rtu2_s="${_rtu2_s#r}"; ;;
	s*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}S"; _rtu2_s="${_rtu2_s#s}"; ;;
	t*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}T"; _rtu2_s="${_rtu2_s#t}"; ;;
	u*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}U"; _rtu2_s="${_rtu2_s#u}"; ;;
	v*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}V"; _rtu2_s="${_rtu2_s#v}"; ;;
	w*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}W"; _rtu2_s="${_rtu2_s#w}"; ;;
	x*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}X"; _rtu2_s="${_rtu2_s#x}"; ;;
	y*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}Y"; _rtu2_s="${_rtu2_s#y}"; ;;
	z*)     _rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}Z"; _rtu2_s="${_rtu2_s#z}"; ;;
	[!abcdefghijklmnopqrstuvwxyz]*)
		_rtu2_s_new="${_rtu2_s_new:+${_rtu2_s_new}}${_rtu2_s%%[abcdefghijklmnopqrstuvwxyz]*}";
		while [ "${_rtu2_s#[!abcdefghijklmnopqrstuvwxyz]}" != "${_rtu2_s}" ]; do
			_rtu2_s="${_rtu2_s#[!abcdefghijklmnopqrstuvwxyz]}";
		done; ;;
	esac; done;
	eval ${_rtu2_rs_out}='${_rtu2_s_new}';

	return 0;
};

# vim:filetype=sh textwidth=0