Line | Branch | Exec | Source |
---|---|---|---|
1 | #include "array.h" | ||
2 | #include "log.h" | ||
3 | #include "str-util.h" | ||
4 | #include "xmalloc.h" | ||
5 | |||
6 | // This can be used to collect all prefix-matched strings from a "flat" array | ||
7 | // (i.e. an array of fixed-length char arrays; *not* pointers to char) | ||
8 | 17 | void collect_strings_from_flat_array ( | |
9 | const char *base, | ||
10 | size_t nr_elements, | ||
11 | size_t element_len, | ||
12 | PointerArray *a, | ||
13 | const char *prefix | ||
14 | ) { | ||
15 | 17 | const char *end = base + (nr_elements * element_len); | |
16 | 17 | size_t prefix_len = strlen(prefix); | |
17 |
2/2✓ Branch 0 (7→3) taken 558 times.
✓ Branch 1 (7→8) taken 17 times.
|
575 | for (const char *str = base; str < end; str += element_len) { |
18 |
2/2✓ Branch 0 (3→4) taken 307 times.
✓ Branch 1 (3→6) taken 251 times.
|
558 | if (str_has_strn_prefix(str, prefix, prefix_len)) { |
19 | 307 | ptr_array_append(a, xstrdup(str)); | |
20 | } | ||
21 | } | ||
22 | 17 | } | |
23 | |||
24 | // Return bitflags corresponding to a set of comma-delimited substrings | ||
25 | // found in an array. For example, if the string is "str3,str7" and | ||
26 | // those 2 substrings are found at array[3] and array[7] respectively, | ||
27 | // the returned value will be `1 << 3 | 1 << 7`. | ||
28 | 147 | unsigned int str_to_bitflags ( | |
29 | const char *str, | ||
30 | const char *base, // Pointer to start of char[nstrs][size] array | ||
31 | size_t nstrs, | ||
32 | size_t size, | ||
33 | bool tolerate_errors // Whether to ignore invalid substrings | ||
34 | ) { | ||
35 | // Copy `str` into a mutable buffer, so that get_delim_str() can be | ||
36 | // used to split (and null-terminate) the comma-delimited substrings | ||
37 | 147 | char buf[512]; | |
38 | 147 | const char *end = memccpy(buf, str, '\0', sizeof(buf)); | |
39 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→6) taken 147 times.
|
147 | if (unlikely(!end)) { |
40 | ✗ | LOG_ERROR("flags string too long: %.*s...", 80, str); | |
41 | ✗ | return 0; | |
42 | } | ||
43 | |||
44 | 147 | unsigned int flags = 0; | |
45 |
2/2✓ Branch 0 (16→7) taken 319 times.
✓ Branch 1 (16→17) taken 146 times.
|
465 | for (size_t pos = 0, len = end - buf - 1; pos < len; ) { |
46 | 319 | const char *substr = get_delim_str(buf, &pos, len, ','); | |
47 | 319 | ssize_t idx = find_str_idx(substr, base, nstrs, size, streq); | |
48 |
2/2✓ Branch 0 (9→10) taken 194 times.
✓ Branch 1 (9→14) taken 125 times.
|
319 | if (unlikely(idx < 0)) { |
49 |
2/2✓ Branch 0 (10→11) taken 1 times.
✓ Branch 1 (10→12) taken 193 times.
|
194 | if (!tolerate_errors) { |
50 | 1 | return 0; | |
51 | } | ||
52 | 193 | LOG_WARNING("unrecognized flag string: '%s'", substr); | |
53 | 193 | continue; | |
54 | } | ||
55 | 125 | flags |= 1u << idx; | |
56 | } | ||
57 | |||
58 | 146 | return flags; | |
59 | } | ||
60 |