From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <gentoo-commits+bounces-1645362-garchives=archives.gentoo.org@lists.gentoo.org> 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 0B4C015817D for <garchives@archives.gentoo.org>; Fri, 21 Jun 2024 13:14:21 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 3DBF3E2A52; Fri, 21 Jun 2024 13:14:19 +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 A1D4CE2A4C for <gentoo-commits@lists.gentoo.org>; Fri, 21 Jun 2024 13:14:18 +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 87656335D0F for <gentoo-commits@lists.gentoo.org>; Fri, 21 Jun 2024 13:14:17 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id A35611D4F for <gentoo-commits@lists.gentoo.org>; Fri, 21 Jun 2024 13:14:14 +0000 (UTC) From: "Sam James" <sam@gentoo.org> 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" <sam@gentoo.org> Message-ID: <1718176002.302084ee5cb39610378d1f963bb16adc7a78abbc.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh test-functions X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: 302084ee5cb39610378d1f963bb16adc7a78abbc X-VCS-Branch: master Date: Fri, 21 Jun 2024 13:14:14 +0000 (UTC) Precedence: bulk List-Post: <mailto:gentoo-commits@lists.gentoo.org> List-Help: <mailto:gentoo-commits+help@lists.gentoo.org> List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org> List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org> List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org> X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 95fa2ef1-f820-4b8a-83aa-adddd99d6bb5 X-Archives-Hash: 7a2e004b5d448fdcc6175f49ba0c9ebb commit: 302084ee5cb39610378d1f963bb16adc7a78abbc Author: Kerin Millar <kfm <AT> plushkava <DOT> net> AuthorDate: Mon Jun 3 01:21:52 2024 +0000 Commit: Sam James <sam <AT> gentoo <DOT> org> CommitDate: Wed Jun 12 07:06:42 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=302084ee Add the trim() function As based on the implementation in Maarten Billemont's bashlib library. Signed-off-by: Kerin Millar <kfm <AT> plushkava.net> functions.sh | 16 ++++++++++++++++ test-functions | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/functions.sh b/functions.sh index 1548bd0..30c2447 100644 --- a/functions.sh +++ b/functions.sh @@ -513,6 +513,22 @@ srandom() srandom } +# +# Trims leading and trailing whitespace from one or more lines. If at least one +# parameter is provided, each positional parameter shall be considered as a line +# to be processed. Otherwise, the lines to be processed shall be read from the +# standard input. The trimmed lines shall be printed to the standard output. +# +trim() +{ + if [ "$#" -gt 0 ]; then + printf '%s\n' "$@" + else + cat + fi | + sed -e 's/^[[:space:]]\{1,\}//' -e 's/[[:space:]]\{1,\}$//' +} + # # Prints a diagnostic message prefixed with the basename of the running script. # diff --git a/test-functions b/test-functions index 400ddb2..bbb74f1 100755 --- a/test-functions +++ b/test-functions @@ -456,6 +456,34 @@ test_newest() { iterate_tests 4 "$@" } +test_trim() { + set -- \ + eq 0 '' '' \ + eq 0 ' ' '' \ + eq 0 ' ' '' \ + eq 0 ' X' 'X' \ + eq 0 ' X' 'X' \ + eq 0 'X ' 'X' \ + eq 0 ' X Y' 'X Y' \ + eq 0 ' X Y' 'X Y' \ + eq 0 'X Y ' 'X Y' \ + eq 0 'X Y ' 'X Y' \ + eq 0 "$(printf ' \tX')" 'X' \ + eq 0 "$(printf ' \tX\t ')" 'X' \ + eq 0 "$(printf 'X\t ')" 'X' \ + eq 0 "$(printf ' \tX Y')" 'X Y' \ + eq 0 "$(printf ' \tX Y\t ')" 'X Y' \ + eq 0 "$(printf 'X Y\t ')" 'X Y' + + callback() { + shift + test_description="trim $(_print_args "$1") (expecting $(_print_args "$2"))" + test "$(trim "$1")" = "$2" + } + + iterate_tests 4 "$@" +} + iterate_tests() { slice_width=$1 shift @@ -518,6 +546,7 @@ test_die || rc=1 test_edo || rc=1 test_srandom || rc=1 test_newest || rc=1 +test_trim || rc=1 cleanup_tmpdir