threads / patch / 42111

patchstring_list: use string-list API in unsorted_string_list_lookup()

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

## tl;dr

6 messages between Apr 22, 2016 and Apr 26, 2016. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

Ralf Thielow· Apr 22, 2016, 17:35 UTC · lore

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(-)
Show changes to string-list.c +5 −4
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· Apr 24, 2016, 07:06 UTC · re: Ralf Thielow · lore

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

Hi Ralf,
On Fri, 22 Apr 2016, Ralf Thielow wrote:
Show 7 quoted lines
> 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.
Show 20 quoted lines
> 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· Apr 25, 2016, 17:35 UTC · re: Johannes Schindelin · lore

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

Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
Show 14 quoted lines
>>  
>> -	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· Apr 25, 2016, 17:40 UTC · re: Ralf Thielow · lore

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

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(-)
Show changes to string-list.c +4 −4
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· Apr 25, 2016, 17:47 UTC · re: Ralf Thielow · lore

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

On Mon, Apr 25, 2016 at 10:40 AM, Ralf Thielow <ralf.thielow@gmail.com> wrote:
Show 7 quoted lines
> 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>
Show 28 quoted lines
>
>  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· Apr 26, 2016, 15:42 UTC · re: Ralf Thielow · lore

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

Hi Ralf,
On Mon, 25 Apr 2016, Ralf Thielow wrote:
Show 7 quoted lines
> 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

← back to recent threads