strv: introduce strv_sort_uniq()
We often call strv_sort() and strv_uniq(). If a strv is already sorted. uniquifying can be faster. Prompted by https://github.com/systemd/systemd/pull/33012#discussion_r1636633627.
This commit is contained in:
@@ -704,6 +704,21 @@ char** strv_sort(char **l) {
|
||||
return l;
|
||||
}
|
||||
|
||||
char** strv_sort_uniq(char **l) {
|
||||
if (strv_isempty(l))
|
||||
return l;
|
||||
|
||||
char **tail = strv_sort(l), *prev = NULL;
|
||||
STRV_FOREACH(i, l)
|
||||
if (streq_ptr(*i, prev))
|
||||
free(*i);
|
||||
else
|
||||
*(tail++) = prev = *i;
|
||||
|
||||
*tail = NULL;
|
||||
return l;
|
||||
}
|
||||
|
||||
int strv_compare(char * const *a, char * const *b) {
|
||||
int r;
|
||||
|
||||
|
||||
@@ -161,6 +161,7 @@ bool strv_overlap(char * const *a, char * const *b) _pure_;
|
||||
_STRV_FOREACH_PAIR(x, y, l, UNIQ_T(i, UNIQ))
|
||||
|
||||
char** strv_sort(char **l);
|
||||
char** strv_sort_uniq(char **l);
|
||||
void strv_print_full(char * const *l, const char *prefix);
|
||||
static inline void strv_print(char * const *l) {
|
||||
strv_print_full(l, NULL);
|
||||
|
||||
Reference in New Issue
Block a user