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 27F7C1581C1 for ; Sun, 7 Jul 2024 05:55:44 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id CDE58E2A6E; Sun, 7 Jul 2024 05:55:42 +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 A9821E2A6A for ; Sun, 7 Jul 2024 05:55:42 +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 72780340CA7 for ; Sun, 7 Jul 2024 05:55:41 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id D98D31DDE 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: <1719801118.feaa7438ef8c749179bf5fb99f93a3683e6d40fd.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: /, functions/ X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh functions/experimental.sh test-functions X-VCS-Directories: / functions/ X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: feaa7438ef8c749179bf5fb99f93a3683e6d40fd 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: cfc7e2a2-a3e3-424a-a192-0808eedd8433 X-Archives-Hash: 7ab68d49e9b4a73d902d8106a26d2629 commit: feaa7438ef8c749179bf5fb99f93a3683e6d40fd Author: Kerin Millar plushkava net> AuthorDate: Sun Jun 30 23:15:36 2024 +0000 Commit: Sam James gentoo org> CommitDate: Mon Jul 1 02:31:58 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=feaa7438 Move substr() to experimental Though it works very well, I'm not yet ready to commit to it being among the core functions for the inaugural API level. Signed-off-by: Kerin Millar plushkava.net> functions.sh | 41 ----------------------------------------- functions/experimental.sh | 41 +++++++++++++++++++++++++++++++++++++++++ test-functions | 2 +- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/functions.sh b/functions.sh index 087b62d..1926c40 100644 --- a/functions.sh +++ b/functions.sh @@ -436,47 +436,6 @@ 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/functions/experimental.sh b/functions/experimental.sh index 1aac078..4d56cfa 100644 --- a/functions/experimental.sh +++ b/functions/experimental.sh @@ -94,6 +94,47 @@ str_between() fi } +# +# 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 +} + # # Takes the first parameter as either a relative pathname or an integer # referring to a number of iterations. To be recognised as a pathname, the first diff --git a/test-functions b/test-functions index 68e73eb..34ff54a 100755 --- a/test-functions +++ b/test-functions @@ -793,7 +793,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 +#test_substr || rc=1 cleanup_tmpdir