-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.sh
More file actions
executable file
·438 lines (392 loc) · 13.3 KB
/
string.sh
File metadata and controls
executable file
·438 lines (392 loc) · 13.3 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
################################################################################
# MIT License
#
# Copyright (c) 2017 Aludirk Wong <aludirkwong@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
################################################################################
#!/usr/bin/env bash
pushd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null
source '.core.sh'
source 'command.sh'
popd &> /dev/null
################################################################################
# Implode an array to string.
#
# Usage: implode_string <string_array> <separator> <string_out>
#
# Parameters:
# string_array [in] The array of string to implode.
# separator [in] The separator.
# string_out [out] The imploded string.
#
# Returns:
# ${LIB_BASH_ERROR_INVALID_PARAM}
# ${LIB_BASH_ERROR_NO_OUTPUT}
################################################################################
function implode_string()
{
local _lbis_array=("${!1}")
local _lbis_first_element="${_lbis_array[0]}"
local _lbis_pop_first_array=("${_lbis_array[@]:1}")
local _lbis_sep="${2}"
local _lbis_array_out=${3}
if [[ ${#} -lt 2 ]]; then
error_code ${LIB_BASH_ERROR_INVALID_PARAM}
return ${?}
fi
if [[ -z "${_lbis_array_out}" ]]; then
error_code ${LIB_BASH_ERROR_NO_OUTPUT}
return ${?}
fi
# Subsitute the front of each element in ${_lbis_pop_first_array} with
# ${_lbis_sep}
printf -v "${_lbis_array_out}" '%b' \
"${_lbis_first_element}${_lbis_pop_first_array[@]/#/$_lbis_sep}"
return 0
}
################################################################################
# Explode a string to array.
#
# Usage: explode_string <string> <delimiter> <array_out>
#
# Parameters:
# string [in] The string to explode.
# delimiter [in] The delimiter list (for IFS (internal field separator) use).
# array_out [out] The exploded array.
#
# Returns:
# ${LIB_BASH_ERROR_INVALID_PARAM}
# ${LIB_BASH_ERROR_NO_OUTPUT}
################################################################################
function explode_string()
{
local _lbes_string="$(printf "%b\x1f" "${1}" | escape_perl)"
local _lbes_IFS="$(printf "%b\x1f" "${2}" | escape_perl_re)"
local _lbes_array_out=${3}
if [[ ${#} -lt 2 ]]; then
error_code ${LIB_BASH_ERROR_INVALID_PARAM}
return ${?}
fi
if [[ -z "${_lbes_array_out}" ]]; then
error_code ${LIB_BASH_ERROR_NO_OUTPUT}
return ${?}
fi
# Clean up the result.
eval "${_lbes_array_out}=()"
local _lbes_perl_out=''
if [[ "${_lbes_IFS}" == $'\x1f' ]]; then
_lbes_perl_out="$(perl -CS -e \
'use utf8;
print join("\x1e",
split(//, "'"${_lbes_string%$'\x1f'}"'"))';
printf "\x1f")"
else
_lbes_perl_out="$(perl -CS -e \
'use utf8;
print join("\x1e",
split(/['"${_lbes_IFS%$'\x1f'}"']/,
"'"${_lbes_string%$'\x1f'}"'",
-1))';
printf "\x1f")"
fi
IFS=$'\x1e'
local _lbes_perl_array=(${_lbes_perl_out})
IFS="${LIB_BASH_ORIGINAL_IFS}"
# Push all outputs to result array.
local _lbes_value
for _lbes_value in "${_lbes_perl_array[@]}"; do
if [[ "${_lbes_value: -1}" == $'\x1f' ]]; then
_lbes_value="$(printf '%s' "${_lbes_value}" | escape_system)"
else
_lbes_value="$(printf "%s\x1f" "${_lbes_value}" | escape_system)"
fi
eval "${_lbes_array_out}+=(\"${_lbes_value%$'\x1f'}\")"
done
return 0
}
################################################################################
# Escape the string.
#
# Usage: escape_string [-e escape_list] <string> <escaped_string>
#
# Options:
# -s escape_list The character list for escaping, default is '"\$'.
#
# Parameters:
# string [in] The string to escape.
# escaped_string [out] The escaped string.
#
# Returns:
# ${LIB_BASH_ERROR_INVALID_PARAM}
# ${LIB_BASH_ERROR_INVALID_OPTION}
# ${LIB_BASH_ERROR_NO_OUTPUT}
################################################################################
function escape_string()
{
local _lbes_args=("${@}")
local _lbes_options=()
local _lbes_params=()
get_option 'e:' _lbes_args[@] _lbes_options _lbes_params
if [[ ${?} -ne 0 ]]; then
error_code ${LIB_BASH_ERROR_INVALID_OPTION}
return ${?}
fi
local _lbes_output=''
if [[ ${#_lbes_params[@]} -lt 1 ]]; then
error_code ${LIB_BASH_ERROR_INVALID_PARAM}
return ${?}
else
_lbes_output="${_lbes_params[0]}"
fi
local _lbes_escaped_string
if [[ ${#_lbes_params[@]} -lt 2 ]] || [[ -z "${_lbes_params[1]}" ]]; then
error_code ${LIB_BASH_ERROR_NO_OUTPUT}
return ${?}
else
_lbes_escaped_string=${_lbes_params[1]}
fi
local _lbes_escape_list="$(printf "%b\x1f" '"\$\\\\')"
local _lbes_option=''
for _lbes_option in "${_lbes_options[@]}"; do
local _lbes_opt=''
local _lbes_data=''
parse_option "${_lbes_option}" _lbes_opt _lbes_data
case "${_lbes_opt}" in
e) _lbes_escape_list="$(printf "%b\x1f" "${_lbes_data}" | escape_perl_re)";;
esac
done
if [[ "${_lbes_escape_list}" == $'\x1f' ]]; then
eval "${_lbes_escaped_string}=\"\${_lbes_output}\""
return 0
fi
_lbes_output="$(printf "%b\x1f" "${_lbes_output}" | \
perl -CS -pe 'use utf8; s/(['"${_lbes_escape_list%$'\x1f'}"'])/\\\1/g')"
eval "${_lbes_escaped_string}=\"\${_lbes_output%\$'\x1f'}\""
return 0
}
################################################################################
# Check whether the string match the given regular expression.
#
# Usage: match_string <string> <pattern> <modifier> <is_match_out>
#
# Parameters:
# string [in] The string for checking.
# pattern [in] The regular expression, it cannot be empty.
# modifier [in] The regular expression modifier.
# is_match_out [out] true if matched or else false.
#
# Returns:
# ${LIB_BASH_ERROR_INVALID_PARAM}
# ${LIB_BASH_ERROR_NO_OUTPUT}
# ${LIB_BASH_INVALID_REGEX}
#
# This function will auto add '^' and '$' at the front and end of the pattern
# respectively.
#
# See http://perldoc.perl.org/perlre.html for more information.
################################################################################
function match_string()
{
local _lbms_string="$(printf "%b\x1f" "${1}" | escape_perl)"
local _lbms_pattern="${2}"
local _lbms_modifier="${3}"
local _lbms_is_match=${4}
if [[ ${#} -lt 3 ]] || [[ -z "${_lbms_pattern}" ]]; then
error_code ${LIB_BASH_ERROR_INVALID_PARAM}
return ${?}
fi
if [[ -z "${_lbms_is_match}" ]]; then
error_code ${LIB_BASH_ERROR_NO_OUTPUT}
return ${?}
fi
# Append '^' at the start of pattern
if [[ "${_lbms_pattern:0:1}" != '^' ]]; then
_lbms_pattern="^${_lbms_pattern}"
fi
# Append '$' at the end of pattern
if [[ "${_lbms_pattern: -1}" != '$' ]]; then
_lbms_pattern="${_lbms_pattern}\$"
fi
_lbms_perl_out="$(perl -CS -e \
'use utf8;
"'"${_lbms_string%$'\x1f'}"'" =~
m/'"${_lbms_pattern}"'/'"${_lbms_modifier}"';
print $&' 2>/dev/null || exit ${?}; printf "\x1f")"
if [[ ${?} -ne 0 ]]; then
unset _lbms_perl_out
error_code ${LIB_BASH_INVALID_REGEX}
return ${?}
fi
if [[ -z "${_lbms_perl_out%$'\x1f'}" ]]; then
eval "${_lbms_is_match}=false"
else
eval "${_lbms_is_match}=true"
fi
unset _lbms_perl_out
return 0
}
################################################################################
# Find the matched pattern from string with the regular experession.
#
# Usage: regex_string <string> <pattern> <modifier> <match_out> <group_out>
#
# Parameters:
# string [in] The string to find pattern.
# pattern [in] The regular expression, it cannot be empty.
# modifier [in] The regular expression modifier.
# match_out [out] The matched string (*).
# group_out [out] The array of captured group.
#
# Returns:
# ${LIB_BASH_ERROR_INVALID_PARAM}
# ${LIB_BASH_ERROR_NO_OUTPUT}
# ${LIB_BASH_INTERNAL_ERROR}
# ${LIB_BASH_INVALID_REGEX}
#
# * The string that matches the regular expression, if the modifier 'g' is used,
# the last matched string will be outputted.
#
# See http://perldoc.perl.org/perlre.html for more information.
################################################################################
function regex_string()
{
local _lbrs_string="$(printf "%b\x1f" "${1}" | escape_perl)"
local _lbrs_pattern="${2}"
local _lbrs_modifier="${3}"
local _lbrs_matched=${4}
local _lbrs_group=${5}
if [[ ${#} -lt 3 ]] || [[ -z "${_lbrs_pattern}" ]]; then
error_code ${LIB_BASH_ERROR_INVALID_PARAM}
return ${?}
fi
if [[ -z "${_lbrs_matched}" ]] || [[ -z "${_lbrs_group}" ]]; then
error_code ${LIB_BASH_ERROR_NO_OUTPUT}
return ${?}
fi
_lbrs_perl_out="$(perl -CS -e \
'use utf8;
@res =
"'"${_lbrs_string%$'\x1f'}"'"
=~ m/'"${_lbrs_pattern}"'/'"${_lbrs_modifier}"';
$\="\x1f";
print $&;
if ((scalar(@res) > 1) || ((scalar(@res) == 1) && ($1 & ~$1))) {
$,="\x1d";
print @res
} else {
print ""
}' 2>/dev/null)"
if [[ ${?} -ne 0 ]]; then
unset _lbrs_perl_out
error_code ${LIB_BASH_INVALID_REGEX}
return ${?}
fi
if [[ -z "${_lbrs_perl_out}" ]]; then
unset _lbrs_perl_out
eval "${_lbrs_matched}=''"
eval "${_lbrs_group}=()"
return 0
fi
# Extract matched string
local _lbrs_matched_out=()
explode_string "${_lbrs_perl_out%$'\x1f'}" $'\x1f' _lbrs_matched_out
if [[ ${?} -ne 0 ]] || [[ ${#_lbrs_matched_out[@]} -ne 2 ]]; then
unset _lbrs_perl_out
error_code ${LIB_BASH_INTERNAL_ERROR}
return ${?}
fi
unset _lbrs_perl_out
# Extract capture groups
local _lbrs_group_out=()
if [[ -n "${_lbrs_matched_out[1]}" ]]; then
explode_string "${_lbrs_matched_out[1]}" $'\x1d' _lbrs_group_out
if [[ ${?} -ne 0 ]]; then
error_code ${LIB_BASH_INTERNAL_ERROR}
return ${?}
fi
fi
# Reset outputs
eval "${_lbrs_matched}=''"
eval "${_lbrs_group}=()"
# Matched string
if [[ -n "${_lbrs_matched_out[0]}" ]]; then
local _lbrs_matched_str="$(printf "%b\x1f" "${_lbrs_matched_out[0]}")"
eval "${_lbrs_matched}=\"\${_lbrs_matched_str%\$'\x1f'}\""
fi
# Capture groups
if [[ ${#_lbrs_group_out[@]} -gt 0 ]]; then
local _lbrs_group_str=''
for _lbrs_group_str in "${_lbrs_group_out[@]}"; do
_lbrs_group_str="$(printf "%b\x1f" "${_lbrs_group_str}" | escape_system)"
eval "${_lbrs_group}+=(\"${_lbrs_group_str%$'\x1f'}\")"
done
fi
return 0
}
################################################################################
# Replace string with given regular expression.
#
# Usage: replace_string <string> <pattern> <subsitution> <modifier>
# <string_out>
#
# Parameters:
# string [in] The string to replace.
# pattern [in] The regular expression, it cannot be empty.
# subsitution [in] The replacing string.
# modifier [in] The regular expression modifier.
# string_out [out] The replaced stirng.
#
# Returns:
# ${LIB_BASH_ERROR_INVALID_PARAM}
# ${LIB_BASH_ERROR_NO_OUTPUT}
# ${LIB_BASH_INVALID_REGEX}
#
# See http://perldoc.perl.org/perlre.html for more information.
################################################################################
function replace_string()
{
local _lbrs_string="$(printf "%b\x1f" "${1}" | escape_perl)"
local _lbrs_pattern="${2}"
local _lbrs_replace="${3}"
local _lbrs_modifier="${4}"
local _lbrs_replace_out=${5}
if [[ ${#} -lt 4 ]] || [[ -z "${_lbrs_pattern}" ]]; then
error_code ${LIB_BASH_ERROR_INVALID_PARAM}
return ${?}
fi
if [[ -z "${_lbrs_replace_out}" ]]; then
error_code ${LIB_BASH_ERROR_NO_OUTPUT}
return ${?}
fi
_lbrs_perl_out="$(perl -CS -e \
'use utf8;
$_="'"${_lbrs_string%$'\x1f'}"'";
s/'"${_lbrs_pattern}"'/'"${_lbrs_replace}"'/'"${_lbrs_modifier}"';
$\="\x1f";
print $_;' 2>/dev/null)"
if [[ ${?} -ne 0 ]]; then
unset _lbrs_perl_out
error_code ${LIB_BASH_INVALID_REGEX}
return ${?}
fi
eval "${_lbrs_replace_out}=\"\${_lbrs_perl_out%\$'\x1f'}\""
unset _lbrs_perl_out
return 0
}