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 | 8910 | static inline size_t string_array_length(char **strings) | |
15 | { | ||
16 | 8910 | size_t n = 0; | |
17 |
2/2✓ Branch 0 (4→3) taken 20249 times.
✓ Branch 1 (4→5) taken 8910 times.
|
29159 | while (strings[n]) { |
18 | 20249 | n++; | |
19 | } | ||
20 | 8910 | return n; | |
21 | } | ||
22 | |||
23 | 48 | static inline bool string_array_contains_prefix(char **strs, const char *prefix) | |
24 | { | ||
25 | 48 | size_t prefix_len = strlen(prefix); | |
26 |
2/2✓ Branch 0 (5→3) taken 104 times.
✓ Branch 1 (5→6) taken 48 times.
|
152 | for (size_t i = 0; strs[i]; i++) { |
27 |
1/2✓ Branch 0 (3→4) taken 104 times.
✗ Branch 1 (3→6) not taken.
|
104 | if (str_has_strn_prefix(strs[i], prefix, prefix_len)) { |
28 | return true; | ||
29 | } | ||
30 | } | ||
31 | return false; | ||
32 | } | ||
33 | |||
34 | 2 | static inline bool string_array_contains_str(char **strs, const char *str) | |
35 | { | ||
36 |
2/2✓ Branch 0 (5→3) taken 1 times.
✓ Branch 1 (5→6) taken 1 times.
|
2 | for (size_t i = 0; strs[i]; i++) { |
37 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 1 times.
|
1 | if (streq(strs[i], str)) { |
38 | return true; | ||
39 | } | ||
40 | } | ||
41 | return false; | ||
42 | } | ||
43 | |||
44 | 451 | static inline char **copy_string_array(char **src, size_t count) | |
45 | { | ||
46 | 451 | char **dst = xnew(char*, count + 1); | |
47 |
2/2✓ Branch 0 (6→4) taken 735 times.
✓ Branch 1 (6→7) taken 451 times.
|
1186 | for (size_t i = 0; i < count; i++) { |
48 | 735 | dst[i] = xstrdup(src[i]); | |
49 | } | ||
50 | 451 | dst[count] = NULL; | |
51 | 451 | return dst; | |
52 | } | ||
53 | |||
54 | NONNULL_ARGS | ||
55 | 1624 | static inline void free_string_array(char **strings) | |
56 | { | ||
57 |
2/2✓ Branch 0 (4→3) taken 770 times.
✓ Branch 1 (4→5) taken 1624 times.
|
2394 | for (size_t i = 0; strings[i]; i++) { |
58 | 770 | free(strings[i]); | |
59 | } | ||
60 | 1624 | free(strings); | |
61 | 1624 | } | |
62 | |||
63 | #endif | ||
64 |