2 if [ "$BASH_VERSION" ]; then
3 substr() # $var_to_get $start $length $var_to_set
5 eval $4=\"\${$1:\$2${3:+:\$3}}\"
8 substr() # $var_to_get $start $length $var_to_set
10 eval local __tmp=\"\$1\" || return
11 local __start="${2:-0}" __len="$3"
12 local __tbuf __tbuf_len __trim __trimq
14 if [ ! "$__tmp" ]; then
18 if [ "$__start" -lt 0 ] 2> /dev/null; then
19 __start=$(( ${#__tmp} + $__start ))
20 if [ $__start -lt 0 ]; then
24 elif [ "$__start" -ge 0 ] 2> /dev/null; then
29 if [ ! "$__len" ]; then
30 if [ $__start -eq 0 ]; then
34 __len=$(( ${#__tmp} - $__start ))
35 elif [ "$__len" -lt 0 ] 2> /dev/null; then
36 __len=$(( ${#__tmp} - $__start + $__len ))
37 elif [ "$__len" -ge 1 ] 2> /dev/null; then
45 while [ $__trim -gt 0 ]; do
48 while [ $__tbuf_len -lt $(( $__trim / $__tbuf_len )) ]
51 __tbuf_len=$(( $__tbuf_len + 1 ))
53 __trimq=$(( $__trim / $__tbuf_len ))
54 __trim=$(( $__trim - $__tbuf_len * $__trimq ))
55 while [ $__trimq -gt 0 ]; do
56 __tmp="${__tmp#$__tbuf}"
57 __trimq=$(( $__trimq - 1 ))
61 local __tmp_len=${#__tmp}
62 local __mask __mask_len
63 __trim=$(( $__tmp_len - ${__len:-$__tmp_len} ))
64 while [ $__trim -gt 0 ]; do
67 if [ $__trim -le $__len ]; then
68 while [ $__tbuf_len -lt $((
72 __tbuf_len=$(( $__tbuf_len + 1 ))
74 __trimq=$(( $__trim / $__tbuf_len ))
75 __trim=$(( $__trim - $__tbuf_len * $__trimq ))
76 while [ $__trimq -gt 0 ]; do
77 __tmp="${__tmp%$__tbuf}"
78 __trimq=$(( $__trimq - 1 ))
82 while [ $__tbuf_len -lt $((
86 __tbuf_len=$(( $__tbuf_len + 1 ))
88 __trimq=$(( $__len / $__tbuf_len ))
89 if [ $__len -ne $(( $__trimq * $__tbuf_len )) ]
92 __tbuf_len=$(( $__tbuf_len + 1 ))
95 $__tmp_len - $__tbuf_len * $__trimq
97 __trim=$(( $__tmp_len - $__mask_len - $__len ))
98 while [ $__trimq -gt 0 ]; do
99 __mask="${__mask#$__tbuf}"
100 __trimq=$(( $__trimq - 1 ))
102 __tmp="${__tmp%"$__mask"}"
110 eval echo \"$4=[\$4]\"