Skip to content

Commit c36cea8

Browse files
committed
refactor(xfunc ARRAY filter): use a simple prefix for local variables
1 parent f1161a0 commit c36cea8

File tree

1 file changed

+42
-41
lines changed

1 file changed

+42
-41
lines changed

completions/ARRAY

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Utility xfunc functions for array manipulations -*- shell-script -*-
22

33
# Filter the array elements with the specified condition.
4-
# @param $1 Array name (that is not "value" or other internal variable names)
4+
# @param $1 Array name (that is not "value", "_*" or other internal variable
5+
# names)
56
# @param $2 When any of the options `-EFG' is specified, the second argument is
67
# used as a pattern string whose meaning is determined by the option `-EFG'.
78
# Otherwise, the second argument specifies the command that tests the array
@@ -53,13 +54,13 @@
5354
# unchanged. ]
5455
_comp_xfunc_ARRAY_filter()
5556
{
56-
local _comp_local_flags='' _comp_local_pattype='' _comp_local_anchoring=''
57-
local OPTIND=1 OPTARG='' OPTERR=0 _comp_local_opt=''
58-
while getopts 'EFGpsmxrC' _comp_local_opt "$@"; do
59-
case $_comp_local_opt in
60-
[EFG]) _comp_local_pattype=$_comp_local_opt ;;
61-
[psmx]) _comp_local_anchoring=$_comp_local_opt ;;
62-
[rC]) _comp_local_flags=$_comp_local_opt$_comp_local_flags ;;
57+
local _flags='' _pattype='' _anchoring=''
58+
local OPTIND=1 OPTARG='' OPTERR=0 _opt=''
59+
while getopts 'EFGpsmxrC' _opt "$@"; do
60+
case $_opt in
61+
[EFG]) _pattype=$_opt ;;
62+
[psmx]) _anchoring=$_opt ;;
63+
[rC]) _flags=$_opt$_flags ;;
6364
*)
6465
printf 'bash_completion: %s: %s\n' "$FUNCNAME" 'usage error' >&2
6566
printf 'usage: %s %s\n' "$FUNCNAME" "[-EFGpsmxrC] ARRAY_NAME CONDITION" >&2
@@ -76,76 +77,76 @@ _comp_xfunc_ARRAY_filter()
7677
elif [[ $1 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
7778
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "invalid array name '$1'." >&2
7879
return 2
79-
elif [[ $1 == @(_comp_local_*|OPTIND|OPTARG|OPTERR) ]]; then
80+
elif [[ $1 == @(_*|OPTIND|OPTARG|OPTERR) ]]; then
8081
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "array name '$1' is reserved for internal uses" >&2
8182
return 2
82-
elif [[ ! $_comp_local_pattype && $1 == value ]]; then
83+
elif [[ ! $_pattype && $1 == value ]]; then
8384
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "array name '$1' cannot be used for the predicate" >&2
8485
return 2
8586
fi
8687
# When the array is empty:
8788
eval "((\${#$1[@]}))" || return 0
8889

89-
local _comp_local_predicate='' _comp_local_pattern=$2
90-
case $_comp_local_pattype in
90+
local _predicate='' _pattern=$2
91+
case $_pattype in
9192
E)
92-
case $_comp_local_anchoring in
93-
p) _comp_local_predicate='[[ $_comp_local_value =~ ^($_comp_local_pattern) ]]' ;;
94-
s) _comp_local_predicate='[[ $_comp_local_value =~ ($_comp_local_pattern)$ ]]' ;;
95-
x) _comp_local_predicate='[[ $_comp_local_value =~ ^($_comp_local_pattern)$ ]]' ;;
96-
*) _comp_local_predicate='[[ $_comp_local_value =~ $_comp_local_pattern ]]' ;;
93+
case $_anchoring in
94+
p) _predicate='[[ $_value =~ ^($_pattern) ]]' ;;
95+
s) _predicate='[[ $_value =~ ($_pattern)$ ]]' ;;
96+
x) _predicate='[[ $_value =~ ^($_pattern)$ ]]' ;;
97+
*) _predicate='[[ $_value =~ $_pattern ]]' ;;
9798
esac
9899
;;
99100
F)
100-
case $_comp_local_anchoring in
101-
p) _comp_local_predicate='[[ $_comp_local_value == "$_comp_local_pattern"* ]]' ;;
102-
s) _comp_local_predicate='[[ $_comp_local_value == *"$_comp_local_pattern" ]]' ;;
103-
x) _comp_local_predicate='[[ $_comp_local_value == "$_comp_local_pattern" ]]' ;;
104-
*) _comp_local_predicate='[[ $_comp_local_value == *"$_comp_local_pattern"* ]]' ;;
101+
case $_anchoring in
102+
p) _predicate='[[ $_value == "$_pattern"* ]]' ;;
103+
s) _predicate='[[ $_value == *"$_pattern" ]]' ;;
104+
x) _predicate='[[ $_value == "$_pattern" ]]' ;;
105+
*) _predicate='[[ $_value == *"$_pattern"* ]]' ;;
105106
esac
106107
;;
107108
G)
108-
case $_comp_local_anchoring in
109-
p) _comp_local_predicate='[[ $_comp_local_value == $_comp_local_pattern* ]]' ;;
110-
s) _comp_local_predicate='[[ $_comp_local_value == *$_comp_local_pattern ]]' ;;
111-
m) _comp_local_predicate='[[ $_comp_local_value == *$_comp_local_pattern* ]]' ;;
112-
*) _comp_local_predicate='[[ $_comp_local_value == $_comp_local_pattern ]]' ;;
109+
case $_anchoring in
110+
p) _predicate='[[ $_value == $_pattern* ]]' ;;
111+
s) _predicate='[[ $_value == *$_pattern ]]' ;;
112+
m) _predicate='[[ $_value == *$_pattern* ]]' ;;
113+
*) _predicate='[[ $_value == $_pattern ]]' ;;
113114
esac
114115
;;
115116
*)
116117
if type -t "$2" &>/dev/null; then
117-
_comp_local_predicate="$2 \"\$_comp_local_value\""
118+
_predicate="$2 \"\$_value\""
118119
else
119-
_comp_local_predicate="local -x value=\$_comp_local_value; $2"
120+
_predicate="local -x value=\$_value; $2"
120121
fi
121122
;;
122123
esac
123124

124-
local _comp_local_unset='' _comp_local_expected_status=0
125-
[[ $_comp_local_flags == *r* ]] && _comp_local_expected_status=1
125+
local _unset='' _expected_status=0
126+
[[ $_flags == *r* ]] && _expected_status=1
126127

127-
local _comp_local_indices _comp_local_index _comp_local_value
128-
eval "_comp_local_indices=(\"\${!$1[@]}\")"
129-
for _comp_local_index in "${_comp_local_indices[@]}"; do
130-
eval "_comp_local_value=\${$1[\$_comp_local_index]}; $_comp_local_predicate"
128+
local _indices _index _value
129+
eval "_indices=(\"\${!$1[@]}\")"
130+
for _index in "${_indices[@]}"; do
131+
eval "_value=\${$1[\$_index]}; $_predicate"
131132
case $? in
132-
"$_comp_local_expected_status") continue ;;
133+
"$_expected_status") continue ;;
133134
[01])
134-
unset -v "$1[\$_comp_local_index]"
135-
_comp_local_unset=1
135+
unset -v "$1[\$_index]"
136+
_unset=1
136137
;;
137138
27) break ;;
138139
*)
139140
printf 'bash_completion: %s: %s\n' "$FUNCNAME" \
140-
"filter condition broken '${_comp_local_pattype:+-$_comp_local_pattype }$2'" >&2
141+
"filter condition broken '${_pattype:+-$_pattype }$2'" >&2
141142
return 2
142143
;;
143144
esac
144145
done
145146

146147
# Compaction of the sparse array
147-
[[ $_comp_local_flags == *C* ]] ||
148+
[[ $_flags == *C* ]] ||
148149
eval "((\${#$1[@]})) && $1=(\"\${$1[@]}\")"
149150

150-
[[ ! $_comp_local_unset ]]
151+
[[ ! $_unset ]]
151152
}

0 commit comments

Comments
 (0)