Line | Branch | Exec | Source |
---|---|---|---|
1 | #ifndef UTIL_STR_ARRAY_H | ||
2 | #define UTIL_STR_ARRAY_H | ||
3 | |||
4 | // Utility functions for working with null-terminated arrays of null-terminated | ||
5 | // strings, like e.g. the "argv" argument passed to main() | ||
6 | |||
7 | #include <stdbool.h> | ||
8 | #include <stdlib.h> | ||
9 | #include "macros.h" | ||
10 | #include "str-util.h" | ||
11 | #include "xmalloc.h" | ||
12 | #include "xstring.h" | ||
13 | |||
14 | 8594 | static inline size_t string_array_length(char **strings) | |
15 | { | ||
16 | 8594 | size_t n = 0; | |
17 |
2/2✓ Branch 0 taken 19767 times.
✓ Branch 1 taken 8594 times.
|
28361 | while (strings[n]) { |
18 | 19767 | n++; | |
19 | } | ||
20 | 8594 | return n; | |
21 | } | ||
22 | |||
23 | 46 | static inline bool string_array_contains_prefix(char **strs, const char *prefix) | |
24 | { | ||
25 |
2/2✓ Branch 0 taken 100 times.
✓ Branch 1 taken 46 times.
|
146 | for (size_t i = 0; strs[i]; i++) { |
26 |
1/2✓ Branch 0 taken 100 times.
✗ Branch 1 not taken.
|
100 | if (str_has_prefix(strs[i], prefix)) { |
27 | return true; | ||
28 | } | ||
29 | } | ||
30 | return false; | ||
31 | } | ||
32 | |||
33 | 2 | static inline bool string_array_contains_str(char **strs, const char *str) | |
34 | { | ||
35 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (size_t i = 0; strs[i]; i++) { |
36 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (streq(strs[i], str)) { |
37 | return true; | ||
38 | } | ||
39 | } | ||
40 | return false; | ||
41 | } | ||
42 | |||
43 | 439 | static inline char **copy_string_array(char **src, size_t count) | |
44 | { | ||
45 | 439 | char **dst = xnew(char*, count + 1); | |
46 |
2/2✓ Branch 0 taken 711 times.
✓ Branch 1 taken 439 times.
|
1150 | for (size_t i = 0; i < count; i++) { |
47 | 711 | dst[i] = xstrdup(src[i]); | |
48 | } | ||
49 | 439 | dst[count] = NULL; | |
50 | 439 | return dst; | |
51 | } | ||
52 | |||
53 | NONNULL_ARGS | ||
54 | 1612 | static inline void free_string_array(char **strings) | |
55 | { | ||
56 |
2/2✓ Branch 0 taken 746 times.
✓ Branch 1 taken 1612 times.
|
2358 | for (size_t i = 0; strings[i]; i++) { |
57 | 746 | free(strings[i]); | |
58 | } | ||
59 | 1612 | free(strings); | |
60 | 1612 | } | |
61 | |||
62 | #endif | ||
63 |