changeset 524:0665a15bd3ca

array.sh: Impplement array_join(), array_join_for_eval() and array_print_join_for_eval()
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 03 Sep 2024 12:20:15 +0200
parents ea199195b214
children 2c31b1d4bd66
files share/local-bsdtools/array.sh
diffstat 1 files changed, 133 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/array.sh	Tue Sep 03 12:19:11 2024 +0200
+++ b/share/local-bsdtools/array.sh	Tue Sep 03 12:20:15 2024 +0200
@@ -598,6 +598,139 @@
 
 
 #:
+#: Join all the elements in given array with a separator and put the result
+#: into a variable.
+#:
+#: Args:
+#:   $1 (str): The name of a variable where to put joined string value into.
+#:   $2 (str): The name of an existing array.
+#:   $3 (str, optional): The separator (default: a space `` ``).
+#:
+array_join() {
+    local __farr_varname __farr_name __farr_separator
+
+    local __farr_gvrname __farr_l __farr_join_idx
+    local __farr_command __farr_real_separator __farr_current_value
+
+    [ $# -lt 1 ] && _farr_fatal "missing variable name"
+    __farr_varname=$1
+    [ $# -lt 2 ] && _farr_fatal "missing array name"
+    __farr_name=$2
+    __farr_gvrname=${_farr_global_prefix}$2
+
+    __farr_separator="${3-" "}"
+
+    # Check whether the variable already exists
+    eval __farr_l=\${${__farr_gvrname}__:-${_farr_unset}}
+    if [ "${__farr_l}" = ${_farr_unset} ]; then
+	_farr_fatal "array \`${__farr_name}' does not exist"
+    fi
+
+    __farr_real_separator=""
+    __farr_command=""
+
+    __farr_join_idx=1
+    while [ ${__farr_join_idx} -le ${__farr_l} ]; do
+	eval __farr_current_value=\"\${${__farr_gvrname}_${__farr_join_idx}}\"
+	__farr_command="${__farr_command}${__farr_real_separator}${__farr_current_value}"
+	__farr_real_separator="${__farr_separator}"
+	__farr_join_idx=$((${__farr_join_idx} + 1))
+    done
+    eval ${__farr_varname}=\"\${__farr_command}\"
+}
+
+
+#:
+#: Join all the elements in given array with a space `` `` character while
+#: escaping properly for feeding into shell's ``eval`` and put it into a
+#: variable.
+#:
+#: It is meant that ``eval "$(array_join_for_eval ARRAY)"`` is safe:
+#: every item of the array becomes a word when the eval evaluates the
+#: joined string.
+#:
+#: Args:
+#:   $1 (str): The name of a variable where to put joined and properly encoded
+#:             string value into.
+#:   $2 (str): The name of an existing array.
+#:
+#: Output (stdout):
+#:   The result string.
+#:
+array_join_for_eval() {
+    local __farr_varname  __farr_name
+
+    local __farr_gvrname __farr_l __farr_join_idx
+    local __farr_command __farr_real_separator __farr_current_value
+
+    [ $# -lt 1 ] && _farr_fatal "missing variable name"
+    __farr_varname=$1
+    [ $# -lt 2 ] && _farr_fatal "missing array name"
+    __farr_name=$2
+    __farr_gvrname=${_farr_global_prefix}$2
+
+    # Check whether the variable already exists
+    eval __farr_l=\${${__farr_gvrname}__:-${_farr_unset}}
+    if [ "${__farr_l}" = ${_farr_unset} ]; then
+	_farr_fatal "array \`${__farr_name}' does not exist"
+    fi
+
+    __farr_real_separator=""
+    __farr_command=""
+
+    __farr_join_idx=1
+    while [ ${__farr_join_idx} -le ${__farr_l} ]; do
+	eval __farr_current_value=\"\${${__farr_gvrname}_${__farr_join_idx}}\"
+	__farr_command="${__farr_command}${__farr_real_separator}\$'$(_farr_quote_for_eval_dsq "${__farr_current_value}")'"
+	__farr_real_separator=' '
+	__farr_join_idx=$((${__farr_join_idx} + 1))
+    done
+    eval ${__farr_varname}=\"\${__farr_command}\"
+}
+
+
+#:
+#: Join all the elements in given array with a space `` `` character while
+#: escaping properly for feeding into shell's ``eval``.
+#:
+#: ``eval "$(array_join_for_eval ARRAY)"`` is safe:
+#: every item of the array becomes a word when the eval evaluates the
+#: joined string.
+#:
+#: Args:
+#:   $1 (str): The name of an existing array.
+#:
+#: Output (stdout):
+#:   The joined and properly encoded string.
+#:
+array_print_join_for_eval() {
+    local __farr_name
+
+    local __farr_gvrname __farr_l __farr_join_idx __farr_current_value
+
+    [ $# -lt 1 ] && _farr_fatal "missing array name"
+    __farr_name=$1
+    __farr_gvrname=${_farr_global_prefix}$1
+
+    # Check whether the variable already exists
+    eval __farr_l=\${${__farr_gvrname}__:-${_farr_unset}}
+    if [ "${__farr_l}" = ${_farr_unset} ]; then
+	_farr_fatal "array \`${__farr_name}' does not exist"
+    fi
+
+    __farr_join_idx=1
+    while [ ${__farr_join_idx} -le ${__farr_l} ]; do
+	eval __farr_current_value=\"\${${__farr_gvrname}_${__farr_join_idx}}\"
+        if [ ${__farr_join_idx} -gt 1 ]; then
+            printf "%s" " "
+        fi
+        printf "%s" "\$'$(_farr_quote_for_eval_dsq "${__farr_current_value}")'"
+	__farr_join_idx=$((${__farr_join_idx} + 1))
+    done
+}
+
+
+#:
 #: Call a function for every element in an array starting at the first index.
 #:
 #: The function to be called must accept three arguments: