Specialize {get,put}_array for regular arrays

This commit is contained in:
Nick Chambers 2021-11-16 01:01:31 -06:00
parent 4b2d6fcabb
commit e933bc443a
1 changed files with 48 additions and 49 deletions

97
rowbot
View File

@ -17,30 +17,57 @@ put_array() {
# The variable named array is a nameref to an array # The variable named array is a nameref to an array
# shellcheck disable=SC2178 # shellcheck disable=SC2178
declare -n array=$1 declare -n array=$1
declare -n scalar=ARR_${1^^} declare -n scalar=RB_A_${1^^}
local key local val
for key in "${!array[@]}"; do for val in "${array[@]}"; do
scalar+=${#key}:$key${array[$key]}$'\n' scalar+=${#val}:$val
done done
scalar=${scalar%$'\n'} export "RB_A_${1^^}"
export "ARR_${1^^}"
} }
get_array() { get_array() {
declare -n scalar=ARR_${1^^}
declare -n array=$1 declare -n array=$1
local entry idx_size declare -n scalar=RB_A_${1^^}
local len val idx=0
while read -r entry; do while [[ $scalar ]]; do
idx_size=${entry%%:*} entry=${entry#"$idx_size":} len=${scalar%%:*} scalar=${scalar#"$len":}
array[${entry:0:idx_size}]=${entry:idx_size} val=${scalar:0:len} scalar=${scalar:len}
done <<< "$scalar" array[$idx]=$val
(( idx += 1 ))
done
unset "RB_A_${1^^}"
} }
is_good_variable() { b64_encode() {
[[ $1 =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]] local idx=0 numerics table_idxs table_idx encoded
local table=( {A..Z} {a..z} {0..9} + / )
for (( ; idx < ${#1}; idx+=3 )); do
read -ra numerics < <(
printf '%d %d %d\n' "'${1:idx:1}" "'${1:idx+1:1}" "'${1:idx+2:1}"
)
(( table_idxs[0] = numerics[0] >> 2 ))
(( table_idxs[1] = (( (numerics[0] & 0x03) << 6) | (numerics[1] & 0xF0) >> 2) >> 2 ))
(( table_idxs[2] = (( (numerics[1] & 0x0F) << 4) | (numerics[2] & 0xC0) >> 4) >> 2 ))
(( table_idxs[3] = numerics[2] & 0x3F ))
for table_idx in "${table_idxs[@]}"; do
encoded+=${table[$table_idx]}
done
done
if (( ${#1} % 3 == 1 )); then
encoded=${encoded::-2}==
elif (( ${#1} % 3 == 2 )); then
encoded=${encoded::-1}=
fi
prints %s "$encoded"
} }
# code reloading helpers # code reloading helpers
@ -62,12 +89,8 @@ get_option() {
fi fi
} }
is_parent() {
(( BASHPID == $$ ))
}
is_reloaded() { is_reloaded() {
[[ $RELOADED = yes ]] || (( LORE_LIVES > 1 )) [[ $RELOADED = yes ]] || (( RELOAD_COUNT ))
} }
# message classification # message classification
@ -196,34 +219,6 @@ url() {
prints %s:%s "${config[server]}" "${config[port]}" prints %s:%s "${config[server]}" "${config[port]}"
} }
b64_encode() {
local idx=0 numerics table_idxs table_idx encoded
local table=( {A..Z} {a..z} {0..9} + / )
for (( ; idx < ${#1}; idx+=3 )); do
read -ra numerics < <(
printf '%d %d %d\n' "'${1:idx:1}" "'${1:idx+1:1}" "'${1:idx+2:1}"
)
(( table_idxs[0] = numerics[0] >> 2 ))
(( table_idxs[1] = (((numerics[0] & 0x03) << 6) | (numerics[1] & 0xF0) >> 2) >> 2 ))
(( table_idxs[2] = (((numerics[1] & 0x0F) << 4) | (numerics[2] & 0xC0) >> 4) >> 2 ))
(( table_idxs[3] = numerics[2] & 0x3F ))
for table_idx in "${table_idxs[@]}"; do
encoded+=${table[$table_idx]}
done
done
if (( ${#1} % 3 == 1 )); then
encoded=${encoded::-2}==
elif (( ${#1} % 3 == 2 )); then
encoded=${encoded::-1}=
fi
prints %s "$encoded"
}
### ###
# configure rowbot's environment # configure rowbot's environment
### ###
@ -902,8 +897,12 @@ irc_who() {
# plugin api # plugin api
### ###
plugin_is_good_variable() {
[[ $1 =~ ^[A-Za-z_][A-Za-z0-9_]+$ ]]
}
plugin_reg() { plugin_reg() {
if ! is_good_variable irc_plugin_array_"$1"; then if ! plugin_is_good_variable irc_plugin_array_"$1"; then
return 1 return 1
fi fi
@ -927,7 +926,7 @@ plugin_run() {
# This is a false positive. # This is a false positive.
# shellcheck disable=SC2178 # shellcheck disable=SC2178
if is_good_variable irc_plugin_array_"$1" && [[ -v irc_plugin_array_"$1" ]]; then if plugin_is_good_variable irc_plugin_array_"$1" && [[ -v irc_plugin_array_"$1" ]]; then
declare -n plugins=irc_plugin_array_"$1" declare -n plugins=irc_plugin_array_"$1"
shift shift
local plugin local plugin