Line | Branch | Exec | Source |
---|---|---|---|
1 | #ifndef UTIL_XSTRING_H | ||
2 | #define UTIL_XSTRING_H | ||
3 | |||
4 | #include <stdbool.h> | ||
5 | #include <string.h> | ||
6 | #include "ascii.h" | ||
7 | #include "debug.h" | ||
8 | #include "macros.h" | ||
9 | |||
10 | // Return true if null-terminated strings a and b are identical | ||
11 | NONNULL_ARGS | ||
12 | 72393 | static inline bool streq(const char *a, const char *b) | |
13 | { | ||
14 | 72393 | return strcmp(a, b) == 0; | |
15 | } | ||
16 | |||
17 | // Like streq(), but allowing one or both parameters to be NULL | ||
18 | 1972 | static inline bool xstreq(const char *a, const char *b) | |
19 | { | ||
20 |
6/6✓ Branch 0 (2→3) taken 1831 times.
✓ Branch 1 (2→6) taken 141 times.
✓ Branch 2 (3→4) taken 1829 times.
✓ Branch 3 (3→6) taken 2 times.
✓ Branch 4 (4→5) taken 1825 times.
✓ Branch 5 (4→6) taken 4 times.
|
1972 | return (a == b) || (a && b && streq(a, b)); |
21 | } | ||
22 | |||
23 | // Like strrchr(3), but for use when `ch` is known to be present in | ||
24 | // `str` (e.g. when searching for '/' in absolute filenames) | ||
25 | NONNULL_ARGS_AND_RETURN | ||
26 | 7 | static inline const char *xstrrchr(const char *str, int ch) | |
27 | { | ||
28 | 7 | const char *ptr = strrchr(str, ch); | |
29 | 7 | BUG_ON(!ptr); | |
30 | 7 | return ptr; | |
31 | } | ||
32 | |||
33 | // See: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf | ||
34 | 46751 | static inline bool mem_equal(const void *s1, const void *s2, size_t n) | |
35 | { | ||
36 | 46751 | BUG_ON(n && (!s1 || !s2)); | |
37 |
3/4✓ Branch 0 (5→6) taken 46648 times.
✗ Branch 1 (5→8) not taken.
✓ Branch 2 (6→7) taken 43286 times.
✓ Branch 3 (6→8) taken 3362 times.
|
46648 | return n == 0 || memcmp(s1, s2, n) == 0; |
38 | } | ||
39 | |||
40 | // Portable version of glibc/FreeBSD mempcpy(3) | ||
41 | NONNULL_ARGS_AND_RETURN | ||
42 | 4830 | static inline void *xmempcpy(void *restrict dest, const void *restrict src, size_t n) | |
43 | { | ||
44 | 4830 | memcpy(dest, src, n); | |
45 | 4830 | return (char*)dest + n; | |
46 | } | ||
47 | |||
48 | NONNULL_ARGS_AND_RETURN | ||
49 | 2036 | static inline void *xmempcpy2 ( | |
50 | void *dest, | ||
51 | const void *p1, size_t n1, | ||
52 | const void *p2, size_t n2 | ||
53 | ) { | ||
54 | 2036 | return xmempcpy(xmempcpy(dest, p1, n1), p2, n2); | |
55 | } | ||
56 | |||
57 | NONNULL_ARGS_AND_RETURN | ||
58 | 212 | static inline void *xmempcpy3 ( // NOLINT(readability-function-size) | |
59 | void *dest, | ||
60 | const void *p1, size_t n1, | ||
61 | const void *p2, size_t n2, | ||
62 | const void *p3, size_t n3 | ||
63 | ) { | ||
64 | 212 | return xmempcpy(xmempcpy2(dest, p1, n1, p2, n2), p3, n3); | |
65 | } | ||
66 | |||
67 | NONNULL_ARGS_AND_RETURN | ||
68 | 117 | static inline void *xmempcpy4 ( // NOLINT(readability-function-size) | |
69 | void *dest, | ||
70 | const void *p1, size_t n1, | ||
71 | const void *p2, size_t n2, | ||
72 | const void *p3, size_t n3, | ||
73 | const void *p4, size_t n4 | ||
74 | ) { | ||
75 | 117 | return xmempcpy(xmempcpy3(dest, p1, n1, p2, n2, p3, n3), p4, n4); | |
76 | } | ||
77 | |||
78 | 64 | static inline bool mem_equal_icase(const void *p1, const void *p2, size_t n) | |
79 | { | ||
80 |
2/2✓ Branch 0 (2→3) taken 60 times.
✓ Branch 1 (2→7) taken 4 times.
|
64 | if (n == 0) { |
81 | return true; | ||
82 | } | ||
83 | |||
84 | 60 | const unsigned char *s1 = p1; | |
85 | 60 | const unsigned char *s2 = p2; | |
86 | 60 | BUG_ON(!s1 || !s2); | |
87 | |||
88 |
2/2✓ Branch 0 (6→5) taken 207 times.
✓ Branch 1 (6→7) taken 24 times.
|
231 | while (n--) { |
89 |
2/2✓ Branch 0 (5→6) taken 171 times.
✓ Branch 1 (5→7) taken 36 times.
|
207 | if (ascii_tolower(*s1++) != ascii_tolower(*s2++)) { |
90 | return false; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | return true; | ||
95 | } | ||
96 | |||
97 | #endif | ||
98 |