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 6328A1581C1 for ; Sun, 7 Jul 2024 05:55:42 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id E0A6BE2A56; Sun, 7 Jul 2024 05:55:40 +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) server-digest SHA256) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id C22AAE2A56 for ; Sun, 7 Jul 2024 05:55:40 +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) server-digest SHA256) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id BCE65340C7C for ; Sun, 7 Jul 2024 05:55:39 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 2FA6A1DD5 for ; Sun, 7 Jul 2024 05:55:38 +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: <1719596341.9caeb57e67c48376fbd7d8121ae4d3a9e1888b35.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: 9caeb57e67c48376fbd7d8121ae4d3a9e1888b35 X-VCS-Branch: master Date: Sun, 7 Jul 2024 05:55:38 +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: fc4998f5-aba7-453b-9c52-7f1588687824 X-Archives-Hash: b935947222ffce20cf7b960189270d2d commit: 9caeb57e67c48376fbd7d8121ae4d3a9e1888b35 Author: Kerin Millar plushkava net> AuthorDate: Thu Jun 27 15:15:51 2024 +0000 Commit: Sam James gentoo org> CommitDate: Fri Jun 28 17:39:01 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=9caeb57e Add the substr() function POSIX sh does not support substring expansion so it may come in handy. The implementation is based on the awk function of the same name. Signed-off-by: Kerin Millar plushkava.net> functions.sh | 41 +++++++++++++++++++++++++++++++++++++++++ test-functions | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/functions.sh b/functions.sh index 798c6c7..43a21e4 100644 --- a/functions.sh +++ b/functions.sh @@ -438,6 +438,47 @@ srandom() srandom } +# +# Takes the first parameter as a string (s), the second parameter as a numerical +# position (m) and, optionally, the third parameter as a numerical length (n). +# It shall then print a terminated substring of s that is at most, n +# characters in length and which begins at position m, numbering from 1. If n is +# omitted, or if n specifies more characters than are left in the string, the +# length of the substring shall be limited by the length of s. The function +# shall return 0 provided that none of the parameters are invalid. +# +substr() +{ + local i str + + if [ "$#" -lt 2 ]; then + warn "substr: too few arguments (got $#, expected at least 2)" + return 1 + elif ! is_int "$2"; then + _warn_for_args substr "$2" + return 1 + elif [ "$#" -ge 3 ]; then + if ! is_int "$3"; then + _warn_for_args substr "$3" + return 1 + elif [ "$3" -lt 0 ]; then + set -- "$1" "$2" 0 + fi + fi + str=$1 + i=0 + while [ "$(( i += 1 ))" -lt "$2" ]; do + str=${str#?} + done + i=0 + while [ "${#str}" -gt "${3-${#str}}" ]; do + str=${str%?} + done + if [ "${#str}" -gt 0 ]; then + printf '%s\n' "${str}" + fi +} + # # 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 diff --git a/test-functions b/test-functions index 13be61c..736836b 100755 --- a/test-functions +++ b/test-functions @@ -664,6 +664,44 @@ test_trueof_any() { iterate_tests 7 "$@" } +test_substr() { + set -- \ + ge 1 - foobar N/A N/A \ + ge 1 - foobar '' N/A \ + ge 1 - foobar x N/A \ + ge 1 - foobar '' '' \ + ge 1 - foobar x y \ + eq 0 foobar foobar 1 N/A \ + eq 0 foobar foobar -1 N/A \ + eq 0 foobar foobar 1 7 \ + eq 0 foobar foobar -1 7 \ + eq 0 foo foobar 1 3 \ + eq 0 foo foobar -1 3 \ + eq 0 f foobar 1 1 \ + eq 0 f foobar -1 1 \ + eq 0 '' foobar 1 0 \ + eq 0 '' foobar 1 -1 \ + eq 0 '' foobar 0 0 \ + eq 0 '' foobar 0 -1 \ + eq 0 '' foobar -1 0 \ + eq 0 '' foobar -1 -1 \ + eq 0 bar foobar 4 N/A \ + eq 0 bar foobar 4 4 \ + eq 0 b foobar 4 1 \ + eq 0 '' foobar 4 0 \ + eq 0 '' foobar 4 -1 + + callback() { + shift + expected=$1 + shift + test_description="substr $(quote_args "$@")" + str=$(substr "$@") && test "${str}" = "${expected}" + } + + iterate_tests 6 "$@" +} + iterate_tests() { slice_width=$1 shift @@ -738,6 +776,7 @@ test_is_anyof || rc=1 test_is_subset || rc=1 test_trueof_all || rc=1 test_trueof_any || rc=1 +test_substr || rc=1 cleanup_tmpdir