src/util/xstring.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef UTIL_XSTRING_H | ||
| 2 | #define UTIL_XSTRING_H | ||
| 3 | |||
| 4 | // Several of the *mem*() wrapper functions below explicitly allow NULL | ||
| 5 | // pointer arguments when the corresponding length bound is 0. This is | ||
| 6 | // unlike the equivalent/underlying library functions (where it would | ||
| 7 | // ordinarily be undefined behavior) and is done in the spirit of the | ||
| 8 | // WG14 N3322 proposal. | ||
| 9 | // See also: | ||
| 10 | // • https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3322.pdf | ||
| 11 | |||
| 12 | #include <stdbool.h> | ||
| 13 | #include <string.h> | ||
| 14 | #include "ascii.h" | ||
| 15 | #include "debug.h" | ||
| 16 | #include "macros.h" | ||
| 17 | |||
| 18 | // Return true if null-terminated strings a and b are identical | ||
| 19 | NONNULL_ARGS | ||
| 20 | 72893 | static inline bool streq(const char *a, const char *b) | |
| 21 | { | ||
| 22 | 72893 | return strcmp(a, b) == 0; | |
| 23 | } | ||
| 24 | |||
| 25 | // Like streq(), but allowing one or both parameters to be NULL | ||
| 26 | 2008 | static inline bool xstreq(const char *a, const char *b) | |
| 27 | { | ||
| 28 |
6/6✓ Branch 2 → 3 taken 1867 times.
✓ Branch 2 → 6 taken 141 times.
✓ Branch 3 → 4 taken 1863 times.
✓ Branch 3 → 6 taken 4 times.
✓ Branch 4 → 5 taken 4 times.
✓ Branch 4 → 6 taken 1859 times.
|
2008 | return (a == b) || (a && b && streq(a, b)); |
| 29 | } | ||
| 30 | |||
| 31 | // Like strrchr(3), but for use when `ch` is known to be present in | ||
| 32 | // `str` (e.g. when searching for '/' in absolute filenames) | ||
| 33 | NONNULL_ARGS_AND_RETURN | ||
| 34 | 5 | static inline const char *xstrrchr(const char *str, int ch) | |
| 35 | { | ||
| 36 | 5 | const char *ptr = strrchr(str, ch); | |
| 37 | 5 | BUG_ON(!ptr); | |
| 38 | 5 | return ptr; | |
| 39 | } | ||
| 40 | |||
| 41 | PURE NONNULL_ARG_IF_NONZERO_LENGTH(1, 3) | ||
| 42 | 79 | static inline void *xmemchr(const void *ptr, int c, size_t n) | |
| 43 | { | ||
| 44 | // NOLINTNEXTLINE(readability-redundant-casting) | ||
| 45 |
2/2✓ Branch 2 → 3 taken 77 times.
✓ Branch 2 → 4 taken 2 times.
|
79 | return likely(n) ? (void*)memchr(ptr, c, n) : NULL; |
| 46 | } | ||
| 47 | |||
| 48 | NONNULL_ARG_IF_NONZERO_LENGTH(1, 3) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3) | ||
| 49 | 53755 | static inline bool mem_equal(const void *s1, const void *s2, size_t n) | |
| 50 | { | ||
| 51 | 53755 | BUG_ON(n && (!s1 || !s2)); // See N3322 reference above | |
| 52 |
3/4✓ Branch 5 → 6 taken 52955 times.
✗ Branch 5 → 8 not taken.
✓ Branch 6 → 7 taken 46513 times.
✓ Branch 6 → 8 taken 6442 times.
|
52955 | return n == 0 || memcmp(s1, s2, n) == 0; |
| 53 | } | ||
| 54 | |||
| 55 | NONNULL_ARG_IF_NONZERO_LENGTH(1, 3) NONNULL_ARG_IF_NONZERO_LENGTH(2, 3) | ||
| 56 | 67 | static inline bool mem_equal_icase(const void *p1, const void *p2, size_t n) | |
| 57 | { | ||
| 58 | 67 | BUG_ON(n && (!p1 || !p2)); // See N3322 reference above | |
| 59 |
2/2✓ Branch 7 → 5 taken 207 times.
✓ Branch 7 → 8 taken 31 times.
|
238 | for (const char *s1 = p1, *s2 = p2; n--; ) { |
| 60 |
2/2✓ Branch 5 → 7 taken 171 times.
✓ Branch 5 → 8 taken 36 times.
|
207 | if (ascii_tolower(*s1++) != ascii_tolower(*s2++)) { |
| 61 | return false; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | return true; | ||
| 66 | } | ||
| 67 | |||
| 68 | // Portable version of glibc/FreeBSD mempcpy(3) | ||
| 69 | NONNULL_ARG(1) RETURNS_NONNULL NONNULL_ARG_IF_NONZERO_LENGTH(2, 3) | ||
| 70 | 9785 | static inline void *xmempcpy(void *restrict dest, const void *restrict src, size_t n) | |
| 71 | { | ||
| 72 | 9785 | BUG_ON(n && !src); // See N3322 reference above | |
| 73 |
2/2✓ Branch 4 → 5 taken 9739 times.
✓ Branch 4 → 6 taken 46 times.
|
9785 | return likely(n) ? (char*)memcpy(dest, src, n) + n : dest; |
| 74 | } | ||
| 75 | |||
| 76 | NONNULL_ARG(1) RETURNS_NONNULL | ||
| 77 | 2218 | static inline void *xmempcpy2 ( | |
| 78 | void *dest, | ||
| 79 | const void *p1, size_t n1, | ||
| 80 | const void *p2, size_t n2 | ||
| 81 | ) { | ||
| 82 | 2218 | return xmempcpy(xmempcpy(dest, p1, n1), p2, n2); | |
| 83 | } | ||
| 84 | |||
| 85 | NONNULL_ARG(1) RETURNS_NONNULL | ||
| 86 | 250 | static inline void *xmempcpy3 ( // NOLINT(readability-function-size) | |
| 87 | void *dest, | ||
| 88 | const void *p1, size_t n1, | ||
| 89 | const void *p2, size_t n2, | ||
| 90 | const void *p3, size_t n3 | ||
| 91 | ) { | ||
| 92 | 250 | return xmempcpy(xmempcpy2(dest, p1, n1, p2, n2), p3, n3); | |
| 93 | } | ||
| 94 | |||
| 95 | NONNULL_ARG(1) RETURNS_NONNULL | ||
| 96 | 162 | static inline void *xmempcpy4 ( // NOLINT(readability-function-size) | |
| 97 | void *dest, | ||
| 98 | const void *p1, size_t n1, | ||
| 99 | const void *p2, size_t n2, | ||
| 100 | const void *p3, size_t n3, | ||
| 101 | const void *p4, size_t n4 | ||
| 102 | ) { | ||
| 103 | 162 | return xmempcpy(xmempcpy3(dest, p1, n1, p2, n2, p3, n3), p4, n4); | |
| 104 | } | ||
| 105 | |||
| 106 | #endif | ||
| 107 |