From: "Sam James" <sam@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/gentoo-functions:master commit in: /, functions/
Date: Sun, 7 Jul 2024 05:55:38 +0000 (UTC) [thread overview]
Message-ID: <1719801118.feaa7438ef8c749179bf5fb99f93a3683e6d40fd.sam@gentoo> (raw)
commit: feaa7438ef8c749179bf5fb99f93a3683e6d40fd
Author: Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sun Jun 30 23:15:36 2024 +0000
Commit: Sam James <sam <AT> gentoo <DOT> 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 <kfm <AT> 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 <newline> 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 <newline> 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
next reply other threads:[~2024-07-07 5:55 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-07 5:55 Sam James [this message]
-- strict thread matches above, loose matches on Subject: below --
2024-07-07 5:55 [gentoo-commits] proj/gentoo-functions:master commit in: /, functions/ Sam James
2024-08-02 23:14 Sam James
2024-08-11 10:11 Sam James
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1719801118.feaa7438ef8c749179bf5fb99f93a3683e6d40fd.sam@gentoo \
--to=sam@gentoo.org \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox