changeset 508:0b7b7b49bb31

array.sh: Implement array_contains() and array_find()
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 31 Aug 2024 18:17:09 +0200
parents e51440a07a9d
children 527d38d0003d
files share/local-bsdtools/array.sh
diffstat 1 files changed, 88 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/array.sh	Sat Aug 31 16:46:18 2024 +0200
+++ b/share/local-bsdtools/array.sh	Sat Aug 31 18:17:09 2024 +0200
@@ -317,6 +317,94 @@
 
 
 #:
+#: Determine whether a value is found within the array
+#:
+#: Args:
+#:   $1: 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) otherwise
+#:
+array_contains() {
+    local _name _searched_value
+
+    local _gvrname _l _idx _existing_value
+
+    [ $# -lt 1 ] && _array_fatal "missing array name"
+    _name=$1
+    _gvrname=${_farr_global_prefix}$1
+    [ $# -ne 2 ] && _array_fatal "missing value to search for"
+    _searched_value="$2"
+
+    # Check whether the variable already exists
+    eval _l=\${${_gvrname}__:-${_farr_unset}}
+    if [ "${_l}" = ${_farr_unset} ]; then
+	_array_fatal "array \`${_name}' does not exist"
+    fi
+
+    _idx=1
+    while [ ${_idx} -le ${_l} ]; do
+        eval _existing_value=\"\${${_gvrname}_${_idx}}\"
+        [ "${_existing_value}" = "${_searched_value}" ] && return 0
+	_idx=$((${_idx} + 1))
+    done
+    return 1
+}
+
+
+#:
+#: Try to find the index of a given value in an existing array.
+#:
+#: Args:
+#:   $1: The name of an existing array
+#:   $2: The value to search for
+#:   $3 (int, optional): The start index to search for (inclusive)
+#:   $4 (int, optional):  The index to stop (inclusive)
+#:
+#: Output (stdout):
+#:   The index number where the value is found -- if any
+#:
+#: Returns:
+#:   - 0 (truish) if the argument value is found within the given array
+#:     and index constraints   
+#:   - 1 (falsy) otherwise
+#:
+array_find() {
+    local _name _searched_value _start _end
+
+    local _gvrname _l _idx _existing_value
+
+    [ $# -lt 1 ] && _array_fatal "missing array name"
+    _name=$1
+    _gvrname=${_farr_global_prefix}$1
+    [ $# -lt 2 ] && _array_fatal "missing value to search for"
+    _searched_value="$2"
+
+    # Check whether the variable already exists
+    eval _l=\${${_gvrname}__:-${_farr_unset}}
+    if [ "${_l}" = ${_farr_unset} ]; then
+	_array_fatal "array \`${_name}' does not exist"
+    fi
+
+    _start=${3-1}
+    _end=${4-${_l}}
+
+    _idx=${_start}
+    while [ ${_idx} -le ${_end} ]; do
+        eval _existing_value=\"\${${_gvrname}_${_idx}}\"
+        if [ "${_existing_value}" = "${_searched_value}" ]; then
+            printf "%d" ${_idx}
+            return 0
+        fi
+	_idx=$((${_idx} + 1))
+    done
+    return 1
+}
+
+
+#:
 #: Call a function for every element in an array starting at the first index.
 #:
 #: The function to be called must accept three arguments: