From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id DAAA415808B for ; Sat, 5 Oct 2024 04:15:29 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 8175EE29ED; Sat, 5 Oct 2024 04:15:27 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 6756FE29ED for ; Sat, 5 Oct 2024 04:15:27 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 85D78340C7C for ; Sat, 5 Oct 2024 04:15:26 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id B97C727F8 for ; Sat, 5 Oct 2024 04:15:22 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1724355466.f77a397df8c2a623a296600ee38a01dbdd98933b.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: f77a397df8c2a623a296600ee38a01dbdd98933b X-VCS-Branch: master Date: Sat, 5 Oct 2024 04:15:22 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 8255b306-eb01-46ae-98a0-58d7f23598c5 X-Archives-Hash: 5f8372f4393ecc44e16b6f11e3ac5a4a commit: f77a397df8c2a623a296600ee38a01dbdd98933b Author: Kerin Millar plushkava net> AuthorDate: Sat Aug 17 15:42:41 2024 +0000 Commit: Sam James gentoo org> CommitDate: Thu Aug 22 19:37:46 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=f77a397d 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 plushkava.net> functions.sh | 19 ++++++++++++++----- 1 file 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 } #