summaryrefslogtreecommitdiffhomepage
path: root/subr.rtl/rtl_string.subr
blob: 4f0910a9e391c834396ca02c4e98665d80ee0a3e (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
#
# 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() {
	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() {
	local _rm_s="${1}" _rm_find="${2}";

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

rtl_matchr() {
	local _rmr_s="${1}" _rmr_find="${2}";

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

rtl_setrstatus() {
	local _rsrs_rstatus="${1#\$}" _rsrs_status="${2}";
	eval ${_rsrs_rstatus}=\"${_rsrs_status}\";
	return 0;
};

rtl_subst() {
	rtl_subst2 "${1}" "${1}" "${2}" "${3}";
};

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() {
	rtl_tolower2 "${1}" "${1}";
};

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() {
	rtl_toupper2 "${1}" "${1}";
};

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