-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_str_replace.sh
More file actions
30 lines (28 loc) · 1.18 KB
/
_str_replace.sh
File metadata and controls
30 lines (28 loc) · 1.18 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
. "$AGLIB"/_str_escape.sh
_str_replace() {
__strrep_str="$1"
__strrep_substr="$2"
__strrep_repstr="$3"
__strrep_limit="${4:-0}"
__strrep_method="${5:-paramsub}"
case $__strrep_method in
paramsub) __strrep_newstr=
while :; do
__strrep_remain="${__strrep_str#*"${__strrep_substr}"}"
case "$__strrep_str" in "$__strrep_remain") __strrep_newstr="${__strrep_newstr}${__strrep_str}"; break;; esac
__strrep_newstr="${__strrep_newstr}${__strrep_str%"${__strrep_substr}${__strrep_remain}"}${__strrep_repstr}"
case $__strrep_limit in 0) :;; *)
__strrep_i=$((${__strrep_i=0} + 1))
case $__strrep_i in $__strrep_limit) __strrep_newstr="${__strrep_newstr}${__strrep_str}"; break; esac
esac
__strrep_str="$__strrep_remain"
done
printf %s "${__strrep_newstr}"
unset __strrep_newstr __strrep_remain __strrep_i ;;
sed)
printf %s "$__strrep_str" \
| sed -e s/"$(_str_escape "$__strrep_substr" -- \/ \[ \^ \$ \( \. \* \+ \? \{)"/"$(_str_escape "$__strrep_repstr" -- /)"/g ;;
*) return 5 ;;
esac
unset __strrep_str __strrep_substr __strrep_repstr __strrep_limit __strrep_method
}