changeset 562:3fb59bd978c0

farray.sh: array_append() can now append multiple values at once. The misfeature of automatically appending null values is not supported any more.
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 11 Sep 2024 22:03:46 +0200
parents d173161a3a0f
children 0ab71eddcfd7
files share/local-bsdtools/farray.sh
diffstat 1 files changed, 23 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/farray.sh	Wed Sep 11 18:19:41 2024 +0200
+++ b/share/local-bsdtools/farray.sh	Wed Sep 11 22:03:46 2024 +0200
@@ -150,7 +150,6 @@
     local __farr_name
 
     local __farr_token __farr_gvrname __farr_len
-    local __farr_el
 
     __farr_name="${1-}"
     [ -z "${__farr_name}" ] && _farr_fatal "missing farray name"
@@ -171,9 +170,9 @@
     # And associate the token with the array name
     eval "${__farr_name}"="${__farr_token}"
 
-    for __farr_el in "$@"; do
-	farray_append ${__farr_name} "${__farr_el}"
-    done
+    if [ $# -gt 0 ]; then
+	farray_append ${__farr_name} "$@"
+    fi
 }
 
 
@@ -291,34 +290,38 @@
 
 
 #:
-#: Append a value to an existing array.
+#: Append one or more values to an existing array.
 #:
 #: Args:
 #:   $1 (str): The name of the existing array.
-#:   $2 (optional): The value to append. If the value is not given the null
-#:                  value will be appended.
+#:   $2...: The values to append.
 #:
 farray_append() {
-    local __farr_name __farr_value
+    local __farr_name
 
     local __farr_token __farr_gvrname __farr_len __farr_len_1
+    local __farr_newval
 
     _farr_array_get_meta "$@"
+    shift
 
-    __farr_value="${2-}"
-
-    __farr_len_1=$((__farr_len + 1))
+    for __farr_newval in "$@"; do
+        __farr_len_1=$((__farr_len + 1))
 
-    #
-    # Set value
-    # Escape properly: use $' ' and escape any backslashes and single quotes.
-    #
-    eval ${__farr_gvrname}_${__farr_len_1}=\$\'"$(_farr_quote_for_eval_dsq "${__farr_value}")"\'
-    # the implementation below line does not escape properly
-    #   eval ${__farr_gvrname}_${__farr_len_1}="\"${__farr_value}\""
+        #
+        # Set value Escape properly: use $' ' and escape any
+        # backslashes and single quotes.
+        #
+        eval ${__farr_gvrname}_${__farr_len_1}=\$\'"$(_farr_quote_for_eval_dsq "${__farr_newval}")"\'
+        # the implementation below line does not escape properly
+        #   eval ${__farr_gvrname}_${__farr_len_1}="\"${__farr_value}\""
 
-    # Set new array length
-    eval ${__farr_gvrname}__=${__farr_len_1}
+        # Set new array length
+        #  ... persistently in the array data storage
+        eval ${__farr_gvrname}__=${__farr_len_1}
+        #  ... and locally
+        __farr_len=${__farr_len_1}
+    done
 }