Skip to content

Commit e242148

Browse files
bebarinogitster
authored andcommitted
string-list: add unsorted_string_list_lookup()
Sometimes users need to lookup a string in an unsorted string_list. In that case they should use this function instead of the version for sorted strings. Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 15cb500 commit e242148

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

Documentation/technical/api-string-list.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ write `string_list_insert(...)->util = ...;`.
104104
`unsorted_string_list_has_string`::
105105

106106
It's like `string_list_has_string()` but for unsorted lists.
107+
108+
`unsorted_string_list_lookup`::
109+
110+
It's like `string_list_lookup()` but for unsorted lists.
107111
+
108-
This function needs to look through all items, as opposed to its
112+
The above two functions need to look through all items, as opposed to their
109113
counterpart for sorted lists, which performs a binary search.
110114

111115
Data structures

string-list.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,19 @@ void sort_string_list(struct string_list *list)
168168
qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
169169
}
170170

171-
int unsorted_string_list_has_string(struct string_list *list, const char *string)
171+
struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
172+
const char *string)
172173
{
173174
int i;
174175
for (i = 0; i < list->nr; i++)
175176
if (!strcmp(string, list->items[i].string))
176-
return 1;
177-
return 0;
177+
return list->items + i;
178+
return NULL;
179+
}
180+
181+
int unsorted_string_list_has_string(struct string_list *list,
182+
const char *string)
183+
{
184+
return unsorted_string_list_lookup(list, string) != NULL;
178185
}
179186

string-list.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ struct string_list_item *string_list_lookup(const char *string, struct string_li
3838
struct string_list_item *string_list_append(const char *string, struct string_list *list);
3939
void sort_string_list(struct string_list *list);
4040
int unsorted_string_list_has_string(struct string_list *list, const char *string);
41-
41+
struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
42+
const char *string);
4243
#endif /* STRING_LIST_H */

0 commit comments

Comments
 (0)