Revision 13230
Added by Aaron Marcuse-Kubitza over 10 years ago
trunk/lib/sh/util.sh | ||
---|---|---|
126 | 126 |
)" |
127 | 127 |
|
128 | 128 |
|
129 |
#### integers |
|
130 |
|
|
131 |
let!() { let "$@" || true; } # always returns true; safe to use for setting |
|
132 |
# "If the last ARG evaluates to 0, let returns 1" (`help let`) |
|
133 |
|
|
134 |
bool2int() { try test ! "$1"; echo "$e"; } # empty->0; non-empty->1 |
|
135 |
|
|
136 |
int2exit() { (( "$1" != 0 )); } |
|
137 |
|
|
138 |
exit2bool() { if (( $? == 0 )); then echo 1; fi } # 0->non-empty; !=0->empty |
|
139 |
|
|
140 |
|
|
141 |
#### floats |
|
142 |
|
|
143 |
int_part() { echo "${1%%.*}"; } |
|
144 |
|
|
145 |
dec_suffix() { echo "${1#$(int_part "$1")}"; } |
|
146 |
|
|
147 |
round_down() { int_part "$1"; } |
|
148 |
|
|
149 |
float+int() { echo "$(($(int_part "$1")+$2))$(dec_suffix "$1")"; } |
|
150 |
|
|
151 |
float_set_min() { if (($(int_part $1) >= $2)); then echo $1; else echo $2; fi; } |
|
152 |
|
|
153 |
|
|
154 |
#### strings |
|
155 |
|
|
156 |
starts_with() { test "${2#$1}" != "$2"; } # usage: starts_with pattern str |
|
157 |
|
|
158 |
ends_with() { test "${2%$1}" != "$2"; } # usage: ends_with pattern str |
|
159 |
|
|
160 |
match_prefix() # usage: match_prefix pattern str |
|
161 |
{ if starts_with "$1" "$2"; then echo "${2%${2#$1}}"; fi } |
|
162 |
|
|
163 |
rm_prefix() { echo "${2#$1}"; } # usage: rm_prefix pattern str |
|
164 |
|
|
165 |
contains_match() { starts_with "*$1" "$2"; } # usage: contains_match pattern str |
|
166 |
|
|
167 |
repeat() # usage: str=... n=... repeat |
|
168 |
{ |
|
169 |
: "${str?}" "${n:?}"; local result= n="$n" # n will be modified in function |
|
170 |
for (( ; n > 0; n-- )); do result="$result$str"; done |
|
171 |
echo "$result" |
|
172 |
} |
|
173 |
|
|
174 |
# usage: outer_cmd $sed_cmd ... |
|
175 |
sed_cmd="env LANG= sed -`case "$(uname)" in Darwin) echo El;; *) echo ru;;esac`" |
|
176 |
# LANG: avoid invalid UTF-8 "illegal byte sequence" errors when LANG=*.UTF-8 |
|
177 |
# -l: line buffered / -u: unbuffered |
|
178 |
alias sed="$sed_cmd" |
|
179 |
# can't be function because this causes segfault in redir() subshell when |
|
180 |
# used with make.sh make() filter |
|
181 |
|
|
182 |
fi # load new aliases |
|
183 |
if self_being_included; then |
|
184 |
|
|
185 |
rtrim() { log_local; log+ 3; sed 's/[[:space:]]+$//' <<<"$1"; } |
|
186 |
|
|
187 |
|
|
188 |
#### arrays |
|
189 |
|
|
190 |
echo1() { echo "$1"; } # usage: echo1 values... |
|
191 |
|
|
192 |
esc() # usage: array=($(log++; prep_env... (eg. cd); esc args...)) |
|
193 |
{ local arg; for arg in "$@"; do printf '%q ' "$arg"; done; } |
|
194 |
|
|
195 |
# usage: split delim str; use ${parts[@]} |
|
196 |
function split() { local IFS="$1"; parts=($2); echo_vars parts; } |
|
197 |
# no echo_func because used by log++ |
|
198 |
alias split='declare parts; "split"' |
|
199 |
|
|
200 |
join() { local IFS="$delim"; echo "$*"; } # usage: delim=char join elems... |
|
201 |
|
|
202 |
reverse() # usage: array=($(reverse args...)) |
|
203 |
{ |
|
204 |
local i |
|
205 |
for (( i=$#; i > 0; i-- )); do printf '%q ' "${!i}"; done |
|
206 |
} |
|
207 |
|
|
208 |
contains() # usage: contains value in_array... |
|
209 |
{ |
|
210 |
local value="$1"; shift |
|
211 |
local elem |
|
212 |
for elem in "$@"; do if test "$elem" = "$value"; then return 0; fi; done |
|
213 |
return 1 |
|
214 |
} |
|
215 |
|
|
216 |
is_array() # handles unset vars (=false) |
|
217 |
{ |
|
218 |
isset "$1" || return 1 |
|
219 |
local decl; decl="$(declare -p "$1")" || return; echo_vars decl |
|
220 |
starts_with 'declare -a' "$decl" # also matches -ax |
|
221 |
} |
|
222 |
|
|
223 |
|
|
129 | 224 |
#### caching |
130 | 225 |
|
131 | 226 |
## shell-variable-based caching |
... | ... | |
308 | 403 |
if self_being_included; then |
309 | 404 |
|
310 | 405 |
|
311 |
#### integers |
|
312 |
|
|
313 |
let!() { let "$@" || true; } # always returns true; safe to use for setting |
|
314 |
# "If the last ARG evaluates to 0, let returns 1" (`help let`) |
|
315 |
|
|
316 |
bool2int() { try test ! "$1"; echo "$e"; } # empty->0; non-empty->1 |
|
317 |
|
|
318 |
int2exit() { (( "$1" != 0 )); } |
|
319 |
|
|
320 |
exit2bool() { if (( $? == 0 )); then echo 1; fi } # 0->non-empty; !=0->empty |
|
321 |
|
|
322 |
|
|
323 |
#### floats |
|
324 |
|
|
325 |
int_part() { echo "${1%%.*}"; } |
|
326 |
|
|
327 |
dec_suffix() { echo "${1#$(int_part "$1")}"; } |
|
328 |
|
|
329 |
round_down() { int_part "$1"; } |
|
330 |
|
|
331 |
float+int() { echo "$(($(int_part "$1")+$2))$(dec_suffix "$1")"; } |
|
332 |
|
|
333 |
float_set_min() { if (($(int_part $1) >= $2)); then echo $1; else echo $2; fi; } |
|
334 |
|
|
335 |
|
|
336 |
#### strings |
|
337 |
|
|
338 |
starts_with() { test "${2#$1}" != "$2"; } # usage: starts_with pattern str |
|
339 |
|
|
340 |
ends_with() { test "${2%$1}" != "$2"; } # usage: ends_with pattern str |
|
341 |
|
|
342 |
match_prefix() # usage: match_prefix pattern str |
|
343 |
{ if starts_with "$1" "$2"; then echo "${2%${2#$1}}"; fi } |
|
344 |
|
|
345 |
rm_prefix() { echo "${2#$1}"; } # usage: rm_prefix pattern str |
|
346 |
|
|
347 |
contains_match() { starts_with "*$1" "$2"; } # usage: contains_match pattern str |
|
348 |
|
|
349 |
repeat() # usage: str=... n=... repeat |
|
350 |
{ |
|
351 |
: "${str?}" "${n:?}"; local result= n="$n" # n will be modified in function |
|
352 |
for (( ; n > 0; n-- )); do result="$result$str"; done |
|
353 |
echo "$result" |
|
354 |
} |
|
355 |
|
|
356 |
# usage: outer_cmd $sed_cmd ... |
|
357 |
sed_cmd="env LANG= sed -`case "$(uname)" in Darwin) echo El;; *) echo ru;;esac`" |
|
358 |
# LANG: avoid invalid UTF-8 "illegal byte sequence" errors when LANG=*.UTF-8 |
|
359 |
# -l: line buffered / -u: unbuffered |
|
360 |
alias sed="$sed_cmd" |
|
361 |
# can't be function because this causes segfault in redir() subshell when |
|
362 |
# used with make.sh make() filter |
|
363 |
|
|
364 |
fi # load new aliases |
|
365 |
if self_being_included; then |
|
366 |
|
|
367 |
rtrim() { log_local; log+ 3; sed 's/[[:space:]]+$//' <<<"$1"; } |
|
368 |
|
|
369 |
|
|
370 |
#### arrays |
|
371 |
|
|
372 |
echo1() { echo "$1"; } # usage: echo1 values... |
|
373 |
|
|
374 |
esc() # usage: array=($(log++; prep_env... (eg. cd); esc args...)) |
|
375 |
{ local arg; for arg in "$@"; do printf '%q ' "$arg"; done; } |
|
376 |
|
|
377 |
# usage: split delim str; use ${parts[@]} |
|
378 |
function split() { local IFS="$1"; parts=($2); echo_vars parts; } |
|
379 |
# no echo_func because used by log++ |
|
380 |
alias split='declare parts; "split"' |
|
381 |
|
|
382 |
join() { local IFS="$delim"; echo "$*"; } # usage: delim=char join elems... |
|
383 |
|
|
384 |
reverse() # usage: array=($(reverse args...)) |
|
385 |
{ |
|
386 |
local i |
|
387 |
for (( i=$#; i > 0; i-- )); do printf '%q ' "${!i}"; done |
|
388 |
} |
|
389 |
|
|
390 |
contains() # usage: contains value in_array... |
|
391 |
{ |
|
392 |
local value="$1"; shift |
|
393 |
local elem |
|
394 |
for elem in "$@"; do if test "$elem" = "$value"; then return 0; fi; done |
|
395 |
return 1 |
|
396 |
} |
|
397 |
|
|
398 |
is_array() # handles unset vars (=false) |
|
399 |
{ |
|
400 |
isset "$1" || return 1 |
|
401 |
local decl; decl="$(declare -p "$1")" || return; echo_vars decl |
|
402 |
starts_with 'declare -a' "$decl" # also matches -ax |
|
403 |
} |
|
404 |
|
|
405 |
|
|
406 | 406 |
#### text |
407 | 407 |
|
408 |
fi # load new aliases |
|
409 |
if self_being_included; then |
|
410 |
|
|
411 | 408 |
nl=' |
412 | 409 |
' |
413 | 410 |
|
Also available in: Unified diff
lib/sh/util.sh: moved primitives sections before more complex sections that depend on them