From: "Sam James" <sam@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/gentoo-functions:master commit in: /
Date: Sun, 7 Jul 2024 05:55:38 +0000 (UTC) [thread overview]
Message-ID: <1719801147.2a0c3ce54dda9e72310745d04960bcea7071fc4e.sam@gentoo> (raw)
commit: 2a0c3ce54dda9e72310745d04960bcea7071fc4e
Author: Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Mon Jul 1 01:34:25 2024 +0000
Commit: Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Mon Jul 1 02:32:27 2024 +0000
URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=2a0c3ce5
Add the contains_all() and contains_any() functions
Here are some examples which presume the default value of IFS.
contains_all " cat mat " cat dog # returns 1
contains_all " cat mat " mat cat # returns 0
contains_any " cat mat " cat dog # returns 0
contains_any " cat mat " dog # returns 1
Here are some examples showing that IFS is taken into account.
IFS=, contains_all "cat,mat" cat dog # returns 1
IFS=, contains_all "cat,mat" mat cat # returns 0
IFS=, contains_any "cat,mat" cat dog # returns 0
IFS=, contains_any "cat,mat" dog # returns 1
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
functions.sh | 117 +++++++++++++++++++++++++++++++++++++++++++++------------
test-functions | 80 +++++++++++++++++++++++++++++++++++++++
2 files changed, 172 insertions(+), 25 deletions(-)
diff --git a/functions.sh b/functions.sh
index 1926c40..a4fa946 100644
--- a/functions.sh
+++ b/functions.sh
@@ -17,7 +17,7 @@
# COLUMNS : may be used by _update_columns() to get the column count
# EPOCHREALTIME : potentially used by _update_time() to get the time
# GENFUN_MODULES : which of the optional function collections must be sourced
-# IFS : multiple warn() operands are joined by its first character
+# IFS : affects contains_all(), contains_any() and warn()
# INVOCATION_ID : used by from_unit()
# PORTAGE_BIN_PATH : used by from_portage()
# RC_OPENRC_PID : used by from_runscript()
@@ -48,6 +48,96 @@ chdir()
CDPATH= cd -- "$@"
}
+#
+# Takes the first parameter as a string comprising zero or more words, composes
+# a set consisting of the intersection of those words, then determines whether
+# the intersection of the remaining parameters forms a subset thereof. The
+# words shall be collected by splitting the string into individual fields, in
+# accordance with section 2.6.5 of the Shell Command Language specification.
+# Therefore, the value of IFS shall be taken into account. If fewer than two
+# parameters are provided, or if the first parameter yields no fields, or if the
+# second set is disjoint from - or a superset of - the first, the return value
+# shall be greater than 0.
+#
+contains_all()
+{
+ [ "$#" -ge 2 ] && IFS=${IFS} awk -f - -- "$@" <<-'EOF'
+ BEGIN {
+ ifs = ENVIRON["IFS"]
+ haystack = ARGV[1]
+ argc = ARGC
+ ARGC = 1
+ if (length(ifs) == 0) {
+ FS = "^"
+ } else if (length(ifs) != 3 || ifs ~ /[^ \t\n]/) {
+ # Split by the first character of IFS.
+ FS = "[" substr(ifs, 1, 1) "]"
+ } else {
+ # Mimic default field splitting behaviour, per section 2.6.5.
+ FS = "[ \t\n]+"
+ sub("^" FS, "", haystack)
+ }
+ # In sh, fields are terminated, not separated.
+ sub(FS "$", "", haystack)
+ len = split(haystack, words)
+ for (i = 1; i <= len; i++) {
+ set2[words[i]]
+ }
+ for (i = 2; i < argc; i++) {
+ set1[ARGV[i]]
+ }
+ for (word in set2) {
+ delete set1[word]
+ }
+ for (word in set1) {
+ exit 1
+ }
+ }
+ EOF
+}
+
+#
+# Takes the first parameter as a string comprising zero or more words then
+# determines whether at least one of the remaining parameters can be matched
+# against any of those words. The words shall be collected by splitting the
+# string into individual fields, in accordance with section 2.6.5 of the Shell
+# Command Language specification. Therefore, the value of IFS shall be taken
+# into account. If fewer than two parameters are provided, or if the first
+# parameter yields no fields, or if none of the following parameters can be
+# matched, the return value shall be greater than 0.
+#
+contains_any()
+{
+ local had_noglob haystack i item needle retval
+
+ [ "$#" -ge 2 ] || return
+ haystack=$1
+ shift
+ i=0
+ case $- in
+ *f*)
+ had_noglob=1
+ ;;
+ *)
+ had_noglob=0
+ esac
+ set -f
+ for needle; do
+ if [ "$(( i += 1 ))" -eq 1 ]; then
+ # shellcheck disable=2086
+ set -- ${haystack}
+ fi
+ for item; do
+ [ "${item}" = "${needle}" ] && break 2
+ done
+ done
+ retval=$?
+ if [ "${had_noglob}" -eq 0 ]; then
+ set +f
+ fi
+ return "${retval}"
+}
+
#
# Considers the first parameter as an URL then attempts to fetch it with either
# curl(1) or wget(1). If the URL does not contain a scheme then the https://
@@ -585,29 +675,6 @@ whenceforth()
#------------------------------------------------------------------------------#
-#
-# Considers the first parameter as containing zero or more blank-separated words
-# then determines whether any of the remaining parameters can be matched in
-# their capacity as discrete words.
-#
-_contains_word()
-{
- local word wordlist
-
- wordlist=$1 word=$2
- case ${word} in
- ''|*[[:blank:]]*)
- ;;
- *)
- case " ${wordlist} " in
- *[[:blank:]]"${word}"[[:blank:]]*)
- return
- ;;
- esac
- esac
- false
-}
-
#
# Determines whether the terminal is a dumb one.
#
@@ -761,7 +828,7 @@ _want_module()
local basename
basename=${1##*/}
- _contains_word "${GENFUN_MODULES}" "${basename%.sh}"
+ contains_any "${GENFUN_MODULES}" "${basename%.sh}"
}
#
diff --git a/test-functions b/test-functions
index 34ff54a..59c0b29 100755
--- a/test-functions
+++ b/test-functions
@@ -719,6 +719,84 @@ test_substr() {
iterate_tests 6 "$@"
}
+test_contains_all() {
+ set -- \
+ ge 1 N/A N/A N/A N/A \
+ ge 1 'foo bar' '' N/A N/A \
+ ge 1 'foo bar' '' ' ' N/A \
+ ge 1 'foo bar' '' ' bar' N/A \
+ ge 1 'foo bar' '' ' bar' N/A \
+ ge 1 'foo bar' '' 'foo ' N/A \
+ ge 1 'foo bar' '' 'foo bar' N/A \
+ ge 1 'foo bar' ' ' '' N/A \
+ ge 1 'foo bar' ' ' ' ' N/A \
+ ge 1 'foo bar' ' ' N/A N/A \
+ ge 1 'foo bar' ' bar' '' N/A \
+ ge 1 'foo bar' ' bar' N/A N/A \
+ ge 1 'foo bar' 'foo ' '' N/A \
+ ge 1 'foo bar' 'foo ' ' bar' N/A \
+ ge 1 'foo bar' 'foo ' N/A N/A \
+ ge 1 'foo bar' 'foo bar' '' N/A \
+ ge 1 'foo bar' 'foo bar' N/A N/A \
+ ge 1 'foo bar' N/A N/A N/A \
+ ge 1 'foo bar' bar foo '' \
+ ge 1 'foo bar' bar foo ' ' \
+ ge 1 'foo bar' baz bar foo \
+ ge 1 'foo bar' fo ba N/A \
+ ge 1 'foo bar' foo bar '' \
+ ge 1 'foo bar' foo bar ' ' \
+ ge 1 'foo bar' foo bar baz \
+ ge 1 'foo bar' o a N/A \
+ ge 1 'foo bar' oo ar N/A \
+ eq 0 'foo bar' foo bar N/A \
+ eq 0 'foo bar' bar foo N/A
+
+ callback() {
+ shift
+ test_description="contains_all $(quote_args "$@")"
+ contains_all "$@"
+ }
+
+ iterate_tests 6 "$@"
+}
+
+test_contains_any() {
+ set -- \
+ ge 1 N/A N/A N/A \
+ ge 1 'foo bar' N/A N/A \
+ ge 1 'foo bar' fo ba \
+ ge 1 'foo bar' oo ar \
+ ge 1 'foo bar' o a \
+ ge 1 'foo bar' 'foo bar' 'foo bar' \
+ ge 1 'foo bar' 'foo bar' _ \
+ ge 1 'foo bar' _ 'foo bar' \
+ ge 1 'foo bar' 'foo ' ' bar' \
+ ge 1 'foo bar' 'foo ' _ \
+ ge 1 'foo bar' _ ' bar' \
+ ge 1 'foo bar' ' bar' _ \
+ ge 1 'foo bar' _ 'foo ' \
+ ge 1 'foo bar' '' '' \
+ ge 1 'foo bar' '' _ \
+ ge 1 'foo bar' _ '' \
+ ge 1 'foo bar' ' ' ' ' \
+ ge 1 'foo bar' ' ' _ \
+ ge 1 'foo bar' _ ' ' \
+ eq 0 'foo bar' foo bar \
+ eq 0 'foo bar' bar foo \
+ eq 0 'foo bar' foo _ \
+ eq 0 'foo bar' _ bar \
+ eq 0 'foo bar' bar _ \
+ eq 0 'foo bar' _ foo
+
+ callback() {
+ shift
+ test_description="contains_any $(quote_args "$@")"
+ contains_any "$@"
+ }
+
+ iterate_tests 5 "$@"
+}
+
iterate_tests() {
slice_width=$1
shift
@@ -794,6 +872,8 @@ test_is_subset || rc=1
test_trueof_all || rc=1
test_trueof_any || rc=1
#test_substr || rc=1
+test_contains_all || rc=1
+test_contains_any || rc=1
cleanup_tmpdir
next reply other threads:[~2024-07-07 5:55 UTC|newest]
Thread overview: 286+ 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 --
2025-05-13 0:30 [gentoo-commits] proj/gentoo-functions:master commit in: / Sam James
2025-05-13 0:30 Sam James
2025-05-13 0:30 Sam James
2025-05-13 0:30 Sam James
2025-05-13 0:30 Sam James
2024-10-05 7:25 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-10-05 4:15 Sam James
2024-08-11 10:23 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-11 10:11 Sam James
2024-08-05 20:39 Sam James
2024-08-05 20:39 Sam James
2024-08-05 2:03 Sam James
2024-08-05 2:02 Sam James
2024-08-05 2:02 Sam James
2024-08-05 2:02 Sam James
2024-08-05 2:02 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-08-02 23:14 Sam James
2024-07-08 3:00 Sam James
2024-07-08 2:31 Sam James
2024-07-08 2:31 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-07-07 5:55 Sam James
2024-06-25 4:06 Sam James
2024-06-25 4:06 Sam James
2024-06-25 4:06 Sam James
2024-06-25 4:06 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-06-21 13:14 Sam James
2024-05-24 6:05 Sam James
2024-05-24 1:18 Sam James
2024-05-24 1:18 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-22 1:12 Sam James
2024-05-19 15:27 Sam James
2024-05-19 15:27 Sam James
2024-05-19 15:27 Sam James
2024-05-19 15:27 Sam James
2024-05-18 16:07 Sam James
2024-05-18 16:06 Sam James
2024-05-18 16:06 Sam James
2024-05-18 15:34 Sam James
2024-05-18 15:32 Sam James
2024-05-18 15:32 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-18 14:04 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-17 4:03 Sam James
2024-05-15 10:28 Sam James
2024-05-15 10:28 Sam James
2024-05-14 0:18 Sam James
2024-05-14 0:15 Sam James
2024-05-14 0:12 Sam James
2024-05-14 0:12 Sam James
2024-05-14 0:08 Sam James
2024-05-14 0:08 Sam James
2024-05-14 0:05 Sam James
2024-05-14 0:05 Sam James
2024-05-14 0:05 Sam James
2024-05-14 0:05 Sam James
2024-05-14 0:05 Sam James
2024-02-16 21:35 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-11 16:47 Sam James
2023-06-10 7:23 Sam James
2023-06-10 7:23 Sam James
2023-06-10 6:04 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-10 4:22 Sam James
2023-06-09 11:17 Sam James
2023-06-09 11:11 Sam James
2023-06-09 11:02 Sam James
2023-06-09 11:02 Sam James
2023-06-09 11:02 Sam James
2023-06-09 11:02 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-06-07 11:13 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-19 16:14 Sam James
2023-02-17 7:44 Sam James
2023-02-17 7:44 Sam James
2023-02-17 7:44 Sam James
2023-02-17 1:33 Sam James
2023-02-17 1:33 Sam James
2023-02-17 1:33 Sam James
2023-02-15 8:18 Sam James
2023-02-15 7:48 Sam James
2023-02-15 7:46 Sam James
2023-02-15 7:46 Sam James
2023-02-15 7:46 Sam James
2023-02-15 7:46 Sam James
2023-02-15 7:46 Sam James
2023-02-15 7:46 Sam James
2023-02-15 2:24 Sam James
2023-02-15 2:24 Sam James
2023-02-15 2:24 Sam James
2023-02-14 3:40 Sam James
2023-02-14 3:40 Sam James
2023-02-14 3:40 Sam James
2023-02-14 3:40 Sam James
2023-02-14 0:09 Sam James
2023-02-14 0:09 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-13 21:37 Sam James
2023-02-12 18:53 Sam James
2023-02-12 18:53 Sam James
2023-02-12 6:53 Sam James
2023-02-12 6:53 Sam James
2023-02-12 6:53 Sam James
2023-02-11 1:43 Sam James
2023-02-11 1:43 Sam James
2023-02-10 6:09 Sam James
2023-02-10 6:09 Sam James
2023-02-10 6:09 Sam James
2023-02-09 3:54 Sam James
2023-02-09 3:54 Sam James
2023-02-08 3:37 Sam James
2023-02-08 1:06 Sam James
2023-02-08 0:03 Sam James
2023-02-08 0:03 Sam James
2023-02-07 23:47 Sam James
2023-02-07 23:42 Sam James
2023-02-07 23:42 Sam James
2023-02-07 23:42 Sam James
2023-02-07 23:42 Sam James
2023-02-07 1:08 Sam James
2023-02-07 1:08 Sam James
2023-02-06 13:47 Sam James
2023-02-06 4:32 Sam James
2023-02-06 4:23 Sam James
2023-02-06 4:19 Sam James
2023-02-06 4:10 Sam James
2023-02-06 4:10 Sam James
2023-02-06 3:59 Sam James
2023-02-06 3:59 Sam James
2023-02-06 3:59 Sam James
2022-07-30 5:48 Sam James
2022-07-29 2:03 Sam James
2022-07-29 2:03 Sam James
2022-07-29 2:03 Sam James
2021-08-30 21:14 Mike Gilbert
2021-08-30 21:14 Mike Gilbert
2020-11-19 18:20 Mike Gilbert
2020-11-19 18:20 Mike Gilbert
2020-11-19 18:20 Mike Gilbert
2020-01-26 23:19 Mike Gilbert
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=1719801147.2a0c3ce54dda9e72310745d04960bcea7071fc4e.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