1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 /* What is interpreted as whitespace? */
12 #define WHITESPACE " \t\n\r"
13 #define NEWLINE "\n\r"
16 #define GLOB_CHARS "*?["
17 #define DIGITS "0123456789"
18 #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
19 #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
20 #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
21 #define ALPHANUMERICAL LETTERS DIGITS
22 #define HEXDIGITS DIGITS "abcdefABCDEF"
24 #define streq(a,b) (strcmp((a),(b)) == 0)
25 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
26 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
27 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
29 int strcmp_ptr(const char *a, const char *b) _pure_;
31 static inline bool streq_ptr(const char *a, const char *b) {
32 return strcmp_ptr(a, b) == 0;
35 static inline const char* strempty(const char *s) {
39 static inline const char* strnull(const char *s) {
43 static inline const char *strna(const char *s) {
47 static inline bool isempty(const char *p) {
51 static inline const char *empty_to_null(const char *p) {
52 return isempty(p) ? NULL : p;
55 #if 0 /// UNNEEDED by elogind
56 static inline const char *empty_to_dash(const char *str) {
57 return isempty(str) ? "-" : str;
61 static inline char *startswith(const char *s, const char *prefix) {
65 if (strncmp(s, prefix, l) == 0)
71 #if 0 /// UNNEEDED by elogind
72 static inline char *startswith_no_case(const char *s, const char *prefix) {
76 if (strncasecmp(s, prefix, l) == 0)
83 char *endswith(const char *s, const char *postfix) _pure_;
84 char *endswith_no_case(const char *s, const char *postfix) _pure_;
86 char *first_word(const char *s, const char *word) _pure_;
88 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
90 #define FOREACH_WORD(word, length, s, state) \
91 _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
93 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
94 _FOREACH_WORD(word, length, s, separator, false, state)
96 #define _FOREACH_WORD(word, length, s, separator, quoted, state) \
97 for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
99 char *strappend(const char *s, const char *suffix);
100 char *strnappend(const char *s, const char *suffix, size_t length);
102 char *strjoin_real(const char *x, ...) _sentinel_;
103 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
105 #define strjoina(a, ...) \
107 const char *_appendees_[] = { a, __VA_ARGS__ }; \
111 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
112 _len_ += strlen(_appendees_[_i_]); \
113 _p_ = _d_ = alloca(_len_ + 1); \
114 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
115 _p_ = stpcpy(_p_, _appendees_[_i_]); \
120 char *strstrip(char *s);
121 #if 0 /// UNNEEDED by elogind
122 char *delete_chars(char *s, const char *bad);
124 char *delete_trailing_chars(char *s, const char *bad);
125 char *truncate_nl(char *s);
127 #if 0 /// UNNEEDED by elogind
128 static inline char *skip_leading_chars(const char *s, const char *bad) {
136 return (char*) s + strspn(s, bad);
139 char ascii_tolower(char x);
140 char *ascii_strlower(char *s);
141 char *ascii_strlower_n(char *s, size_t n);
143 char ascii_toupper(char x);
144 char *ascii_strupper(char *s);
146 int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
147 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
149 bool chars_intersect(const char *a, const char *b) _pure_;
152 static inline bool _pure_ in_charset(const char *s, const char* charset) {
155 return s[strspn(s, charset)] == '\0';
158 bool string_has_cc(const char *p, const char *ok) _pure_;
160 char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
161 static inline char *ellipsize(const char *s, size_t length, unsigned percent) {
162 return ellipsize_mem(s, strlen(s), length, percent);
165 char *cellescape(char *buf, size_t len, const char *s);
167 /* This limit is arbitrary, enough to give some idea what the string contains */
168 #define CELLESCAPE_DEFAULT_LENGTH 64
170 bool nulstr_contains(const char *nulstr, const char *needle);
172 char* strshorten(char *s, size_t l);
174 char *strreplace(const char *text, const char *old_string, const char *new_string);
176 char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
178 char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_;
180 #define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__)
182 char *strrep(const char *s, unsigned n);
184 int split_pair(const char *s, const char *sep, char **l, char **r);
186 int free_and_strdup(char **p, const char *s);
188 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
189 static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
192 return (void*) haystack;
194 if (haystacklen < needlelen)
200 return memmem(haystack, haystacklen, needle, needlelen);
203 #if !HAVE_EXPLICIT_BZERO
204 void explicit_bzero(void *p, size_t l);
207 char *string_erase(char *x);
209 char *string_free_erase(char *s);
210 DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
211 #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
213 bool string_is_safe(const char *p) _pure_;
215 static inline size_t strlen_ptr(const char *s) {
222 /* Like startswith(), but operates on arbitrary memory blocks */
223 static inline void *memory_startswith(const void *p, size_t sz, const char *token) {
234 if (memcmp(p, token, n) != 0)
237 return (uint8_t*) p + n;