Re: [PATCH 5/7] completion: fix expansion issues in __gitcomp_nl()
- From
Felipe Contreras <felipe.contreras@gmail.com>
- Date
- Nov 18, 2012, 00:55 UTC
- Message-ID
- <CAMP44s22aG65Tvg3y7ZSEKqJy+Xuvwt_1BjSBV7vqOz+vgzg3g@mail.gmail.com>
- In-Reply-To
- <CAMP44s1zaxey7TQxGaLtaMUwPTVTmRBn1w6=Zqefy7FJzEepBw@mail.gmail.com>
On Sat, Nov 17, 2012 at 8:28 PM, Felipe Contreras <felipe.contreras@gmail.com> wrote:
Show 57 quoted lines
> On Sat, Nov 17, 2012 at 8:08 PM, Felipe Contreras
> <felipe.contreras@gmail.com> wrote:
>> On Sat, Nov 17, 2012 at 3:14 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
>>> On Sat, Nov 17, 2012 at 12:50:39PM +0100, Felipe Contreras wrote:
>>>> On Sat, Nov 17, 2012 at 12:05 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:
>>>>
>>>> > __gitcomp_nl ()
>>>> > {
>>>> > local IFS=$'\n'
>>>> > - COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$1" -- "${3-$cur}"))
>>>> > + COMPREPLY=($(awk -v pfx="${2-}" -v sfx="${4- }" -v cur="${3-$cur}" '
>>>> > + BEGIN {
>>>> > + FS="\n";
>>>> > + len=length(cur);
>>>> > + }
>>>> > + {
>>>> > + if (cur == substr($1, 1, len))
>>>> > + print pfx$1sfx;
>>>> > + }' <<< "$1" ))
>>>> > }
>>>>
>>>> Does this really perform better than my alternative?
>>>>
>>>> + for x in $1; do
>>>> + if [[ "$x" = "$3"* ]]; then
>>>> + COMPREPLY+=("$2$x$4")
>>>> + fi
>>>> + done
>>>
>>> It does:
>>>
>>> My version:
>>>
>>> $ refs="$(for i in {0..9999} ; do echo branch$i ; done)"
>>> $ time __gitcomp_nl "$refs"
>>>
>>> real 0m0.109s
>>> user 0m0.096s
>>> sys 0m0.012s
>>>
>>> Yours:
>>>
>>> $ time __gitcomp_nl "$refs"
>>>
>>> real 0m0.321s
>>> user 0m0.312s
>>> sys 0m0.008s
>>
>> Yeah, for 10000 refs, which is not the common case:
>
> And this beats both in every case:
>
> while read -r x; do
> if [[ "$x" == "$3"* ]]; then
> COMPREPLY+=("$2$x$4")
> fi
> done <<< $1Nevermind that.
Here's another:
local IFS=$'\n' COMPREPLY=($(printf -- "$2%s$4\n" $1 | grep "^$2$3"))
-- Felipe Contreras