# [PATCH] string_list: use string-list API in unsorted_string_list_lookup()

6 messages from 2016-04-22 to 2016-04-26. Participants: Ralf Thielow, Johannes Schindelin, Stefan Beller.
Thread: https://gitlist.dev/t/42111

## Ralf Thielow, 2016-04-22 17:35

Subject: [PATCH] string_list: use string-list API in unsorted_string_list_lookup()
Message-ID: <20160422173500.32329-1-ralf.thielow@gmail.com>
URL: https://gitlist.dev/e/20160422173500.32329-1-ralf.thielow%40gmail.com

```
Using the string-list API in function unsorted_string_list_lookup()
makes the code more readable.  So let's do this.

Signed-off-by: Ralf Thielow <ralf.thielow@gmail.com>
---
 string-list.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/string-list.c b/string-list.c
index 2a32a3f..8127e12 100644
--- a/string-list.c
+++ b/string-list.c
@@ -231,12 +231,13 @@ void string_list_sort(struct string_list *list)
 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
 						     const char *string)
 {
-	int i;
+	struct string_list_item *item;
 	compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
 
-	for (i = 0; i < list->nr; i++)
-		if (!cmp(string, list->items[i].string))
-			return list->items + i;
+	for_each_string_list_item(item, list) {
+		if (!cmp(string, item->string))
+			return item;
+	}
 	return NULL;
 }
 
-- 
2.8.1.382.g1352ede

```

## Johannes Schindelin, 2016-04-24 07:06

Subject: Re: [PATCH] string_list: use string-list API in unsorted_string_list_lookup()
Message-ID: <alpine.DEB.2.20.1604240901570.2896@virtualbox>
URL: https://gitlist.dev/e/alpine.DEB.2.20.1604240901570.2896%40virtualbox
In-Reply-To: <20160422173500.32329-1-ralf.thielow@gmail.com>

```
Hi Ralf,

On Fri, 22 Apr 2016, Ralf Thielow wrote:

> Using the string-list API in function unsorted_string_list_lookup()
> makes the code more readable.  So let's do this.
> 
> Signed-off-by: Ralf Thielow <ralf.thielow@gmail.com>
> ---
>  string-list.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)

Oh. I hoped for a reduction, not an increase.

> diff --git a/string-list.c b/string-list.c
> index 2a32a3f..8127e12 100644
> --- a/string-list.c
> +++ b/string-list.c
> @@ -231,12 +231,13 @@ void string_list_sort(struct string_list *list)
>  struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
>  						     const char *string)
>  {
> -	int i;
> +	struct string_list_item *item;
>  	compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
>  
> -	for (i = 0; i < list->nr; i++)
> -		if (!cmp(string, list->items[i].string))
> -			return list->items + i;
> +	for_each_string_list_item(item, list) {
> +		if (!cmp(string, item->string))
> +			return item;
> +	}
>  	return NULL;

If you drop the extra curly braces (thereby matching Git's coding style as
an additional bonus), at least the patch won't increase the number of
lines.

In any case, I like the simplification of the code.

Ciao,
Dscho

```

## Ralf Thielow, 2016-04-25 17:35

Subject: Re: [PATCH] string_list: use string-list API in unsorted_string_list_lookup()
Message-ID: <87eg9t8uc2.fsf@gmail.com>
URL: https://gitlist.dev/e/87eg9t8uc2.fsf%40gmail.com
In-Reply-To: <alpine.DEB.2.20.1604240901570.2896@virtualbox>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

>>  
>> -	for (i = 0; i < list->nr; i++)
>> -		if (!cmp(string, list->items[i].string))
>> -			return list->items + i;
>> +	for_each_string_list_item(item, list) {
>> +		if (!cmp(string, item->string))
>> +			return item;
>> +	}
>>  	return NULL;
>
> If you drop the extra curly braces (thereby matching Git's coding style as
> an additional bonus), at least the patch won't increase the number of
> lines.
>

Oops. I'll send a new version of the patch without these
extra curly braces.

Thanks!

> In any case, I like the simplification of the code.
>
> Ciao,
> Dscho

```

## Ralf Thielow, 2016-04-25 17:40

Subject: [PATCH v2] string_list: use string-list API in unsorted_string_list_lookup()
Message-ID: <20160425174000.24943-1-ralf.thielow@gmail.com>
URL: https://gitlist.dev/e/20160425174000.24943-1-ralf.thielow%40gmail.com
In-Reply-To: <20160422173500.32329-1-ralf.thielow@gmail.com>

```
Using the string-list API in function unsorted_string_list_lookup()
makes the code more readable.

Signed-off-by: Ralf Thielow <ralf.thielow@gmail.com>
---
Changes since v1:
- remove extra curly braces

 string-list.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/string-list.c b/string-list.c
index 2a32a3f..62d2084 100644
--- a/string-list.c
+++ b/string-list.c
@@ -231,12 +231,12 @@ void string_list_sort(struct string_list *list)
 struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
 						     const char *string)
 {
-	int i;
+	struct string_list_item *item;
 	compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
 
-	for (i = 0; i < list->nr; i++)
-		if (!cmp(string, list->items[i].string))
-			return list->items + i;
+	for_each_string_list_item(item, list)
+		if (!cmp(string, item->string))
+			return item;
 	return NULL;
 }
 
-- 
2.8.1.430.g7c694c5

```

## Stefan Beller, 2016-04-25 17:47

Subject: Re: [PATCH v2] string_list: use string-list API in unsorted_string_list_lookup()
Message-ID: <CAGZ79kbjtcnRmKPz-vP3C38hsjD3OauAvqctCmhhhfy7g4WDFQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAGZ79kbjtcnRmKPz-vP3C38hsjD3OauAvqctCmhhhfy7g4WDFQ%40mail.gmail.com
In-Reply-To: <20160425174000.24943-1-ralf.thielow@gmail.com>

```
On Mon, Apr 25, 2016 at 10:40 AM, Ralf Thielow <ralf.thielow@gmail.com> wrote:
> Using the string-list API in function unsorted_string_list_lookup()
> makes the code more readable.
>
> Signed-off-by: Ralf Thielow <ralf.thielow@gmail.com>
> ---
> Changes since v1:
> - remove extra curly braces

Reviewed-by: Stefan Beller <sbeller@google.com>

>
>  string-list.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/string-list.c b/string-list.c
> index 2a32a3f..62d2084 100644
> --- a/string-list.c
> +++ b/string-list.c
> @@ -231,12 +231,12 @@ void string_list_sort(struct string_list *list)
>  struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
>                                                      const char *string)
>  {
> -       int i;
> +       struct string_list_item *item;
>         compare_strings_fn cmp = list->cmp ? list->cmp : strcmp;
>
> -       for (i = 0; i < list->nr; i++)
> -               if (!cmp(string, list->items[i].string))
> -                       return list->items + i;
> +       for_each_string_list_item(item, list)
> +               if (!cmp(string, item->string))
> +                       return item;
>         return NULL;
>  }
>
> --
> 2.8.1.430.g7c694c5
>

```

## Johannes Schindelin, 2016-04-26 15:42

Subject: Re: [PATCH v2] string_list: use string-list API in unsorted_string_list_lookup()
Message-ID: <alpine.DEB.2.20.1604261742390.2896@virtualbox>
URL: https://gitlist.dev/e/alpine.DEB.2.20.1604261742390.2896%40virtualbox
In-Reply-To: <20160425174000.24943-1-ralf.thielow@gmail.com>

```
Hi Ralf,

On Mon, 25 Apr 2016, Ralf Thielow wrote:

> Using the string-list API in function unsorted_string_list_lookup()
> makes the code more readable.
> 
> Signed-off-by: Ralf Thielow <ralf.thielow@gmail.com>
> ---
> Changes since v1:
> - remove extra curly braces

ACK!
Dscho

```
