summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKerin Millar <kfm@plushkava.net>2024-08-17 16:42:41 +0100
committerKerin Millar <kfm@plushkava.net>2024-08-22 20:37:46 +0100
commitf77a397df8c2a623a296600ee38a01dbdd98933b (patch)
tree6fae154684964fa2b9e902ee9db9a2f01e839010
parentMove an SC2317 exemption closer to where it is needed (diff)
downloadgentoo-functions-f77a397df8c2a623a296600ee38a01dbdd98933b.tar.gz
gentoo-functions-f77a397df8c2a623a296600ee38a01dbdd98933b.tar.bz2
gentoo-functions-f77a397df8c2a623a296600ee38a01dbdd98933b.zip
Optimise trim() for bash where processing the positional parameters
Render trim() faster in bash for cases where only the positional parameters are to be processed e.g. var=$(trim "$var") or var=${ trim "$var"; }. Signed-off-by: Kerin Millar <kfm@plushkava.net>
-rw-r--r--functions.sh19
1 files changed, 14 insertions, 5 deletions
diff --git a/functions.sh b/functions.sh
index bd96df6..3dd2969 100644
--- a/functions.sh
+++ b/functions.sh
@@ -650,12 +650,21 @@ srandom()
#
trim()
{
- if [ "$#" -gt 0 ]; then
- printf '%s\n' "$@"
+ local arg
+
+ if [ "$#" -gt 0 ] && [ "${BASH}" ]; then
+ for arg; do
+ eval '[[ ${arg} =~ ^[[:space:]]+ ]] && arg=${arg:${#BASH_REMATCH}}'
+ eval '[[ ${arg} =~ [[:space:]]+$ ]] && arg=${arg:0:${#arg} - ${#BASH_REMATCH}}'
+ printf '%s\n' "${arg}"
+ done
else
- cat
- fi |
- sed -e 's/^[[:space:]]\{1,\}//' -e 's/[[:space:]]\{1,\}$//'
+ if [ "$#" -gt 0 ]; then
+ printf '%s\n' "$@"
+ else
+ cat
+ fi | sed -e 's/^[[:space:]]\{1,\}//' -e 's/[[:space:]]\{1,\}$//'
+ fi
}
#