changeset 579:4fd6be157c70

farray.sh: farray_contains() now accepts multiple search values and returns true if any of them is found within the array
author Franz Glasner <fzglas.hg@dom66.de>
date Sun, 15 Sep 2024 22:35:37 +0200
parents 721737ce1ea0
children ac7ab3d98cd2
files share/local-bsdtools/farray.sh
diffstat 1 files changed, 23 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/farray.sh	Sun Sep 15 21:58:33 2024 +0200
+++ b/share/local-bsdtools/farray.sh	Sun Sep 15 22:35:37 2024 +0200
@@ -576,32 +576,33 @@
 
 
 #:
-#: Determine whether a value is found within the array
+#: Determine whether any of given values is found within the array.
 #:
 #: Args:
-#:   $1: The name of an existing array.
-#:   $2: The value to search for.
+#:   $1 (str): The name of an existing array.
+#:   $2...: The value to search for.
 #:
 #: Returns:
-#:   0 (truish) if the argument value is found within the given array,
-#:   1 (falsy) if the value is not found.
+#:   int: 0 (truish) if any of the argument value is found within the given
+#:        array,
+#:        1 (falsy) if the value is not found.
 #:
 farray_contains() {
-    local __farr_name __farr_searched_value
+    local __farr_name
 
     local __farr_token __farr_gvrname __farr_len
-    local __farr_idx __farr_existing_value
+    local __farr_idx __farr_existing_value __farr_searched_value
 
     _farr_array_get_meta "$@"
-    __farr_searched_value="${2+SET}"
-    [ -z "${__farr_searched_value}" ] && _farr_fatal "no search value given"
-    __farr_searched_value="$2"
-
-    __farr_idx=1
-    while [ ${__farr_idx} -le ${__farr_len} ]; do
-        eval __farr_existing_value=\"\$\{${__farr_gvrname}_${__farr_idx}\}\"
-        [ "${__farr_existing_value}" = "${__farr_searched_value}" ] && return 0
-	__farr_idx=$((__farr_idx + 1))
+    shift
+    [ $# -lt 1 ] && _farr_fatal "no search value given"
+    for __farr_searched_value in "$@"; do
+        __farr_idx=1
+        while [ ${__farr_idx} -le ${__farr_len} ]; do
+            eval __farr_existing_value=\"\$\{${__farr_gvrname}_${__farr_idx}\}\"
+            [ "${__farr_existing_value}" = "${__farr_searched_value}" ] && return 0
+	    __farr_idx=$((__farr_idx + 1))
+        done
     done
     return 1
 }
@@ -1642,6 +1643,12 @@
     if ! farray_contains TEST $'" 123" \\\'45 ' ; then      # '
        echo "NOT CONTAINS (ERROR)"
     fi
+    if ! farray_contains TEST 'NO VAL 1' $'" 123" \\\'45 ' 'NO VAL 3'; then      # '
+       echo "NOT CONTAINS (ERROR)"
+    fi
+    if farray_contains TEST 'NO VAL 1' 'NO VAL 3'; then
+       echo "CONTAINS (ERROR)"
+    fi
     if ! farray_find _i TEST $'" 123" \\\'45 ' ; then       # '
        echo "NOT CONTAINS (ERROR)"
     fi