/src/systemd/src/fundamental/string-util-fundamental.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | | #pragma once |
3 | | |
4 | | #ifdef SD_BOOT |
5 | | # include <efi.h> |
6 | | # include <efilib.h> |
7 | | # include "efi-string.h" |
8 | | #else |
9 | | # include <string.h> |
10 | | #endif |
11 | | |
12 | | #include "macro-fundamental.h" |
13 | | |
14 | | #ifdef SD_BOOT |
15 | | # define strlen strlen16 |
16 | | # define strcmp strcmp16 |
17 | | # define strncmp strncmp16 |
18 | | # define strcasecmp strcasecmp16 |
19 | | # define strncasecmp strncasecmp16 |
20 | | # define STR_C(str) (L ## str) |
21 | | typedef char16_t sd_char; |
22 | | #else |
23 | 0 | # define STR_C(str) (str) |
24 | | typedef char sd_char; |
25 | | #endif |
26 | | |
27 | | #define streq(a,b) (strcmp((a),(b)) == 0) |
28 | | #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0) |
29 | | #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0) |
30 | | #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0) |
31 | | |
32 | 0 | static inline int strcmp_ptr(const sd_char *a, const sd_char *b) { |
33 | 0 | if (a && b) |
34 | 0 | return strcmp(a, b); |
35 | 0 |
|
36 | 0 | return CMP(a, b); |
37 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:strcmp_ptr Unexecuted instantiation: link-config.c:strcmp_ptr Unexecuted instantiation: link-config-gperf.c:strcmp_ptr |
38 | | |
39 | 0 | static inline int strcasecmp_ptr(const sd_char *a, const sd_char *b) { |
40 | 0 | if (a && b) |
41 | 0 | return strcasecmp(a, b); |
42 | 0 |
|
43 | 0 | return CMP(a, b); |
44 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:strcasecmp_ptr Unexecuted instantiation: link-config.c:strcasecmp_ptr Unexecuted instantiation: link-config-gperf.c:strcasecmp_ptr |
45 | | |
46 | 0 | static inline bool streq_ptr(const sd_char *a, const sd_char *b) { |
47 | 0 | return strcmp_ptr(a, b) == 0; |
48 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:streq_ptr Unexecuted instantiation: link-config.c:streq_ptr Unexecuted instantiation: link-config-gperf.c:streq_ptr |
49 | | |
50 | 0 | static inline bool strcaseeq_ptr(const sd_char *a, const sd_char *b) { |
51 | 0 | return strcasecmp_ptr(a, b) == 0; |
52 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:strcaseeq_ptr Unexecuted instantiation: link-config.c:strcaseeq_ptr Unexecuted instantiation: link-config-gperf.c:strcaseeq_ptr |
53 | | |
54 | 0 | static inline size_t strlen_ptr(const sd_char *s) { |
55 | 0 | if (!s) |
56 | 0 | return 0; |
57 | 0 |
|
58 | 0 | return strlen(s); |
59 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:strlen_ptr Unexecuted instantiation: link-config.c:strlen_ptr Unexecuted instantiation: link-config-gperf.c:strlen_ptr |
60 | | |
61 | | sd_char *startswith(const sd_char *s, const sd_char *prefix) _pure_; |
62 | | #ifndef SD_BOOT |
63 | | sd_char *startswith_no_case(const sd_char *s, const sd_char *prefix) _pure_; |
64 | | #endif |
65 | | sd_char *endswith(const sd_char *s, const sd_char *postfix) _pure_; |
66 | | sd_char *endswith_no_case(const sd_char *s, const sd_char *postfix) _pure_; |
67 | | |
68 | 6.58k | static inline bool isempty(const sd_char *a) { |
69 | 6.58k | return !a || a[0] == '\0'; |
70 | 6.58k | } Unexecuted instantiation: fuzz-link-parser.c:isempty Line | Count | Source | 68 | 6.58k | static inline bool isempty(const sd_char *a) { | 69 | 6.58k | return !a || a[0] == '\0'; | 70 | 6.58k | } |
Unexecuted instantiation: link-config-gperf.c:isempty |
71 | | |
72 | 0 | static inline const sd_char *strempty(const sd_char *s) { |
73 | 0 | return s ?: STR_C(""); |
74 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:strempty Unexecuted instantiation: link-config.c:strempty Unexecuted instantiation: link-config-gperf.c:strempty |
75 | | |
76 | 0 | static inline const sd_char *yes_no(bool b) { |
77 | 0 | return b ? STR_C("yes") : STR_C("no"); |
78 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:yes_no Unexecuted instantiation: link-config.c:yes_no Unexecuted instantiation: link-config-gperf.c:yes_no |
79 | | |
80 | 0 | static inline const sd_char* comparison_operator(int result) { |
81 | 0 | return result < 0 ? STR_C("<") : result > 0 ? STR_C(">") : STR_C("=="); |
82 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:comparison_operator Unexecuted instantiation: link-config.c:comparison_operator Unexecuted instantiation: link-config-gperf.c:comparison_operator |
83 | | |
84 | | int strverscmp_improved(const sd_char *a, const sd_char *b); |
85 | | |
86 | | /* Like startswith(), but operates on arbitrary memory blocks */ |
87 | 0 | static inline void *memory_startswith(const void *p, size_t sz, const sd_char *token) { |
88 | 0 | assert(token); |
89 | 0 |
|
90 | 0 | size_t n = strlen(token) * sizeof(sd_char); |
91 | 0 | if (sz < n) |
92 | 0 | return NULL; |
93 | 0 |
|
94 | 0 | assert(p); |
95 | 0 |
|
96 | 0 | if (memcmp(p, token, n) != 0) |
97 | 0 | return NULL; |
98 | 0 |
|
99 | 0 | return (uint8_t*) p + n; |
100 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:memory_startswith Unexecuted instantiation: link-config.c:memory_startswith Unexecuted instantiation: link-config-gperf.c:memory_startswith |
101 | | |
102 | | #define _STRV_FOREACH(s, l, i) \ |
103 | 0 | for (typeof(*(l)) *s, *i = (l); (s = i) && *i; i++) |
104 | | |
105 | | #define STRV_FOREACH(s, l) \ |
106 | 0 | _STRV_FOREACH(s, l, UNIQ_T(i, UNIQ)) |
107 | | |
108 | 0 | static inline bool ascii_isdigit(sd_char a) { |
109 | 0 | /* A pure ASCII, locale independent version of isdigit() */ |
110 | 0 | return a >= '0' && a <= '9'; |
111 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:ascii_isdigit Unexecuted instantiation: link-config.c:ascii_isdigit Unexecuted instantiation: link-config-gperf.c:ascii_isdigit |
112 | | |
113 | 0 | static inline bool ascii_isalpha(sd_char a) { |
114 | 0 | /* A pure ASCII, locale independent version of isalpha() */ |
115 | 0 | return (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z'); |
116 | 0 | } Unexecuted instantiation: fuzz-link-parser.c:ascii_isalpha Unexecuted instantiation: link-config.c:ascii_isalpha Unexecuted instantiation: link-config-gperf.c:ascii_isalpha |