Debian Codegraph @

src/fundamental/string-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #if SD_BOOT
5 # include "efi.h"
6 # include "efi-string.h"
7 #else
8 # include <string.h>
9 #endif
10
11 #include "assert-util.h" /* IWYU pragma: keep */
12 #include "macro.h"
13
14 /* What is interpreted as whitespace? */
15 #define WHITESPACE " \t\n\r"
16 #define NEWLINE "\n\r"
17 #define QUOTES "\"\'"
18 #define COMMENTS "#;"
19 #define GLOB_CHARS "*?["
20 #define DIGITS "0123456789"
21 #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
22 #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
23 #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
24 #define ALPHANUMERICAL LETTERS DIGITS
25 #define HEXDIGITS DIGITS "abcdefABCDEF"
26 #define LOWERCASE_HEXDIGITS DIGITS "abcdef"
27 #define UPPERCASE_HEXDIGITS DIGITS "ABCDEF"
28 #define URI_RESERVED ":/?#[]@!$&'()*+;=" /* [RFC3986] */
29 #define URI_UNRESERVED ALPHANUMERICAL "-._~" /* [RFC3986] */
30 #define URI_VALID URI_RESERVED URI_UNRESERVED /* [RFC3986] */
31
32 #if SD_BOOT
33 # define strlen strlen16
34 # define strcmp strcmp16
35 # define strncmp strncmp16
36 # define strcasecmp strcasecmp16
37 # define strncasecmp strncasecmp16
38 # define strspn strspn16
39 # define strcspn strcspn16
40 # define STR_C(str) (L ## str)
41 typedef char16_t sd_char;
42 #else
43 # define STR_C(str) (str)
44 typedef char sd_char;
45 #endif
46
47 #define streq(a,b) (strcmp((a),(b)) == 0)
48 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
49 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
50 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
51
52 static inline int strcmp_ptr(const sd_char *a, const sd_char *b) {
53 if (a && b)
54 return strcmp(a, b);
55
56 return CMP(a, b);
57 }
58
59 static inline int strncmp_ptr(const sd_char *a, const sd_char *b, size_t n) {
60 if (a && b)
61 return strncmp(a, b, n);
62
63 return CMP(a, b);
64 }
65
66 static inline int strcasecmp_ptr(const sd_char *a, const sd_char *b) {
67 if (a && b)
68 return strcasecmp(a, b);
69
70 return CMP(a, b);
71 }
72
73 static inline bool streq_ptr(const sd_char *a, const sd_char *b) {
74 return strcmp_ptr(a, b) == 0;
75 }
76
77 static inline bool strneq_ptr(const sd_char *a, const sd_char *b, size_t n) {
78 return strncmp_ptr(a, b, n) == 0;
79 }
80
81 static inline bool strcaseeq_ptr(const sd_char *a, const sd_char *b) {
82 return strcasecmp_ptr(a, b) == 0;
83 }
84
85 static inline size_t strlen_ptr(const sd_char *s) {
86 if (!s)
87 return 0;
88
89 return strlen(s);
90 }
91
92 sd_char *startswith_internal(const sd_char *s, const sd_char *prefix) _pure_;
93 #define startswith(s, prefix) const_generic(s, startswith_internal(s, prefix))
94
95 sd_char *startswith_no_case_internal(const sd_char *s, const sd_char *prefix) _pure_;
96 #define startswith_no_case(s, prefix) const_generic(s, startswith_no_case_internal(s, prefix))
97
98 sd_char *endswith_internal(const sd_char *s, const sd_char *suffix) _pure_;
99 #define endswith(s, suffix) const_generic(s, endswith_internal(s, suffix))
100
101 sd_char *endswith_no_case_internal(const sd_char *s, const sd_char *suffix) _pure_;
102 #define endswith_no_case(s, suffix) const_generic(s, endswith_no_case_internal(s, suffix))
103
104 static inline bool isempty(const sd_char *a) {
105 return !a || a[0] == '\0';
106 }
107
108 static inline const sd_char *strempty(const sd_char *s) {
109 return s ?: STR_C("");
110 }
111
112 static inline const sd_char *yes_no(bool b) {
113 return b ? STR_C("yes") : STR_C("no");
114 }
115
116 static inline const sd_char *on_off(bool b) {
117 return b ? STR_C("on") : STR_C("off");
118 }
119
120 static inline const sd_char* comparison_operator(int result) {
121 return result < 0 ? STR_C("<") : result > 0 ? STR_C(">") : STR_C("==");
122 }
123
124 int strverscmp_improved(const sd_char *a, const sd_char *b);
125
126 /* Like startswith(), but operates on arbitrary memory blocks */
127 static inline void *memory_startswith(const void *p, size_t sz, const sd_char *token) {
128 assert(token);
129
130 size_t n = strlen(token) * sizeof(sd_char);
131 if (sz < n)
132 return NULL;
133
134 assert(p);
135
136 if (memcmp(p, token, n) != 0)
137 return NULL;
138
139 return (uint8_t*) p + n;
140 }
141
142 static inline bool ascii_isdigit(sd_char a) {
143 /* A pure ASCII, locale independent version of isdigit() */
144 return a >= '0' && a <= '9';
145 }
146
147 static inline bool ascii_ishex(sd_char a) {
148 return ascii_isdigit(a) || (a >= 'a' && a <= 'f') || (a >= 'A' && a <= 'F');
149 }
150
151 static inline bool ascii_isalpha(sd_char a) {
152 /* A pure ASCII, locale independent version of isalpha() */
153 return (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z');
154 }