perf(bash-v2): use backslash escape string expansion for tab (#1682)

Using a command substitution, i.e. a subshell, with `printf` is
expensive for this purpose. For example `__*_format_comp_descriptions`
is run once for each completion candidate; the expense adds up and
shows when there are a lot of them.
This commit is contained in:
Ville Skyttä 2022-04-28 05:02:30 +03:00 committed by GitHub
parent d8184d3269
commit 2722a75ba3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -154,8 +154,7 @@ __%[1]s_handle_completion_types() {
# If the user requested inserting one completion at a time, or all # If the user requested inserting one completion at a time, or all
# completions at once on the command-line we must remove the descriptions. # completions at once on the command-line we must remove the descriptions.
# https://github.com/spf13/cobra/issues/1508 # https://github.com/spf13/cobra/issues/1508
local tab comp local tab=$'\t' comp
tab=$(printf '\t')
while IFS='' read -r comp; do while IFS='' read -r comp; do
# Strip any description # Strip any description
comp=${comp%%%%$tab*} comp=${comp%%%%$tab*}
@ -175,8 +174,7 @@ __%[1]s_handle_completion_types() {
} }
__%[1]s_handle_standard_completion_case() { __%[1]s_handle_standard_completion_case() {
local tab comp local tab=$'\t' comp
tab=$(printf '\t')
local longest=0 local longest=0
# Look for the longest completion so that we can format things nicely # Look for the longest completion so that we can format things nicely
@ -231,8 +229,7 @@ __%[1]s_handle_special_char()
__%[1]s_format_comp_descriptions() __%[1]s_format_comp_descriptions()
{ {
local tab local tab=$'\t'
tab=$(printf '\t')
local comp="$1" local comp="$1"
local longest=$2 local longest=$2