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 | 63807 | static inline bool streq(const char *a, const char *b) | |
13 | { | ||
14 | 63807 | return strcmp(a, b) == 0; | |
15 | } | ||
16 | |||
17 | // Like streq(), but allowing one or both parameters to be NULL | ||
18 | 1836 | static inline bool xstreq(const char *a, const char *b) | |
19 | { | ||
20 |
6/6✓ Branch 0 (2→3) taken 1694 times.
✓ Branch 1 (2→6) taken 142 times.
✓ Branch 2 (3→4) taken 1692 times.
✓ Branch 3 (3→6) taken 2 times.
✓ Branch 4 (4→5) taken 1688 times.
✓ Branch 5 (4→6) taken 4 times.
|
1836 | 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/n3261.pdf | ||
34 | 37501 | static inline bool mem_equal(const void *s1, const void *s2, size_t n) | |
35 | { | ||
36 | 37501 | BUG_ON(n && (!s1 || !s2)); | |
37 |
3/4✓ Branch 0 (5→6) taken 37480 times.
✗ Branch 1 (5→8) not taken.
✓ Branch 2 (6→7) taken 34591 times.
✓ Branch 3 (6→8) taken 2889 times.
|
37480 | return n == 0 || memcmp(s1, s2, n) == 0; |
38 | } | ||
39 | |||
40 | 64 | static inline bool mem_equal_icase(const void *p1, const void *p2, size_t n) | |
41 | { | ||
42 |
2/2✓ Branch 0 (2→3) taken 60 times.
✓ Branch 1 (2→7) taken 4 times.
|
64 | if (n == 0) { |
43 | return true; | ||
44 | } | ||
45 | |||
46 | 60 | const unsigned char *s1 = p1; | |
47 | 60 | const unsigned char *s2 = p2; | |
48 | 60 | BUG_ON(!s1 || !s2); | |
49 | |||
50 |
2/2✓ Branch 0 (6→5) taken 207 times.
✓ Branch 1 (6→7) taken 24 times.
|
231 | while (n--) { |
51 |
2/2✓ Branch 0 (5→6) taken 171 times.
✓ Branch 1 (5→7) taken 36 times.
|
207 | if (ascii_tolower(*s1++) != ascii_tolower(*s2++)) { |
52 | return false; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | return true; | ||
57 | } | ||
58 | |||
59 | #endif | ||
60 |