[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    Re: [PATCH v2 2/2] findutils/grep.c: Short-circuit -v to bail out on first match
From:       Ari Sundholm <ari () tuxera ! com>
Date:       2019-01-29 15:38:43
Message-ID: 9dc343e7-0e13-f9ff-20b1-d14718d754a4 () tuxera ! com
[Download RAW message or body]

On 1/29/19 3:53 PM, Denys Vlasenko wrote:
> I'm going with this:
> 
> 
> -                       /* If it's non-inverted search, we can stop
> -                        * at first match */
> -                       if (found && !invert_search)
> -                               goto do_found;
> +                       /* If it's a non-inverted search, we can stop
> +                        * at first match and report it.
> +                        * If it's an inverted search, we can move on
> +                        * to the next line of input, ignoring the
> +                        * rest of the patterns.
> +                        */
> +                       if (found) {
> +                               //if (invert_search)
> +                               //      goto do_not_found;
> +                               //goto do_found;
> +                               break; // this accomplishes both
> +                       }
>                          pattern_ptr = pattern_ptr->link;
>                  } /* while (pattern_ptr) */
> 
>                  if (found ^ invert_search) {
> - do_found:
> + //do_found:
> 

Looks good. Thanks for addressing this issue so quickly!

Best regards,
Ari Sundholm
ari@tuxera.com

> 
> Thanks!
> 
> 
> On Mon, Jan 28, 2019 at 6:41 PM Ari Sundholm <ari@tuxera.com> wrote:
>>
>> A small optimization. There is no need to try matching the current
>> input line against any further patterns if a match was already
>> found and -v is specified.
>>
>> Signed-off-by: Ari Sundholm <ari@tuxera.com>
>> Signed-off-by: Niko Vähäsarja <niko@tuxera.com>
>> ---
>> v2: Make the code more readable and modify the relevant comment
>>
>>   findutils/grep.c | 16 ++++++++++++----
>>   1 file changed, 12 insertions(+), 4 deletions(-)
>>
>> diff --git a/findutils/grep.c b/findutils/grep.c
>> index 9d9da422c..0251e8082 100644
>> --- a/findutils/grep.c
>> +++ b/findutils/grep.c
>> @@ -443,10 +443,18 @@ static int grep_file(FILE *file)
>>                                          }
>>                                  }
>>                          }
>> -                       /* If it's non-inverted search, we can stop
>> -                        * at first match */
>> -                       if (found && !invert_search)
>> -                               goto do_found;
>> +                       /* If it's a non-inverted search, we can stop
>> +                        * at first match and report it.
>> +                        * If it's an inverted search, we can move on
>> +                        * to the next line of input, ignoring the
>> +                        * rest of the patterns.
>> +                        */
>> +                       if (found) {
>> +                               if (invert_search)
>> +                                       break;
>> +                               else
>> +                                       goto do_found;
>> +                       }
>>                          pattern_ptr = pattern_ptr->link;
>>                  } /* while (pattern_ptr) */
>>
>> --
>> 2.17.1
>>
>> _______________________________________________
>> busybox mailing list
>> busybox@busybox.net
>> http://lists.busybox.net/mailman/listinfo/busybox

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic