Skip to content

Commit 0a8727c

Browse files
committed
shorten
Signed-off-by: Harmen Stoppels <me@harmenstoppels.nl>
1 parent bbc8dee commit 0a8727c

1 file changed

Lines changed: 30 additions & 49 deletions

File tree

cc.sh

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -114,91 +114,72 @@ append() {
114114
fi
115115
}
116116

117-
# extend LISTNAME1 LISTNAME2 [PREFIX]
117+
# extend DST_LISTNAME SRC_LISTNAME [PREFIX]
118118
#
119-
# Append the elements stored in the variable LISTNAME2
120-
# to the list stored in LISTNAME1.
119+
# Append the elements stored in the variable SRC_LISTNAME
120+
# to the list stored in DST_LISTNAME.
121121
# If PREFIX is provided, prepend it to each element.
122122
extend() {
123-
_listname="$1"
124-
_src_list="$2"
123+
_dst="$1"
124+
_src="$2"
125125
_prefix="$3"
126126

127-
setsep "$_src_list"
128-
if [ "$sep" != " " ]; then
129-
IFS="$sep"
130-
fi
131-
# Turn list into positional parameters
132-
eval "set -- \${$_src_list}"
127+
# Turn source list into positional parameters
128+
setsep "$_src"
129+
[ "$sep" != " " ] && IFS="$sep"
130+
eval "set -- \${$_src}"
133131
unset IFS
134132

135-
if [ $# -eq 0 ]; then
136-
return
137-
fi
133+
[ $# -eq 0 ] && return
138134

139-
setsep "$_listname"
140-
_target_sep="$sep"
135+
setsep "$_dst"; _dst_sep="$sep"
141136

142137
if [ -z "$_prefix" ]; then
143-
IFS="$_target_sep"
144-
_ext_str="$*"
145-
unset IFS
138+
# Fast concatenation when no prefix is needed
139+
IFS="$_dst_sep"; _ext_str="$*"; unset IFS
146140
else
147141
_ext_str="${_prefix}$1"
148142
shift
149143
for elt in "$@"; do
150-
_ext_str="${_ext_str}${_target_sep}${_prefix}${elt}"
144+
_ext_str="${_ext_str}${_dst_sep}${_prefix}${elt}"
151145
done
152146
fi
153147

154-
if empty "$_listname"; then
155-
eval "$_listname=\"\${_ext_str}\""
156-
else
157-
eval "$_listname=\"\${$_listname}${_target_sep}\${_ext_str}\""
158-
fi
148+
eval "$_dst=\"\${$_dst:+\${$_dst}$_dst_sep}\${_ext_str}\""
159149
}
160150

161-
# preextend LISTNAME1 LISTNAME2 [PREFIX]
151+
# preextend DST_LISTNAME SRC_LISTNAME [PREFIX]
162152
#
163-
# Prepend the elements stored in the list at LISTNAME2
164-
# to the list at LISTNAME1, preserving order.
153+
# Prepend the elements stored in the list at SRC_LISTNAME
154+
# to the list at DST_LISTNAME, preserving order.
165155
# If PREFIX is provided, prepend it to each element.
166156
preextend() {
167-
_listname="$1"
168-
_src_list="$2"
157+
_dst="$1"
158+
_src="$2"
169159
_prefix="$3"
170160

171-
setsep "$_src_list"
172-
if [ "$sep" != " " ]; then
173-
IFS="$sep"
174-
fi
175-
eval "set -- \${$_src_list}"
161+
# Turn source list into positional parameters
162+
setsep "$_src"
163+
[ "$sep" != " " ] && IFS="$sep"
164+
eval "set -- \${$_src}"
176165
unset IFS
177166

178-
if [ $# -eq 0 ]; then
179-
return
180-
fi
167+
[ $# -eq 0 ] && return
181168

182-
setsep "$_listname"
183-
_target_sep="$sep"
169+
setsep "$_dst"; _dst_sep="$sep"
184170

185171
if [ -z "$_prefix" ]; then
186-
IFS="$_target_sep"
187-
_ext_str="$*"
188-
unset IFS
172+
# Fast concatenation when no prefix is needed
173+
IFS="$_dst_sep"; _ext_str="$*"; unset IFS
189174
else
190175
_ext_str="${_prefix}$1"
191176
shift
192177
for elt in "$@"; do
193-
_ext_str="${_ext_str}${_target_sep}${_prefix}${elt}"
178+
_ext_str="${_ext_str}${_dst_sep}${_prefix}${elt}"
194179
done
195180
fi
196181

197-
if empty "$_listname"; then
198-
eval "$_listname=\"\${_ext_str}\""
199-
else
200-
eval "$_listname=\"\${_ext_str}${_target_sep}\${$_listname}\""
201-
fi
182+
eval "$_dst=\"\${_ext_str}\${$_dst:+$_dst_sep\${$_dst}}\""
202183
}
203184

204185
# END list functions

0 commit comments

Comments
 (0)