Mercurial > hgrepos > FreeBSD > ports > sysutils > local-bsdtools
changeset 613:17194ffe3638
farray.sh: falist_create() and falist_set() now accept a sequence of key-value pairs.
This sequence may be empty.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Thu, 19 Sep 2024 20:03:29 +0200 |
| parents | c9ef2339618d |
| children | 62fbc4316d97 |
| files | share/local-bsdtools/farray.sh tests/farray-alist.t |
| diffstat | 2 files changed, 65 insertions(+), 41 deletions(-) [+] |
line wrap: on
line diff
--- a/share/local-bsdtools/farray.sh Thu Sep 19 18:29:57 2024 +0200 +++ b/share/local-bsdtools/farray.sh Thu Sep 19 20:03:29 2024 +0200 @@ -1265,6 +1265,8 @@ #: Args: #: $1 (str): The name of the alist. #: Must conform to shell variable naming conventions. +#: $2, $3 ... (optional): A sequence of key-value pairs the alist will be +#: populated with. #: #: Exit: #: Iff the alist already exists. @@ -1294,6 +1296,15 @@ # And associate the token with the array name eval "${__farr_name}"="${__farr_token}" + + # + # When there are key-value pairs populate the alist with. + # Note that the alist's name is not shifted away yet and must be taken + # into account. It is also needed for `falist_set`. + # + if [ $# -gt 2 ]; then + falist_set "$@" + fi } @@ -1491,8 +1502,8 @@ #: #: Args: #: $1 (str): The name of an existing alist. -#: $2: The key. -#: $3: The value. +#: $2, $3 ... (optional): A sequence of key-value pairs the alist will be +#: populated with. #: #: Exit: #: If one of the underlying arrays that implement the alist does not exist @@ -1505,36 +1516,41 @@ local __farr_idx __farr_elkey _farr_alist_get_meta "$@" - [ $# -lt 2 ] && _farr_fatal "missing key" - __farr_key="$2" - [ $# -lt 3 ] && _farr_fatal "missing value" - __farr_value="$3" + shift + + while [ $# -ne 0 ]; do + [ $# -lt 1 ] && _farr_fatal "missing key" + __farr_key="$1" + [ $# -lt 2 ] && _farr_fatal "missing value" + __farr_value="$2" - __farr_idx=1 - while [ ${__farr_idx} -le ${__farr_len} ]; do - eval __farr_elkey=\"\$\{${__farr_keyname}_${__farr_idx}+SET\}\" - if [ -n "${__farr_elkey}" ]; then - eval __farr_elkey=\"\$\{${__farr_keyname}_${__farr_idx}\}\" - if [ "${__farr_elkey}" = "${__farr_key}" ]; then - eval ${__farr_valname}_${__farr_idx}="$(_farr_quote_for_eval "${__farr_value}")" - return 0 + __farr_idx=1 + while [ ${__farr_idx} -le ${__farr_len} ]; do + eval __farr_elkey=\"\$\{${__farr_keyname}_${__farr_idx}+SET\}\" + if [ -n "${__farr_elkey}" ]; then + eval __farr_elkey=\"\$\{${__farr_keyname}_${__farr_idx}\}\" + if [ "${__farr_elkey}" = "${__farr_key}" ]; then + eval ${__farr_valname}_${__farr_idx}="$(_farr_quote_for_eval "${__farr_value}")" + return 0 + fi + else + _farr_fatal "key unexpectedly unset (index ${__farr_idx})" fi - else - _farr_fatal "key unexpectedly unset (index ${__farr_idx})" - fi - __farr_idx=$((__farr_idx + 1)) + __farr_idx=$((__farr_idx + 1)) + done + # + # Not yet found: "append" .. + # + # NOTE: __farr_idx already the new correct length + # + __farr_len=${__farr_idx} + # ... the key/value pairs to storage + eval ${__farr_keyname}_${__farr_len}="$(_farr_quote_for_eval "${__farr_key}")" + eval ${__farr_valname}_${__farr_len}="$(_farr_quote_for_eval "${__farr_value}")" + # ... the new length + eval ${__farr_objname}__=${__farr_len} + shift 2 done - # - # Not yet found: "append" .. - # - # NOTE: __farr_idx already the new correct length - # - __farr_len=${__farr_idx} - # ... the key/value pairs to storage - eval ${__farr_keyname}_${__farr_len}="$(_farr_quote_for_eval "${__farr_key}")" - eval ${__farr_valname}_${__farr_len}="$(_farr_quote_for_eval "${__farr_value}")" - # ... the new length - eval ${__farr_objname}__=${__farr_len} return 0 }
--- a/tests/farray-alist.t Thu Sep 19 18:29:57 2024 +0200 +++ b/tests/farray-alist.t Thu Sep 19 20:03:29 2024 +0200 @@ -230,21 +230,29 @@ Compare ======= - $ falist_create LIST1 - $ falist_set LIST1 K1 V1 - $ falist_set LIST1 K2 V2 + $ falist_create LIST1 K1 V1 K2 V2 + $ falist_debug LIST1 + DEBUG: alist `LIST1' has length 2 + DEBUG: `K1' -> `V1' + DEBUG: `K2' -> `V2' - $ falist_create LIST2 - $ falist_set LIST2 K2 V2 - $ falist_set LIST2 K1 V1 + $ falist_create LIST2 K2 V2 K1 V1 + $ falist_debug LIST2 + DEBUG: alist `LIST2' has length 2 + DEBUG: `K2' -> `V2' + DEBUG: `K1' -> `V1' - $ falist_create LIST3 - $ falist_set LIST3 K1 V1 - $ falist_set LIST3 K2 V2 + $ falist_create LIST3 K1 V1 K2 V2 + $ falist_debug LIST3 + DEBUG: alist `LIST3' has length 2 + DEBUG: `K1' -> `V1' + DEBUG: `K2' -> `V2' - $ falist_create LIST4 - $ falist_set LIST4 K1 V1 - $ falist_set LIST4 K2 V2-4 + $ falist_create LIST4 K1 V1 K2 V2-4 + $ falist_debug LIST4 + DEBUG: alist `LIST4' has length 2 + DEBUG: `K1' -> `V1' + DEBUG: `K2' -> `V2-4' $ falist_are_equal LIST1 LIST2 $ falist_are_equal LIST1 LIST4
