dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 37 / 0 / 37
Functions: 100.0% 7 / 0 / 7
Branches: 91.7% 22 / 4 / 28

src/util/str-array.h
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 <string.h>
10 #include "debug.h"
11 #include "macros.h"
12 #include "str-util.h"
13 #include "string-view.h"
14 #include "xmalloc.h"
15 #include "xstring.h"
16
17 11594 static inline size_t string_array_length(char **strings)
18 {
19 11594 size_t n = 0;
20
2/2
✓ Branch 4 → 3 taken 25332 times.
✓ Branch 4 → 5 taken 11594 times.
36926 while (strings[n]) {
21 25332 n++;
22 }
23 11594 return n;
24 }
25
26 41 static inline bool string_array_contains_prefix(char **strs, const char *prefix)
27 {
28 41 size_t prefix_len = strlen(prefix);
29
2/2
✓ Branch 5 → 3 taken 118 times.
✓ Branch 5 → 6 taken 41 times.
159 for (size_t i = 0; strs[i]; i++) {
30
1/2
✓ Branch 3 → 4 taken 118 times.
✗ Branch 3 → 6 not taken.
118 if (str_has_strn_prefix(strs[i], prefix, prefix_len)) {
31 return true;
32 }
33 }
34 return false;
35 }
36
37 2 static inline bool string_array_contains_str(char **strs, const char *str)
38 {
39
2/2
✓ Branch 5 → 3 taken 1 time.
✓ Branch 5 → 6 taken 1 time.
2 for (size_t i = 0; strs[i]; i++) {
40
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 1 time.
1 if (streq(strs[i], str)) {
41 return true;
42 }
43 }
44 return false;
45 }
46
47 548 static inline char **copy_string_array(char **src, size_t count)
48 {
49 548 char **dst = xmallocarray(count + 1, sizeof(*dst));
50
2/2
✓ Branch 6 → 4 taken 1163 times.
✓ Branch 6 → 7 taken 548 times.
2259 for (size_t i = 0; i < count; i++) {
51 1163 dst[i] = xstrdup(src[i]);
52 }
53 548 dst[count] = NULL;
54 548 return dst;
55 }
56
57 // Concatenates an array of strings into a fixed-size buffer, as a single,
58 // delimiter-separated string. This is separate from string_array_concat()
59 // only as a convenience for testing.
60 WARN_UNUSED_RESULT NONNULL_ARGS
61 100 static inline bool string_array_concat_ (
62 char *buf, size_t bufsize,
63 const char *const *strs, size_t nstrs,
64 StringView delim
65 ) {
66 100 BUG_ON(bufsize == 0);
67 100 const char *end = buf + bufsize;
68 100 char *ptr = buf;
69 100 buf[0] = '\0'; // Always null-terminate `buf`, even if `nstrs == 0`
70
71
2/2
✓ Branch 12 → 5 taken 543 times.
✓ Branch 12 → 13 taken 71 times.
614 for (size_t i = 0; i < nstrs; i++) {
72 543 bool last_iter = (i + 1 == nstrs);
73 543 ptr = memccpy(ptr, strs[i], '\0', end - ptr);
74
6/6
✓ Branch 6 → 7 taken 534 times.
✓ Branch 6 → 13 taken 9 times.
✓ Branch 7 → 8 taken 466 times.
✓ Branch 7 → 9 taken 68 times.
✓ Branch 8 → 9 taken 446 times.
✓ Branch 8 → 13 taken 20 times.
543 if (unlikely(!ptr || (!last_iter && ptr + delim.length > end))) {
75 return false;
76 }
77 // Append `delim` (over the copied '\0'), if not on the last iteration
78
2/2
✓ Branch 9 → 10 taken 446 times.
✓ Branch 9 → 11 taken 68 times.
514 ptr = last_iter ? ptr : xmempcpy(ptr - 1, delim.data, delim.length);
79 }
80
81 return true;
82 }
83
84 // Like string_array_concat_(), but asserting that it should never return
85 // false (i.e. because the required `bufsize` is known at compile-time)
86 68 static inline void string_array_concat (
87 char *buf, size_t bufsize,
88 const char *const *strs, size_t nstrs,
89 StringView delim
90 ) {
91 68 bool r = string_array_concat_(buf, bufsize, strs, nstrs, delim);
92 68 BUG_ON(!r);
93 68 }
94
95 NONNULL_ARGS
96 2495 static inline void free_string_array(char **strings)
97 {
98
2/2
✓ Branch 4 → 3 taken 1221 times.
✓ Branch 4 → 5 taken 2495 times.
3716 for (size_t i = 0; strings[i]; i++) {
99 1221 free(strings[i]);
100 }
101 2495 free(strings);
102 2495 }
103
104 #endif
105