From 3931cbff5f34a1660dbbdd1c3495a14b95b59977 Mon Sep 17 00:00:00 2001 From: Krzesimir Nowak Date: Thu, 10 Apr 2025 17:56:04 +0200 Subject: [PATCH] pkg-auto: Add a global variable name generator function Some upcoming libraries will use this for their global variables. The function is using a single counter, which ensures that the generated names will be globally unique. --- pkg_auto/impl/util.sh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg_auto/impl/util.sh b/pkg_auto/impl/util.sh index 23e8de104e..11afcdf8b5 100644 --- a/pkg_auto/impl/util.sh +++ b/pkg_auto/impl/util.sh @@ -3,6 +3,9 @@ if [[ -z ${__UTIL_SH_INCLUDED__:-} ]]; then __UTIL_SH_INCLUDED__=x +declare -gra EMPTY_ARRAY=() +declare -grA EMPTY_MAP=() + # Works like dirname, but without spawning new processes. # # Params: @@ -299,4 +302,26 @@ function sets_split() { done } +declare -gi __UTIL_SH_COUNTER=0 + +# Generates a globally unique name for a variable. Can be given a +# prefix to override the default __PA_VAR one. +# +# Params: +# +# (optional) a prefix +# 1 - name of a variable, where the generated name will be stored +function gen_varname() { + local prefix='__PA_VAR' # pa = pkg-auto + if [[ ${#} -gt 1 ]]; then + # we passed a prefix + prefix=${1}; shift + fi + local -n name_ref=${1}; shift + + # shellcheck disable=SC2034 # shellcheck does not grok references + name_ref="${prefix}_${__UTIL_SH_COUNTER}" + __UTIL_SH_COUNTER=$((__UTIL_SH_COUNTER + 1)) +} + fi