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:
Yu Watanabe
2024-06-18 00:22:33 +09:00
parent 1b3449d8c1
commit 3dc546ad75
3 changed files with 91 additions and 0 deletions
+15
View File
@@ -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;
+1
View File
@@ -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);