Skip to contentSkip to conversation

string_list: use string-list API in unsorted_string_list_lookup()

PATCH Superseded Ralf Thielow 3 participants 6 messages Jun 16, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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. So let's do this.

---
 string-list.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
string-list.c +5 -4
index 2a32a3f..8127e12 100644
@@ -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;
 }
 
Signature · 1 line
2.8.1.382.g1352ede
#1 lore raw

Hi Ralf,

Quoted 7 lines · Ralf Thielow

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

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

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

Quoted 10 lines
string-list.c +1 -1
index 2a32a3f..8127e12 100644
@@ -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;

string-list.c void string_list_sort(struct string_list *list) · line 241

Quoted 10 lines
string-list.c +4 -3
 	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

#2 lore raw
Quoted 14 lines · Johannes Schindelin

string-list.c void string_list_sort(struct string_list *list) · line 241

Quoted 9 lines
string-list.c +4 -3
-	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!

Quoted 4 lines

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

Ciao,
Dscho

#3 lore raw

PATCH v2 1/1 string_list: use string-list API in unsorted_string_list_lookup()

In reply to Ralf Thielow

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

---
Changes since v1:
- remove extra curly braces

 string-list.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
string-list.c +4 -4
index 2a32a3f..62d2084 100644
@@ -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;
 }
 
Signature · 1 line
2.8.1.430.g7c694c5
Reviewed-by on v2 1/1
#4 lore raw
Quoted 7 lines · Ralf Thielow

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

---
Changes since v1:
- remove extra curly braces

Quoted 28 lines
 string-list.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
string-list.c +4 -5
index 2a32a3f..62d2084 100644
@@ -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